1234567891011121314151617import privateKey from '../substrate/privateKey';18import {approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess, UNIQUE} from '../util/helpers';19import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, transferBalanceToEth} from './util/helpers';20import fungibleAbi from './fungibleAbi.json';21import {expect} from 'chai';2223describe('Fungible: Information getting', () => {24 itWeb3('totalSupply', async ({api, web3}) => {25 const collection = await createCollectionExpectSuccess({26 name: 'token name',27 mode: {type: 'Fungible', decimalPoints: 0},28 });29 const alice = privateKey('//Alice');3031 const caller = await createEthAccountWithBalance(api, web3);3233 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Substrate: alice.address});3435 const address = collectionIdToAddress(collection);36 const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS});37 const totalSupply = await contract.methods.totalSupply().call();3839 expect(totalSupply).to.equal('200');40 });4142 itWeb3('balanceOf', async ({api, web3}) => {43 const collection = await createCollectionExpectSuccess({44 name: 'token name',45 mode: {type: 'Fungible', decimalPoints: 0},46 });47 const alice = privateKey('//Alice');4849 const caller = await createEthAccountWithBalance(api, web3);5051 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: caller});5253 const address = collectionIdToAddress(collection);54 const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS});55 const balance = await contract.methods.balanceOf(caller).call();5657 expect(balance).to.equal('200');58 });59});6061describe('Fungible: Plain calls', () => {62 itWeb3('Can perform approve()', async ({web3, api}) => {63 const collection = await createCollectionExpectSuccess({64 name: 'token name',65 mode: {type: 'Fungible', decimalPoints: 0},66 });67 const alice = privateKey('//Alice');6869 const owner = await createEthAccountWithBalance(api, web3);7071 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});7273 const spender = createEthAccount(web3);7475 const address = collectionIdToAddress(collection);76 const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});7778 {79 const result = await contract.methods.approve(spender, 100).send({from: owner});80 const events = normalizeEvents(result.events);8182 expect(events).to.be.deep.equal([83 {84 address,85 event: 'Approval',86 args: {87 owner,88 spender,89 value: '100',90 },91 },92 ]);93 }9495 {96 const allowance = await contract.methods.allowance(owner, spender).call();97 expect(+allowance).to.equal(100);98 }99 });100101 itWeb3('Can perform transferFrom()', async ({web3, api}) => {102 const collection = await createCollectionExpectSuccess({103 name: 'token name',104 mode: {type: 'Fungible', decimalPoints: 0},105 });106 const alice = privateKey('//Alice');107108 const owner = createEthAccount(web3);109 await transferBalanceToEth(api, alice, owner);110111 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});112113 const spender = createEthAccount(web3);114 await transferBalanceToEth(api, alice, spender);115116 const receiver = createEthAccount(web3);117118 const address = collectionIdToAddress(collection);119 const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});120121 await contract.methods.approve(spender, 100).send();122123 {124 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});125 const events = normalizeEvents(result.events);126 expect(events).to.be.deep.equal([127 {128 address,129 event: 'Transfer',130 args: {131 from: owner,132 to: receiver,133 value: '49',134 },135 },136 {137 address,138 event: 'Approval',139 args: {140 owner,141 spender,142 value: '51',143 },144 },145 ]);146 }147148 {149 const balance = await contract.methods.balanceOf(receiver).call();150 expect(+balance).to.equal(49);151 }152153 {154 const balance = await contract.methods.balanceOf(owner).call();155 expect(+balance).to.equal(151);156 }157 });158159 itWeb3('Can perform transfer()', async ({web3, api}) => {160 const collection = await createCollectionExpectSuccess({161 name: 'token name',162 mode: {type: 'Fungible', decimalPoints: 0},163 });164 const alice = privateKey('//Alice');165166 const owner = createEthAccount(web3);167 await transferBalanceToEth(api, alice, owner);168169 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});170171 const receiver = createEthAccount(web3);172 await transferBalanceToEth(api, alice, receiver);173174 const address = collectionIdToAddress(collection);175 const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});176177 {178 const result = await contract.methods.transfer(receiver, 50).send({from: owner});179 const events = normalizeEvents(result.events);180 expect(events).to.be.deep.equal([181 {182 address,183 event: 'Transfer',184 args: {185 from: owner,186 to: receiver,187 value: '50',188 },189 },190 ]);191 }192193 {194 const balance = await contract.methods.balanceOf(owner).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});204205describe('Fungible: Fees', () => {206 itWeb3('approve() call fee is less than 0.2UNQ', async ({web3, api}) => {207 const collection = await createCollectionExpectSuccess({208 mode: {type: 'Fungible', decimalPoints: 0},209 });210 const alice = privateKey('//Alice');211212 const owner = await createEthAccountWithBalance(api, web3);213 const spender = createEthAccount(web3);214215 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});216217 const address = collectionIdToAddress(collection);218 const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});219220 const cost = await recordEthFee(api, owner, () => contract.methods.approve(spender, 100).send({from: owner}));221 expect(cost < BigInt(0.2 * Number(UNIQUE)));222 });223224 itWeb3('transferFrom() call fee is less than 0.2UNQ', async ({web3, api}) => {225 const collection = await createCollectionExpectSuccess({226 mode: {type: 'Fungible', decimalPoints: 0},227 });228 const alice = privateKey('//Alice');229230 const owner = await createEthAccountWithBalance(api, web3);231 const spender = await createEthAccountWithBalance(api, web3);232233 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});234235 const address = collectionIdToAddress(collection);236 const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});237238 await contract.methods.approve(spender, 100).send({from: owner});239240 const cost = await recordEthFee(api, spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));241 expect(cost < BigInt(0.2 * Number(UNIQUE)));242 });243244 itWeb3('transfer() call fee is less than 0.2UNQ', async ({web3, api}) => {245 const collection = await createCollectionExpectSuccess({246 mode: {type: 'Fungible', decimalPoints: 0},247 });248 const alice = privateKey('//Alice');249250 const owner = await createEthAccountWithBalance(api, web3);251 const receiver = createEthAccount(web3);252253 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});254255 const address = collectionIdToAddress(collection);256 const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});257258 const cost = await recordEthFee(api, owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));259 expect(cost < BigInt(0.2 * Number(UNIQUE)));260 });261});262263describe('Fungible: Substrate calls', () => {264 itWeb3('Events emitted for approve()', async ({web3}) => {265 const collection = await createCollectionExpectSuccess({266 mode: {type: 'Fungible', decimalPoints: 0},267 });268 const alice = privateKey('//Alice');269270 const receiver = createEthAccount(web3);271272 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n});273274 const address = collectionIdToAddress(collection);275 const contract = new web3.eth.Contract(fungibleAbi as any, address);276277 const events = await recordEvents(contract, async () => {278 await approveExpectSuccess(collection, 0, alice, {Ethereum: receiver}, 100);279 });280281 expect(events).to.be.deep.equal([282 {283 address,284 event: 'Approval',285 args: {286 owner: subToEth(alice.address),287 spender: receiver,288 value: '100',289 },290 },291 ]);292 });293294 itWeb3('Events emitted for transferFrom()', async ({web3}) => {295 const collection = await createCollectionExpectSuccess({296 mode: {type: 'Fungible', decimalPoints: 0},297 });298 const alice = privateKey('//Alice');299 const bob = privateKey('//Bob');300301 const receiver = createEthAccount(web3);302303 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n});304 await approveExpectSuccess(collection, 0, alice, bob.address, 100);305306 const address = collectionIdToAddress(collection);307 const contract = new web3.eth.Contract(fungibleAbi as any, address);308309 const events = await recordEvents(contract, async () => {310 await transferFromExpectSuccess(collection, 0, bob, alice, {Ethereum: receiver}, 51, 'Fungible');311 });312313 expect(events).to.be.deep.equal([314 {315 address,316 event: 'Transfer',317 args: {318 from: subToEth(alice.address),319 to: receiver,320 value: '51',321 },322 },323 {324 address,325 event: 'Approval',326 args: {327 owner: subToEth(alice.address),328 spender: subToEth(bob.address),329 value: '49',330 },331 },332 ]);333 });334335 itWeb3('Events emitted for transfer()', async ({web3}) => {336 const collection = await createCollectionExpectSuccess({337 mode: {type: 'Fungible', decimalPoints: 0},338 });339 const alice = privateKey('//Alice');340341 const receiver = createEthAccount(web3);342343 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n});344345 const address = collectionIdToAddress(collection);346 const contract = new web3.eth.Contract(fungibleAbi as any, address);347348 const events = await recordEvents(contract, async () => {349 await transferExpectSuccess(collection, 0, alice, {Ethereum:receiver}, 51, 'Fungible');350 });351352 expect(events).to.be.deep.equal([353 {354 address,355 event: 'Transfer',356 args: {357 from: subToEth(alice.address),358 to: receiver,359 value: '51',360 },361 },362 ]);363 });364});