difftreelog
Merge branch 'develop' into feature/NFTPAR-250
in: master
4 files changed
pallets/nft/src/lib.rsdiffbeforeafterboth--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -395,7 +395,7 @@
// Basic collections
pub Collection get(fn collection) config(): map hasher(identity) CollectionId => CollectionType<T::AccountId>;
pub AdminList get(fn admin_list_collection): map hasher(identity) CollectionId => Vec<T::AccountId>;
- pub WhiteList get(fn white_list): map hasher(identity) CollectionId => Vec<T::AccountId>;
+ pub WhiteList get(fn white_list): double_map hasher(identity) CollectionId, hasher(twox_64_concat) T::AccountId => bool;
/// Balance owner per collection map
pub Balance get(fn balance_count): double_map hasher(identity) CollectionId, hasher(twox_64_concat) T::AccountId => u128;
@@ -421,6 +421,8 @@
pub ContractSelfSponsoring get(fn contract_self_sponsoring): map hasher(twox_64_concat) T::AccountId => bool;
pub ContractSponsorBasket get(fn contract_sponsor_basket): map hasher(twox_64_concat) (T::AccountId, T::AccountId) => T::BlockNumber;
pub ContractSponsoringRateLimit get(fn contract_sponsoring_rate_limit): map hasher(twox_64_concat) T::AccountId => T::BlockNumber;
+ pub ContractWhiteListEnabled get(fn contract_white_list_enabled): map hasher(twox_64_concat) T::AccountId => bool;
+ pub ContractWhiteList get(fn contract_white_list): double_map hasher(twox_64_concat) T::AccountId, hasher(twox_64_concat) T::AccountId => bool;
}
add_extra_genesis {
build(|config: &GenesisConfig<T>| {
@@ -610,7 +612,7 @@
<ItemListIndex>::remove(collection_id);
<AdminList<T>>::remove(collection_id);
<Collection<T>>::remove(collection_id);
- <WhiteList<T>>::remove(collection_id);
+ <WhiteList<T>>::remove_prefix(collection_id);
<NftItemList<T>>::remove_prefix(collection_id);
<FungibleItemList<T>>::remove_prefix(collection_id);
@@ -651,20 +653,8 @@
let sender = ensure_signed(origin)?;
Self::check_owner_or_admin_permissions(collection_id, sender)?;
- let mut white_list_collection: Vec<T::AccountId>;
- if <WhiteList<T>>::contains_key(collection_id) {
- white_list_collection = <WhiteList<T>>::get(collection_id);
- if !white_list_collection.contains(&address.clone())
- {
- white_list_collection.push(address.clone());
- }
- }
- else {
- white_list_collection = Vec::new();
- white_list_collection.push(address.clone());
- }
-
- <WhiteList<T>>::insert(collection_id, white_list_collection);
+ <WhiteList<T>>::insert(collection_id, address, true);
+
Ok(())
}
@@ -686,14 +676,7 @@
let sender = ensure_signed(origin)?;
Self::check_owner_or_admin_permissions(collection_id, sender)?;
- if <WhiteList<T>>::contains_key(collection_id) {
- let mut white_list_collection = <WhiteList<T>>::get(collection_id);
- if white_list_collection.contains(&address.clone())
- {
- white_list_collection.retain(|i| *i != address.clone());
- <WhiteList<T>>::insert(collection_id, white_list_collection);
- }
- }
+ <WhiteList<T>>::remove(collection_id, address);
Ok(())
}
@@ -1396,12 +1379,7 @@
#[cfg(feature = "runtime-benchmarks")]
<ContractOwner<T>>::insert(contract_address.clone(), sender.clone());
- let mut is_owner = false;
- if <ContractOwner<T>>::contains_key(contract_address.clone()) {
- let owner = <ContractOwner<T>>::get(&contract_address);
- is_owner = sender == owner;
- }
- ensure!(is_owner, Error::<T>::NoPermission);
+ Self::ensure_contract_owned(sender, &contract_address)?;
<ContractSelfSponsoring<T>>::insert(contract_address, enable);
Ok(())
@@ -1431,18 +1409,85 @@
rate_limit: T::BlockNumber
) -> DispatchResult {
let sender = ensure_signed(origin)?;
- let mut is_owner = false;
- if <ContractOwner<T>>::contains_key(contract_address.clone()) {
- let owner = <ContractOwner<T>>::get(&contract_address);
- is_owner = sender == owner;
- }
- ensure!(is_owner, Error::<T>::NoPermission);
+ Self::ensure_contract_owned(sender, &contract_address)?;
<ContractSponsoringRateLimit<T>>::insert(contract_address, rate_limit);
Ok(())
}
+ /// Enable the white list for a contract. Only addresses added to the white list with addToContractWhiteList will be able to call this smart contract.
+ ///
+ /// # Permissions
+ ///
+ /// * Address that deployed smart contract.
+ ///
+ /// # Arguments
+ ///
+ /// -`contract_address`: Address of the contract.
+ ///
+ /// - `enable`: .
+ #[weight = 0]
+ pub fn toggle_contract_white_list(
+ origin,
+ contract_address: T::AccountId,
+ enable: bool
+ ) -> DispatchResult {
+ let sender = ensure_signed(origin)?;
+ Self::ensure_contract_owned(sender, &contract_address)?;
+
+ <ContractWhiteListEnabled<T>>::insert(contract_address, enable);
+ Ok(())
+ }
+
+ /// Add an address to smart contract white list.
+ ///
+ /// # Permissions
+ ///
+ /// * Address that deployed smart contract.
+ ///
+ /// # Arguments
+ ///
+ /// -`contract_address`: Address of the contract.
+ ///
+ /// -`account_address`: Address to add.
+ #[weight = 0]
+ pub fn add_to_contract_white_list(
+ origin,
+ contract_address: T::AccountId,
+ account_address: T::AccountId
+ ) -> DispatchResult {
+ let sender = ensure_signed(origin)?;
+ Self::ensure_contract_owned(sender, &contract_address)?;
+
+ <ContractWhiteList<T>>::insert(contract_address, account_address, true);
+ Ok(())
+ }
+
+ /// Remove an address from smart contract white list.
+ ///
+ /// # Permissions
+ ///
+ /// * Address that deployed smart contract.
+ ///
+ /// # Arguments
+ ///
+ /// -`contract_address`: Address of the contract.
+ ///
+ /// -`account_address`: Address to remove.
#[weight = 0]
+ pub fn remove_from_contract_white_list(
+ origin,
+ contract_address: T::AccountId,
+ account_address: T::AccountId
+ ) -> DispatchResult {
+ let sender = ensure_signed(origin)?;
+ Self::ensure_contract_owned(sender, &contract_address)?;
+
+ <ContractWhiteList<T>>::remove(contract_address, account_address);
+ Ok(())
+ }
+
+ #[weight = 0]
pub fn set_collection_limits(
origin,
collection_id: u32,
@@ -1778,9 +1823,7 @@
fn check_white_list(collection_id: CollectionId, address: &T::AccountId) -> DispatchResult {
let mes = Error::<T>::AddresNotInWhiteList;
- ensure!(<WhiteList<T>>::contains_key(collection_id), mes);
- let wl = <WhiteList<T>>::get(collection_id);
- ensure!(wl.contains(address), mes);
+ ensure!(<WhiteList<T>>::contains_key(collection_id, address), mes);
Ok(())
}
@@ -2138,6 +2181,17 @@
Ok(())
}
+
+ fn ensure_contract_owned(account: T::AccountId, contract: &T::AccountId) -> DispatchResult {
+ if <ContractOwner<T>>::contains_key(contract.clone()) {
+ let owner = <ContractOwner<T>>::get(contract);
+ ensure!(account == owner, Error::<T>::NoPermission);
+ } else {
+ fail!(Error::<T>::NoPermission);
+ }
+
+ Ok(())
+ }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -2357,6 +2411,16 @@
let called_contract: T::AccountId = T::Lookup::lookup((*dest).clone()).unwrap_or(T::AccountId::default());
+ let owned_contract = <ContractOwner<T>>::contains_key(called_contract.clone())
+ && <ContractOwner<T>>::get(called_contract.clone()) == *who;
+ let white_list_enabled = <ContractWhiteListEnabled<T>>::contains_key(called_contract.clone()) && <ContractWhiteListEnabled<T>>::get(called_contract.clone());
+
+ if !owned_contract && white_list_enabled {
+ if !<ContractWhiteList<T>>::contains_key(called_contract.clone(), who) {
+ return Err(InvalidTransaction::Call.into());
+ }
+ }
+
let mut sponsor_transfer = false;
if <ContractSponsoringRateLimit<T>>::contains_key(called_contract.clone()) {
let last_tx_block = <ContractSponsorBasket<T>>::get((&called_contract, &who));
pallets/nft/src/tests.rsdiffbeforeafterboth960960961 let origin1 = Origin::signed(1);961 let origin1 = Origin::signed(1);962 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));962 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));963 assert_eq!(TemplateModule::white_list(collection_id)[0], 2);963 assert_eq!(TemplateModule::white_list(collection_id, 2), true);964 });964 });965}965}966966975975976 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection_id, 2));976 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection_id, 2));977 assert_ok!(TemplateModule::add_to_white_list(origin2.clone(), collection_id, 3));977 assert_ok!(TemplateModule::add_to_white_list(origin2.clone(), collection_id, 3));978 assert_eq!(TemplateModule::white_list(collection_id)[0], 3);978 assert_eq!(TemplateModule::white_list(collection_id, 3), true);979 });979 });980}980}9819811035 1035 1036 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));1036 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));1037 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));1037 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));1038 assert_eq!(TemplateModule::white_list(collection_id)[0], 2);1038 assert_eq!(TemplateModule::white_list(collection_id, 2), true);1039 assert_eq!(TemplateModule::white_list(collection_id).len(), 1);1040 });1039 });1041}1040}104210411054 collection_id,1053 collection_id,1055 21054 21056 ));1055 ));1057 assert_eq!(TemplateModule::white_list(collection_id).len(), 0);1056 assert_eq!(TemplateModule::white_list(collection_id, 2), false);1058 });1057 });1059}1058}106010591075 collection_id,1074 collection_id,1076 31075 31077 ));1076 ));1078 assert_eq!(TemplateModule::white_list(collection_id).len(), 0);1077 assert_eq!(TemplateModule::white_list(collection_id, 3), false);1079 });1078 });1080}1079}108110801093 TemplateModule::remove_from_white_list(origin2.clone(), collection_id, 2),1092 TemplateModule::remove_from_white_list(origin2.clone(), collection_id, 2),1094 Error::<Test>::NoPermission1093 Error::<Test>::NoPermission1095 );1094 );1096 assert_eq!(TemplateModule::white_list(collection_id)[0], 2);1095 assert_eq!(TemplateModule::white_list(collection_id, 2), true);1097 });1096 });1098}1097}109910981125 TemplateModule::remove_from_white_list(origin2.clone(), collection_id, 2),1124 TemplateModule::remove_from_white_list(origin2.clone(), collection_id, 2),1126 Error::<Test>::CollectionNotFound1125 Error::<Test>::CollectionNotFound1127 );1126 );1128 assert_eq!(TemplateModule::white_list(collection_id).len(), 0);1127 assert_eq!(TemplateModule::white_list(collection_id, 2), false);1129 });1128 });1130}1129}113111301149 collection_id,1148 collection_id,1150 21149 21151 ));1150 ));1152 assert_eq!(TemplateModule::white_list(collection_id).len(), 0);1151 assert_eq!(TemplateModule::white_list(collection_id, 2), false);1153 });1152 });1154}1153}11551154tests/src/contracts.test.tsdiffbeforeafterboth--- a/tests/src/contracts.test.ts
+++ b/tests/src/contracts.test.ts
@@ -1,16 +1,15 @@
-//
-// This file is subject to the terms and conditions defined in
-// file 'LICENSE', which is part of this source code package.
-//
-
-import { ApiPromise } from "@polkadot/api";
-import { expect } from "chai";
-import { default as usingApi, submitTransactionAsync } from "./substrate/substrate-api";
+import chai from "chai";
+import chaiAsPromised from 'chai-as-promised';
+import usingApi, { submitTransactionAsync, submitTransactionExpectFailAsync } from "./substrate/substrate-api";
import fs from "fs";
-import { Abi, BlueprintPromise, CodePromise } from "@polkadot/api-contract";
+import { Abi, BlueprintPromise as Blueprint, CodePromise, ContractPromise as Contract } from "@polkadot/api-contract";
import { IKeyringPair } from "@polkadot/types/types";
-import { Keyring } from "@polkadot/api";
+import { ApiPromise, Keyring } from "@polkadot/api";
import { ApiTypes, SubmittableExtrinsic } from "@polkadot/api/types";
+import privateKey from "./substrate/privateKey";
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
import { BigNumber } from 'bignumber.js';
import { findUnusedAddress } from './util/helpers'
@@ -18,8 +17,8 @@
const gasLimit = 3000n * 1000000n;
const endowment = `1000000000000000`;
-function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<BlueprintPromise> {
- return new Promise<BlueprintPromise>(async (resolve, reject) => {
+function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<Blueprint> {
+ return new Promise<Blueprint>(async (resolve, reject) => {
const unsub = await code
.createBlueprint()
.signAndSend(alice, (result) => {
@@ -32,7 +31,7 @@
});
}
-function deployContract(alice: IKeyringPair, blueprint: BlueprintPromise) : Promise<any> {
+function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {
return new Promise<any>(async (resolve, reject) => {
const initValue = true;
@@ -62,39 +61,112 @@
return deployer;
}
-describe('Contracts smoke test', () => {
+async function deployFlipper(api: ApiPromise): Promise<[Contract, IKeyringPair]> {
+ const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));
+ const abi = new Abi(metadata);
+
+ const deployer = await prepareDeployer(api);
+
+ const wasm = fs.readFileSync('./src/flipper/flipper.wasm');
+
+ const code = new CodePromise(api, abi, wasm);
+
+ const blueprint = await deployBlueprint(deployer, code);
+ const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;
+
+ const initialGetResponse = await getFlipValue(contract, deployer);
+ expect(initialGetResponse).to.be.true;
+
+ return [contract, deployer];
+}
+
+async function getFlipValue(contract: Contract, deployer: IKeyringPair) {
+ const result = await contract.query.get(deployer.address, value, gasLimit);
+
+ if(!result.result.isSuccess) {
+ throw `Failed to get flipper value`;
+ }
+ return (result.result.asSuccess.data[0] == 0x00) ? false : true;
+}
+
+describe('Contracts', () => {
it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {
await usingApi(async api => {
- const deployer = await prepareDeployer(api);
+ const [contract, deployer] = await deployFlipper(api);
+ const initialGetResponse = await getFlipValue(contract, deployer);
+
+ const bob = privateKey("//Bob");
+ const flip = contract.exec('flip', value, gasLimit);
+ await submitTransactionAsync(bob, flip);
+
+ const afterFlipGetResponse = await getFlipValue(contract, deployer);
+ expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');
+ });
+ });
+
+ it(`Whitelisted account can call contract.`, async () => {
+ await usingApi(async api => {
+ const bob = privateKey("//Bob");
- const wasm = fs.readFileSync('./src/flipper/flipper.wasm');
-
- const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));
- const abi = new Abi(metadata);
+ const [contract, deployer] = await deployFlipper(api);
+ const consoleError = console.error;
+ console.error = (...data: any[]) => {
+ };
- const code = new CodePromise(api, abi, wasm);
+ let expectedFlipValue = await getFlipValue(contract, deployer);
- const blueprint = await deployBlueprint(deployer, code);
- const contract = (await deployContract(deployer, blueprint))['contract'];
+ const flip = contract.exec('flip', value, gasLimit);
+ await submitTransactionAsync(bob, flip);
+ expectedFlipValue = !expectedFlipValue;
+ const afterFlip = await getFlipValue(contract,deployer);
+ expect(afterFlip).to.be.eq(expectedFlipValue, `Anyone can call new contract.`);
- const getFlipValue = async () => {
- const result = await contract.query.get(deployer.address, value, gasLimit);
+ const deployerCanFlip = async () => {
+ expectedFlipValue = !expectedFlipValue;
+ const deployerFlip = contract.exec('flip', value, gasLimit);
+ await submitTransactionAsync(deployer, deployerFlip);
+ const aliceFlip1Response = await getFlipValue(contract, deployer);
+ expect(aliceFlip1Response).to.be.eq(expectedFlipValue, `Deployer always can flip.`);
+ };
+ await deployerCanFlip();
- if(!result.result.isSuccess) {
- throw `Failed to get flipper value`;
- }
- return (result.result.asSuccess.data[0] == 0x00) ? false : true;
- }
+ const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, true);
+ const enableResult = await submitTransactionAsync(deployer, enableWhiteListTx);
+ const flipWithEnabledWhiteList = contract.exec('flip', value, gasLimit);
+ await expect(submitTransactionExpectFailAsync(bob, flipWithEnabledWhiteList)).to.be.rejected;
+ const flipValueAfterEnableWhiteList = await getFlipValue(contract, deployer);
+ expect(flipValueAfterEnableWhiteList).to.be.eq(expectedFlipValue, `Enabling whitelist doesn't make it possible to call contract for everyone.`);
- const initialGetResponse = await getFlipValue();
- expect(initialGetResponse).to.be.true;
+ await deployerCanFlip();
- const flip = contract.exec('flip', value, gasLimit);
- await submitTransactionAsync(deployer, flip);
+ const addBobToWhiteListTx = api.tx.nft.addToContractWhiteList(contract.address, bob.address);
+ const addBobResult = await submitTransactionAsync(deployer, addBobToWhiteListTx);
+ const flipWithWhitelistedBob = contract.exec('flip', value, gasLimit);
+ await submitTransactionAsync(bob, flipWithWhitelistedBob);
+ expectedFlipValue = !expectedFlipValue;
+ const flipAfterWhiteListed = await getFlipValue(contract,deployer);
+ expect(flipAfterWhiteListed).to.be.eq(expectedFlipValue, `Bob was whitelisted, now he can flip.`);
- const afterFlipGetResponse = await getFlipValue();
+ await deployerCanFlip();
- expect(afterFlipGetResponse).to.be.false;
+ const removeBobFromWhiteListTx = api.tx.nft.removeFromContractWhiteList(contract.address, bob.address);
+ const removeBobResult = await submitTransactionAsync(deployer, removeBobFromWhiteListTx);
+ const bobRemoved = contract.exec('flip', value, gasLimit);
+ await expect(submitTransactionExpectFailAsync(bob, bobRemoved)).to.be.rejected;
+ const afterBobRemoved = await getFlipValue(contract, deployer);
+ expect(afterBobRemoved).to.be.eq(expectedFlipValue, `Bob can't call contract, now when he is removeed from white list.`);
+
+ await deployerCanFlip();
+
+ const disableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, false);
+ const disableWhiteListResult = await submitTransactionAsync(deployer, disableWhiteListTx);
+ const whiteListDisabledFlip = contract.exec('flip', value, gasLimit);
+ await submitTransactionAsync(bob, whiteListDisabledFlip);
+ expectedFlipValue = !expectedFlipValue;
+ const afterWhiteListDisabled = await getFlipValue(contract,deployer);
+ expect(afterWhiteListDisabled).to.be.eq(expectedFlipValue, `Anyone can call contract with disabled whitelist.`);
+
+ console.error = consoleError;
});
});
tests/src/substrate/substrate-api.tsdiffbeforeafterboth--- a/tests/src/substrate/substrate-api.ts
+++ b/tests/src/substrate/substrate-api.ts
@@ -56,3 +56,25 @@
}
});
}
+
+export function submitTransactionExpectFailAsync(sender: IKeyringPair, transaction: SubmittableExtrinsic<ApiTypes>): Promise<EventRecord[]> {
+ return new Promise(async function(resolve, reject) {
+ try {
+ await transaction.signAndSend(sender, ({ events = [], status }) => {
+ if (status.isReady) {
+ // nothing to do
+ // console.log(`Current tx status is Ready`);
+ } else if (status.isBroadcast) {
+ // nothing to do
+ // console.log(`Current tx status is Broadcast`);
+ } else if (status.isInBlock || status.isFinalized) {
+ resolve(events);
+ } else {
+ reject("Transaction failed");
+ }
+ });
+ } catch (e) {
+ reject(e);
+ }
+ });
+}
\ No newline at end of file