git.delta.rocks / unique-network / refs/commits / 135789fda83b

difftreelog

Add test to transfer NFT using smart contract

Greg Zaitsev2021-02-01parent: #b5214e3.patch.diff
in: master

5 files changed

modifiedtests/src/addToContractWhiteList.test.tsdiffbeforeafterboth
1414
15describe('Integration Test addToContractWhiteList', () => {15describe('Integration Test addToContractWhiteList', () => {
1616
17 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);
modifiedtests/src/contracts.test.tsdiffbeforeafterboth
6import 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";
12
13import {
14 createCollectionExpectSuccess,
15 createItemExpectSuccess,
16 getGenericResult
17} from "./util/helpers";
18
1119
12chai.use(chaiAsPromised);20chai.use(chaiAsPromised);
23 const initialGetResponse = await getFlipValue(contract, deployer);31 const initialGetResponse = await getFlipValue(contract, deployer);
2432
25 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);
2836
29 const afterFlipGetResponse = await getFlipValue(contract, deployer);37 const afterFlipGetResponse = await getFlipValue(contract, deployer);
41 });49 });
42 });50 });
4351
44 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));56
4957 // Prep work
50 // 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 // Transfer
5664 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);68
6169 // tslint:disable-next-line:no-unused-expression
62 // 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]);
66
67 // expect(alicesBalanceAfter < alicesBalanceBefore).to.be.true;
68 // expect(bobsBalanceAfter > bobsBalanceBefore).to.be.true;
69 });73 });
70 });74 });
71});75});
addedtests/src/transfer_contract/metadata.jsondiffbeforeafterboth

no changes

addedtests/src/transfer_contract/nft_transfer.wasmdiffbeforeafterboth

binary blob — no preview

modifiedtests/src/util/contracthelpers.tsdiffbeforeafterboth
37function 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;
41
42 // const unsub = await blueprint
43 // .createContract(constructorIndex, { gasLimit: gasLimit, salt: null, value: endowment }, initValue)
44
45 // const unsub = await blueprint.tx
46 // .new({ gasLimit: gasLimit, salt: null, value: endowment }, initValue)
4740
48 const unsub = await blueprint.tx41 const unsub = await blueprint.tx
49 .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) {
52
53 console.log("Contract deployed: ", result);
54
55 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);
7363
102 return (result.result.asOk.data[0] == 0x00) ? false : true;92 return (result.result.asOk.data[0] == 0x00) ? false : true;
103}93}
94
95function instantiateTransferContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {
96 return new Promise<any>(async (resolve, reject) => {
97 const unsub = await blueprint.tx
98 .default(endowment, gasLimit)
99 .signAndSend(alice, (result) => {
100 if (result.status.isInBlock || result.status.isFinalized) {
101 unsub();
102 resolve(result);
103 }
104 });
105 });
106}
107
108export 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);
111
112 const deployer = await prepareDeployer(api);
113
114 const wasm = fs.readFileSync('./src/transfer_contract/nft_transfer.wasm');
115
116 const code = new CodePromise(api, abi, wasm);
117
118 const blueprint = await deployBlueprint(deployer, code);
119 const contract = (await instantiateTransferContract(deployer, blueprint))['contract'] as Contract;
120
121 return [contract, deployer];
122}
104123