difftreelog
eth/proxy/fungibleProxy migrated
in: master
1 file changed
tests/src/eth/proxy/fungibleProxy.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {createCollectionExpectSuccess, createFungibleItemExpectSuccess} from '../../util/helpers';18import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents} from '../util/helpers';19import fungibleAbi from '../fungibleAbi.json';20import {expect} from 'chai';21import {ApiPromise} from '@polkadot/api';22import Web3 from 'web3';23import {readFile} from 'fs/promises';24import {IKeyringPair} from '@polkadot/types/types';2526async function proxyWrap(api: ApiPromise, web3: Web3, wrapped: any, privateKeyWrapper: (account: string) => IKeyringPair) {27 // Proxy owner has no special privilegies, we don't need to reuse them28 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);29 const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {30 from: owner,31 ...GAS_ARGS,32 });33 const proxy = await proxyContract.deploy({data: (await readFile(`${__dirname}/UniqueFungibleProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});34 return proxy;35}3637describe('Fungible (Via EVM proxy): Information getting', () => {38 itWeb3('totalSupply', async ({api, web3, privateKeyWrapper}) => {39 const collection = await createCollectionExpectSuccess({40 name: 'token name',41 mode: {type: 'Fungible', decimalPoints: 0},42 });43 const alice = privateKeyWrapper('//Alice');44 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);4546 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Substrate: alice.address});4748 const address = collectionIdToAddress(collection);49 const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}), privateKeyWrapper);50 const totalSupply = await contract.methods.totalSupply().call();5152 expect(totalSupply).to.equal('200');53 });5455 itWeb3('balanceOf', async ({api, web3, privateKeyWrapper}) => {56 const collection = await createCollectionExpectSuccess({57 name: 'token name',58 mode: {type: 'Fungible', decimalPoints: 0},59 });60 const alice = privateKeyWrapper('//Alice');61 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);6263 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: caller});6465 const address = collectionIdToAddress(collection);66 const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}), privateKeyWrapper);67 const balance = await contract.methods.balanceOf(caller).call();6869 expect(balance).to.equal('200');70 });71});7273describe('Fungible (Via EVM proxy): Plain calls', () => {74 itWeb3('Can perform approve()', async ({web3, api, privateKeyWrapper}) => {75 const collection = await createCollectionExpectSuccess({76 name: 'token name',77 mode: {type: 'Fungible', decimalPoints: 0},78 });79 const alice = privateKeyWrapper('//Alice');80 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);81 const spender = createEthAccount(web3);8283 const address = collectionIdToAddress(collection);84 const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}), privateKeyWrapper);85 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: contract.options.address});8687 {88 const result = await contract.methods.approve(spender, 100).send({from: caller});89 const events = normalizeEvents(result.events);9091 expect(events).to.be.deep.equal([92 {93 address,94 event: 'Approval',95 args: {96 owner: contract.options.address,97 spender,98 value: '100',99 },100 },101 ]);102 }103104 {105 const allowance = await contract.methods.allowance(contract.options.address, spender).call();106 expect(+allowance).to.equal(100);107 }108 });109110 itWeb3('Can perform transferFrom()', async ({web3, api, privateKeyWrapper}) => {111 const collection = await createCollectionExpectSuccess({112 name: 'token name',113 mode: {type: 'Fungible', decimalPoints: 0},114 });115 const alice = privateKeyWrapper('//Alice');116 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);117 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);118119 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});120121 const receiver = createEthAccount(web3);122123 const address = collectionIdToAddress(collection);124 const evmCollection = new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS});125 const contract = await proxyWrap(api, web3, evmCollection, privateKeyWrapper);126127 await evmCollection.methods.approve(contract.options.address, 100).send({from: owner});128129 {130 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: caller});131 const events = normalizeEvents(result.events);132 expect(events).to.be.deep.equal([133 {134 address,135 event: 'Transfer',136 args: {137 from: owner,138 to: receiver,139 value: '49',140 },141 },142 {143 address,144 event: 'Approval',145 args: {146 owner,147 spender: contract.options.address,148 value: '51',149 },150 },151 ]);152 }153154 {155 const balance = await contract.methods.balanceOf(receiver).call();156 expect(+balance).to.equal(49);157 }158159 {160 const balance = await contract.methods.balanceOf(owner).call();161 expect(+balance).to.equal(151);162 }163 });164165 itWeb3('Can perform transfer()', async ({web3, api, privateKeyWrapper}) => {166 const collection = await createCollectionExpectSuccess({167 name: 'token name',168 mode: {type: 'Fungible', decimalPoints: 0},169 });170 const alice = privateKeyWrapper('//Alice');171 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);172 const receiver = await createEthAccountWithBalance(api, web3, privateKeyWrapper);173174 const address = collectionIdToAddress(collection);175 const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}), privateKeyWrapper);176 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: contract.options.address});177178 {179 const result = await contract.methods.transfer(receiver, 50).send({from: caller});180 const events = normalizeEvents(result.events);181 expect(events).to.be.deep.equal([182 {183 address,184 event: 'Transfer',185 args: {186 from: contract.options.address,187 to: receiver,188 value: '50',189 },190 },191 ]);192 }193194 {195 const balance = await contract.methods.balanceOf(contract.options.address).call();196 expect(+balance).to.equal(150);197 }198199 {200 const balance = await contract.methods.balanceOf(receiver).call();201 expect(+balance).to.equal(50);202 }203 });204});1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {GAS_ARGS, normalizeEvents} from '../util/helpers';18import {expect} from 'chai';19import {readFile} from 'fs/promises';20import {IKeyringPair} from '@polkadot/types/types';21import {EthUniqueHelper, itEth, usingEthPlaygrounds} from '../util/playgrounds';2223async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {24 // Proxy owner has no special privilegies, we don't need to reuse them25 const owner = await helper.eth.createAccountWithBalance(donor);26 const proxyContract = new helper.web3!.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {27 from: owner,28 ...GAS_ARGS,29 });30 const proxy = await proxyContract.deploy({data: (await readFile(`${__dirname}/UniqueFungibleProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});31 return proxy;32}3334describe('Fungible (Via EVM proxy): Information getting', () => {35 let alice: IKeyringPair;36 let donor: IKeyringPair;3738 before(async function() {39 await usingEthPlaygrounds(async (helper, privateKey) => {40 donor = privateKey('//Alice');41 [alice] = await helper.arrange.createAccounts([10n], donor);42 });43 });4445 itEth('totalSupply', async ({helper}) => {46 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);47 const caller = await helper.eth.createAccountWithBalance(donor);48 await collection.mint(alice, 200n, {Substrate: alice.address});4950 const address = helper.ethAddress.fromCollectionId(collection.collectionId);51 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);52 const contract = await proxyWrap(helper, evmCollection, donor);53 const totalSupply = await contract.methods.totalSupply().call();5455 expect(totalSupply).to.equal('200');56 });5758 itEth('balanceOf', async ({helper}) => {59 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);60 const caller = await helper.eth.createAccountWithBalance(donor);6162 await collection.mint(alice, 200n, {Ethereum: caller});6364 const address = helper.ethAddress.fromCollectionId(collection.collectionId);65 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);66 const contract = await proxyWrap(helper, evmCollection, donor);67 const balance = await contract.methods.balanceOf(caller).call();6869 expect(balance).to.equal('200');70 });71});7273describe('Fungible (Via EVM proxy): Plain calls', () => {74 let alice: IKeyringPair;75 let donor: IKeyringPair;7677 before(async function() {78 await usingEthPlaygrounds(async (helper, privateKey) => {79 donor = privateKey('//Alice');80 [alice] = await helper.arrange.createAccounts([10n], donor);81 });82 });8384 itEth('Can perform approve()', async ({helper}) => {85 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);86 const caller = await helper.eth.createAccountWithBalance(donor);87 const spender = helper.eth.createAccount();8889 const address = helper.ethAddress.fromCollectionId(collection.collectionId);90 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);91 const contract = await proxyWrap(helper, evmCollection, donor);92 await collection.mint(alice, 200n, {Ethereum: contract.options.address});9394 {95 const result = await contract.methods.approve(spender, 100).send({from: caller});96 const events = normalizeEvents(result.events);9798 expect(events).to.be.deep.equal([99 {100 address,101 event: 'Approval',102 args: {103 owner: contract.options.address,104 spender,105 value: '100',106 },107 },108 ]);109 }110111 {112 const allowance = await contract.methods.allowance(contract.options.address, spender).call();113 expect(+allowance).to.equal(100);114 }115 });116117 itEth('Can perform transferFrom()', async ({helper}) => {118 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);119 const caller = await helper.eth.createAccountWithBalance(donor);120 const owner = await helper.eth.createAccountWithBalance(donor);121122 await collection.mint(alice, 200n, {Ethereum: owner});123 const receiver = helper.eth.createAccount();124125 const address = helper.ethAddress.fromCollectionId(collection.collectionId);126 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);127 const contract = await proxyWrap(helper, evmCollection, donor);128129 await evmCollection.methods.approve(contract.options.address, 100).send({from: owner});130131 {132 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: caller});133 const events = normalizeEvents(result.events);134 expect(events).to.be.deep.equal([135 {136 address,137 event: 'Transfer',138 args: {139 from: owner,140 to: receiver,141 value: '49',142 },143 },144 {145 address,146 event: 'Approval',147 args: {148 owner,149 spender: contract.options.address,150 value: '51',151 },152 },153 ]);154 }155156 {157 const balance = await contract.methods.balanceOf(receiver).call();158 expect(+balance).to.equal(49);159 }160161 {162 const balance = await contract.methods.balanceOf(owner).call();163 expect(+balance).to.equal(151);164 }165 });166167 itEth('Can perform transfer()', async ({helper}) => {168 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);169 const caller = await helper.eth.createAccountWithBalance(donor);170 const receiver = await helper.eth.createAccountWithBalance(donor);171172 const address = helper.ethAddress.fromCollectionId(collection.collectionId);173 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);174 const contract = await proxyWrap(helper, evmCollection, donor);175 await collection.mint(alice, 200n, {Ethereum: contract.options.address});176177 {178 const result = await contract.methods.transfer(receiver, 50).send({from: caller});179 const events = normalizeEvents(result.events);180 expect(events).to.be.deep.equal([181 {182 address,183 event: 'Transfer',184 args: {185 from: contract.options.address,186 to: receiver,187 value: '50',188 },189 },190 ]);191 }192193 {194 const balance = await contract.methods.balanceOf(contract.options.address).call();195 expect(+balance).to.equal(150);196 }197198 {199 const balance = await contract.methods.balanceOf(receiver).call();200 expect(+balance).to.equal(50);201 }202 });203});