--- a/tests/src/contracts.test.ts +++ b/tests/src/contracts.test.ts @@ -16,9 +16,15 @@ } from "./util/contracthelpers"; import { + addToWhiteListExpectSuccess, + approveExpectSuccess, createCollectionExpectSuccess, createItemExpectSuccess, - getGenericResult + enablePublicMintingExpectSuccess, + enableWhiteListExpectSuccess, + getGenericResult, + isWhitelisted, + transferFromExpectSuccess } from "./util/helpers"; @@ -26,7 +32,7 @@ const expect = chai.expect; const value = 0; -const gasLimit = 3000n * 1000000n; +const gasLimit = 9000n * 1000000n; const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6'; describe('Contracts', () => { @@ -53,8 +59,10 @@ expect(newContractInstance.address.toString()).to.equal(marketContractAddress); }); }); +}); - it('Can transfer NFT using smart contract.', async () => { +describe.only('Chain extensions', () => { + it('Transfer CE', async () => { await usingApi(async api => { const alice = privateKey("//Alice"); const bob = privateKey("//Bob"); @@ -63,6 +71,9 @@ const collectionId = await createCollectionExpectSuccess(); const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT'); const [contract, deployer] = await deployTransferContract(api); + const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, contract.address); + await submitTransactionAsync(alice, changeAdminTx); + const tokenBefore: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap(); // Transfer @@ -77,4 +88,165 @@ expect(tokenAfter.Owner.toString()).to.be.equal(bob.address); }); }); + + it('Mint CE', async () => { + await usingApi(async api => { + const alice = privateKey('//Alice'); + const bob = privateKey('//Bob'); + + const collectionId = await createCollectionExpectSuccess(); + const [contract, deployer] = await deployTransferContract(api); + await enablePublicMintingExpectSuccess(alice, collectionId); + await enableWhiteListExpectSuccess(alice, collectionId); + await addToWhiteListExpectSuccess(alice, collectionId, contract.address); + await addToWhiteListExpectSuccess(alice, collectionId, bob.address); + + const transferTx = contract.tx.createItem(value, gasLimit, bob.address, collectionId, { Nft: {const_data: '0x010203', variable_data: '0x020304' }}); + const events = await submitTransactionAsync(alice, transferTx); + const result = getGenericResult(events); + expect(result.success).to.be.true; + + const tokensAfter: any = (await api.query.nft.nftItemList.entries(collectionId) as any).map((kv: any) => kv[1].toJSON()); + expect(tokensAfter).to.be.deep.equal([ + { + Owner: bob.address, + ConstData: '0x010203', + VariableData: '0x020304', + }, + ]); + }); + }); + + it('Bulk mint CE', async () => { + await usingApi(async api => { + const alice = privateKey('//Alice'); + const bob = privateKey('//Bob'); + + const collectionId = await createCollectionExpectSuccess(); + const [contract, deployer] = await deployTransferContract(api); + await enablePublicMintingExpectSuccess(alice, collectionId); + await enableWhiteListExpectSuccess(alice, collectionId); + await addToWhiteListExpectSuccess(alice, collectionId, contract.address); + await addToWhiteListExpectSuccess(alice, collectionId, bob.address); + + const transferTx = contract.tx.createMultipleItems(value, gasLimit, bob.address, collectionId, [ + { Nft: { const_data: '0x010203', variable_data: '0x020304' } }, + { Nft: { const_data: '0x010204', variable_data: '0x020305' } }, + { Nft: { const_data: '0x010205', variable_data: '0x020306' } } + ]); + const events = await submitTransactionAsync(alice, transferTx); + const result = getGenericResult(events); + expect(result.success).to.be.true; + + const tokensAfter: any = (await api.query.nft.nftItemList.entries(collectionId) as any) + .map((kv: any) => kv[1].toJSON()) + .sort((a: any, b: any) => a.ConstData.localeCompare(b.ConstData)); + expect(tokensAfter).to.be.deep.equal([ + { + Owner: bob.address, + ConstData: '0x010203', + VariableData: '0x020304', + }, + { + Owner: bob.address, + ConstData: '0x010204', + VariableData: '0x020305', + }, + { + Owner: bob.address, + ConstData: '0x010205', + VariableData: '0x020306', + }, + ]); + }); + }); + + it('Approve CE', async () => { + await usingApi(async api => { + const alice = privateKey('//Alice'); + const bob = privateKey('//Bob'); + const charlie = privateKey('//Charlie'); + + const collectionId = await createCollectionExpectSuccess(); + const [contract, deployer] = await deployTransferContract(api); + const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address); + + const transferTx = contract.tx.approve(value, gasLimit, bob.address, collectionId, tokenId, 1); + const events = await submitTransactionAsync(alice, transferTx); + const result = getGenericResult(events); + expect(result.success).to.be.true; + + await transferFromExpectSuccess(collectionId, tokenId, bob, contract.address.toString(), charlie, 1, 'NFT'); + }); + }); + + it('TransferFrom CE', async () => { + await usingApi(async api => { + const alice = privateKey('//Alice'); + const bob = privateKey('//Bob'); + const charlie = privateKey('//Charlie'); + + const collectionId = await createCollectionExpectSuccess(); + const [contract, deployer] = await deployTransferContract(api); + const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address); + await approveExpectSuccess(collectionId, tokenId, bob, contract.address.toString(), 1); + + const transferTx = contract.tx.transferFrom(value, gasLimit, bob.address, charlie.address, collectionId, tokenId, 1); + const events = await submitTransactionAsync(alice, transferTx); + const result = getGenericResult(events); + expect(result.success).to.be.true; + + const token: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap() + expect(token.Owner.toString()).to.be.equal(charlie.address); + }); + }); + + it('SetVariableMetaData CE', async () => { + await usingApi(async api => { + const alice = privateKey('//Alice'); + + const collectionId = await createCollectionExpectSuccess(); + const [contract, deployer] = await deployTransferContract(api); + const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address); + + const transferTx = contract.tx.setVariableMetaData(value, gasLimit, collectionId, tokenId, '0x121314'); + const events = await submitTransactionAsync(alice, transferTx); + const result = getGenericResult(events); + expect(result.success).to.be.true; + + const token: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap() + expect(token.VariableData.toString()).to.be.equal('0x121314'); + }); + }); + + it('ToggleWhiteList CE', async () => { + await usingApi(async api => { + const alice = privateKey('//Alice'); + const bob = privateKey('//Bob'); + + const collectionId = await createCollectionExpectSuccess(); + const [contract, deployer] = await deployTransferContract(api); + const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, contract.address); + await submitTransactionAsync(alice, changeAdminTx); + + expect(await isWhitelisted(collectionId, bob.address)).to.be.false; + + { + const transferTx = contract.tx.toggleWhiteList(value, gasLimit, collectionId, bob.address, true); + const events = await submitTransactionAsync(alice, transferTx); + const result = getGenericResult(events); + expect(result.success).to.be.true; + + expect(await isWhitelisted(collectionId, bob.address)).to.be.true; + } + { + const transferTx = contract.tx.toggleWhiteList(value, gasLimit, collectionId, bob.address, false); + const events = await submitTransactionAsync(alice, transferTx); + const result = getGenericResult(events); + expect(result.success).to.be.true; + + expect(await isWhitelisted(collectionId, bob.address)).to.be.false; + } + }); + }); });