git.delta.rocks / unique-network / refs/commits / fea73f40565c

difftreelog

test tests for contract fees

Grigoriy Simonov2022-09-30parent: #0231a43.patch.diff
in: master

3 files changed

modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -114,7 +114,7 @@
 use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};
 use sp_core::H160;
 use sp_runtime::{ArithmeticError, DispatchError, DispatchResult, TransactionOutcome};
-use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap, collections::btree_set::BTreeSet};
+use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};
 use core::ops::Deref;
 use codec::{Encode, Decode, MaxEncodedLen};
 use scale_info::TypeInfo;
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -194,6 +194,7 @@
 	fn create_nonfungible_collection(
 		&mut self,
 		caller: caller,
+		value: value,
 		name: string,
 		description: string,
 		token_prefix: string,
modifiedtests/src/eth/payable.test.tsdiffbeforeafterboth
1616
17import {IKeyringPair} from '@polkadot/types/types';17import {IKeyringPair} from '@polkadot/types/types';
1818
19import {itEth, expect, usingEthPlaygrounds} from './util/playgrounds';19import {itEth, expect, usingEthPlaygrounds, EthUniqueHelper} from './util/playgrounds';
2020
21describe('EVM payable contracts', () => {21describe('EVM payable contracts', () => {
22 let donor: IKeyringPair;22 let donor: IKeyringPair;
105 });105 });
106});106});
107
108describe('EVM transaction fees', () => {
109 let donor: IKeyringPair;
110
111 before(async function() {
112 await usingEthPlaygrounds(async (_, privateKey) => {
113 donor = privateKey('//Alice');
114 });
115 });
116
117 itEth('Fee is withdrawn from the user', async({helper}) => {
118 const deployer = await helper.eth.createAccountWithBalance(donor);
119 const caller = await helper.eth.createAccountWithBalance(donor);
120 const contract = await helper.eth.deployFlipper(deployer);
121
122 const initialCallerBalance = await helper.balance.getEthereum(caller);
123 await contract.methods.flip().send({from: caller});
124 const finalCallerBalance = await helper.balance.getEthereum(caller);
125 expect(finalCallerBalance < initialCallerBalance).to.be.true;
126 });
127
128 itEth('Fee for nested calls is withdrawn from the user', async({helper}) => {
129 const deployer = await helper.eth.createAccountWithBalance(donor);
130 const caller = await helper.eth.createAccountWithBalance(donor);
131 const contract = await deployProxyContract(helper, deployer);
132
133 const initialCallerBalance = await helper.balance.getEthereum(caller);
134 const initialContractBalance = await helper.balance.getEthereum(contract.options.address);
135 await contract.methods.flip().send({from: caller});
136 const finalCallerBalance = await helper.balance.getEthereum(caller);
137 const finalContractBalance = await helper.balance.getEthereum(contract.options.address);
138 expect(finalCallerBalance < initialCallerBalance).to.be.true;
139 expect(finalContractBalance == initialContractBalance).to.be.true;
140 });
141
142 itEth('Fee for nested calls to native methods is withdrawn from the user', async({helper}) => {
143 const CONTRACT_BALANCE = 3n * helper.balance.getOneTokenNominal();
144
145 const deployer = await helper.eth.createAccountWithBalance(donor);
146 const caller = await helper.eth.createAccountWithBalance(donor);
147 const contract = await deployProxyContract(helper, deployer);
148
149 const web3 = helper.getWeb3();
150 await web3.eth.sendTransaction({from: deployer, to: contract.options.address, value: CONTRACT_BALANCE.toString(), gas: helper.eth.DEFAULT_GAS});
151
152 const collectionAddress = (await contract.methods.createNonfungibleCollection().send({from: caller})).events.CollectionCreated.returnValues.collection;
153 const initialCallerBalance = await helper.balance.getEthereum(caller);
154 const initialContractBalance = await helper.balance.getEthereum(contract.options.address);
155 await contract.methods.mintNftToken(collectionAddress).send({from: caller});
156 const finalCallerBalance = await helper.balance.getEthereum(caller);
157 const finalContractBalance = await helper.balance.getEthereum(contract.options.address);
158 expect(finalCallerBalance < initialCallerBalance).to.be.true;
159 expect(finalContractBalance == initialContractBalance).to.be.true;
160 });
161
162 itEth('Fee for nested calls to create*Collection methods is withdrawn from the user and from the contract', async({helper}) => {
163 const CONTRACT_BALANCE = 3n * helper.balance.getOneTokenNominal();
164
165 const deployer = await helper.eth.createAccountWithBalance(donor);
166 const caller = await helper.eth.createAccountWithBalance(donor);
167 const contract = await deployProxyContract(helper, deployer);
168
169 const web3 = helper.getWeb3();
170 await web3.eth.sendTransaction({from: deployer, to: contract.options.address, value: CONTRACT_BALANCE.toString(), gas: helper.eth.DEFAULT_GAS});
171
172 const initialCallerBalance = await helper.balance.getEthereum(caller);
173 const initialContractBalance = await helper.balance.getEthereum(contract.options.address);
174 await contract.methods.createNonfungibleCollection().send({from: caller});
175 const finalCallerBalance = await helper.balance.getEthereum(caller);
176 const finalContractBalance = await helper.balance.getEthereum(contract.options.address);
177 expect(finalCallerBalance < initialCallerBalance).to.be.true;
178 expect(finalContractBalance < initialContractBalance).to.be.true;
179 });
180
181 async function deployProxyContract(helper: EthUniqueHelper, deployer: string) {
182 return await helper.ethContract.deployByCode(
183 deployer,
184 'ProxyContract',
185 `
186 // SPDX-License-Identifier: UNLICENSED
187 pragma solidity ^0.8.6;
188
189 import {CollectionHelpers} from "../api/CollectionHelpers.sol";
190 import {UniqueNFT} from "../api/UniqueNFT.sol";
191
192 contract ProxyContract {
193 bool value = false;
194 address flipper;
195
196 event CollectionCreated(address collection);
197 event TokenMinted(uint256 tokenId);
198
199 receive() external payable {}
200
201 constructor() {
202 flipper = address(new Flipper());
203 }
204
205 function flip() public {
206 value = !value;
207 Flipper(flipper).flip();
208 }
209
210 function createNonfungibleCollection() public {
211 address collectionHelpers = 0x6C4E9fE1AE37a41E93CEE429e8E1881aBdcbb54F;
212 address nftCollection = CollectionHelpers(collectionHelpers).createNonfungibleCollection("A", "B", "C");
213 emit CollectionCreated(nftCollection);
214 }
215
216 function mintNftToken(address collectionAddress) public {
217 UniqueNFT collection = UniqueNFT(collectionAddress);
218 uint256 tokenId = collection.nextTokenId();
219 collection.mint(msg.sender, tokenId);
220 emit TokenMinted(tokenId);
221 }
222
223 function getValue() public view returns (bool) {
224 return Flipper(flipper).getValue();
225 }
226 }
227
228 contract Flipper {
229 bool value = false;
230 function flip() public {
231 value = !value;
232 }
233 function getValue() public view returns (bool) {
234 return value;
235 }
236 }
237 `,
238 [
239 {
240 solPath: 'api/CollectionHelpers.sol',
241 fsPath: `${__dirname}/api/CollectionHelpers.sol`,
242 },
243 {
244 solPath: 'api/UniqueNFT.sol',
245 fsPath: `${__dirname}/api/UniqueNFT.sol`,
246 },
247 ],
248 );
249 }
250});
107251