difftreelog
test basic evm integration tests
in: master
7 files changed
tests/src/eth/fungible.test.tsdiffbeforeafterboth1import privateKey from "../substrate/privateKey";2import { approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from "../util/helpers";3import { collectionIdToAddress, createEthAccount, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from "./util/helpers"4import fungibleAbi from './fungibleAbi.json';5import { expect } from "chai";67describe('Information getting', () => {8 itWeb3('totalSupply', async ({ web3 }) => {9 const collection = await createCollectionExpectSuccess({10 name: 'token name',11 mode: { type: 'Fungible', decimalPoints: 0 }12 });13 const alice = privateKey('//Alice');1415 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { substrate: alice.address });1617 const address = collectionIdToAddress(collection);18 const contract = new web3.eth.Contract(fungibleAbi as any, address);19 const totalSupply = await contract.methods.totalSupply().call();2021 // FIXME: always equals to 0, because this method is not implemented22 expect(totalSupply).to.equal('0');23 });2425 itWeb3('balanceOf', async ({ web3 }) => {26 const collection = await createCollectionExpectSuccess({27 name: 'token name',28 mode: { type: 'Fungible', decimalPoints: 0 }29 });30 const alice = privateKey('//Alice');3132 const caller = createEthAccount(web3);33 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: caller });3435 const address = collectionIdToAddress(collection);36 const contract = new web3.eth.Contract(fungibleAbi as any, address);37 const balance = await contract.methods.balanceOf(caller).call();3839 expect(balance).to.equal('200');40 });41});4243describe('Plain calls', () => {44 itWeb3('Can perform approve()', async ({ web3, api }) => {45 const collection = await createCollectionExpectSuccess({46 name: 'token name',47 mode: { type: 'Fungible', decimalPoints: 0 }48 });49 const alice = privateKey('//Alice');5051 const owner = createEthAccount(web3);52 await transferBalanceToEth(api, alice, owner, 999999999999999);5354 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });5556 const spender = createEthAccount(web3);5758 const address = collectionIdToAddress(collection);59 const contract = new web3.eth.Contract(fungibleAbi as any, address);6061 {62 const result = await contract.methods.approve(spender, 100).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });63 const events = normalizeEvents(result.events);6465 expect(events).to.be.deep.equal([66 {67 address,68 event: 'Approval',69 args: {70 owner,71 spender,72 value: '100',73 }74 }75 ]);76 }7778 {79 const allowance = await contract.methods.allowance(owner, spender).call();80 expect(+allowance).to.equal(100);81 }82 });8384 itWeb3('Can perform transferFrom()', async ({ web3, api }) => {85 const collection = await createCollectionExpectSuccess({86 name: 'token name',87 mode: { type: 'Fungible', decimalPoints: 0 }88 });89 const alice = privateKey('//Alice');9091 const owner = createEthAccount(web3);92 await transferBalanceToEth(api, alice, owner, 999999999999999);9394 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });9596 const spender = createEthAccount(web3);97 await transferBalanceToEth(api, alice, spender, 999999999999999);9899 const receiver = createEthAccount(web3);100101 const address = collectionIdToAddress(collection);102 const contract = new web3.eth.Contract(fungibleAbi as any, address);103104 await contract.methods.approve(spender, 100).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });105106 {107 const result = await contract.methods.transferFrom(owner, receiver, 49).send({ from: spender, gas: '0x1000000', gasPrice: '0x01' });108 const events = normalizeEvents(result.events);109 expect(events).to.be.deep.equal([110 {111 address,112 event: 'Transfer',113 args: {114 from: owner,115 to: receiver,116 value: '49',117 }118 },119 {120 address,121 event: 'Approval',122 args: {123 owner,124 spender,125 value: '51',126 }127 }128 ]);129 }130131 {132 const balance = await contract.methods.balanceOf(receiver).call();133 expect(+balance).to.equal(49);134 }135136 {137 const balance = await contract.methods.balanceOf(owner).call();138 expect(+balance).to.equal(151);139 }140 });141142 itWeb3('Can perform transfer()', async ({ web3, api }) => {143 const collection = await createCollectionExpectSuccess({144 name: 'token name',145 mode: { type: 'Fungible', decimalPoints: 0 }146 });147 const alice = privateKey('//Alice');148149 const owner = createEthAccount(web3);150 await transferBalanceToEth(api, alice, owner, 999999999999999);151152 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });153154 const receiver = createEthAccount(web3);155 await transferBalanceToEth(api, alice, receiver, 999999999999999);156157 const address = collectionIdToAddress(collection);158 const contract = new web3.eth.Contract(fungibleAbi as any, address);159160 {161 const result = await contract.methods.transfer(receiver, 50).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });162 const events = normalizeEvents(result.events);163 expect(events).to.be.deep.equal([164 {165 address,166 event: 'Transfer',167 args: {168 from: owner,169 to: receiver,170 value: '50',171 }172 },173 ])174 }175176 {177 const balance = await contract.methods.balanceOf(owner).call();178 expect(+balance).to.equal(150);179 }180181 {182 const balance = await contract.methods.balanceOf(receiver).call();183 expect(+balance).to.equal(50);184 }185 });186});187188describe('Substrate calls', () => {189 itWeb3('Events emitted for approve()', async ({ web3 }) => {190 const collection = await createCollectionExpectSuccess({191 mode: { type: 'Fungible', decimalPoints: 0 }192 });193 const alice = privateKey('//Alice');194195 const receiver = createEthAccount(web3);196197 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n });198199 const address = collectionIdToAddress(collection);200 const contract = new web3.eth.Contract(fungibleAbi as any, address);201202 const events = await recordEvents(contract, async () => {203 await approveExpectSuccess(collection, 1, alice, { ethereum: receiver }, 100);204 });205206 expect(events).to.be.deep.equal([207 {208 address,209 event: 'Approval',210 args: {211 owner: subToEth(alice.address),212 spender: receiver,213 value: '100',214 }215 }216 ]);217 });218219 itWeb3('Events emitted for transferFrom()', async ({ web3 }) => {220 const collection = await createCollectionExpectSuccess({221 mode: { type: 'Fungible', decimalPoints: 0 }222 });223 const alice = privateKey('//Alice');224 const bob = privateKey('//Bob');225226 const receiver = createEthAccount(web3);227228 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n });229 await approveExpectSuccess(collection, 1, alice, bob, 100);230231 const address = collectionIdToAddress(collection);232 const contract = new web3.eth.Contract(fungibleAbi as any, address);233234 const events = await recordEvents(contract, async () => {235 await transferFromExpectSuccess(collection, 1, bob, alice, { ethereum: receiver }, 51, 'Fungible');236 });237238 expect(events).to.be.deep.equal([239 {240 address,241 event: 'Transfer',242 args: {243 from: subToEth(alice.address),244 to: receiver,245 value: '51',246 }247 },248 {249 address,250 event: 'Approval',251 args: {252 owner: subToEth(alice.address),253 spender: subToEth(bob.address),254 value: '49',255 }256 }257 ]);258 });259260 itWeb3('Events emitted for transfer()', async ({ web3 }) => {261 const collection = await createCollectionExpectSuccess({262 mode: { type: 'Fungible', decimalPoints: 0 }263 });264 const alice = privateKey('//Alice');265266 const receiver = createEthAccount(web3);267268 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n });269270 const address = collectionIdToAddress(collection);271 const contract = new web3.eth.Contract(fungibleAbi as any, address);272273 const events = await recordEvents(contract, async () => {274 await transferExpectSuccess(collection, 1, alice, { ethereum: receiver }, 51, 'Fungible');275 });276277 expect(events).to.be.deep.equal([278 {279 address,280 event: 'Transfer',281 args: {282 from: subToEth(alice.address),283 to: receiver,284 value: '51',285 }286 },287 ]);288 });289});tests/src/eth/fungibleAbi.jsondiffbeforeafterboth--- /dev/null
+++ b/tests/src/eth/fungibleAbi.json
@@ -0,0 +1,175 @@
+[
+ {
+ "constant": false,
+ "inputs": [
+ {
+ "name": "_spender",
+ "type": "address"
+ },
+ {
+ "name": "_value",
+ "type": "uint256"
+ }
+ ],
+ "name": "approve",
+ "outputs": [
+ {
+ "name": "",
+ "type": "bool"
+ }
+ ],
+ "payable": false,
+ "stateMutability": "nonpayable",
+ "type": "function"
+ },
+ {
+ "constant": true,
+ "inputs": [],
+ "name": "totalSupply",
+ "outputs": [
+ {
+ "name": "",
+ "type": "uint256"
+ }
+ ],
+ "payable": false,
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "constant": false,
+ "inputs": [
+ {
+ "name": "_from",
+ "type": "address"
+ },
+ {
+ "name": "_to",
+ "type": "address"
+ },
+ {
+ "name": "_value",
+ "type": "uint256"
+ }
+ ],
+ "name": "transferFrom",
+ "outputs": [
+ {
+ "name": "",
+ "type": "bool"
+ }
+ ],
+ "payable": false,
+ "stateMutability": "nonpayable",
+ "type": "function"
+ },
+ {
+ "constant": true,
+ "inputs": [
+ {
+ "name": "_owner",
+ "type": "address"
+ }
+ ],
+ "name": "balanceOf",
+ "outputs": [
+ {
+ "name": "balance",
+ "type": "uint256"
+ }
+ ],
+ "payable": false,
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "constant": false,
+ "inputs": [
+ {
+ "name": "_to",
+ "type": "address"
+ },
+ {
+ "name": "_value",
+ "type": "uint256"
+ }
+ ],
+ "name": "transfer",
+ "outputs": [
+ {
+ "name": "",
+ "type": "bool"
+ }
+ ],
+ "payable": false,
+ "stateMutability": "nonpayable",
+ "type": "function"
+ },
+ {
+ "constant": true,
+ "inputs": [
+ {
+ "name": "_owner",
+ "type": "address"
+ },
+ {
+ "name": "_spender",
+ "type": "address"
+ }
+ ],
+ "name": "allowance",
+ "outputs": [
+ {
+ "name": "",
+ "type": "uint256"
+ }
+ ],
+ "payable": false,
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "anonymous": false,
+ "inputs": [
+ {
+ "indexed": true,
+ "name": "owner",
+ "type": "address"
+ },
+ {
+ "indexed": true,
+ "name": "spender",
+ "type": "address"
+ },
+ {
+ "indexed": false,
+ "name": "value",
+ "type": "uint256"
+ }
+ ],
+ "name": "Approval",
+ "type": "event"
+ },
+ {
+ "anonymous": false,
+ "inputs": [
+ {
+ "indexed": true,
+ "name": "from",
+ "type": "address"
+ },
+ {
+ "indexed": true,
+ "name": "to",
+ "type": "address"
+ },
+ {
+ "indexed": false,
+ "name": "value",
+ "type": "uint256"
+ }
+ ],
+ "name": "Transfer",
+ "type": "event"
+ }
+]
\ No newline at end of file
tests/src/eth/fungibleMetadataAbi.jsondiffbeforeafterboth--- /dev/null
+++ b/tests/src/eth/fungibleMetadataAbi.json
@@ -0,0 +1,41 @@
+[
+ {
+ "inputs": [],
+ "name": "name",
+ "outputs": [
+ {
+ "internalType": "string",
+ "name": "",
+ "type": "string"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "inputs": [],
+ "name": "symbol",
+ "outputs": [
+ {
+ "internalType": "string",
+ "name": "",
+ "type": "string"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "inputs": [],
+ "name": "decimals",
+ "outputs": [
+ {
+ "internalType": "uint8",
+ "name": "",
+ "type": "uint8"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ }
+]
\ No newline at end of file
tests/src/eth/metadata.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/eth/metadata.test.ts
@@ -0,0 +1,53 @@
+import { expect } from "chai";
+import privateKey from "../substrate/privateKey";
+import { createCollectionExpectSuccess } from "../util/helpers";
+import { collectionIdToAddress, createEthAccount, itWeb3, transferBalanceToEth } from "./util/helpers";
+import fungibleMetadataAbi from './fungibleMetadataAbi.json';
+
+describe('Common metadata', () => {
+ itWeb3('Returns collection name', async ({ api, web3 }) => {
+ const collection = await createCollectionExpectSuccess({
+ name: 'token name',
+ mode: { type: 'NFT' }
+ });
+ const caller = createEthAccount(web3);
+ await transferBalanceToEth(api, privateKey('//Alice'), caller, 999999);
+
+ const address = collectionIdToAddress(collection);
+ const contract = new web3.eth.Contract(fungibleMetadataAbi as any, address);
+ const name = await contract.methods.name().call({ from: caller });
+
+ expect(name).to.equal('token name');
+ });
+
+ itWeb3('Returns symbol name', async ({ api, web3 }) => {
+ const collection = await createCollectionExpectSuccess({
+ tokenPrefix: 'TOK',
+ mode: { type: 'NFT' }
+ });
+ const caller = createEthAccount(web3);
+ await transferBalanceToEth(api, privateKey('//Alice'), caller, 999999);
+
+ const address = collectionIdToAddress(collection);
+ const contract = new web3.eth.Contract(fungibleMetadataAbi as any, address);
+ const symbol = await contract.methods.symbol().call({ from: caller });
+
+ expect(symbol).to.equal('TOK');
+ });
+});
+
+describe('Fungible metadata', () => {
+ itWeb3('Returns fungible decimals', async ({ api, web3 }) => {
+ const collection = await createCollectionExpectSuccess({
+ mode: { type: 'Fungible', decimalPoints: 6 }
+ });
+ const caller = createEthAccount(web3);
+ await transferBalanceToEth(api, privateKey('//Alice'), caller, 999999);
+
+ const address = collectionIdToAddress(collection);
+ const contract = new web3.eth.Contract(fungibleMetadataAbi as any, address);
+ const decimals = await contract.methods.decimals().call({ from: caller });
+
+ expect(+decimals).to.equal(6);
+ })
+})
\ No newline at end of file
tests/src/eth/nonFungible.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/eth/nonFungible.test.ts
@@ -0,0 +1,280 @@
+import privateKey from "../substrate/privateKey";
+import { approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, createItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from "../util/helpers";
+import { collectionIdToAddress, createEthAccount, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from "./util/helpers"
+import nonFungibleAbi from './nonFungibleAbi.json';
+import { expect } from "chai";
+
+describe('Information getting', () => {
+ itWeb3('totalSupply', async ({ web3 }) => {
+ const collection = await createCollectionExpectSuccess({
+ mode: { type: 'NFT' }
+ });
+ const alice = privateKey('//Alice');
+
+ await createItemExpectSuccess(alice, collection, 'NFT', { substrate: alice.address });
+
+ const address = collectionIdToAddress(collection);
+ const contract = new web3.eth.Contract(nonFungibleAbi as any, address);
+ const totalSupply = await contract.methods.totalSupply().call();
+
+ // FIXME: always equals to 0, because this method is not implemented
+ expect(totalSupply).to.equal('0');
+ });
+
+ itWeb3('balanceOf', async ({ web3 }) => {
+ const collection = await createCollectionExpectSuccess({
+ mode: { type: 'NFT' }
+ });
+ const alice = privateKey('//Alice');
+
+ const caller = createEthAccount(web3);
+ await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });
+ await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });
+ await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });
+
+ const address = collectionIdToAddress(collection);
+ const contract = new web3.eth.Contract(nonFungibleAbi as any, address);
+ const balance = await contract.methods.balanceOf(caller).call();
+
+ expect(balance).to.equal('3');
+ });
+
+ itWeb3('ownerOf', async ({ web3 }) => {
+ const collection = await createCollectionExpectSuccess({
+ mode: { type: 'NFT' }
+ });
+ const alice = privateKey('//Alice');
+
+ const caller = createEthAccount(web3);
+ const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });
+
+ const address = collectionIdToAddress(collection);
+ const contract = new web3.eth.Contract(nonFungibleAbi as any, address);
+ const owner = await contract.methods.ownerOf(tokenId).call();
+
+ expect(owner).to.equal(caller);
+ });
+});
+
+describe.only('Plain calls', () => {
+ itWeb3('Can perform approve()', async ({ web3, api }) => {
+ const collection = await createCollectionExpectSuccess({
+ mode: { type: 'NFT' }
+ });
+ const alice = privateKey('//Alice');
+
+ const owner = createEthAccount(web3);
+ await transferBalanceToEth(api, alice, owner, 999999999999999);
+
+ const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });
+
+ const spender = createEthAccount(web3);
+
+ const address = collectionIdToAddress(collection);
+ const contract = new web3.eth.Contract(nonFungibleAbi as any, address);
+
+ {
+ const result = await contract.methods.approve(spender, tokenId).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });
+ const events = normalizeEvents(result.events);
+
+ expect(events).to.be.deep.equal([
+ {
+ address,
+ event: 'Approval',
+ args: {
+ owner,
+ approved: spender,
+ tokenId: tokenId.toString(),
+ }
+ }
+ ]);
+ }
+ });
+
+ itWeb3('Can perform transferFrom()', async ({ web3, api }) => {
+ const collection = await createCollectionExpectSuccess({
+ mode: { type: 'NFT' }
+ });
+ const alice = privateKey('//Alice');
+
+ const owner = createEthAccount(web3);
+ await transferBalanceToEth(api, alice, owner, 999999999999999);
+
+ const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });
+
+ const spender = createEthAccount(web3);
+ await transferBalanceToEth(api, alice, spender, 999999999999999);
+
+ const receiver = createEthAccount(web3);
+
+ const address = collectionIdToAddress(collection);
+ const contract = new web3.eth.Contract(nonFungibleAbi as any, address);
+
+ await contract.methods.approve(spender, tokenId).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });
+
+ {
+ const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({ from: spender, gas: '0x1000000', gasPrice: '0x01' });
+ const events = normalizeEvents(result.events);
+ expect(events).to.be.deep.equal([
+ {
+ address,
+ event: 'Transfer',
+ args: {
+ from: owner,
+ to: receiver,
+ tokenId: tokenId.toString(),
+ },
+ },
+ ]);
+ }
+
+ {
+ const balance = await contract.methods.balanceOf(receiver).call();
+ expect(+balance).to.equal(1);
+ }
+
+ {
+ const balance = await contract.methods.balanceOf(owner).call();
+ expect(+balance).to.equal(0);
+ }
+ });
+
+ itWeb3('Can perform transfer()', async ({ web3, api }) => {
+ const collection = await createCollectionExpectSuccess({
+ mode: { type: 'NFT' }
+ });
+ const alice = privateKey('//Alice');
+
+ const owner = createEthAccount(web3);
+ await transferBalanceToEth(api, alice, owner, 999999999999999);
+
+ const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });
+
+ const receiver = createEthAccount(web3);
+ await transferBalanceToEth(api, alice, receiver, 999999999999999);
+
+ const address = collectionIdToAddress(collection);
+ const contract = new web3.eth.Contract(nonFungibleAbi as any, address);
+
+ {
+ const result = await contract.methods.transfer(receiver, tokenId).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });
+ console.log(result);
+ const events = normalizeEvents(result.events);
+ expect(events).to.be.deep.equal([
+ {
+ address,
+ event: 'Transfer',
+ args: {
+ from: owner,
+ to: receiver,
+ tokenId: tokenId.toString(),
+ }
+ },
+ ])
+ }
+
+ {
+ const balance = await contract.methods.balanceOf(owner).call();
+ expect(+balance).to.equal(0);
+ }
+
+ {
+ const balance = await contract.methods.balanceOf(receiver).call();
+ expect(+balance).to.equal(1);
+ }
+ });
+});
+
+describe('Substrate calls', () => {
+ itWeb3('Events emitted for approve()', async ({ web3 }) => {
+ const collection = await createCollectionExpectSuccess({
+ mode: { type: 'NFT' }
+ });
+ const alice = privateKey('//Alice');
+
+ const receiver = createEthAccount(web3);
+
+ const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');
+
+ const address = collectionIdToAddress(collection);
+ const contract = new web3.eth.Contract(nonFungibleAbi as any, address);
+
+ const events = await recordEvents(contract, async () => {
+ await approveExpectSuccess(collection, tokenId, alice, { ethereum: receiver }, 1);
+ });
+
+ expect(events).to.be.deep.equal([
+ {
+ address,
+ event: 'Approval',
+ args: {
+ owner: subToEth(alice.address),
+ approved: receiver,
+ tokenId: tokenId.toString(),
+ }
+ }
+ ]);
+ });
+
+ itWeb3('Events emitted for transferFrom()', async ({ web3 }) => {
+ const collection = await createCollectionExpectSuccess({
+ mode: { type: 'NFT' }
+ });
+ const alice = privateKey('//Alice');
+ const bob = privateKey('//Bob');
+
+ const receiver = createEthAccount(web3);
+
+ const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');
+ await approveExpectSuccess(collection, tokenId, alice, bob, 1);
+
+ const address = collectionIdToAddress(collection);
+ const contract = new web3.eth.Contract(nonFungibleAbi as any, address);
+
+ const events = await recordEvents(contract, async () => {
+ await transferFromExpectSuccess(collection, tokenId, bob, alice, { ethereum: receiver }, 1, 'NFT');
+ });
+
+ expect(events).to.be.deep.equal([
+ {
+ address,
+ event: 'Transfer',
+ args: {
+ from: subToEth(alice.address),
+ to: receiver,
+ tokenId: tokenId.toString(),
+ }
+ },
+ ]);
+ });
+
+ itWeb3('Events emitted for transfer()', async ({ web3 }) => {
+ const collection = await createCollectionExpectSuccess({
+ mode: { type: 'NFT' }
+ });
+ const alice = privateKey('//Alice');
+
+ const receiver = createEthAccount(web3);
+
+ const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');
+
+ const address = collectionIdToAddress(collection);
+ const contract = new web3.eth.Contract(nonFungibleAbi as any, address);
+
+ const events = await recordEvents(contract, async () => {
+ await transferExpectSuccess(collection, tokenId, alice, { ethereum: receiver }, 1, 'NFT');
+ });
+
+ expect(events).to.be.deep.equal([
+ {
+ address,
+ event: 'Transfer',
+ args: {
+ from: subToEth(alice.address),
+ to: receiver,
+ tokenId: tokenId.toString(),
+ }
+ },
+ ]);
+ });
+});
\ No newline at end of file
tests/src/eth/nonFungibleAbi.jsondiffbeforeafterboth--- /dev/null
+++ b/tests/src/eth/nonFungibleAbi.json
@@ -0,0 +1,406 @@
+[
+ {
+ "anonymous": false,
+ "inputs": [
+ {
+ "indexed": true,
+ "internalType": "address",
+ "name": "owner",
+ "type": "address"
+ },
+ {
+ "indexed": true,
+ "internalType": "address",
+ "name": "approved",
+ "type": "address"
+ },
+ {
+ "indexed": true,
+ "internalType": "uint256",
+ "name": "tokenId",
+ "type": "uint256"
+ }
+ ],
+ "name": "Approval",
+ "type": "event"
+ },
+ {
+ "anonymous": false,
+ "inputs": [
+ {
+ "indexed": true,
+ "internalType": "address",
+ "name": "owner",
+ "type": "address"
+ },
+ {
+ "indexed": true,
+ "internalType": "address",
+ "name": "operator",
+ "type": "address"
+ },
+ {
+ "indexed": false,
+ "internalType": "bool",
+ "name": "approved",
+ "type": "bool"
+ }
+ ],
+ "name": "ApprovalForAll",
+ "type": "event"
+ },
+ {
+ "anonymous": false,
+ "inputs": [
+ {
+ "indexed": true,
+ "internalType": "address",
+ "name": "from",
+ "type": "address"
+ },
+ {
+ "indexed": true,
+ "internalType": "address",
+ "name": "to",
+ "type": "address"
+ },
+ {
+ "indexed": true,
+ "internalType": "uint256",
+ "name": "tokenId",
+ "type": "uint256"
+ }
+ ],
+ "name": "Transfer",
+ "type": "event"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "address",
+ "name": "approved",
+ "type": "address"
+ },
+ {
+ "internalType": "uint256",
+ "name": "tokenId",
+ "type": "uint256"
+ }
+ ],
+ "name": "approve",
+ "outputs": [],
+ "stateMutability": "payable",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "address",
+ "name": "owner",
+ "type": "address"
+ }
+ ],
+ "name": "balanceOf",
+ "outputs": [
+ {
+ "internalType": "uint256",
+ "name": "",
+ "type": "uint256"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "uint256",
+ "name": "tokenId",
+ "type": "uint256"
+ }
+ ],
+ "name": "getApproved",
+ "outputs": [
+ {
+ "internalType": "address",
+ "name": "",
+ "type": "address"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "address",
+ "name": "owner",
+ "type": "address"
+ },
+ {
+ "internalType": "address",
+ "name": "operator",
+ "type": "address"
+ }
+ ],
+ "name": "isApprovedForAll",
+ "outputs": [
+ {
+ "internalType": "bool",
+ "name": "",
+ "type": "bool"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "inputs": [],
+ "name": "name",
+ "outputs": [
+ {
+ "internalType": "string",
+ "name": "res_name",
+ "type": "string"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "uint256",
+ "name": "tokenId",
+ "type": "uint256"
+ }
+ ],
+ "name": "ownerOf",
+ "outputs": [
+ {
+ "internalType": "address",
+ "name": "",
+ "type": "address"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "address",
+ "name": "from",
+ "type": "address"
+ },
+ {
+ "internalType": "address",
+ "name": "to",
+ "type": "address"
+ },
+ {
+ "internalType": "uint256",
+ "name": "tokenId",
+ "type": "uint256"
+ }
+ ],
+ "name": "safeTransferFrom",
+ "outputs": [],
+ "stateMutability": "payable",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "address",
+ "name": "from",
+ "type": "address"
+ },
+ {
+ "internalType": "address",
+ "name": "to",
+ "type": "address"
+ },
+ {
+ "internalType": "uint256",
+ "name": "tokenId",
+ "type": "uint256"
+ },
+ {
+ "internalType": "bytes",
+ "name": "data",
+ "type": "bytes"
+ }
+ ],
+ "name": "safeTransferFrom",
+ "outputs": [],
+ "stateMutability": "payable",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "address",
+ "name": "operator",
+ "type": "address"
+ },
+ {
+ "internalType": "bool",
+ "name": "approved",
+ "type": "bool"
+ }
+ ],
+ "name": "setApprovalForAll",
+ "outputs": [],
+ "stateMutability": "nonpayable",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "bytes4",
+ "name": "interfaceID",
+ "type": "bytes4"
+ }
+ ],
+ "name": "supportsInterface",
+ "outputs": [
+ {
+ "internalType": "bool",
+ "name": "",
+ "type": "bool"
+ }
+ ],
+ "stateMutability": "pure",
+ "type": "function"
+ },
+ {
+ "inputs": [],
+ "name": "symbol",
+ "outputs": [
+ {
+ "internalType": "string",
+ "name": "res_symbol",
+ "type": "string"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "uint256",
+ "name": "index",
+ "type": "uint256"
+ }
+ ],
+ "name": "tokenByIndex",
+ "outputs": [
+ {
+ "internalType": "uint256",
+ "name": "",
+ "type": "uint256"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "address",
+ "name": "owner",
+ "type": "address"
+ },
+ {
+ "internalType": "uint256",
+ "name": "index",
+ "type": "uint256"
+ }
+ ],
+ "name": "tokenOfOwnerByIndex",
+ "outputs": [
+ {
+ "internalType": "uint256",
+ "name": "",
+ "type": "uint256"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "uint256",
+ "name": "tokenId",
+ "type": "uint256"
+ }
+ ],
+ "name": "tokenURI",
+ "outputs": [
+ {
+ "internalType": "string",
+ "name": "",
+ "type": "string"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "inputs": [],
+ "name": "totalSupply",
+ "outputs": [
+ {
+ "internalType": "uint256",
+ "name": "",
+ "type": "uint256"
+ }
+ ],
+ "stateMutability": "view",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "address",
+ "name": "from",
+ "type": "address"
+ },
+ {
+ "internalType": "address",
+ "name": "to",
+ "type": "address"
+ },
+ {
+ "internalType": "uint256",
+ "name": "tokenId",
+ "type": "uint256"
+ }
+ ],
+ "name": "transferFrom",
+ "outputs": [],
+ "stateMutability": "payable",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "internalType": "address",
+ "name": "to",
+ "type": "address"
+ },
+ {
+ "internalType": "uint256",
+ "name": "tokenId",
+ "type": "uint256"
+ }
+ ],
+ "name": "transfer",
+ "outputs": [],
+ "stateMutability": "payable",
+ "type": "function"
+ }
+]
\ No newline at end of file
tests/src/eth/util/helpers.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/eth/util/helpers.ts
@@ -0,0 +1,116 @@
+import { ApiPromise } from "@polkadot/api";
+import { addressToEvm, evmToAddress } from "@polkadot/util-crypto";
+import Web3 from "web3";
+import usingApi, { submitTransactionAsync } from "../../substrate/substrate-api";
+import { IKeyringPair } from '@polkadot/types/types';
+import { expect } from "chai";
+import { getGenericResult } from "../../util/helpers";
+
+let web3Connected = false;
+export async function usingWeb3<T>(cb: (web3: Web3) => Promise<T> | T): Promise<T> {
+ if (web3Connected) throw new Error('do not nest usingWeb3 calls');
+ web3Connected = true;
+
+ const provider = new Web3.providers.WebsocketProvider("http://localhost:9944");
+ const web3 = new Web3(provider);
+
+ try {
+ return await cb(web3);
+ } finally {
+ // provider.disconnect(3000, 'normal disconnect');
+ provider.connection.close();
+ web3Connected = false;
+ }
+}
+
+export function collectionIdToAddress(address: number): string {
+ if (address >= 0xffffffff || address < 0) throw new Error('id overflow');
+ const buf = Buffer.from([0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,
+ address >> 24,
+ (address >> 16) & 0xff,
+ (address >> 8) & 0xff,
+ address & 0xff,
+ ]);
+ return Web3.utils.toChecksumAddress('0x' + buf.toString('hex'));
+}
+
+export function createEthAccount(web3: Web3) {
+ const account = web3.eth.accounts.create();
+ web3.eth.accounts.wallet.add(account.privateKey);
+ return account.address;
+}
+
+export async function transferBalanceToEth(api: ApiPromise, source: IKeyringPair, target: string, amount: number) {
+ const tx = api.tx.balances.transfer(evmToAddress(target), amount);
+ const events = await submitTransactionAsync(source, tx);
+ const result = getGenericResult(events);
+ expect(result.success).to.be.true;
+}
+
+export async function itWeb3(name: string, cb: (apis: { web3: Web3, api: ApiPromise }) => any, opts: { only?: boolean, skip?: boolean } = {}) {
+ let i: any = it;
+ if (opts.only) i = i.only;
+ else if (opts.skip) i = i.skip;
+ i(name, async () => {
+ await usingApi(async api => {
+ await usingWeb3(async web3 => {
+ await cb({ api, web3 });
+ });
+ });
+ });
+}
+itWeb3.only = (name: string, cb: (apis: { web3: Web3, api: ApiPromise }) => any) => itWeb3(name, cb, { only: true });
+itWeb3.skip = (name: string, cb: (apis: { web3: Web3, api: ApiPromise }) => any) => itWeb3(name, cb, { skip: true });
+
+export async function generateSubstrateEthPair(web3: Web3) {
+ let account = web3.eth.accounts.create();
+ const evm = evmToAddress(account.address);
+}
+
+type NormalizedEvent = {
+ address: string,
+ event: string,
+ args: { [key: string]: string }
+};
+
+export function normalizeEvents(events: any): NormalizedEvent[] {
+ const output = [];
+ for (let key of Object.keys(events)) {
+ if (key.match(/^[0-9]+$/)) {
+ output.push(events[key]);
+ } else if (Array.isArray(events[key])) {
+ output.push(...events[key]);
+ } else {
+ output.push(events[key]);
+ }
+ }
+ output.sort((a, b) => a.logIndex - b.logIndex);
+ return output.map(({ address, event, returnValues }) => {
+ const args: { [key: string]: string } = {};
+ for (let key of Object.keys(returnValues)) {
+ if (!key.match(/^[0-9]+$/)) {
+ args[key] = returnValues[key];
+ }
+ }
+ return {
+ address,
+ event,
+ args,
+ };
+ });
+}
+
+export async function recordEvents(contract: any, action: () => Promise<void>): Promise<NormalizedEvent[]> {
+ const out: any = [];
+ contract.events.allEvents((_: any, event: any) => {
+ out.push(event);
+ });
+ await action();
+ return normalizeEvents(out);
+}
+
+export function subToEth(eth: string): string {
+ const bytes = addressToEvm(eth);
+ const string = '0x' + Buffer.from(bytes).toString('hex');
+ return Web3.utils.toChecksumAddress(string);
+}
\ No newline at end of file