add example for transfer erc20, balance
MODIFY
examples/config.js
examples/config.js
@@ -48,6 +48,6 @@ module.exports = {
address: process.env.FROM
},
user2: {
- address: '<paste address here>', // Your address
+ address: process.env.USER2_ADDRESS
},
}

MODIFY
examples/pos/erc20/balance.js
examples/pos/erc20/balance.js
-const { getMaticPOSClient, from, pos } = require('../../utils')
+const { pos } = require('../../config');
+const { getPOSClient, from } = require('../../utils');
const execute = async () => {
- try {
- const maticPOSClient = getMaticPOSClient()
- const tx = await maticPOSClient.balanceOfERC20(from, pos.child.erc20, {})
+ const client = await getPOSClient();
+ const erc720Token = client.erc20(pos.child.erc20);
+
+ const result = await erc720Token.getBalance(from);
+
+ console.log("result", result);
- console.log(tx) // eslint-disable-line
- } catch (e) {
- console.error(e) // eslint-disable-line
- }
}
-execute().then(() => process.exit(0))
+execute().then(() => {
+}).catch(err => {
+ console.error("err", err);
+}).finally(_ => {
+ process.exit(0);
+})
\ No newline at end of file

ADD
examples/pos/erc20/transfer.js
+const { pos } = require('../../config');
+const { getPOSClient, to } = require('../../utils');
+
+const execute = async () => {
+ const client = await getPOSClient();
+ const erc720Token = client.erc20(pos.parent.erc20);
+
+ const result = await erc720Token.transfer(to, 10, {
+ gasPrice: '4000000000',
+ nonce: 11795,
+ });
+
+ const txHash = await result.getTransactionHash();
+ console.log("txHash", txHash);
+ const receipt = await result.getReceipt();
+ console.log("receipt", receipt);
+
+}
+execute().then(() => {
+}).catch(err => {
+ console.error("err", err);
+}).finally(_ => {
+ process.exit(0);
+})
\ No newline at end of file

MODIFY
src/constant.ts
src/constant.ts
@@ -16,3 +16,5 @@ export const BIG_TWO = new BN(2)
export const CHECKPOINT_INTERVAL = new BN(10000)
export const MAX_AMOUNT = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
+
+export const MATIC_TOKEN_ADDRESS_ON_POLYGON = '0x0000000000000000000000000000000000001010'
\ No newline at end of file

MODIFY
src/plasma/erc20.ts
src/plasma/erc20.ts
@@ -3,6 +3,7 @@ import { Log_Event_Signature } from "../enums";
import { IPlasmaContracts, ITransactionOption } from "../interfaces";
import { BaseToken, Converter, promiseResolve, Web3SideChainClient } from "../utils";
import { PlasmaToken } from "./plasma_token";
+import { MATIC_TOKEN_ADDRESS_ON_POLYGON } from "../constant";
export class ERC20 extends PlasmaToken {
@@ -111,8 +112,11 @@ export class ERC20 extends PlasmaToken {
return this.withdrawChallenge_(burnTxHash, true, option);
}
- transfer(to: string, amount: TYPE_AMOUNT, option?: ITransactionOption) {
- return this['transferERC20_'](to, amount, option);
+ transfer(to: string, amount: TYPE_AMOUNT, option: ITransactionOption = {}) {
+ if (this.contractParam.address === MATIC_TOKEN_ADDRESS_ON_POLYGON) {
+ option.to = to;
+ }
+ return this.transferERC20_(to, amount, option);
}

MODIFY
src/plasma/erc721.ts
src/plasma/erc721.ts
@@ -127,7 +127,7 @@ export class ERC721 extends PlasmaToken {
return this.withdrawChallenge_(burnTxHash, true, option);
}
- transfer(from: string, to: string, tokenId: string, option?: ITransactionOption) {
+ transfer(tokenId: string, from: string, to: string, option?: ITransactionOption) {
return this.transferERC721_(
from,
to,

MODIFY
src/plasma/index.ts
src/plasma/index.ts
@@ -7,6 +7,7 @@ import { RegistryContract } from "./registry";
import { Ether } from "./ether";
import { ExitManager, RootChain } from "../pos";
import { WithdrawManager } from "./withdraw_manager";
+import { MATIC_TOKEN_ADDRESS_ON_POLYGON } from "../constant";
export class PlasmaClient extends BridgeClient {
@@ -28,6 +29,7 @@ export class PlasmaClient extends BridgeClient {
}
erc20(tokenAddress: string, isParent?: boolean) {
+ tokenAddress = tokenAddress ? tokenAddress : MATIC_TOKEN_ADDRESS_ON_POLYGON;
return new ERC20(
tokenAddress,
isParent,

MODIFY
src/pos/erc20.ts
src/pos/erc20.ts
@@ -168,8 +168,17 @@ export class ERC20 extends POSToken {
});
}
+ /**
+ * transfer amount to another user
+ *
+ * @param {string} to
+ * @param {TYPE_AMOUNT} amount
+ * @param {ITransactionOption} [option]
+ * @returns
+ * @memberof ERC20
+ */
transfer(to: string, amount: TYPE_AMOUNT, option?: ITransactionOption) {
- return this['transfer_'](to, amount, option);
+ return this.transferERC20_(to, amount, option);
}
}
\ No newline at end of file

MODIFY
src/pos/erc721.ts
src/pos/erc721.ts
@@ -304,4 +304,23 @@ export class ERC721 extends POSToken {
});
}
+ /**
+ * tranfer to another user
+ *
+ * @param {string} tokenId
+ * @param {string} from
+ * @param {string} to
+ * @param {ITransactionOption} [option]
+ * @returns
+ * @memberof ERC721
+ */
+ transfer(tokenId: string, from: string, to: string, option?: ITransactionOption) {
+ return this.transferERC721_(
+ from,
+ to,
+ tokenId,
+ option
+ );
+ }
+
}
\ No newline at end of file

add example for transfer erc20, balance