git.delta.rocks / unique-network / refs/commits / 1b4e18a1f423

difftreelog

toggleContractWhiteList tests

Greg Zaitsev2021-01-18parent: #2ace66d.patch.diff
in: master

4 files changed

modifiedtests/package.jsondiffbeforeafterboth
26 "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",26 "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",
27 "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",27 "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",
28 "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",28 "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",
29 "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"29 "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",
30 "testToggleContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/toggleContractWhiteList.test.ts"
30 },31 },
31 "author": "",32 "author": "",
32 "license": "Apache 2.0",33 "license": "Apache 2.0",
modifiedtests/src/contracts.test.tsdiffbeforeafterboth
2import chaiAsPromised from 'chai-as-promised';2import chaiAsPromised from 'chai-as-promised';
3import usingApi, { submitTransactionAsync, submitTransactionExpectFailAsync } from "./substrate/substrate-api";3import usingApi, { submitTransactionAsync, submitTransactionExpectFailAsync } from "./substrate/substrate-api";
4import fs from "fs";4import fs from "fs";
5import { Abi, BlueprintPromise as Blueprint, CodePromise, ContractPromise as Contract } from "@polkadot/api-contract";5import { Abi, ContractPromise as Contract } from "@polkadot/api-contract";
6import { IKeyringPair } from "@polkadot/types/types";6import privateKey from "./substrate/privateKey";
7import { ApiPromise, Keyring } from "@polkadot/api";
8import { ApiTypes, SubmittableExtrinsic } from "@polkadot/api/types";7import {
9import privateKey from "./substrate/privateKey";8 deployFlipper,
9 getFlipValue
10} from "./util/contracthelpers";
1011
11chai.use(chaiAsPromised);12chai.use(chaiAsPromised);
12const expect = chai.expect;13const expect = chai.expect;
13import { BigNumber } from 'bignumber.js';
14import { findUnusedAddress } from './util/helpers';
1514
16const value = 0;15const value = 0;
17const gasLimit = 3000n * 1000000n;16const gasLimit = 3000n * 1000000n;
18const endowment = `1000000000000000`;
19const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';17const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';
20
21function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<Blueprint> {
22 return new Promise<Blueprint>(async (resolve, reject) => {
23 const unsub = await code
24 .createBlueprint()
25 .signAndSend(alice, (result) => {
26 if (result.status.isInBlock || result.status.isFinalized) {
27 // here we have an additional field in the result, containing the blueprint
28 resolve(result.blueprint);
29 unsub();
30 }
31 })
32 });
33}
34
35function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {
36 return new Promise<any>(async (resolve, reject) => {
37 const endowment = 1000000000000000n;
38 const initValue = true;
39
40 const unsub = await blueprint.tx
41 .new(endowment, gasLimit, initValue)
42 .signAndSend(alice, (result) => {
43 if (result.status.isInBlock || result.status.isFinalized) {
44 unsub();
45 resolve(result);
46 }
47 });
48 });
49}
50
51async function prepareDeployer(api: ApiPromise) {
52 // Find unused address
53 const deployer = await findUnusedAddress(api);
54
55 // Transfer balance to it
56 const keyring = new Keyring({ type: 'sr25519' });
57 const alice = keyring.addFromUri(`//Alice`);
58 let amount = new BigNumber(endowment);
59 amount = amount.plus(1e15);
60 const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());
61 await submitTransactionAsync(alice, tx);
62
63 return deployer;
64}
65
66export async function deployFlipper(api: ApiPromise): Promise<[Contract, IKeyringPair]> {
67 const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));
68 const abi = new Abi(metadata);
69
70 const deployer = await prepareDeployer(api);
71
72 const wasm = fs.readFileSync('./src/flipper/flipper.wasm');
73
74 const code = new CodePromise(api, abi, wasm);
75
76 const blueprint = await deployBlueprint(deployer, code);
77 const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;
78
79 const initialGetResponse = await getFlipValue(contract, deployer);
80 expect(initialGetResponse).to.be.true;
81
82 return [contract, deployer];
83}
84
85async function getFlipValue(contract: Contract, deployer: IKeyringPair) {
86 const result = await contract.query.get(deployer.address, value, gasLimit);
87
88 if(!result.result.isSuccess) {
89 throw `Failed to get flipper value`;
90 }
91 return (result.result.asSuccess.data[0] == 0x00) ? false : true;
92}
9318
94describe('Contracts', () => {19describe('Contracts', () => {
95 it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {20 it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {
106 });31 });
107 });32 });
108
109 it(`Whitelisted account can call contract.`, async () => {
110 await usingApi(async api => {
111 const bob = privateKey("//Bob");
112
113 const [contract, deployer] = await deployFlipper(api);
114
115 let expectedFlipValue = await getFlipValue(contract, deployer);
116
117 const flip = contract.exec('flip', value, gasLimit);
118 await submitTransactionAsync(bob, flip);
119 expectedFlipValue = !expectedFlipValue;
120 const afterFlip = await getFlipValue(contract,deployer);
121 expect(afterFlip).to.be.eq(expectedFlipValue, `Anyone can call new contract.`);
122
123 const deployerCanFlip = async () => {
124 expectedFlipValue = !expectedFlipValue;
125 const deployerFlip = contract.exec('flip', value, gasLimit);
126 await submitTransactionAsync(deployer, deployerFlip);
127 const aliceFlip1Response = await getFlipValue(contract, deployer);
128 expect(aliceFlip1Response).to.be.eq(expectedFlipValue, `Deployer always can flip.`);
129 };
130 await deployerCanFlip();
131
132 const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, true);
133 const enableResult = await submitTransactionAsync(deployer, enableWhiteListTx);
134 const flipWithEnabledWhiteList = contract.exec('flip', value, gasLimit);
135 await expect(submitTransactionExpectFailAsync(bob, flipWithEnabledWhiteList)).to.be.rejected;
136 const flipValueAfterEnableWhiteList = await getFlipValue(contract, deployer);
137 expect(flipValueAfterEnableWhiteList).to.be.eq(expectedFlipValue, `Enabling whitelist doesn't make it possible to call contract for everyone.`);
138
139 await deployerCanFlip();
140
141 const addBobToWhiteListTx = api.tx.nft.addToContractWhiteList(contract.address, bob.address);
142 const addBobResult = await submitTransactionAsync(deployer, addBobToWhiteListTx);
143 const flipWithWhitelistedBob = contract.exec('flip', value, gasLimit);
144 await submitTransactionAsync(bob, flipWithWhitelistedBob);
145 expectedFlipValue = !expectedFlipValue;
146 const flipAfterWhiteListed = await getFlipValue(contract,deployer);
147 expect(flipAfterWhiteListed).to.be.eq(expectedFlipValue, `Bob was whitelisted, now he can flip.`);
148
149 await deployerCanFlip();
150
151 const removeBobFromWhiteListTx = api.tx.nft.removeFromContractWhiteList(contract.address, bob.address);
152 const removeBobResult = await submitTransactionAsync(deployer, removeBobFromWhiteListTx);
153 const bobRemoved = contract.exec('flip', value, gasLimit);
154 await expect(submitTransactionExpectFailAsync(bob, bobRemoved)).to.be.rejected;
155 const afterBobRemoved = await getFlipValue(contract, deployer);
156 expect(afterBobRemoved).to.be.eq(expectedFlipValue, `Bob can't call contract, now when he is removeed from white list.`);
157
158 await deployerCanFlip();
159
160 const disableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, false);
161 const disableWhiteListResult = await submitTransactionAsync(deployer, disableWhiteListTx);
162 const whiteListDisabledFlip = contract.exec('flip', value, gasLimit);
163 await submitTransactionAsync(bob, whiteListDisabledFlip);
164 expectedFlipValue = !expectedFlipValue;
165 const afterWhiteListDisabled = await getFlipValue(contract,deployer);
166 expect(afterWhiteListDisabled).to.be.eq(expectedFlipValue, `Anyone can call contract with disabled whitelist.`);
167
168 });
169 });
17033
171 it('Can initialize contract instance', async () => {34 it('Can initialize contract instance', async () => {
172 await usingApi(async (api) => {35 await usingApi(async (api) => {
addedtests/src/toggleContractWhiteList.test.tsdiffbeforeafterboth

no changes

addedtests/src/util/contracthelpers.tsdiffbeforeafterboth

no changes