implement test for erc1155
MODIFY
src/pos/erc1155.ts
src/pos/erc1155.ts
@@ -9,7 +9,9 @@ export class ERC1155 extends POSToken {
mintablePredicateAddress: string;
- addressConfig: IPOSERC1155Address;
+ get addressConfig(): IPOSERC1155Address {
+ return this.client.config.erc1155 || {};
+ }
constructor(
tokenAddress: string,
@@ -24,7 +26,6 @@ export class ERC1155 extends POSToken {
bridgeType: 'pos'
}, client, getContracts);
- this.addressConfig = client.config.erc1155 || {};
}
private getAddress_(value: string) {
@@ -138,7 +139,7 @@ export class ERC1155 extends POSToken {
tokenIds: [param.tokenId],
userAddress: param.userAddress,
data: param.data
- });
+ }, option);
}
/**
@@ -151,12 +152,14 @@ export class ERC1155 extends POSToken {
*/
depositMany(param: POSERC1155DepositBatchParam, option?: ITransactionOption) {
this.checkForRoot("depositMany");
+
const { tokenIds, amounts, data, userAddress } = param;
+ const emptyHex = Converter.toHex(0);
const amountInABI = this.client.parent.encodeParameters(
[
tokenIds.map(t => Converter.toHex(t)),
amounts.map(a => Converter.toHex(a)),
- data || '0x0'
+ data || emptyHex
],
['uint256[]', 'uint256[]', 'bytes'],
);
@@ -179,7 +182,7 @@ export class ERC1155 extends POSToken {
* @return {*}
* @memberof ERC1155
*/
- withdrawStart(tokenId: string, amount: TYPE_AMOUNT, option?: ITransactionOption) {
+ withdrawStart(tokenId: TYPE_AMOUNT, amount: TYPE_AMOUNT, option?: ITransactionOption) {
this.checkForChild("withdrawStart");
return this.getContract().then(contract => {

MODIFY
test/debug.js
test/debug.js
@@ -52,10 +52,22 @@ const execute = async () => {
// return console.log(await client.isDeposited('0x05b6d0d2280557c04de48d395f1f4ea9deb498fabb9bb09b9aec929db5ce62fa'));
- const balance = await goerliERC1155Token.getBalance(
- from,123
- );
- return console.log("balance", balance);
+ // var tx = await goerliERC1155Token.isApprovedAll(from);
+ // return console.log('isapp', tx);
+ var tx = await goerliERC1155Token.deposit({
+ amount: 10,
+ tokenId: 123,
+ userAddress: from
+ }, {
+ returnTransaction: true
+ });
+
+ return console.log('tx', tx);
+
+ console.log("hash", await tx.getTransactionHash());
+ console.log("receipt", await tx.getReceipt());
+
+ return;
// const tokens = await goerliERC721Token.getAllTokens(
// from

MODIFY
test/specs/client.ts
test/specs/client.ts
@@ -16,8 +16,10 @@ export const erc721 = {
parent: pos.parent.erc721,
child: pos.child.erc721
}
-
-
+export const erc1155 = {
+ parent: pos.parent.erc1155,
+ child: pos.child.erc1155
+}
export const posClient = new POSClient();

ADD
test/specs/erc1155.spec.ts
+import { erc1155, from, posClient, posClientForTo, to, } from "./client";
+import { expect } from 'chai'
+import { ABIManager, setProofApi } from '@maticnetwork/maticjs'
+
+
+describe('ERC1155', () => {
+
+ console.log('erc1155', posClient.erc1155);
+
+ let erc1155Child = posClient.erc1155(erc1155.child);
+ let erc1155Parent = posClient.erc1155(erc1155.parent, true);
+
+ console.log('erc1155 token created');
+
+ const abiManager = new ABIManager("testnet", "mumbai");
+ before(() => {
+ return Promise.all([
+ abiManager.init()
+ ]);
+ });
+
+ it('getBalance child', async () => {
+ const balance = await erc1155Child.getBalance(from, 123);
+ expect(balance).to.be.an('string');
+ expect(Number(balance)).gte(0);
+ })
+
+ it('getBalance parent', async () => {
+ const balance = await erc1155Parent.getBalance(from, '123');
+ expect(balance).to.be.an('string');
+ expect(Number(balance)).gte(0);
+ })
+
+ it('isWithdrawExited', async () => {
+ // const exitHash = '0xa3ed203336807249dea53dc99434e2d06b71c85f55c89ee49ca10244ab3dbcf5';
+ const isExited = await erc1155Parent.isWithdrawExited('0xbc48c0ccd9821141779a200586ef52033a3487c4e1419625fe7a0ea984521052');
+ expect(isExited).equal(true);
+ })
+
+ it('isDeposited', async () => {
+ const txHash = '0x507ea7267693d477917265f52c23c08f1830215a0c7d86643b9c1fb4997a021e';
+ const isDeposited = await posClient.isDeposited(txHash);
+ expect(isDeposited).to.be.an('boolean').equal(true);
+ })
+
+ it('transfer return tx', async () => {
+ const targetToken = 123;
+
+ const result = await erc1155Child.transfer({
+ amount: 1,
+ from: from,
+ to: to,
+ tokenId: targetToken
+ }, {
+ returnTransaction: true
+ });
+ // console.log(result);
+ expect(result['to'].toLowerCase()).equal(erc1155.child.toLowerCase());
+ })
+
+ it('approveAll return tx', async () => {
+ const result = await erc1155Parent.approveAll({
+ returnTransaction: true
+ });
+ expect(result['to'].toLowerCase()).equal(erc1155.parent.toLowerCase());
+ })
+
+ it('deposit return tx', async () => {
+ const tx = await erc1155Parent.deposit({
+ amount: 10,
+ tokenId: 123,
+ userAddress: from
+ }, {
+ returnTransaction: true
+ });
+ const rootChainManager = await abiManager.getConfig("Main.POSContracts.RootChainManagerProxy");
+ console.log('tx', tx['to'], 'root', rootChainManager);
+ expect(tx['to'].toLowerCase()).equal(rootChainManager.toLowerCase());
+ })
+
+ // it('depositMany return tx', async () => {
+ // const tx = await erc1155Parent.depositMany(allTokens, from, {
+ // returnTransaction: true
+ // });
+ // const rootChainManager = await abiManager.getConfig("Main.POSContracts.RootChainManagerProxy")
+ // expect(tx['to'].toLowerCase()).equal(rootChainManager.toLowerCase());
+ // })
+
+ it('withdrawStart return tx', async () => {
+ const result = await erc1155Child.withdrawStart(123, 10, {
+ returnTransaction: true
+ });
+ expect(result['to'].toLowerCase()).equal(erc1155.child.toLowerCase());
+ })
+
+
+ it('transfer write', async () => {
+ const targetToken = 123;
+ const allTokensFrom = await erc1155Child.getBalance(from, targetToken);
+ console.log('allTokensFrom', allTokensFrom);
+ const allTokensTo = await erc1155Child.getBalance(to, targetToken);
+ console.log('allTokensTo', allTokensTo);
+ const amountToTransfer = 1;
+ let result = await erc1155Child.transfer({
+ tokenId: targetToken, from, to,
+ amount: amountToTransfer
+ });
+
+ 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(erc1155.child.toLowerCase());
+ expect(txReceipt.type).equal(2);
+ expect(txReceipt.gasUsed).to.be.an('number').gt(0);
+ expect(txReceipt.cumulativeGasUsed).to.be.an('number').gt(0);
+
+
+ const newAllTokensFrom = await erc1155Child.getBalance(from, targetToken);
+ const newAllTokensFromInNumber = Number(newAllTokensFrom);
+ console.log('newAllTokensFrom', newAllTokensFrom);
+ expect(newAllTokensFromInNumber).equal(Number(allTokensFrom) - 1);
+ const newAllTokensTo = await erc1155Child.getBalance(to, targetToken);
+ const newAllTokensToInNumber = Number(newAllTokensTo);
+ console.log('newAllTokensTo', newAllTokensTo);
+ expect(newAllTokensToInNumber).equal(Number(allTokensTo) + 1);
+
+ const erc1155ChildToken = posClientForTo.erc1155(erc1155.child);
+
+ // transfer token back to sender
+ result = await erc1155ChildToken.transfer({
+ tokenId: targetToken,
+ to: from,
+ from: to,
+ amount: amountToTransfer
+ });
+ txHash = await result.getTransactionHash();
+ txReceipt = await result.getReceipt();
+
+ const newFromCount = await erc1155Child.getBalance(from, targetToken);
+ const newToCount = await erc1155Child.getBalance(to, targetToken);
+
+ expect(newFromCount).equal(allTokensFrom);
+ expect(newToCount).equal(allTokensTo);
+
+ })
+
+ if (process.env.NODE_ENV !== 'test_all') return;
+
+ it('approve', async () => {
+ const result = await erc1155Parent.approveAll({
+ returnTransaction: true
+ });
+ expect(result['to'].toLowerCase()).equal(erc1155.parent.toLowerCase());
+ })
+});

MODIFY
test/specs/index.ts
test/specs/index.ts
@@ -10,3 +10,4 @@ import './map_promise.spec'
import './pos_client.spec'
import './erc20.spec'
import './erc721.spec'
+import './erc1155.spec'

MODIFY
test/specs/pos_client.spec.ts
test/specs/pos_client.spec.ts
-import { erc721, from, posClient, posClientForTo, privateKey, RPC, to, toPrivateKey } from "./client";
+import { from, posClient, posClientForTo, privateKey, RPC, to, toPrivateKey } from "./client";
import { expect } from 'chai'
import { ABIManager } from '@maticnetwork/maticjs'
import { providers, Wallet } from "ethers";

implement test for erc1155