add test for erc71 transfer
MODIFY
src/pos/erc20.ts
src/pos/erc20.ts
@@ -210,7 +210,7 @@ export class ERC20 extends POSToken {
* @returns
* @memberof ERC20
*/
- transfer(to: string, amount: TYPE_AMOUNT, option?: ITransactionOption) {
+ transfer(amount: TYPE_AMOUNT, to: string, option?: ITransactionOption) {
return this.transferERC20(to, amount, option);
}

MODIFY
src/pos/erc721.ts
src/pos/erc721.ts
@@ -46,7 +46,9 @@ export class ERC721 extends POSToken {
"balanceOf",
userAddress
);
- return this.processRead<number>(method, options);
+ return this.processRead<string>(method, options);
+ }).then(count => {
+ return Number(count);
});
}

MODIFY
src/utils/set_proof_api_url.ts
src/utils/set_proof_api_url.ts
import { service, NetworkService } from "../services";
export const setProofApi = (url: string) => {
+ if (url[url.length - 1] !== '/') {
+
+ }
service.network = new NetworkService(url) as any;
};
\ No newline at end of file

MODIFY
test/config.js
test/config.js
@@ -28,5 +28,6 @@ module.exports = {
},
user2: {
address: process.env.USER2_FROM, // Your address
+ "privateKey": process.env.USER2_PRIVATE_KEY,
},
}

MODIFY
test/debug.js
test/debug.js
@@ -35,21 +35,24 @@ const execute = async () => {
const mumbaiERC20Token = client.erc20(mumbaiERC20);
const goerliERC20Token = client.erc20(goerliERC20, true);
+ const goerliERC721Token = client.erc721(pos.parent.erc721, true);
+
+
// return console.log(await client.isDeposited('0xc67599f5c967f2040786d5924ec55d37bf943c009bdd23f3b50e5ae66efde258'));
- const balance = await mumbaiERC20Token.getBalance(
- from
- );
- return console.log("balance", balance);
+ // const balance = await mumbaiERC20Token.getBalance(
+ // from
+ // );
+ // return console.log("balance", balance);
// const tokens = await goerliERC720Token.getAllTokens(
// from
// );
// return console.log("tokens", tokens);
- const tx = await goerliERC20Token.deposit(10, from, {
+ const tx = await goerliERC721Token.deposit(10, from, {
returnTransaction: true
});
console.log('tx', tx);

MODIFY
test/specs/client.ts
test/specs/client.ts
@@ -2,9 +2,13 @@ import { POSClient } from "@maticnetwork/maticjs";
import HDWalletProvider from "@truffle/hdwallet-provider";
import { user1, rpc, pos, user2 } from "../config";
-const privateKey = user1.privateKey;
+export const privateKey = user1.privateKey;
export const from = user1.address;
export const to = user2.address;
+export const toPrivateKey = user2.privateKey;
+
+export const RPC = rpc;
+
export const erc20 = {
parent: pos.parent.erc20,
child: pos.child.erc20
@@ -31,3 +35,21 @@ export const posClient = new POSClient({
}
}
});
+
+export const posClientForTo = new POSClient({
+ // log: true,
+ network: 'testnet',
+ version: 'mumbai',
+ parent: {
+ provider: new HDWalletProvider(toPrivateKey, rpc.parent),
+ defaultConfig: {
+ from: to
+ }
+ },
+ child: {
+ provider: new HDWalletProvider(toPrivateKey, rpc.child),
+ defaultConfig: {
+ from: to
+ }
+ }
+});
\ No newline at end of file

MODIFY
test/specs/erc20.spec.ts
test/specs/erc20.spec.ts
-import { erc20, from, posClient, to } from "./client";
+import { erc20, from, posClient, posClientForTo, to } from "./client";
import { expect } from 'chai'
import { ABIManager } from '@maticnetwork/maticjs'
import BN from "bn.js";
@@ -53,7 +53,7 @@ describe('ERC20', () => {
it('child transfer returnTransaction with erp1159', async () => {
const amount = 10;
try {
- const result = await erc20Child.transfer(to, amount, {
+ const result = await erc20Child.transfer(amount, to, {
maxFeePerGas: 10,
maxPriorityFeePerGas: 10,
returnTransaction: true
@@ -70,10 +70,9 @@ describe('ERC20', () => {
it('child transfer returnTransaction', async () => {
const amount = 10;
- const result = await erc20Child.transfer(to, amount, {
+ const result = await erc20Child.transfer(amount, to, {
returnTransaction: true
});
- console.log('result', result);
expect(result).to.have.not.property('maxFeePerGas')
expect(result).to.have.not.property('maxPriorityFeePerGas')
expect(result).to.have.property('gasPrice')
@@ -83,12 +82,11 @@ describe('ERC20', () => {
it('parent transfer returnTransaction with erp1159', async () => {
const amount = 10;
- const result = await erc20Parent.transfer(to, amount, {
+ const result = await erc20Parent.transfer(amount, to, {
maxFeePerGas: 20,
maxPriorityFeePerGas: 20,
returnTransaction: true
});
- console.log('result', result);
expect(result).to.have.property('maxFeePerGas', 20)
expect(result).to.have.property('maxPriorityFeePerGas', 20)
@@ -132,16 +130,27 @@ describe('ERC20', () => {
expect(result['to'].toLowerCase()).equal(rootChainManager.toLowerCase());
});
+ it('withdrawExit return tx', async () => {
+ const result = await erc20Parent.withdrawExit('0x1c20c41b9d97d1026aa456a21f13725df63edec1b1f43aacb180ebcc6340a2d3', {
+ returnTransaction: true
+ });
+ console.log('result', result);
+
+ const rootChainManager = await abiManager.getConfig("Main.POSContracts.RootChainManagerProxy")
+ expect(result['to'].toLowerCase()).equal(rootChainManager.toLowerCase());
+ });
+
+
it('child transfer', async () => {
const oldBalance = await erc20Child.getBalance(to);
console.log('oldBalance', oldBalance);
- const amount = 10;
- const result = await erc20Child.transfer(to, amount);
- const txHash = await result.getTransactionHash();
+ const amount = 10000000;
+ let result = await erc20Child.transfer(amount, to);
+ let txHash = await result.getTransactionHash();
expect(txHash).to.be.an('string');
- console.log('txHash', txHash);
- const txReceipt = await result.getReceipt();
- console.log("txReceipt", txReceipt);
+ // console.log('txHash', txHash);
+ let txReceipt = await result.getReceipt();
+ // console.log("txReceipt", txReceipt);
expect(txReceipt.transactionHash).equal(txHash);
expect(txReceipt).to.be.an('object');
@@ -168,6 +177,16 @@ describe('ERC20', () => {
expect(newBalanceBig.toString()).equal(
oldBalanceBig.add(new BN(amount)).toString()
)
+
+
+
+ //transfer money back to user
+ await posClientForTo.init();
+ const erc20ChildToken = posClientForTo.erc20(erc20.child);
+
+ result = await erc20ChildToken.transfer(amount, to);
+ txHash = await result.getTransactionHash();
+ txReceipt = await result.getReceipt();
});
if (process.env.NODE_ENV !== 'test_all') return;

MODIFY
test/specs/erc721.spec.ts
test/specs/erc721.spec.ts
-import { erc721, from, posClient } from "./client";
+import { erc721, from, posClient, posClientForTo, to, } from "./client";
import { expect } from 'chai'
@@ -14,15 +14,15 @@ describe('ERC721', () => {
it('getTokensCounts child', async () => {
const tokensCount = await erc721Child.getTokensCount(from);
console.log('tokensCount', tokensCount);
- expect(tokensCount).to.be.an('string');
- expect(Number(tokensCount)).gte(0);
+ expect(tokensCount).to.be.an('number');
+ expect(tokensCount).gte(0);
})
it('getTokensCount parent', async () => {
const tokensCount = await erc721Parent.getTokensCount(from);
console.log('tokensCount', tokensCount);
- expect(tokensCount).to.be.an('string');
- expect(Number(tokensCount)).gte(0);
+ expect(tokensCount).to.be.an('number');
+ expect(tokensCount).gte(0);
})
it('getAllTokens child', async () => {
@@ -36,4 +36,90 @@ describe('ERC721', () => {
const allTokens = await erc721Parent.getAllTokens(from);
expect(allTokens).to.be.an('array').length(tokensCount);
})
+
+ it('transfer return tx', async () => {
+ const allTokensFrom = await erc721Child.getAllTokens(from);
+ const targetToken = allTokensFrom[0];
+ const result = await erc721Child.transfer(targetToken, from, to, {
+ returnTransaction: true
+ });
+ // console.log(result);
+ expect(result['to'].toLowerCase()).equal(erc721.child.toLowerCase());
+ })
+
+ it('approve return tx', async () => {
+ const allTokens = await erc721Parent.getAllTokens(from);
+ const result = await erc721Parent.approve(allTokens[0], {
+ returnTransaction: true
+ });
+ expect(result['to'].toLowerCase()).equal(erc721.parent.toLowerCase());
+ })
+
+ it('approveAll return tx', async () => {
+ const result = await erc721Parent.approveAll({
+ returnTransaction: true
+ });
+ expect(result['to'].toLowerCase()).equal(erc721.parent.toLowerCase());
+ })
+
+ // return;
+
+ it('transfer write', async () => {
+ const allTokensFrom = await erc721Child.getAllTokens(from);
+ console.log('allTokensFrom', allTokensFrom);
+ const allTokensTo = await erc721Child.getAllTokens(to);
+ console.log('allTokensTo', allTokensTo);
+
+ const targetToken = allTokensFrom[0];
+ let result = await erc721Child.transfer(targetToken, from, to);
+
+ let txHash = await result.getTransactionHash();
+ expect(txHash).to.be.an('string');
+ // console.log('txHash', txHash);
+ let txReceipt = await result.getReceipt();
+ // console.log("txReceipt", txReceipt);
+
+ expect(txReceipt.transactionHash).equal(txHash);
+ expect(txReceipt).to.be.an('object');
+ expect(txReceipt.from).equal(from);
+ expect(txReceipt.to.toLowerCase()).equal(erc721.child.toLowerCase());
+ expect(txReceipt.type).equal('0x0');
+ expect(txReceipt.gasUsed).to.be.an('number').gt(0);
+ expect(txReceipt.cumulativeGasUsed).to.be.an('number').gt(0);
+
+
+ const newAllTokensFrom = await erc721Child.getAllTokens(from);
+ console.log('newAllTokensFrom', newAllTokensFrom);
+ expect(newAllTokensFrom.length).equal(allTokensFrom.length - 1);
+ const newAllTokensTo = await erc721Child.getAllTokens(to);
+ console.log('newAllTokensTo', newAllTokensTo);
+ expect(newAllTokensTo.length).equal(allTokensTo.length + 1);
+
+ await posClientForTo.init();
+
+ const erc721ChildToken = posClientForTo.erc721(erc721.child);
+
+
+ // transfer token back to sender
+ result = await erc721ChildToken.transfer(targetToken, to, from);
+ txHash = await result.getTransactionHash();
+ txReceipt = await result.getReceipt();
+
+ const newFromCount = await erc721Child.getTokensCount(from);
+ const newToCount = await erc721Child.getTokensCount(to);
+
+ expect(newFromCount).equal(allTokensFrom.length);
+ expect(newToCount).equal(allTokensTo.length);
+
+ })
+
+ if (process.env.NODE_ENV !== 'test_all') return;
+
+ it('approve', async () => {
+ const allTokens = await erc721Parent.getAllTokens(from);
+ const result = await erc721Parent.approve(allTokens[0], {
+ returnTransaction: true
+ });
+ expect(result['to'].toLowerCase()).equal(erc721.parent.toLowerCase());
+ })
});
\ No newline at end of file

add test for erc71 transfer