difftreelog
Merge pull request #85 from usetech-llc/feature/NFTPAR-244_setContractSponsoringRateLimit
in: master
Add tests for setContractSponsoringRateLimit
4 files changed
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -35,7 +35,8 @@
"testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",
"testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",
"testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts",
- "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts"
+ "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",
+ "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts"
},
"author": "",
"license": "SEE LICENSE IN ../LICENSE",
tests/src/setContractSponsoringRateLimit.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/setContractSponsoringRateLimit.test.ts
@@ -0,0 +1,62 @@
+import { IKeyringPair } from '@polkadot/types/types';
+import privateKey from './substrate/privateKey';
+import usingApi from './substrate/substrate-api';
+import waitNewBlocks from './substrate/wait-new-blocks';
+import { deployFlipper, toggleFlipValueExpectFailure, toggleFlipValueExpectSuccess } from './util/contracthelpers';
+import {
+ enableContractSponsoringExpectSuccess,
+ findUnusedAddress,
+ setContractSponsoringRateLimitExpectFailure,
+ setContractSponsoringRateLimitExpectSuccess,
+} from './util/helpers';
+
+describe('Integration Test setContractSponsoringRateLimit', () => {
+ it('ensure sponsored contract can\'t be called twice without pause for free', async () => {
+ await usingApi(async (api) => {
+ const user = await findUnusedAddress(api);
+
+ const [flipper, deployer] = await deployFlipper(api);
+ await enableContractSponsoringExpectSuccess(deployer, flipper.address, true);
+ await setContractSponsoringRateLimitExpectSuccess(deployer, flipper.address, 10);
+ await toggleFlipValueExpectSuccess(user, flipper);
+ await toggleFlipValueExpectFailure(user, flipper);
+ });
+ });
+
+ it('ensure sponsored contract can be called twice with pause for free', async () => {
+ await usingApi(async (api) => {
+ const user = await findUnusedAddress(api);
+
+ const [flipper, deployer] = await deployFlipper(api);
+ await enableContractSponsoringExpectSuccess(deployer, flipper.address, true);
+ await setContractSponsoringRateLimitExpectSuccess(deployer, flipper.address, 1);
+ await toggleFlipValueExpectSuccess(user, flipper);
+ await waitNewBlocks(api, 1);
+ await toggleFlipValueExpectSuccess(user, flipper);
+ });
+ });
+});
+
+describe('Negative Integration Test setContractSponsoringRateLimit', () => {
+ let alice: IKeyringPair;
+
+ before(async () => {
+ alice = privateKey('//Alice');
+ });
+
+ it('fails when called for non-contract address', async () => {
+ await usingApi(async (api) => {
+ const user = await findUnusedAddress(api);
+
+ await setContractSponsoringRateLimitExpectFailure(alice, user.address, 1);
+ });
+ });
+
+ it('fails when called by non-owning user', async () => {
+ await usingApi(async (api) => {
+ const [flipper, _] = await deployFlipper(api);
+
+ await setContractSponsoringRateLimitExpectFailure(alice, flipper.address, 1);
+ });
+ });
+});
tests/src/util/contracthelpers.tsdiffbeforeafterboth1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import chai from "chai";7import chaiAsPromised from 'chai-as-promised';8import { submitTransactionAsync } from "../substrate/substrate-api";9import fs from "fs";10import { Abi, BlueprintPromise as Blueprint, CodePromise, ContractPromise as Contract } from "@polkadot/api-contract";11import { IKeyringPair } from "@polkadot/types/types";12import { ApiPromise, Keyring } from "@polkadot/api";1314chai.use(chaiAsPromised);15const expect = chai.expect;16import { BigNumber } from 'bignumber.js';17import { findUnusedAddress } from '../util/helpers';1819const value = 0;20const gasLimit = '200000000000';21const endowment = '100000000000000000';2223function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<Blueprint> {24 return new Promise<Blueprint>(async (resolve, reject) => {25 const unsub = await code26 .createBlueprint()27 .signAndSend(alice, (result) => {28 if (result.status.isInBlock || result.status.isFinalized) {29 // here we have an additional field in the result, containing the blueprint30 resolve(result.blueprint);31 unsub();32 }33 })34 });35}3637function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {38 return new Promise<any>(async (resolve, reject) => {39 const initValue = true;4041 const unsub = await blueprint.tx42 .new(endowment, gasLimit, initValue)43 .signAndSend(alice, (result) => {44 if (result.status.isInBlock || result.status.isFinalized) {45 unsub();46 resolve(result);47 }48 }); 49 });50}5152async function prepareDeployer(api: ApiPromise) {53 // Find unused address54 const deployer = await findUnusedAddress(api);5556 // Transfer balance to it57 const keyring = new Keyring({ type: 'sr25519' });58 const alice = keyring.addFromUri(`//Alice`);59 let amount = new BigNumber(endowment);60 amount = amount.plus(100e15);61 const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());62 await submitTransactionAsync(alice, tx);6364 return deployer;65}6667export async function deployFlipper(api: ApiPromise): Promise<[Contract, IKeyringPair]> {68 const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));69 const abi = new Abi(metadata);7071 const deployer = await prepareDeployer(api);7273 const wasm = fs.readFileSync('./src/flipper/flipper.wasm');7475 const code = new CodePromise(api, abi, wasm);7677 const blueprint = await deployBlueprint(deployer, code);78 const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;7980 const initialGetResponse = await getFlipValue(contract, deployer);81 expect(initialGetResponse).to.be.true;8283 return [contract, deployer];84}8586export async function getFlipValue(contract: Contract, deployer: IKeyringPair) {87 const result = await contract.query.get(deployer.address, value, gasLimit);8889 if(!result.result.isOk) {90 throw `Failed to get flipper value`;91 }92 return (result.result.asOk.data[0] == 0x00) ? false : true;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}1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import chai from "chai";7import chaiAsPromised from 'chai-as-promised';8import { submitTransactionAsync, submitTransactionExpectFailAsync } from "../substrate/substrate-api";9import fs from "fs";10import { Abi, BlueprintPromise as Blueprint, CodePromise, ContractPromise as Contract } from "@polkadot/api-contract";11import { IKeyringPair } from "@polkadot/types/types";12import { ApiPromise, Keyring } from "@polkadot/api";1314chai.use(chaiAsPromised);15const expect = chai.expect;16import { BigNumber } from 'bignumber.js';17import { findUnusedAddress, getGenericResult } from '../util/helpers';1819const value = 0;20const gasLimit = '200000000000';21const endowment = '100000000000000000';2223function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<Blueprint> {24 return new Promise<Blueprint>(async (resolve, reject) => {25 const unsub = await code26 .createBlueprint()27 .signAndSend(alice, (result) => {28 if (result.status.isInBlock || result.status.isFinalized) {29 // here we have an additional field in the result, containing the blueprint30 resolve(result.blueprint);31 unsub();32 }33 })34 });35}3637function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {38 return new Promise<any>(async (resolve, reject) => {39 const initValue = true;4041 const unsub = await blueprint.tx42 .new(endowment, gasLimit, initValue)43 .signAndSend(alice, (result) => {44 if (result.status.isInBlock || result.status.isFinalized) {45 unsub();46 resolve(result);47 }48 }); 49 });50}5152async function prepareDeployer(api: ApiPromise) {53 // Find unused address54 const deployer = await findUnusedAddress(api);5556 // Transfer balance to it57 const keyring = new Keyring({ type: 'sr25519' });58 const alice = keyring.addFromUri(`//Alice`);59 let amount = new BigNumber(endowment);60 amount = amount.plus(100e15);61 const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());62 await submitTransactionAsync(alice, tx);6364 return deployer;65}6667export async function deployFlipper(api: ApiPromise): Promise<[Contract, IKeyringPair]> {68 const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));69 const abi = new Abi(metadata);7071 const deployer = await prepareDeployer(api);7273 const wasm = fs.readFileSync('./src/flipper/flipper.wasm');7475 const code = new CodePromise(api, abi, wasm);7677 const blueprint = await deployBlueprint(deployer, code);78 const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;7980 const initialGetResponse = await getFlipValue(contract, deployer);81 expect(initialGetResponse).to.be.true;8283 return [contract, deployer];84}8586export async function getFlipValue(contract: Contract, deployer: IKeyringPair) {87 const result = await contract.query.get(deployer.address, value, gasLimit);8889 if(!result.result.isOk) {90 throw `Failed to get flipper value`;91 }92 return (result.result.asOk.data[0] == 0x00) ? false : true;93}9495export async function toggleFlipValueExpectSuccess(sender: IKeyringPair, contract: Contract) {96 const tx = contract.tx.flip(value, gasLimit);97 const events = await submitTransactionAsync(sender, tx);98 const result = getGenericResult(events);99100 expect(result.success).to.be.true;101}102103export async function toggleFlipValueExpectFailure(sender: IKeyringPair, contract: Contract) {104 const tx = contract.tx.flip(value, gasLimit);105 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;106}107108function instantiateTransferContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {109 return new Promise<any>(async (resolve, reject) => {110 const unsub = await blueprint.tx111 .default(endowment, gasLimit)112 .signAndSend(alice, (result) => {113 if (result.status.isInBlock || result.status.isFinalized) {114 unsub();115 resolve(result);116 }117 }); 118 });119}120121export async function deployTransferContract(api: ApiPromise): Promise<[Contract, IKeyringPair]> {122 const metadata = JSON.parse(fs.readFileSync('./src/transfer_contract/metadata.json').toString('utf-8'));123 const abi = new Abi(metadata);124125 const deployer = await prepareDeployer(api);126127 const wasm = fs.readFileSync('./src/transfer_contract/nft_transfer.wasm');128129 const code = new CodePromise(api, abi, wasm);130131 const blueprint = await deployBlueprint(deployer, code);132 const contract = (await instantiateTransferContract(deployer, blueprint))['contract'] as Contract;133134 return [contract, deployer];135}tests/src/util/helpers.tsdiffbeforeafterboth--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -384,6 +384,46 @@
});
}
+export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);
+ const events = await submitTransactionAsync(sender, tx);
+ const result = getGenericResult(events);
+
+ expect(result.success).to.be.true;
+ });
+}
+
+export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);
+ const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
+ const result = getGenericResult(events);
+
+ expect(result.success).to.be.false;
+ });
+}
+
+export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);
+ const events = await submitTransactionAsync(sender, tx);
+ const result = getGenericResult(events);
+
+ expect(result.success).to.be.true;
+ });
+}
+
+export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);
+ const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
+ const result = getGenericResult(events);
+
+ expect(result.success).to.be.false;
+ });
+}
+
export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {
await usingApi(async (api) => {
const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));