difftreelog
NFTPAR-231 Smart Contract White List. Anyone can call contract with disabled white list.
in: master
2 files changed
pallets/nft/src/lib.rsdiffbeforeafterboth2514 let owned_contract = <ContractOwner<T>>::contains_key(called_contract.clone())2514 let owned_contract = <ContractOwner<T>>::contains_key(called_contract.clone())2515 && <ContractOwner<T>>::get(called_contract.clone()) == *who;2515 && <ContractOwner<T>>::get(called_contract.clone()) == *who;2516 2517 if !owned_contract {2518 let white_list_enabled = <ContractWhiteListEnabled<T>>::contains_key(called_contract.clone()) && <ContractWhiteListEnabled<T>>::get(called_contract.clone());2516 let white_list_enabled = <ContractWhiteListEnabled<T>>::contains_key(called_contract.clone()) && <ContractWhiteListEnabled<T>>::get(called_contract.clone());2517 2519 if !white_list_enabled || !<ContractWhiteList<T>>::contains_key(called_contract.clone(), who) {2518 if !owned_contract && white_list_enabled {2519 if !<ContractWhiteList<T>>::contains_key(called_contract.clone(), who) {2520 return Err(InvalidTransaction::Call.into());2520 return Err(InvalidTransaction::Call.into());2521 }2521 }2522 }2522 }252325232524 let mut sponsor_transfer = false;2524 let mut sponsor_transfer = false;2525 if <ContractSponsoringRateLimit<T>>::contains_key(called_contract.clone()) {2525 if <ContractSponsoringRateLimit<T>>::contains_key(called_contract.clone()) {tests/src/contracts.test.tsdiffbeforeafterboth115 let expectedFlipValue = await getFlipValue(contract, deployer);115 let expectedFlipValue = await getFlipValue(contract, deployer);116116117 const flip = contract.exec('flip', value, gasLimit);117 const flip = contract.exec('flip', value, gasLimit);118 await expect(submitTransactionExpectFailAsync(bob, flip)).to.be.rejected;118 await submitTransactionAsync(bob, flip);119 expectedFlipValue = !expectedFlipValue;119 const firstFailResponse = await getFlipValue(contract, deployer);120 const afterFlip = await getFlipValue(contract,deployer);120 expect(firstFailResponse).to.be.eq(expectedFlipValue, `Only account who deployed contract can flip value.`);121 expect(afterFlip).to.be.eq(expectedFlipValue, `Anyone can call new contract.`);121122122 const deployerCanFlip = async () => {123 const deployerCanFlip = async () => {123 expectedFlipValue = !expectedFlipValue;124 expectedFlipValue = !expectedFlipValue;145 const flipAfterWhiteListed = await getFlipValue(contract,deployer);146 const flipAfterWhiteListed = await getFlipValue(contract,deployer);146 expect(flipAfterWhiteListed).to.be.eq(expectedFlipValue, `Bob was whitelisted, now he can flip.`);147 expect(flipAfterWhiteListed).to.be.eq(expectedFlipValue, `Bob was whitelisted, now he can flip.`);147148 await deployerCanFlip();149150 const disableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, false);151 const disableeResult = await submitTransactionAsync(deployer, disableWhiteListTx);152 const flipWithDisabledWhitelist = contract.exec('flip', value, gasLimit);153 await expect(submitTransactionExpectFailAsync(bob, flipWithDisabledWhitelist)).to.be.rejected;154 const flipWithDisabledWhiteList = await getFlipValue(contract, deployer);155 expect(flipWithDisabledWhiteList).to.be.eq(expectedFlipValue, `Bob can't flip when whitelist is disabled, even tho he is in whitelist.`);156157 await deployerCanFlip();158159 const enableWhiteListOneMoreTimeTx = api.tx.nft.toggleContractWhiteList(contract.address, true);160 const enableOneMoreTimeResult = await submitTransactionAsync(deployer, enableWhiteListOneMoreTimeTx);161148162 await deployerCanFlip();149 await deployerCanFlip();163150166 const bobRemoved = contract.exec('flip', value, gasLimit);153 const bobRemoved = contract.exec('flip', value, gasLimit);167 await expect(submitTransactionExpectFailAsync(bob, bobRemoved)).to.be.rejected;154 await expect(submitTransactionExpectFailAsync(bob, bobRemoved)).to.be.rejected;168 const afterBobRemoved = await getFlipValue(contract, deployer);155 const afterBobRemoved = await getFlipValue(contract, deployer);169 expect(afterBobRemoved).to.be.eq(expectedFlipValue, `Enabling whitelist doesn't make it possible to call contract for everyone.`);156 expect(afterBobRemoved).to.be.eq(expectedFlipValue, `Bob can't call contract, now when he is removeed from white list.`);170157171 await deployerCanFlip();158 await deployerCanFlip();172159173 const cleanupTx = api.tx.nft.toggleContractWhiteList(contract.address, false);160 const disableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, false);174 const cleanupResult = await submitTransactionAsync(deployer, cleanupTx);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.`);175167176 console.error = consoleError;168 console.error = consoleError;177 });169 });