diff --git a/src/tokens/Datatoken.ts b/src/tokens/Datatoken.ts index 46e22988..9d642f46 100644 --- a/src/tokens/Datatoken.ts +++ b/src/tokens/Datatoken.ts @@ -1112,6 +1112,7 @@ export class Datatoken { return trxReceipt } + /** Estimate gas for cleanPermissions method * @param dtAddress Datatoken address where we want to clean permissions * @param address User adress diff --git a/src/tokens/NFT.ts b/src/tokens/NFT.ts index 1c6f6859..1392807c 100644 --- a/src/tokens/NFT.ts +++ b/src/tokens/NFT.ts @@ -881,15 +881,13 @@ export class Nft { /** * Estimate gas cost for setMetadata method * @param {String} nftAddress erc721 contract adress - * @param {String} nftOwner Current NFT Owner adress - * @param {String} nftReceiver User which will receive the NFT, will also be set as Manager - * @param {Number} tokenId The id of the token to be transfered + * @param {String} metadataUpdater metadataUpdater address * @param {Contract} nftContract optional contract instance * @return {Promise} */ public async estGasSetMetadata( nftAddress: string, - nftOwner: string, + metadataUpdater: string, metadataState: number, metaDataDecryptorUrl: string, metaDataDecryptorAddress: string, @@ -913,7 +911,7 @@ export class Nft { data, metadataHash ) - .estimateGas({ from: nftOwner }, (err, estGas) => + .estimateGas({ from: metadataUpdater}, (err, estGas) => err ? gasLimitDefault : estGas ) } catch (e) { @@ -928,8 +926,6 @@ export class Nft { * will clean all permissions both on erc721 and erc20 level. * @param {String} nftAddress erc721 contract adress * @param {String} address Caller address NFT Owner adress - * @param {String} nftReceiver User which will receive the NFT, will also be set as Manager - * @param {Number} tokenId The id of the token to be transfered * @return {Promise} trxReceipt */ public async setMetadata( @@ -944,9 +940,9 @@ export class Nft { ): Promise { const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - // if (!(await this.getNFTPermissions(nftAddress, address)).updateMetadata) { - // throw new Error(`Caller is not NFT Owner`) - // } + if (!(await this.getNftPermissions(nftAddress, address)).updateMetadata) { + throw new Error(`Caller is not Metadata updater`) + } const estGas = await this.estGasSetMetadata( nftAddress, @@ -979,6 +975,139 @@ export class Nft { return trxReceipt } + /** + * Estimate gas cost for setMetadataState method + * @param {String} nftAddress erc721 contract adress + * @param {String} nftOwner Current NFT Owner adress + * @param {Number} metadataState new metadata state + * @param {Contract} nftContract optional contract instance + * @return {Promise} + */ + public async estGasSetMetadataState( + nftAddress: string, + metadataUpdater: string, + metadataState: number, + contractInstance?: Contract + ): Promise { + const nftContract = + contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + + const gasLimitDefault = this.GASLIMIT_DEFAULT + let estGas + try { + estGas = await nftContract.methods + .setMetaDataState( + metadataState + ) + .estimateGas({ from: metadataUpdater }, (err, estGas) => + err ? gasLimitDefault : estGas + ) + } catch (e) { + estGas = gasLimitDefault + } + + return estGas + } + + /** + * setMetadataState Used for updating the metadata State + * @param {String} nftAddress erc721 contract adress + * @param {String} address Caller address => metadata updater + * @param {Number} metadataState new metadata state + * @return {Promise} trxReceipt + */ + public async setMetadataState( + nftAddress: string, + address: string, + metadataState: number + ): Promise { + const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + + if (!(await this.getNftPermissions(nftAddress, address)).updateMetadata) { + throw new Error(`Caller is not Metadata updater`) + } + + const estGas = await this.estGasSetMetadataState( + nftAddress, + address, + metadataState + ) + + // Call transferFrom function of the contract + const trxReceipt = await nftContract.methods + .setMetaDataState( + metadataState + ) + .send({ + from: address, + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3) + }) + + return trxReceipt + } + + /** Estimate gas for setTokenURI method + * @param {String} dtAddress Datatoken address + * @param {String} address User address + * @param {String} value Data to be stored into 725Y standard + * @param {Contract} contractInstance optional contract instance + * @return {Promise} + */ + public async estGasSetTokenURI( + nftAddress: string, + address: string, + tokenId: number, + tokenURI: string, + contractInstance?: Contract + ): Promise { + const dtContract = + contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + + const gasLimitDefault = this.GASLIMIT_DEFAULT + let estGas + try { + estGas = await dtContract.methods + .setTokenURI(tokenId,tokenURI) + .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) + } catch (e) { + estGas = gasLimitDefault + } + return estGas + } + + /** setTokenURI + * This function allows to update the token URI + * only NFT Owner can succeed + * @param {String} nftAddress Datatoken address + * @param {String} address User address + * @return {Promise} transactionId + */ + public async setTokenURI( + nftAddress: string, + address: string, + tokenURI: string + ): Promise { + if (await this.getNftOwner(nftAddress) !== address) { + throw new Error(`Caller is not NFT Owner`) + } + const tokenId = 1; + + const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const estGas = await this.estGasSetTokenURI(nftAddress, address, tokenId,tokenURI, nftContract) + + // Call setData function of the contract + const trxReceipt = await nftContract.methods.setTokenURI(tokenId,tokenURI).send({ + from: address, + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3) + }) + + return trxReceipt + } + + + /** Get Owner * @param {String} nftAddress erc721 contract adress * @return {Promise} string