git.delta.rocks / unique-network / refs/commits / 6df0cdabe849

difftreelog

test basic tests for CE

Yaroslav Bolyukin2021-06-04parent: #a0285b6.patch.diff
in: master

1 file changed

modifiedtests/src/contracts.test.tsdiffbeforeafterboth
16} from "./util/contracthelpers";16} from "./util/contracthelpers";
1717
18import {18import {
19 addToWhiteListExpectSuccess,
20 approveExpectSuccess,
19 createCollectionExpectSuccess,21 createCollectionExpectSuccess,
20 createItemExpectSuccess,22 createItemExpectSuccess,
23 enablePublicMintingExpectSuccess,
24 enableWhiteListExpectSuccess,
21 getGenericResult25 getGenericResult,
26 isWhitelisted,
27 transferFromExpectSuccess
22} from "./util/helpers";28} from "./util/helpers";
2329
2430
25chai.use(chaiAsPromised);31chai.use(chaiAsPromised);
26const expect = chai.expect;32const expect = chai.expect;
2733
28const value = 0;34const value = 0;
29const gasLimit = 3000n * 1000000n;35const gasLimit = 9000n * 1000000n;
30const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';36const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';
37
38describe('Contracts', () => {
39 it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {
40 await usingApi(async api => {
41 const [contract, deployer] = await deployFlipper(api);
42 const initialGetResponse = await getFlipValue(contract, deployer);
43
44 const bob = privateKey("//Bob");
45 const flip = contract.tx.flip(value, gasLimit);
46 await submitTransactionAsync(bob, flip);
47
48 const afterFlipGetResponse = await getFlipValue(contract, deployer);
49 expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');
50 });
51 });
52
53 it('Can initialize contract instance', async () => {
54 await usingApi(async (api) => {
55 const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));
56 const abi = new Abi(metadata);
57 const newContractInstance = new Contract(api, abi, marketContractAddress);
58 expect(newContractInstance).to.have.property('address');
59 expect(newContractInstance.address.toString()).to.equal(marketContractAddress);
60 });
61 });
62});
3163
32describe('Contracts', () => {64describe.only('Chain extensions', () => {
33 it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {65 it('Transfer CE', async () => {
34 await usingApi(async api => {66 await usingApi(async api => {
67 const alice = privateKey("//Alice");
68 const bob = privateKey("//Bob");
69
70 // Prep work
71 const collectionId = await createCollectionExpectSuccess();
72 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');
35 const [contract, deployer] = await deployFlipper(api);73 const [contract, deployer] = await deployTransferContract(api);
36 const initialGetResponse = await getFlipValue(contract, deployer);74 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, contract.address);
75 await submitTransactionAsync(alice, changeAdminTx);
3776
38 const bob = privateKey("//Bob");77 const tokenBefore: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();
78
79 // Transfer
39 const flip = contract.tx.flip(value, gasLimit);80 const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);
40 await submitTransactionAsync(bob, flip);81 const events = await submitTransactionAsync(alice, transferTx);
4182 const result = getGenericResult(events);
42 const afterFlipGetResponse = await getFlipValue(contract, deployer);83 const tokenAfter: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();
84
85 // tslint:disable-next-line:no-unused-expression
86 expect(result.success).to.be.true;
43 expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');87 expect(tokenBefore.Owner.toString()).to.be.equal(alice.address);
88 expect(tokenAfter.Owner.toString()).to.be.equal(bob.address);
44 });89 });
45 });90 });
91
92 it('Mint CE', async () => {
93 await usingApi(async api => {
94 const alice = privateKey('//Alice');
95 const bob = privateKey('//Bob');
96
97 const collectionId = await createCollectionExpectSuccess();
98 const [contract, deployer] = await deployTransferContract(api);
99 await enablePublicMintingExpectSuccess(alice, collectionId);
100 await enableWhiteListExpectSuccess(alice, collectionId);
101 await addToWhiteListExpectSuccess(alice, collectionId, contract.address);
102 await addToWhiteListExpectSuccess(alice, collectionId, bob.address);
103
104 const transferTx = contract.tx.createItem(value, gasLimit, bob.address, collectionId, { Nft: {const_data: '0x010203', variable_data: '0x020304' }});
105 const events = await submitTransactionAsync(alice, transferTx);
106 const result = getGenericResult(events);
107 expect(result.success).to.be.true;
108
109 const tokensAfter: any = (await api.query.nft.nftItemList.entries(collectionId) as any).map((kv: any) => kv[1].toJSON());
110 expect(tokensAfter).to.be.deep.equal([
111 {
112 Owner: bob.address,
113 ConstData: '0x010203',
114 VariableData: '0x020304',
115 },
116 ]);
117 });
118 });
46119
47 it('Can initialize contract instance', async () => {120 it('Bulk mint CE', async () => {
48 await usingApi(async (api) => {121 await usingApi(async api => {
122 const alice = privateKey('//Alice');
123 const bob = privateKey('//Bob');
124
125 const collectionId = await createCollectionExpectSuccess();
126 const [contract, deployer] = await deployTransferContract(api);
127 await enablePublicMintingExpectSuccess(alice, collectionId);
128 await enableWhiteListExpectSuccess(alice, collectionId);
129 await addToWhiteListExpectSuccess(alice, collectionId, contract.address);
130 await addToWhiteListExpectSuccess(alice, collectionId, bob.address);
131
49 const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));132 const transferTx = contract.tx.createMultipleItems(value, gasLimit, bob.address, collectionId, [
133 { Nft: { const_data: '0x010203', variable_data: '0x020304' } },
134 { Nft: { const_data: '0x010204', variable_data: '0x020305' } },
135 { Nft: { const_data: '0x010205', variable_data: '0x020306' } }
136 ]);
50 const abi = new Abi(metadata);137 const events = await submitTransactionAsync(alice, transferTx);
51 const newContractInstance = new Contract(api, abi, marketContractAddress);138 const result = getGenericResult(events);
52 expect(newContractInstance).to.have.property('address');139 expect(result.success).to.be.true;
140
53 expect(newContractInstance.address.toString()).to.equal(marketContractAddress);141 const tokensAfter: any = (await api.query.nft.nftItemList.entries(collectionId) as any)
142 .map((kv: any) => kv[1].toJSON())
143 .sort((a: any, b: any) => a.ConstData.localeCompare(b.ConstData));
144 expect(tokensAfter).to.be.deep.equal([
145 {
146 Owner: bob.address,
147 ConstData: '0x010203',
148 VariableData: '0x020304',
149 },
150 {
151 Owner: bob.address,
152 ConstData: '0x010204',
153 VariableData: '0x020305',
154 },
155 {
156 Owner: bob.address,
157 ConstData: '0x010205',
158 VariableData: '0x020306',
159 },
160 ]);
54 });161 });
55 });162 });
163
164 it('Approve CE', async () => {
165 await usingApi(async api => {
166 const alice = privateKey('//Alice');
167 const bob = privateKey('//Bob');
168 const charlie = privateKey('//Charlie');
169
170 const collectionId = await createCollectionExpectSuccess();
171 const [contract, deployer] = await deployTransferContract(api);
172 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address);
173
174 const transferTx = contract.tx.approve(value, gasLimit, bob.address, collectionId, tokenId, 1);
175 const events = await submitTransactionAsync(alice, transferTx);
176 const result = getGenericResult(events);
177 expect(result.success).to.be.true;
178
179 await transferFromExpectSuccess(collectionId, tokenId, bob, contract.address.toString(), charlie, 1, 'NFT');
180 });
181 });
56182
57 it('Can transfer NFT using smart contract.', async () => {183 it('TransferFrom CE', async () => {
58 await usingApi(async api => {184 await usingApi(async api => {
59 const alice = privateKey("//Alice");185 const alice = privateKey('//Alice');
60 const bob = privateKey("//Bob");186 const bob = privateKey('//Bob');
61
62 // Prep work
63 const collectionId = await createCollectionExpectSuccess();187 const charlie = privateKey('//Charlie');
188
64 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');189 const collectionId = await createCollectionExpectSuccess();
65 const [contract, deployer] = await deployTransferContract(api);190 const [contract, deployer] = await deployTransferContract(api);
66 const tokenBefore: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();191 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);
67 192 await approveExpectSuccess(collectionId, tokenId, bob, contract.address.toString(), 1);
68 // Transfer193
69 const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);194 const transferTx = contract.tx.transferFrom(value, gasLimit, bob.address, charlie.address, collectionId, tokenId, 1);
70 const events = await submitTransactionAsync(alice, transferTx);195 const events = await submitTransactionAsync(alice, transferTx);
71 const result = getGenericResult(events);196 const result = getGenericResult(events);
197 expect(result.success).to.be.true;
198
72 const tokenAfter: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();199 const token: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap()
73
74 // tslint:disable-next-line:no-unused-expression
75 expect(result.success).to.be.true;
76 expect(tokenBefore.Owner.toString()).to.be.equal(alice.address);200 expect(token.Owner.toString()).to.be.equal(charlie.address);
77 expect(tokenAfter.Owner.toString()).to.be.equal(bob.address);
78 });201 });
79 });202 });
203
204 it('SetVariableMetaData CE', async () => {
205 await usingApi(async api => {
206 const alice = privateKey('//Alice');
207
208 const collectionId = await createCollectionExpectSuccess();
209 const [contract, deployer] = await deployTransferContract(api);
210 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', contract.address);
211
212 const transferTx = contract.tx.setVariableMetaData(value, gasLimit, collectionId, tokenId, '0x121314');
213 const events = await submitTransactionAsync(alice, transferTx);
214 const result = getGenericResult(events);
215 expect(result.success).to.be.true;
216
217 const token: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap()
218 expect(token.VariableData.toString()).to.be.equal('0x121314');
219 });
220 });
221
222 it('ToggleWhiteList CE', async () => {
223 await usingApi(async api => {
224 const alice = privateKey('//Alice');
225 const bob = privateKey('//Bob');
226
227 const collectionId = await createCollectionExpectSuccess();
228 const [contract, deployer] = await deployTransferContract(api);
229 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, contract.address);
230 await submitTransactionAsync(alice, changeAdminTx);
231
232 expect(await isWhitelisted(collectionId, bob.address)).to.be.false;
233
234 {
235 const transferTx = contract.tx.toggleWhiteList(value, gasLimit, collectionId, bob.address, true);
236 const events = await submitTransactionAsync(alice, transferTx);
237 const result = getGenericResult(events);
238 expect(result.success).to.be.true;
239
240 expect(await isWhitelisted(collectionId, bob.address)).to.be.true;
241 }
242 {
243 const transferTx = contract.tx.toggleWhiteList(value, gasLimit, collectionId, bob.address, false);
244 const events = await submitTransactionAsync(alice, transferTx);
245 const result = getGenericResult(events);
246 expect(result.success).to.be.true;
247
248 expect(await isWhitelisted(collectionId, bob.address)).to.be.false;
249 }
250 });
251 });
80});252});
81253