git.delta.rocks / unique-network / refs/commits / b6f730ecff68

difftreelog

source

tests/src/eth/nonFungible.test.ts10.1 KiBsourcehistory
1import privateKey from "../substrate/privateKey";2import { approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, createItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from "../util/helpers";3import { collectionIdToAddress, createEthAccount, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from "./util/helpers"4import nonFungibleAbi from './nonFungibleAbi.json';5import { expect } from "chai";67describe('Information getting', () => {8    itWeb3('totalSupply', async ({ web3 }) => {9        const collection = await createCollectionExpectSuccess({10            mode: { type: 'NFT' }11        });12        const alice = privateKey('//Alice');1314        await createItemExpectSuccess(alice, collection, 'NFT', { substrate: alice.address });1516        const address = collectionIdToAddress(collection);17        const contract = new web3.eth.Contract(nonFungibleAbi as any, address);18        const totalSupply = await contract.methods.totalSupply().call();1920        // FIXME: always equals to 0, because this method is not implemented21        expect(totalSupply).to.equal('0');22    });2324    itWeb3('balanceOf', async ({ web3 }) => {25        const collection = await createCollectionExpectSuccess({26            mode: { type: 'NFT' }27        });28        const alice = privateKey('//Alice');2930        const caller = createEthAccount(web3);31        await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });32        await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });33        await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });3435        const address = collectionIdToAddress(collection);36        const contract = new web3.eth.Contract(nonFungibleAbi as any, address);37        const balance = await contract.methods.balanceOf(caller).call();3839        expect(balance).to.equal('3');40    });4142    itWeb3('ownerOf', async ({ web3 }) => {43        const collection = await createCollectionExpectSuccess({44            mode: { type: 'NFT' }45        });46        const alice = privateKey('//Alice');4748        const caller = createEthAccount(web3);49        const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });5051        const address = collectionIdToAddress(collection);52        const contract = new web3.eth.Contract(nonFungibleAbi as any, address);53        const owner = await contract.methods.ownerOf(tokenId).call();5455        expect(owner).to.equal(caller);56    });57});5859describe.only('Plain calls', () => {60    itWeb3('Can perform approve()', async ({ web3, api }) => {61        const collection = await createCollectionExpectSuccess({62            mode: { type: 'NFT' }63        });64        const alice = privateKey('//Alice');6566        const owner = createEthAccount(web3);67        await transferBalanceToEth(api, alice, owner, 999999999999999);6869        const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });7071        const spender = createEthAccount(web3);7273        const address = collectionIdToAddress(collection);74        const contract = new web3.eth.Contract(nonFungibleAbi as any, address);7576        {77            const result = await contract.methods.approve(spender, tokenId).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });78            const events = normalizeEvents(result.events);7980            expect(events).to.be.deep.equal([81                {82                    address,83                    event: 'Approval',84                    args: {85                        owner,86                        approved: spender,87                        tokenId: tokenId.toString(),88                    }89                }90            ]);91        }92    });9394    itWeb3('Can perform transferFrom()', async ({ web3, api }) => {95        const collection = await createCollectionExpectSuccess({96            mode: { type: 'NFT' }97        });98        const alice = privateKey('//Alice');99100        const owner = createEthAccount(web3);101        await transferBalanceToEth(api, alice, owner, 999999999999999);102103        const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });104105        const spender = createEthAccount(web3);106        await transferBalanceToEth(api, alice, spender, 999999999999999);107108        const receiver = createEthAccount(web3);109110        const address = collectionIdToAddress(collection);111        const contract = new web3.eth.Contract(nonFungibleAbi as any, address);112113        await contract.methods.approve(spender, tokenId).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });114115        {116            const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({ from: spender, gas: '0x1000000', gasPrice: '0x01' });117            const events = normalizeEvents(result.events);118            expect(events).to.be.deep.equal([119                {120                    address,121                    event: 'Transfer',122                    args: {123                        from: owner,124                        to: receiver,125                        tokenId: tokenId.toString(),126                    },127                },128            ]);129        }130131        {132            const balance = await contract.methods.balanceOf(receiver).call();133            expect(+balance).to.equal(1);134        }135136        {137            const balance = await contract.methods.balanceOf(owner).call();138            expect(+balance).to.equal(0);139        }140    });141142    itWeb3('Can perform transfer()', async ({ web3, api }) => {143        const collection = await createCollectionExpectSuccess({144            mode: { type: 'NFT' }145        });146        const alice = privateKey('//Alice');147148        const owner = createEthAccount(web3);149        await transferBalanceToEth(api, alice, owner, 999999999999999);150151        const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });152153        const receiver = createEthAccount(web3);154        await transferBalanceToEth(api, alice, receiver, 999999999999999);155156        const address = collectionIdToAddress(collection);157        const contract = new web3.eth.Contract(nonFungibleAbi as any, address);158159        {160            const result = await contract.methods.transfer(receiver, tokenId).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });161            console.log(result);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                        tokenId: tokenId.toString(),171                    }172                },173            ])174        }175176        {177            const balance = await contract.methods.balanceOf(owner).call();178            expect(+balance).to.equal(0);179        }180181        {182            const balance = await contract.methods.balanceOf(receiver).call();183            expect(+balance).to.equal(1);184        }185    });186});187188describe('Substrate calls', () => {189    itWeb3('Events emitted for approve()', async ({ web3 }) => {190        const collection = await createCollectionExpectSuccess({191            mode: { type: 'NFT' }192        });193        const alice = privateKey('//Alice');194195        const receiver = createEthAccount(web3);196197        const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');198199        const address = collectionIdToAddress(collection);200        const contract = new web3.eth.Contract(nonFungibleAbi as any, address);201202        const events = await recordEvents(contract, async () => {203            await approveExpectSuccess(collection, tokenId, alice, { ethereum: receiver }, 1);204        });205206        expect(events).to.be.deep.equal([207            {208                address,209                event: 'Approval',210                args: {211                    owner: subToEth(alice.address),212                    approved: receiver,213                    tokenId: tokenId.toString(),214                }215            }216        ]);217    });218219    itWeb3('Events emitted for transferFrom()', async ({ web3 }) => {220        const collection = await createCollectionExpectSuccess({221            mode: { type: 'NFT' }222        });223        const alice = privateKey('//Alice');224        const bob = privateKey('//Bob');225226        const receiver = createEthAccount(web3);227228        const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');229        await approveExpectSuccess(collection, tokenId, alice, bob, 1);230231        const address = collectionIdToAddress(collection);232        const contract = new web3.eth.Contract(nonFungibleAbi as any, address);233234        const events = await recordEvents(contract, async () => {235            await transferFromExpectSuccess(collection, tokenId, bob, alice, { ethereum: receiver }, 1, 'NFT');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                    tokenId: tokenId.toString(),246                }247            },248        ]);249    });250251    itWeb3('Events emitted for transfer()', async ({ web3 }) => {252        const collection = await createCollectionExpectSuccess({253            mode: { type: 'NFT' }254        });255        const alice = privateKey('//Alice');256257        const receiver = createEthAccount(web3);258259        const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');260261        const address = collectionIdToAddress(collection);262        const contract = new web3.eth.Contract(nonFungibleAbi as any, address);263264        const events = await recordEvents(contract, async () => {265            await transferExpectSuccess(collection, tokenId, alice, { ethereum: receiver }, 1, 'NFT');266        });267268        expect(events).to.be.deep.equal([269            {270                address,271                event: 'Transfer',272                args: {273                    from: subToEth(alice.address),274                    to: receiver,275                    tokenId: tokenId.toString(),276                }277            },278        ]);279    });280});