difftreelog
CORE-325 Fix PR
in: master
2 files changed
pallets/nonfungible/src/erc.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -94,18 +94,18 @@
/// Returns token's const_metadata
#[solidity(rename_selector = "tokenURI")]
fn token_uri(&self, token_id: uint256) -> Result<string> {
- if let SchemaVersion::ImageURL = self.schema_version {
- self.consume_store_reads(1)?;
- let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;
- Ok(string::from_utf8_lossy(
- &<TokenData<T>>::get((self.id, token_id))
- .ok_or("token not found")?
- .const_data,
- )
- .into())
- } else {
- Err(error_unsupported_shema_version())
+ if !matches!(self.schema_version, SchemaVersion::ImageURL) {
+ return Err(error_unsupported_shema_version());
}
+
+ self.consume_store_reads(1)?;
+ let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;
+ Ok(string::from_utf8_lossy(
+ &<TokenData<T>>::get((self.id, token_id))
+ .ok_or("token not found")?
+ .const_data,
+ )
+ .into())
}
}
@@ -285,34 +285,34 @@
token_id: uint256,
token_uri: string,
) -> Result<bool> {
- if let SchemaVersion::ImageURL = self.schema_version {
- let caller = T::CrossAccountId::from_eth(caller);
- let to = T::CrossAccountId::from_eth(to);
- let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;
- if <TokensMinted<T>>::get(self.id)
- .checked_add(1)
- .ok_or("item id overflow")?
- != token_id
- {
- return Err("item id should be next".into());
- }
+ if !matches!(self.schema_version, SchemaVersion::ImageURL) {
+ return Err(error_unsupported_shema_version());
+ }
- <Pallet<T>>::create_item(
- self,
- &caller,
- CreateItemData::<T> {
- const_data: Vec::<u8>::from(token_uri)
- .try_into()
- .map_err(|_| "token uri is too long")?,
- variable_data: BoundedVec::default(),
- owner: to,
- },
- )
- .map_err(dispatch_to_evm::<T>)?;
- Ok(true)
- } else {
- Err(error_unsupported_shema_version())
+ let caller = T::CrossAccountId::from_eth(caller);
+ let to = T::CrossAccountId::from_eth(to);
+ let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;
+ if <TokensMinted<T>>::get(self.id)
+ .checked_add(1)
+ .ok_or("item id overflow")?
+ != token_id
+ {
+ return Err("item id should be next".into());
}
+
+ <Pallet<T>>::create_item(
+ self,
+ &caller,
+ CreateItemData::<T> {
+ const_data: Vec::<u8>::from(token_uri)
+ .try_into()
+ .map_err(|_| "token uri is too long")?,
+ variable_data: BoundedVec::default(),
+ owner: to,
+ },
+ )
+ .map_err(dispatch_to_evm::<T>)?;
+ Ok(true)
}
/// Not implemented
@@ -430,36 +430,36 @@
to: address,
tokens: Vec<(uint256, string)>,
) -> Result<bool> {
- if let SchemaVersion::ImageURL = self.schema_version {
- let caller = T::CrossAccountId::from_eth(caller);
- let to = T::CrossAccountId::from_eth(to);
- let mut expected_index = <TokensMinted<T>>::get(self.id)
- .checked_add(1)
- .ok_or("item id overflow")?;
+ if !matches!(self.schema_version, SchemaVersion::ImageURL) {
+ return Err(error_unsupported_shema_version());
+ }
- let mut data = Vec::with_capacity(tokens.len());
- for (id, token_uri) in tokens {
- let id: u32 = id.try_into().map_err(|_| "token id overflow")?;
- if id != expected_index {
- panic!("item id should be next ({}) but got {}", expected_index, id);
- }
- expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;
+ let caller = T::CrossAccountId::from_eth(caller);
+ let to = T::CrossAccountId::from_eth(to);
+ let mut expected_index = <TokensMinted<T>>::get(self.id)
+ .checked_add(1)
+ .ok_or("item id overflow")?;
- data.push(CreateItemData::<T> {
- const_data: Vec::<u8>::from(token_uri)
- .try_into()
- .map_err(|_| "token uri is too long")?,
- variable_data: vec![].try_into().unwrap(),
- owner: to.clone(),
- });
+ let mut data = Vec::with_capacity(tokens.len());
+ for (id, token_uri) in tokens {
+ let id: u32 = id.try_into().map_err(|_| "token id overflow")?;
+ if id != expected_index {
+ panic!("item id should be next ({}) but got {}", expected_index, id);
}
+ expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;
- <Pallet<T>>::create_multiple_items(self, &caller, data)
- .map_err(dispatch_to_evm::<T>)?;
- Ok(true)
- } else {
- Err(error_unsupported_shema_version())
+ data.push(CreateItemData::<T> {
+ const_data: Vec::<u8>::from(token_uri)
+ .try_into()
+ .map_err(|_| "token uri is too long")?,
+ variable_data: vec![].try_into().unwrap(),
+ owner: to.clone(),
+ });
}
+
+ <Pallet<T>>::create_multiple_items(self, &caller, data)
+ .map_err(dispatch_to_evm::<T>)?;
+ Ok(true)
}
}
tests/src/eth/metadata.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {expect} from 'chai';18import {createCollectionExpectSuccess} from '../util/helpers';19import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents} from './util/helpers';20import fungibleMetadataAbi from './fungibleMetadataAbi.json';21import privateKey from '../substrate/privateKey';22import {submitTransactionAsync} from '../substrate/substrate-api';23import nonFungibleAbi from './nonFungibleAbi.json';2425describe('Common metadata', () => {26 itWeb3('Returns collection name', async ({api, web3}) => {27 const collection = await createCollectionExpectSuccess({28 name: 'token name',29 mode: {type: 'NFT'},30 });31 const caller = await createEthAccountWithBalance(api, web3);3233 const address = collectionIdToAddress(collection);34 const contract = new web3.eth.Contract(fungibleMetadataAbi as any, address, {from: caller, ...GAS_ARGS});35 const name = await contract.methods.name().call();3637 expect(name).to.equal('token name');38 });3940 itWeb3('Returns symbol name', async ({api, web3}) => {41 const collection = await createCollectionExpectSuccess({42 tokenPrefix: 'TOK',43 mode: {type: 'NFT'},44 });45 const caller = await createEthAccountWithBalance(api, web3);4647 const address = collectionIdToAddress(collection);48 const contract = new web3.eth.Contract(fungibleMetadataAbi as any, address, {from: caller, ...GAS_ARGS});49 const symbol = await contract.methods.symbol().call();5051 expect(symbol).to.equal('TOK');52 });53});5455describe('Fungible metadata', () => {56 itWeb3('Returns fungible decimals', async ({api, web3}) => {57 const collection = await createCollectionExpectSuccess({58 mode: {type: 'Fungible', decimalPoints: 6},59 });60 const caller = await createEthAccountWithBalance(api, web3);6162 const address = collectionIdToAddress(collection);63 const contract = new web3.eth.Contract(fungibleMetadataAbi as any, address, {from: caller, ...GAS_ARGS});64 const decimals = await contract.methods.decimals().call();6566 expect(+decimals).to.equal(6);67 });68});6970describe('Support ERC721Metadata', () => {71 itWeb3('Check unsupport ERC721Metadata ShemaVersion::Unique', async ({web3, api}) => {72 const collectionId = await createCollectionExpectSuccess({73 mode: {type: 'NFT'},74 shemaVersion: 'Unique',75 name: 'some_name',76 tokenPrefix: 'some_prefix',77 });78 const collection = await api.rpc.unique.collectionById(collectionId);79 expect(collection.isSome).to.be.true;80 expect(collection.unwrap().schemaVersion.toHuman()).to.be.eq('Unique');8182 const alice = privateKey('//Alice');8384 const caller = await createEthAccountWithBalance(api, web3);85 const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, {Ethereum: caller});86 await submitTransactionAsync(alice, changeAdminTx);8788 const address = collectionIdToAddress(collectionId);89 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});9091 expect(await contract.methods.name().call()).to.be.eq('some_name');92 expect(await contract.methods.symbol().call()).to.be.eq('some_prefix');9394 const receiver = createEthAccount(web3);95 const nextTokenId = await contract.methods.nextTokenId().call();96 expect(nextTokenId).to.be.equal('1');97 await expect(contract.methods.mintWithTokenURI(98 receiver,99 nextTokenId,100 'Test URI',101 ).send({from: caller})).to.be.rejected;102103 await expect(contract.methods.mintBulkWithTokenURI(104 receiver,105 [106 [nextTokenId, 'Test URI 0'],107 [+nextTokenId + 1, 'Test URI 1'],108 [+nextTokenId + 2, 'Test URI 2'],109 ],110 ).send({from: caller})).to.be.rejected;111 });112113 itWeb3('Check support ERC721Metadata for ShemaVersion::ImageURL', async ({web3, api}) => {114 const collectionId = await createCollectionExpectSuccess({115 mode: {type: 'NFT'},116 name: 'some_name',117 tokenPrefix: 'some_prefix',118 });119 const collection = await api.rpc.unique.collectionById(collectionId);120 expect(collection.isSome).to.be.true;121 expect(collection.unwrap().schemaVersion.toHuman()).to.be.eq('ImageURL');122123 const alice = privateKey('//Alice');124125 const caller = await createEthAccountWithBalance(api, web3);126 const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, {Ethereum: caller});127 await submitTransactionAsync(alice, changeAdminTx);128129 const address = collectionIdToAddress(collectionId);130 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});131 132 expect(await contract.methods.name().call()).to.be.eq('some_name');133 expect(await contract.methods.symbol().call()).to.be.eq('some_prefix');134135 const receiver = createEthAccount(web3);136 { // mintWithTokenURI137 const nextTokenId = await contract.methods.nextTokenId().call();138 expect(nextTokenId).to.be.equal('1');139 const result = await contract.methods.mintWithTokenURI(140 receiver,141 nextTokenId,142 'Test URI',143 ).send({from: caller});144 const events = normalizeEvents(result.events);145 146 expect(events).to.be.deep.equal([147 {148 address,149 event: 'Transfer',150 args: {151 from: '0x0000000000000000000000000000000000000000',152 to: receiver,153 tokenId: nextTokenId,154 },155 },156 ]);157 158 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');159 }160161 { // mintBulkWithTokenURI162 const nextTokenId = await contract.methods.nextTokenId().call();163 expect(nextTokenId).to.be.equal('2');164 const result = await contract.methods.mintBulkWithTokenURI(165 receiver,166 [167 [nextTokenId, 'Test URI 0'],168 [+nextTokenId + 1, 'Test URI 1'],169 [+nextTokenId + 2, 'Test URI 2'],170 ],171 ).send({from: caller});172 const events = normalizeEvents(result.events);173174 expect(events).to.be.deep.equal([175 {176 address,177 event: 'Transfer',178 args: {179 from: '0x0000000000000000000000000000000000000000',180 to: receiver,181 tokenId: nextTokenId,182 },183 },184 {185 address,186 event: 'Transfer',187 args: {188 from: '0x0000000000000000000000000000000000000000',189 to: receiver,190 tokenId: String(+nextTokenId + 1),191 },192 },193 {194 address,195 event: 'Transfer',196 args: {197 from: '0x0000000000000000000000000000000000000000',198 to: receiver,199 tokenId: String(+nextTokenId + 2),200 },201 },202 ]);203204 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');205 expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');206 expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');207 }208 });209});2101// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {expect} from 'chai';18import {createCollectionExpectSuccess} from '../util/helpers';19import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents} from './util/helpers';20import fungibleMetadataAbi from './fungibleMetadataAbi.json';21import privateKey from '../substrate/privateKey';22import {submitTransactionAsync} from '../substrate/substrate-api';23import nonFungibleAbi from './nonFungibleAbi.json';2425describe('Common metadata', () => {26 itWeb3('Returns collection name', async ({api, web3}) => {27 const collection = await createCollectionExpectSuccess({28 name: 'token name',29 mode: {type: 'NFT'},30 });31 const caller = await createEthAccountWithBalance(api, web3);3233 const address = collectionIdToAddress(collection);34 const contract = new web3.eth.Contract(fungibleMetadataAbi as any, address, {from: caller, ...GAS_ARGS});35 const name = await contract.methods.name().call();3637 expect(name).to.equal('token name');38 });3940 itWeb3('Returns symbol name', async ({api, web3}) => {41 const collection = await createCollectionExpectSuccess({42 tokenPrefix: 'TOK',43 mode: {type: 'NFT'},44 });45 const caller = await createEthAccountWithBalance(api, web3);4647 const address = collectionIdToAddress(collection);48 const contract = new web3.eth.Contract(fungibleMetadataAbi as any, address, {from: caller, ...GAS_ARGS});49 const symbol = await contract.methods.symbol().call();5051 expect(symbol).to.equal('TOK');52 });53});5455describe('Fungible metadata', () => {56 itWeb3('Returns fungible decimals', async ({api, web3}) => {57 const collection = await createCollectionExpectSuccess({58 mode: {type: 'Fungible', decimalPoints: 6},59 });60 const caller = await createEthAccountWithBalance(api, web3);6162 const address = collectionIdToAddress(collection);63 const contract = new web3.eth.Contract(fungibleMetadataAbi as any, address, {from: caller, ...GAS_ARGS});64 const decimals = await contract.methods.decimals().call();6566 expect(+decimals).to.equal(6);67 });68});6970describe('Support ERC721Metadata', () => {71 itWeb3('Check unsupport ERC721Metadata ShemaVersion::Unique', async ({web3, api}) => {72 const collectionId = await createCollectionExpectSuccess({73 mode: {type: 'NFT'},74 shemaVersion: 'Unique',75 name: 'some_name',76 tokenPrefix: 'some_prefix',77 });78 const collection = await api.rpc.unique.collectionById(collectionId);79 expect(collection.isSome).to.be.true;80 expect(collection.unwrap().schemaVersion.toHuman()).to.be.eq('Unique');8182 const alice = privateKey('//Alice');8384 const caller = await createEthAccountWithBalance(api, web3);85 const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, {Ethereum: caller});86 await submitTransactionAsync(alice, changeAdminTx);8788 const address = collectionIdToAddress(collectionId);89 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});9091 expect(await contract.methods.name().call()).to.be.eq('some_name');92 expect(await contract.methods.symbol().call()).to.be.eq('some_prefix');9394 const receiver = createEthAccount(web3);95 const nextTokenId = await contract.methods.nextTokenId().call();96 expect(nextTokenId).to.be.equal('1');97 await expect(contract.methods.mintWithTokenURI(98 receiver,99 nextTokenId,100 'Test URI',101 ).call({from: caller})).to.be.rejectedWith('Unsupported shema version! Support only ImageURL');102103 await expect(contract.methods.mintBulkWithTokenURI(104 receiver,105 [106 [nextTokenId, 'Test URI 0'],107 [+nextTokenId + 1, 'Test URI 1'],108 [+nextTokenId + 2, 'Test URI 2'],109 ],110 ).call({from: caller})).to.be.rejectedWith('Unsupported shema version! Support only ImageURL');111 });112113 itWeb3('Check support ERC721Metadata for ShemaVersion::ImageURL', async ({web3, api}) => {114 const collectionId = await createCollectionExpectSuccess({115 mode: {type: 'NFT'},116 name: 'some_name',117 tokenPrefix: 'some_prefix',118 });119 const collection = await api.rpc.unique.collectionById(collectionId);120 expect(collection.isSome).to.be.true;121 expect(collection.unwrap().schemaVersion.toHuman()).to.be.eq('ImageURL');122123 const alice = privateKey('//Alice');124125 const caller = await createEthAccountWithBalance(api, web3);126 const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, {Ethereum: caller});127 await submitTransactionAsync(alice, changeAdminTx);128129 const address = collectionIdToAddress(collectionId);130 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});131 132 expect(await contract.methods.name().call()).to.be.eq('some_name');133 expect(await contract.methods.symbol().call()).to.be.eq('some_prefix');134135 const receiver = createEthAccount(web3);136 { // mintWithTokenURI137 const nextTokenId = await contract.methods.nextTokenId().call();138 expect(nextTokenId).to.be.equal('1');139 const result = await contract.methods.mintWithTokenURI(140 receiver,141 nextTokenId,142 'Test URI',143 ).send({from: caller});144 const events = normalizeEvents(result.events);145 146 expect(events).to.be.deep.equal([147 {148 address,149 event: 'Transfer',150 args: {151 from: '0x0000000000000000000000000000000000000000',152 to: receiver,153 tokenId: nextTokenId,154 },155 },156 ]);157 158 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');159 }160161 { // mintBulkWithTokenURI162 const nextTokenId = await contract.methods.nextTokenId().call();163 expect(nextTokenId).to.be.equal('2');164 const result = await contract.methods.mintBulkWithTokenURI(165 receiver,166 [167 [nextTokenId, 'Test URI 0'],168 [+nextTokenId + 1, 'Test URI 1'],169 [+nextTokenId + 2, 'Test URI 2'],170 ],171 ).send({from: caller});172 const events = normalizeEvents(result.events);173174 expect(events).to.be.deep.equal([175 {176 address,177 event: 'Transfer',178 args: {179 from: '0x0000000000000000000000000000000000000000',180 to: receiver,181 tokenId: nextTokenId,182 },183 },184 {185 address,186 event: 'Transfer',187 args: {188 from: '0x0000000000000000000000000000000000000000',189 to: receiver,190 tokenId: String(+nextTokenId + 1),191 },192 },193 {194 address,195 event: 'Transfer',196 args: {197 from: '0x0000000000000000000000000000000000000000',198 to: receiver,199 tokenId: String(+nextTokenId + 2),200 },201 },202 ]);203204 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');205 expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');206 expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');207 }208 });209});210