difftreelog
Add test to transfer NFT using smart contract
in: master
5 files changed
tests/src/addToContractWhiteList.test.tsdiffbeforeafterboth141415describe('Integration Test addToContractWhiteList', () => {15describe('Integration Test addToContractWhiteList', () => {161617 it.only(`Add an address to a contract white list`, async () => {17 it(`Add an address to a contract white list`, async () => {18 await usingApi(async api => {18 await usingApi(async api => {19 const bob = privateKey("//Bob");19 const bob = privateKey("//Bob");20 const [contract, deployer] = await deployFlipper(api);20 const [contract, deployer] = await deployFlipper(api);tests/src/contracts.test.tsdiffbeforeafterboth6import privateKey from "./substrate/privateKey";6import privateKey from "./substrate/privateKey";7import {7import {8 deployFlipper,8 deployFlipper,9 getFlipValue9 getFlipValue,10 deployTransferContract,10} from "./util/contracthelpers";11} from "./util/contracthelpers";1213import {14 createCollectionExpectSuccess,15 createItemExpectSuccess,16 getGenericResult17} from "./util/helpers";18111912chai.use(chaiAsPromised);20chai.use(chaiAsPromised);23 const initialGetResponse = await getFlipValue(contract, deployer);31 const initialGetResponse = await getFlipValue(contract, deployer);243225 const bob = privateKey("//Bob");33 const bob = privateKey("//Bob");26 const flip = contract.exec('flip', value, gasLimit);34 const flip = contract.tx.flip(value, gasLimit);27 await submitTransactionAsync(bob, flip);35 await submitTransactionAsync(bob, flip);283629 const afterFlipGetResponse = await getFlipValue(contract, deployer);37 const afterFlipGetResponse = await getFlipValue(contract, deployer);41 });49 });42 });50 });435144 it.skip('Can transfer balance using smart contract.', async () => {52 it('Can transfer NFT using smart contract.', async () => {45 await usingApi(async api => {53 await usingApi(async api => {46 // const [alicesBalanceBefore, bobsBalanceBefore] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);54 const alice = privateKey("//Alice");47 // const wasm = fs.readFileSync('./src/balance-transfer-contract/calls.wasm');55 const bob = privateKey("//Bob");48 // const contract = compactAddLength(u8aToU8a(wasm));564957 // Prep work50 // const metadata = JSON.parse(fs.readFileSync('./src/balance-transfer-contract/metadata.json').toString('utf-8'));58 const collectionId = await createCollectionExpectSuccess();51 // const abi = new Abi(api.registry as any, metadata);59 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');5260 const [contract, deployer] = await deployTransferContract(api);53 // const alicesPrivateKey = privateKey('//Alice');61 const tokenBefore: any = await api.query.nft.nftItemList(collectionId, tokenId);5462 55 // const contractHash = await deployContract(api, contract, alicesPrivateKey);63 // Transfer5664 const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);57 // // const args = abi.constructors[0]();65 const events = await submitTransactionAsync(alice, transferTx);58 // const instanceAccountId = await instantiateContract(api, contractHash, /*args,*/ alicesPrivateKey);66 const result = getGenericResult(events);59 // const contractInstance = new ContractPromise(api, abi, instanceAccountId);67 const tokenAfter: any = await api.query.nft.nftItemList(collectionId, tokenId);60 // const bob = new GenericAccountId(api.registry, bobsPublicKey);686169 // tslint:disable-next-line:no-unused-expression62 // const transfer = contractInstance.exec('balance_transfer', 0, 1000000000000n, [bob, new u128(api.registry, 1000000)]);70 expect(result.success).to.be.true;63 // await submitTransactionAsync(alicesPrivateKey, transfer);71 expect(tokenBefore.Owner.toString()).to.be.equal(alice.address);6472 expect(tokenAfter.Owner.toString()).to.be.equal(bob.address);65 // const [alicesBalanceAfter, bobsBalanceAfter] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);6667 // expect(alicesBalanceAfter < alicesBalanceBefore).to.be.true;68 // expect(bobsBalanceAfter > bobsBalanceBefore).to.be.true;69 });73 });70 });74 });71});75});tests/src/transfer_contract/metadata.jsondiffbeforeafterbothno changes
tests/src/transfer_contract/nft_transfer.wasmdiffbeforeafterbothbinary blob — no preview
tests/src/util/contracthelpers.tsdiffbeforeafterboth37function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {37function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {38 return new Promise<any>(async (resolve, reject) => {38 return new Promise<any>(async (resolve, reject) => {39 const initValue = true;39 const initValue = true;40 const constructorIndex = 0;4142 // const unsub = await blueprint43 // .createContract(constructorIndex, { gasLimit: gasLimit, salt: null, value: endowment }, initValue)4445 // const unsub = await blueprint.tx46 // .new({ gasLimit: gasLimit, salt: null, value: endowment }, initValue)474048 const unsub = await blueprint.tx41 const unsub = await blueprint.tx49 .new(endowment, gasLimit, initValue)42 .new(endowment, gasLimit, initValue)50 .signAndSend(alice, (result) => {43 .signAndSend(alice, (result) => {51 if (result.status.isInBlock || result.status.isFinalized) {44 if (result.status.isInBlock || result.status.isFinalized) {5253 console.log("Contract deployed: ", result);5455 unsub();45 unsub();56 resolve(result);46 resolve(result);67 const keyring = new Keyring({ type: 'sr25519' });57 const keyring = new Keyring({ type: 'sr25519' });68 const alice = keyring.addFromUri(`//Alice`);58 const alice = keyring.addFromUri(`//Alice`);69 let amount = new BigNumber(endowment);59 let amount = new BigNumber(endowment);70 amount = amount.plus(1e15);60 amount = amount.plus(100e15);71 const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());61 const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());72 await submitTransactionAsync(alice, tx);62 await submitTransactionAsync(alice, tx);7363102 return (result.result.asOk.data[0] == 0x00) ? false : true;92 return (result.result.asOk.data[0] == 0x00) ? false : true;103}93}9495function instantiateTransferContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {96 return new Promise<any>(async (resolve, reject) => {97 const unsub = await blueprint.tx98 .default(endowment, gasLimit)99 .signAndSend(alice, (result) => {100 if (result.status.isInBlock || result.status.isFinalized) {101 unsub();102 resolve(result);103 }104 }); 105 });106}107108export async function deployTransferContract(api: ApiPromise): Promise<[Contract, IKeyringPair]> {109 const metadata = JSON.parse(fs.readFileSync('./src/transfer_contract/metadata.json').toString('utf-8'));110 const abi = new Abi(metadata);111112 const deployer = await prepareDeployer(api);113114 const wasm = fs.readFileSync('./src/transfer_contract/nft_transfer.wasm');115116 const code = new CodePromise(api, abi, wasm);117118 const blueprint = await deployBlueprint(deployer, code);119 const contract = (await instantiateTransferContract(deployer, blueprint))['contract'] as Contract;120121 return [contract, deployer];122}104123