difftreelog
CORE-386 Implement eth methods
in: master
5 files changed
pallets/common/src/erc.rsdiffbeforeafterboth254 Ok(())254 Ok(())255 }255 }256257 fn set_access(&mut self, caller: caller, mode: string) -> Result<void> {258 let caller = T::CrossAccountId::from_eth(caller);259 self.check_is_owner_or_admin(&caller)260 .map_err(dispatch_to_evm::<T>)?;261 self.collection.permissions.access = Some(match mode.as_str() {262 "Normal" => AccessMode::Normal,263 "AllowList" => AccessMode::AllowList,264 _ => return Err("Not supported access mode".into()),265 });266 save(self);267 Ok(())268 }269270 fn add_to_allow_list(&self, caller: caller, user: address) -> Result<void> {271 let caller = check_is_owner_or_admin(caller, self)?;272 let user = T::CrossAccountId::from_eth(user);273 <Pallet<T>>::toggle_allowlist(self, &caller, &user, true)274 .map_err(dispatch_to_evm::<T>)?;275 Ok(())276 }277278 fn remove_from_allow_list(&self, caller: caller, user: address) -> Result<void> {279 let caller = check_is_owner_or_admin(caller, self)?;280 let user = T::CrossAccountId::from_eth(user);281 <Pallet<T>>::toggle_allowlist(self, &caller, &user, false)282 .map_err(dispatch_to_evm::<T>)?;283 Ok(())284 }285286 fn set_mint_mode(&mut self, caller: caller, mode: bool) -> Result<void> {287 check_is_owner_or_admin(caller, self)?;288 self.collection.permissions.mint_mode = Some(mode);289 save(self);290 Ok(())291 }256}292}257293258fn check_is_owner<T: Config>(caller: caller, collection: &CollectionHandle<T>) -> Result<void> {294fn check_is_owner<T: Config>(caller: caller, collection: &CollectionHandle<T>) -> Result<void> {274 Ok(caller)307 Ok(caller)275}308}276309277fn save<T: Config>(collection: &CollectionHandle<T>) -> Result<void> {310fn save<T: Config>(collection: &CollectionHandle<T>) {278 // TODO possibly delete for the lack of transaction279 collection280 .check_is_internal()281 .map_err(dispatch_to_evm::<T>)?;282 <crate::CollectionById<T>>::insert(collection.id, collection.collection.clone());311 <crate::CollectionById<T>>::insert(collection.id, collection.collection.clone());283 Ok(())312 Ok(())284}313}pallets/nonfungible/src/stubs/UniqueNFT.soldiffbeforeafterboth51 event MintingFinished();51 event MintingFinished();52}52}5354// Selector: 3a54513b55contract Collection is Dummy, ERC165 {56 // Selector: setCollectionProperty(string,bytes) 2f073f6657 function setCollectionProperty(string memory key, bytes memory value)58 public59 {60 require(false, stub_error);61 key;62 value;63 dummy = 0;64 }6566 // Selector: deleteCollectionProperty(string) 7b7debce67 function deleteCollectionProperty(string memory key) public {68 require(false, stub_error);69 key;70 dummy = 0;71 }7273 // Throws error if key not found74 //75 // Selector: collectionProperty(string) cf24fd6d76 function collectionProperty(string memory key)77 public78 view79 returns (bytes memory)80 {81 require(false, stub_error);82 key;83 dummy;84 return hex"";85 }8687 // Selector: ethSetSponsor(address) 8f9af35688 function ethSetSponsor(address sponsor) public {89 require(false, stub_error);90 sponsor;91 dummy = 0;92 }9394 // Selector: ethConfirmSponsorship() a8580d1a95 function ethConfirmSponsorship() public {96 require(false, stub_error);97 dummy = 0;98 }99100 // Selector: setLimit(string,uint32) 68db30ca101 function setLimit(string memory limit, uint32 value) public {102 require(false, stub_error);103 limit;104 value;105 dummy = 0;106 }107108 // Selector: setLimit(string,bool) ea67e4c2109 function setLimit(string memory limit, bool value) public {110 require(false, stub_error);111 limit;112 value;113 dummy = 0;114 }115116 // Selector: contractAddress() f6b4dfb4117 function contractAddress() public view returns (address) {118 require(false, stub_error);119 dummy;120 return 0x0000000000000000000000000000000000000000;121 }122123 // Selector: addAdmin(address) 70480275124 function addAdmin(address newAdmin) public view {125 require(false, stub_error);126 newAdmin;127 dummy;128 }129130 // Selector: removeAdmin(address) 1785f53c131 function removeAdmin(address admin) public view {132 require(false, stub_error);133 admin;134 dummy;135 }136137 // Selector: setNesting(bool) e8fc50dd138 function setNesting(bool enable) public {139 require(false, stub_error);140 enable;141 dummy = 0;142 }143144 // Selector: setNesting(bool,address[]) 7df12a9a145 function setNesting(bool enable, address[] memory collections) public {146 require(false, stub_error);147 enable;148 collections;149 dummy = 0;150 }151}15253153// Selector: 4136937754// Selector: 41369377154contract TokenProperties is Dummy, ERC165 {55contract TokenProperties is Dummy, ERC165 {479 }380 }480}381}382383// Selector: f56cd7fa384contract Collection is Dummy, ERC165 {385 // Selector: setCollectionProperty(string,bytes) 2f073f66386 function setCollectionProperty(string memory key, bytes memory value)387 public388 {389 require(false, stub_error);390 key;391 value;392 dummy = 0;393 }394395 // Selector: deleteCollectionProperty(string) 7b7debce396 function deleteCollectionProperty(string memory key) public {397 require(false, stub_error);398 key;399 dummy = 0;400 }401402 // Throws error if key not found403 //404 // Selector: collectionProperty(string) cf24fd6d405 function collectionProperty(string memory key)406 public407 view408 returns (bytes memory)409 {410 require(false, stub_error);411 key;412 dummy;413 return hex"";414 }415416 // Selector: ethSetSponsor(address) 8f9af356417 function ethSetSponsor(address sponsor) public {418 require(false, stub_error);419 sponsor;420 dummy = 0;421 }422423 // Selector: ethConfirmSponsorship() a8580d1a424 function ethConfirmSponsorship() public {425 require(false, stub_error);426 dummy = 0;427 }428429 // Selector: setLimit(string,uint32) 68db30ca430 function setLimit(string memory limit, uint32 value) public {431 require(false, stub_error);432 limit;433 value;434 dummy = 0;435 }436437 // Selector: setLimit(string,bool) ea67e4c2438 function setLimit(string memory limit, bool value) public {439 require(false, stub_error);440 limit;441 value;442 dummy = 0;443 }444445 // Selector: contractAddress() f6b4dfb4446 function contractAddress() public view returns (address) {447 require(false, stub_error);448 dummy;449 return 0x0000000000000000000000000000000000000000;450 }451452 // Selector: addAdmin(address) 70480275453 function addAdmin(address newAdmin) public view {454 require(false, stub_error);455 newAdmin;456 dummy;457 }458459 // Selector: removeAdmin(address) 1785f53c460 function removeAdmin(address admin) public view {461 require(false, stub_error);462 admin;463 dummy;464 }465466 // Selector: setNesting(bool) e8fc50dd467 function setNesting(bool enable) public {468 require(false, stub_error);469 enable;470 dummy = 0;471 }472473 // Selector: setNesting(bool,address[]) 7df12a9a474 function setNesting(bool enable, address[] memory collections) public {475 require(false, stub_error);476 enable;477 collections;478 dummy = 0;479 }480481 // Selector: setAccess(string) 488f56aa482 function setAccess(string memory mode) public {483 require(false, stub_error);484 mode;485 dummy = 0;486 }487488 // Selector: addToAllowList(address) 31f59102489 function addToAllowList(address user) public view {490 require(false, stub_error);491 user;492 dummy;493 }494495 // Selector: removeFromAllowList(address) eba8dabc496 function removeFromAllowList(address user) public view {497 require(false, stub_error);498 user;499 dummy;500 }501502 // Selector: setMintMode(bool) 5dea9bd5503 function setMintMode(bool mode) public {504 require(false, stub_error);505 mode;506 dummy = 0;507 }508}481509482contract UniqueNFT is510contract UniqueNFT is483 Dummy,511 Dummy,tests/src/eth/api/UniqueNFT.soldiffbeforeafterboth42 event MintingFinished();42 event MintingFinished();43}43}4445// Selector: 3a54513b46interface Collection is Dummy, ERC165 {47 // Selector: setCollectionProperty(string,bytes) 2f073f6648 function setCollectionProperty(string memory key, bytes memory value)49 external;5051 // Selector: deleteCollectionProperty(string) 7b7debce52 function deleteCollectionProperty(string memory key) external;5354 // Throws error if key not found55 //56 // Selector: collectionProperty(string) cf24fd6d57 function collectionProperty(string memory key)58 external59 view60 returns (bytes memory);6162 // Selector: ethSetSponsor(address) 8f9af35663 function ethSetSponsor(address sponsor) external;6465 // Selector: ethConfirmSponsorship() a8580d1a66 function ethConfirmSponsorship() external;6768 // Selector: setLimit(string,uint32) 68db30ca69 function setLimit(string memory limit, uint32 value) external;7071 // Selector: setLimit(string,bool) ea67e4c272 function setLimit(string memory limit, bool value) external;7374 // Selector: contractAddress() f6b4dfb475 function contractAddress() external view returns (address);7677 // Selector: addAdmin(address) 7048027578 function addAdmin(address newAdmin) external view;7980 // Selector: removeAdmin(address) 1785f53c81 function removeAdmin(address admin) external view;8283 // Selector: setNesting(bool) e8fc50dd84 function setNesting(bool enable) external;8586 // Selector: setNesting(bool,address[]) 7df12a9a87 function setNesting(bool enable, address[] memory collections) external;88}894490// Selector: 4136937745// Selector: 4136937791interface TokenProperties is Dummy, ERC165 {46interface TokenProperties is Dummy, ERC165 {258 returns (bool);213 returns (bool);259}214}215216// Selector: f56cd7fa217interface Collection is Dummy, ERC165 {218 // Selector: setCollectionProperty(string,bytes) 2f073f66219 function setCollectionProperty(string memory key, bytes memory value)220 external;221222 // Selector: deleteCollectionProperty(string) 7b7debce223 function deleteCollectionProperty(string memory key) external;224225 // Throws error if key not found226 //227 // Selector: collectionProperty(string) cf24fd6d228 function collectionProperty(string memory key)229 external230 view231 returns (bytes memory);232233 // Selector: ethSetSponsor(address) 8f9af356234 function ethSetSponsor(address sponsor) external;235236 // Selector: ethConfirmSponsorship() a8580d1a237 function ethConfirmSponsorship() external;238239 // Selector: setLimit(string,uint32) 68db30ca240 function setLimit(string memory limit, uint32 value) external;241242 // Selector: setLimit(string,bool) ea67e4c2243 function setLimit(string memory limit, bool value) external;244245 // Selector: contractAddress() f6b4dfb4246 function contractAddress() external view returns (address);247248 // Selector: addAdmin(address) 70480275249 function addAdmin(address newAdmin) external view;250251 // Selector: removeAdmin(address) 1785f53c252 function removeAdmin(address admin) external view;253254 // Selector: setNesting(bool) e8fc50dd255 function setNesting(bool enable) external;256257 // Selector: setNesting(bool,address[]) 7df12a9a258 function setNesting(bool enable, address[] memory collections) external;259260 // Selector: setAccess(string) 488f56aa261 function setAccess(string memory mode) external;262263 // Selector: addToAllowList(address) 31f59102264 function addToAllowList(address user) external view;265266 // Selector: removeFromAllowList(address) eba8dabc267 function removeFromAllowList(address user) external view;268269 // Selector: setMintMode(bool) 5dea9bd5270 function setMintMode(bool mode) external;271}260272261interface UniqueNFT is273interface UniqueNFT is262 Dummy,274 Dummy,tests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth221 expect(await helpers.methods.getSponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');221 expect(await helpers.methods.getSponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');222 });222 });223223224 //TODO: CORE-302 add eth methods224 itWeb3('Sponsoring collection from evm address via access list', async ({api, web3}) => {225 itWeb3('Sponsoring collection from evm address via access list', async ({api, web3}) => {225 const owner = await createEthAccountWithBalance(api, web3);226 const owner = await createEthAccountWithBalance(api, web3);226 const collectionHelpers = evmCollectionHelpers(web3, owner);227 const collectionHelpers = evmCollectionHelpers(web3, owner);227 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();228 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();228 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);229 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);229 const sponsor = await createEthAccountWithBalance(api, web3);230 const sponsor = await createEthAccountWithBalance(api, web3);230 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);231 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);231 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});232 result = await collectionEvm.methods.ethSetSponsor(sponsor).send({from: owner});232 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;233 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;233 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;234 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;234 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));235 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));235 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('Caller is not set as sponsor');236 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('Caller is not set as sponsor');236237237 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});238 await collectionEvm.methods.ethConfirmSponsorship().send({from: sponsor});238 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;239 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;239 expect(collectionSub.sponsorship.isConfirmed).to.be.true;240 expect(collectionSub.sponsorship.isConfirmed).to.be.true;240 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));241 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));241242242 const user = createEthAccount(web3);243 const user = createEthAccount(web3);243 let nextTokenId = await collectionEvm.methods.nextTokenId().call();244 const nextTokenId = await collectionEvm.methods.nextTokenId().call();244 expect(nextTokenId).to.be.equal('1');245 expect(nextTokenId).to.be.equal('1');245246246 const oldPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();247 const oldPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();247 expect(oldPermissions.mintMode).to.be.false;248 expect(oldPermissions.mintMode).to.be.false;248 expect(oldPermissions.access).to.be.equal('Normal');249 expect(oldPermissions.access).to.be.equal('Normal');249250250 //TODO: change value, when enum generated251 await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});251 await collectionEvm.methods.setAccess('AllowList').send({from: owner});252 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});252 await collectionEvm.methods.addToAllowList(user).send({from: owner});253 await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});253 await collectionEvm.methods.setMintMode(true).send({from: owner});254254255 const newPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();255 const newPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();256 expect(newPermissions.mintMode).to.be.true;256 expect(newPermissions.mintMode).to.be.true;257 expect(newPermissions.access).to.be.equal('AllowList');257 expect(newPermissions.access).to.be.equal('AllowList');258258259 // const [alicesBalanceBefore] = await getBalance(api, [alicesPublicKey]);260261 {259 const ownerBalanceBefore = await ethBalanceViaSub(api, owner);262 const nextTokenId = await collectionEvm.methods.nextTokenId().call();260 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);261262 nextTokenId = await collectionEvm.methods.nextTokenId().call({from: user});263 expect(nextTokenId).to.be.equal('1');263 expect(nextTokenId).to.be.equal('1');264 result = await collectionEvm.methods.mintWithTokenURI(264 const result = await collectionEvm.methods.mintWithTokenURI(265 user,265 user,266 nextTokenId,266 nextTokenId,267 'Test URI',267 'Test URI',268 ).send({from: user});268 ).call({from: user});269 console.log(result);269 const events = normalizeEvents(result.events);270 const events = normalizeEvents(result.events);270 events[0].address = events[0].address.toLocaleLowerCase();271271272 expect(events).to.be.deep.equal([272 expect(events).to.be.deep.equal([273 {273 {274 address: collectionIdAddress.toLocaleLowerCase(),274 collectionIdAddress,275 event: 'Transfer',275 event: 'Transfer',276 args: {276 args: {277 from: '0x0000000000000000000000000000000000000000',277 from: '0x0000000000000000000000000000000000000000',282 ]);282 ]);283283284 expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');284 expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');285285 }286 const ownerBalanceAfter = await ethBalanceViaSub(api, owner);287 expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);288 const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);289 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;290 });286 });291287292 itWeb3('Check that transaction via EVM spend money from sponsor address', async ({api, web3}) => {288 itWeb3('Check that transaction via EVM spend money from sponsor address', async ({api, web3}) => {296 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);292 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);297 const sponsor = await createEthAccountWithBalance(api, web3);293 const sponsor = await createEthAccountWithBalance(api, web3);298 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);294 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);299 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send();295 result = await collectionEvm.methods.ethSetSponsor(sponsor).send();300 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;296 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;301 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;297 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;302 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));298 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));303 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('Caller is not set as sponsor');299 await expect(collectionEvm.methods.ethConfirmSponsorship().call()).to.be.rejectedWith('Caller is not set as sponsor');304 const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);300 const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);305 await sponsorCollection.methods.confirmCollectionSponsorship().send();301 await sponsorCollection.methods.ethConfirmSponsorship().send();306 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;302 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;307 expect(collectionSub.sponsorship.isConfirmed).to.be.true;303 expect(collectionSub.sponsorship.isConfirmed).to.be.true;308 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));304 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));309305310 const user = createEthAccount(web3);306 const user = createEthAccount(web3);311 await collectionEvm.methods.addCollectionAdmin(user).send();307 await collectionEvm.methods.addAdmin(user).send();312 308 313 const ownerBalanceBefore = await ethBalanceViaSub(api, owner);309 const ownerBalanceBefore = await ethBalanceViaSub(api, owner);314 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);310 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);tests/src/eth/nonFungibleAbi.jsondiffbeforeafterboth89 "stateMutability": "view",89 "stateMutability": "view",90 "type": "function"90 "type": "function"91 },91 },92 {93 "inputs": [94 { "internalType": "address", "name": "user", "type": "address" }95 ],96 "name": "addToAllowList",97 "outputs": [],98 "stateMutability": "view",99 "type": "function"100 },92 {101 {93 "inputs": [102 "inputs": [94 { "internalType": "address", "name": "approved", "type": "address" },103 { "internalType": "address", "name": "approved", "type": "address" },289 "stateMutability": "view",298 "stateMutability": "view",290 "type": "function"299 "type": "function"291 },300 },301 {302 "inputs": [303 { "internalType": "address", "name": "user", "type": "address" }304 ],305 "name": "removeFromAllowList",306 "outputs": [],307 "stateMutability": "view",308 "type": "function"309 },292 {310 {293 "inputs": [311 "inputs": [294 { "internalType": "address", "name": "from", "type": "address" },312 { "internalType": "address", "name": "from", "type": "address" },312 "stateMutability": "nonpayable",330 "stateMutability": "nonpayable",313 "type": "function"331 "type": "function"314 },332 },333 {334 "inputs": [{ "internalType": "string", "name": "mode", "type": "string" }],335 "name": "setAccess",336 "outputs": [],337 "stateMutability": "nonpayable",338 "type": "function"339 },315 {340 {316 "inputs": [341 "inputs": [317 { "internalType": "address", "name": "operator", "type": "address" },342 { "internalType": "address", "name": "operator", "type": "address" },396 "stateMutability": "nonpayable",421 "stateMutability": "nonpayable",397 "type": "function"422 "type": "function"398 },423 },424 {425 "inputs": [{ "internalType": "bool", "name": "mode", "type": "bool" }],426 "name": "setMintMode",427 "outputs": [],428 "stateMutability": "nonpayable",429 "type": "function"430 },399 {431 {400 "inputs": [432 "inputs": [401 { "internalType": "bool", "name": "enable", "type": "bool" },433 { "internalType": "bool", "name": "enable", "type": "bool" },