difftreelog
Add test to transfer NFT using smart contract
in: master
5 files changed
tests/src/addToContractWhiteList.test.tsdiffbeforeafterboth--- a/tests/src/addToContractWhiteList.test.ts
+++ b/tests/src/addToContractWhiteList.test.ts
@@ -14,7 +14,7 @@
describe('Integration Test addToContractWhiteList', () => {
- it.only(`Add an address to a contract white list`, async () => {
+ it(`Add an address to a contract white list`, async () => {
await usingApi(async api => {
const bob = privateKey("//Bob");
const [contract, deployer] = await deployFlipper(api);
tests/src/contracts.test.tsdiffbeforeafterboth--- a/tests/src/contracts.test.ts
+++ b/tests/src/contracts.test.ts
@@ -6,9 +6,17 @@
import privateKey from "./substrate/privateKey";
import {
deployFlipper,
- getFlipValue
+ getFlipValue,
+ deployTransferContract,
} from "./util/contracthelpers";
+import {
+ createCollectionExpectSuccess,
+ createItemExpectSuccess,
+ getGenericResult
+} from "./util/helpers";
+
+
chai.use(chaiAsPromised);
const expect = chai.expect;
@@ -23,7 +31,7 @@
const initialGetResponse = await getFlipValue(contract, deployer);
const bob = privateKey("//Bob");
- const flip = contract.exec('flip', value, gasLimit);
+ const flip = contract.tx.flip(value, gasLimit);
await submitTransactionAsync(bob, flip);
const afterFlipGetResponse = await getFlipValue(contract, deployer);
@@ -41,31 +49,27 @@
});
});
- it.skip('Can transfer balance using smart contract.', async () => {
+ it('Can transfer NFT using smart contract.', async () => {
await usingApi(async api => {
- // const [alicesBalanceBefore, bobsBalanceBefore] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);
- // const wasm = fs.readFileSync('./src/balance-transfer-contract/calls.wasm');
- // const contract = compactAddLength(u8aToU8a(wasm));
+ const alice = privateKey("//Alice");
+ const bob = privateKey("//Bob");
- // const metadata = JSON.parse(fs.readFileSync('./src/balance-transfer-contract/metadata.json').toString('utf-8'));
- // const abi = new Abi(api.registry as any, metadata);
-
- // const alicesPrivateKey = privateKey('//Alice');
+ // Prep work
+ const collectionId = await createCollectionExpectSuccess();
+ const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');
+ const [contract, deployer] = await deployTransferContract(api);
+ const tokenBefore: any = await api.query.nft.nftItemList(collectionId, tokenId);
+
+ // Transfer
+ const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);
+ const events = await submitTransactionAsync(alice, transferTx);
+ const result = getGenericResult(events);
+ const tokenAfter: any = await api.query.nft.nftItemList(collectionId, tokenId);
- // const contractHash = await deployContract(api, contract, alicesPrivateKey);
-
- // // const args = abi.constructors[0]();
- // const instanceAccountId = await instantiateContract(api, contractHash, /*args,*/ alicesPrivateKey);
- // const contractInstance = new ContractPromise(api, abi, instanceAccountId);
- // const bob = new GenericAccountId(api.registry, bobsPublicKey);
-
- // const transfer = contractInstance.exec('balance_transfer', 0, 1000000000000n, [bob, new u128(api.registry, 1000000)]);
- // await submitTransactionAsync(alicesPrivateKey, transfer);
-
- // const [alicesBalanceAfter, bobsBalanceAfter] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);
-
- // expect(alicesBalanceAfter < alicesBalanceBefore).to.be.true;
- // expect(bobsBalanceAfter > bobsBalanceBefore).to.be.true;
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.true;
+ expect(tokenBefore.Owner.toString()).to.be.equal(alice.address);
+ expect(tokenAfter.Owner.toString()).to.be.equal(bob.address);
});
});
});
tests/src/transfer_contract/metadata.jsondiffbeforeafterboth1{2 "metadataVersion": "0.1.0",3 "source": {4 "hash": "0xfbbc0729acefed9d99f93f6cbb751d12e317958fd0fe183f5781329b37f1bf6e",5 "language": "ink! 3.0.0-rc2",6 "compiler": "rustc 1.51.0-nightly"7 },8 "contract": {9 "name": "nft_transfer",10 "version": "0.1.0",11 "authors": [12 "[Greg Zaitsev] <[your_email]>"13 ]14 },15 "spec": {16 "constructors": [17 {18 "args": [],19 "docs": [20 "Default Constructor",21 "",22 "Constructors can delegate to other constructors."23 ],24 "name": [25 "default"26 ],27 "selector": "0x6a3712e2"28 }29 ],30 "docs": [],31 "events": [],32 "messages": [33 {34 "args": [35 {36 "name": "recipient",37 "type": {38 "displayName": [39 "AccountId"40 ],41 "type": 142 }43 },44 {45 "name": "collection_id",46 "type": {47 "displayName": [48 "u32"49 ],50 "type": 451 }52 },53 {54 "name": "token_id",55 "type": {56 "displayName": [57 "u32"58 ],59 "type": 460 }61 },62 {63 "name": "amount",64 "type": {65 "displayName": [66 "u128"67 ],68 "type": 569 }70 }71 ],72 "docs": [73 " Transfer one NFT token"74 ],75 "mutates": true,76 "name": [77 "transfer"78 ],79 "payable": false,80 "returnType": null,81 "selector": "0xfae3a09d"82 }83 ]84 },85 "storage": {86 "struct": {87 "fields": []88 }89 },90 "types": [91 {92 "def": {93 "composite": {94 "fields": [95 {96 "type": 297 }98 ]99 }100 },101 "path": [102 "ink_env",103 "types",104 "AccountId"105 ]106 },107 {108 "def": {109 "array": {110 "len": 32,111 "type": 3112 }113 }114 },115 {116 "def": {117 "primitive": "u8"118 }119 },120 {121 "def": {122 "primitive": "u32"123 }124 },125 {126 "def": {127 "primitive": "u128"128 }129 }130 ]131}tests/src/transfer_contract/nft_transfer.wasmdiffbeforeafterbothbinary blob — no preview
tests/src/util/contracthelpers.tsdiffbeforeafterboth--- a/tests/src/util/contracthelpers.ts
+++ b/tests/src/util/contracthelpers.ts
@@ -37,21 +37,11 @@
function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {
return new Promise<any>(async (resolve, reject) => {
const initValue = true;
- const constructorIndex = 0;
-
- // const unsub = await blueprint
- // .createContract(constructorIndex, { gasLimit: gasLimit, salt: null, value: endowment }, initValue)
- // const unsub = await blueprint.tx
- // .new({ gasLimit: gasLimit, salt: null, value: endowment }, initValue)
-
const unsub = await blueprint.tx
.new(endowment, gasLimit, initValue)
.signAndSend(alice, (result) => {
if (result.status.isInBlock || result.status.isFinalized) {
-
- console.log("Contract deployed: ", result);
-
unsub();
resolve(result);
}
@@ -67,7 +57,7 @@
const keyring = new Keyring({ type: 'sr25519' });
const alice = keyring.addFromUri(`//Alice`);
let amount = new BigNumber(endowment);
- amount = amount.plus(1e15);
+ amount = amount.plus(100e15);
const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());
await submitTransactionAsync(alice, tx);
@@ -101,3 +91,32 @@
}
return (result.result.asOk.data[0] == 0x00) ? false : true;
}
+
+function instantiateTransferContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {
+ return new Promise<any>(async (resolve, reject) => {
+ const unsub = await blueprint.tx
+ .default(endowment, gasLimit)
+ .signAndSend(alice, (result) => {
+ if (result.status.isInBlock || result.status.isFinalized) {
+ unsub();
+ resolve(result);
+ }
+ });
+ });
+}
+
+export async function deployTransferContract(api: ApiPromise): Promise<[Contract, IKeyringPair]> {
+ const metadata = JSON.parse(fs.readFileSync('./src/transfer_contract/metadata.json').toString('utf-8'));
+ const abi = new Abi(metadata);
+
+ const deployer = await prepareDeployer(api);
+
+ const wasm = fs.readFileSync('./src/transfer_contract/nft_transfer.wasm');
+
+ const code = new CodePromise(api, abi, wasm);
+
+ const blueprint = await deployBlueprint(deployer, code);
+ const contract = (await instantiateTransferContract(deployer, blueprint))['contract'] as Contract;
+
+ return [contract, deployer];
+}