From e2c0a512c1681c7196579c19555cb3c6dad1563c Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Thu, 9 Dec 2021 14:02:46 +0200 Subject: [PATCH] Delete Nft file --- src/tokens/Nft.ts | 1024 --------------------------------------------- 1 file changed, 1024 deletions(-) delete mode 100644 src/tokens/Nft.ts diff --git a/src/tokens/Nft.ts b/src/tokens/Nft.ts deleted file mode 100644 index 1c6f6859..00000000 --- a/src/tokens/Nft.ts +++ /dev/null @@ -1,1024 +0,0 @@ -import Web3 from 'web3' -import { AbiItem } from 'web3-utils' -import { TransactionReceipt } from 'web3-eth' -import defaultNftAbi from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' -import { LoggerInstance, getFairGasPrice, generateDtName } from '../utils' -import { Contract } from 'web3-eth-contract' - -/** - * ERC721 ROLES - */ -interface Roles { - manager: boolean - deployErc20: boolean - updateMetadata: boolean - store: boolean -} - -export class Nft { - public GASLIMIT_DEFAULT = 1000000 - public factory721Address: string - public factory721Abi: AbiItem | AbiItem[] - public nftAbi: AbiItem | AbiItem[] - public web3: Web3 - public startBlock: number - - constructor(web3: Web3, nftAbi?: AbiItem | AbiItem[], startBlock?: number) { - this.nftAbi = nftAbi || (defaultNftAbi.abi as AbiItem[]) - this.web3 = web3 - this.startBlock = startBlock || 0 - } - - /** - * Estimate gas cost for createERC20 token creation - * @param {String} nftAddress ERC721 addreess - * @param {String} address User address - * @param {String} minter User set as initial minter for the ERC20 - * @param {String} feeManager initial feeManager for this DT - * @param {String} mpFeeAddress Consume marketplace fee address - * @param {String} feeToken address of the token marketplace wants to add fee on top - * @param {String} feeAmount amount of feeToken to be transferred to mpFeeAddress on top, will be converted to WEI - * @param {String} cap Maximum cap (Number) - will be converted to wei - * @param {String} name Token name - * @param {String} symbol Token symbol - * @param {Number} templateIndex NFT template index - * @param {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async estGasCreateErc20( - nftAddress: string, - address: string, - minter: string, - feeManager: string, - mpFeeAddress: string, - feeToken: string, - feeAmount: string, - cap: string, - name?: string, - symbol?: string, - templateIndex?: 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 - .createERC20( - templateIndex, - [name, symbol], - [minter, feeManager, mpFeeAddress, feeToken], - [this.web3.utils.toWei(cap), this.web3.utils.toWei(feeAmount)], - [] - ) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas - } - - /** - * Create new ERC20 datatoken - only user with ERC20Deployer permission can succeed - * @param {String} nftAddress ERC721 addreess - * @param {String} address User address - * @param {String} minter User set as initial minter for the ERC20 - * @param {String} feeManager initial feeManager for this DT - * @param {String} mpFeeAddress Consume marketplace fee address - * @param {String} feeToken address of the token marketplace wants to add fee on top - * @param {String} feeAmount amount of feeToken to be transferred to mpFeeAddress on top, will be converted to WEI - * @param {String} cap Maximum cap (Number) - will be converted to wei - * @param {String} name Token name - * @param {String} symbol Token symbol - * @param {Number} templateIndex NFT template index - * @return {Promise} ERC20 datatoken address - */ - public async createErc20( - nftAddress: string, - address: string, - minter: string, - feeManager: string, - mpFeeAddress: string, - feeToken: string, - feeAmount: string, - cap: string, - name?: string, - symbol?: string, - templateIndex?: number - ): Promise { - if (!templateIndex) templateIndex = 1 - - // Generate name & symbol if not present - if (!name || !symbol) { - ;({ name, symbol } = generateDtName()) - } - - // Create 721contract object - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - - const estGas = await this.estGasCreateErc20( - nftAddress, - address, - minter, - feeManager, - mpFeeAddress, - feeToken, - feeAmount, - cap, - name, - symbol, - templateIndex, - nftContract - ) - - // Call createERC20 token function of the contract - const trxReceipt = await nftContract.methods - .createERC20( - templateIndex, - [name, symbol], - [minter, feeManager, mpFeeAddress, feeToken], - [this.web3.utils.toWei(cap), this.web3.utils.toWei(feeAmount)], - [] - ) - .send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - let tokenAddress = null - try { - tokenAddress = trxReceipt.events.TokenCreated.returnValues[0] - } catch (e) { - LoggerInstance.error(`ERROR: Failed to create datatoken : ${e.message}`) - } - return tokenAddress - } - - /** - * Estimate gas cost for add manager call - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Owner adress - * @param {String} manager User adress which is going to be assing manager - * @param {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async estGasAddManager( - nftAddress: string, - address: string, - manager: string, - contractInstance?: Contract - ) { - const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) - - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .addManager(manager) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas - } - - /** - * Add Manager for NFT Contract (only NFT Owner can succeed) - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Owner adress - * @param {String} manager User adress which is going to be assing manager - * @return {Promise} trxReceipt - */ - public async addManager(nftAddress: string, address: string, manager: string) { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - - if ((await this.getNftOwner(nftAddress)) !== address) { - throw new Error(`Caller is not NFT Owner`) - } - - const estGas = await this.estGasAddManager(nftAddress, address, manager, nftContract) - - // Invoke addManager function of the contract - const trxReceipt = await nftContract.methods.addManager(manager).send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - return trxReceipt - } - - /** - * Estimate gas cost for removeManager method - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Owner adress - * @param {String} manager User adress which is going to be removed as manager - * @param {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async estGasRemoveManager( - nftAddress: string, - address: string, - manager: string, - contractInstance?: Contract - ) { - const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .removeManager(manager) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas - } - - /** - * Removes a specific manager for NFT Contract (only NFT Owner can succeed) - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Owner adress - * @param {String} manager User adress which is going to be removed as manager - * @return {Promise} trxReceipt - */ - public async removeManager(nftAddress: string, address: string, manager: string) { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - - if ((await this.getNftOwner(nftAddress)) !== address) { - throw new Error(`Caller is not NFT Owner`) - } - - const estGas = await this.estGasRemoveManager( - nftAddress, - address, - manager, - nftContract - ) - - // Invoke removeManager function of the contract - const trxReceipt = await nftContract.methods.removeManager(manager).send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - return trxReceipt - } - - /** - * Estimate gas cost for addToCreateERC20List method - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Manager adress - * @param {String} erc20Deployer User adress which is going to have erc20Deployer permission - * @param {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async estGasAddErc20Deployer( - nftAddress: string, - address: string, - erc20Deployer: string, - 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 - .addToCreateERC20List(erc20Deployer) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas - } - - /** - * Add ERC20Deployer permission - only Manager can succeed - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Manager adress - * @param {String} erc20Deployer User adress which is going to have erc20Deployer permission - * @return {Promise} trxReceipt - */ - public async addErc20Deployer( - nftAddress: string, - address: string, - erc20Deployer: string - ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - - // if ((await this.getNFTPermissions(nftAddress, address)).manager !== true) { - // throw new Error(`Caller is not Manager`) - // } - - // Estimate gas for addToCreateERC20List method - const estGas = await this.estGasAddErc20Deployer( - nftAddress, - address, - erc20Deployer, - nftContract - ) - - // Invoke addToCreateERC20List function of the contract - const trxReceipt = await nftContract.methods - .addToCreateERC20List(erc20Deployer) - .send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - return trxReceipt - } - - /** - * Estimate gas cost for removeFromCreateERC20List method - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Manager adress - * @param {String} erc20Deployer Address of the user to be revoked ERC20Deployer Permission - * @param {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async estGasRemoveErc20Deployer( - nftAddress: string, - address: string, - erc20Deployer: string, - 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 - .removeFromCreateErc20List(erc20Deployer) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas - } - - /** - * Remove ERC20Deployer permission - only Manager can succeed - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Manager adress - * @param {String} erc20Deployer Address of the user to be revoked ERC20Deployer Permission - * @return {Promise} trxReceipt - */ - public async removeErc20Deployer( - nftAddress: string, - address: string, - erc20Deployer: string - ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - - // if ((await this.getNFTPermissions(nftAddress, address)).manager !== true) { - // throw new Error(`Caller is not Manager`) - // } - - const estGas = await this.estGasRemoveErc20Deployer( - nftAddress, - address, - erc20Deployer, - nftContract - ) - - // Call removeFromCreateERC20List function of the contract - const trxReceipt = await nftContract.methods - .removeFromCreateERC20List(erc20Deployer) - .send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - return trxReceipt - } - - /** - * Estimate gas cost for addToMetadataList method - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Manager adress - * @param {String} metadataUpdater User adress which is going to have Metadata Updater permission - * @param {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async estGasAddMetadataUpdater( - nftAddress: string, - address: string, - metadataUpdater: string, - 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 - .addToMetadataList(metadataUpdater) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas - } - - /** - * Add Metadata Updater permission - only Manager can succeed - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Manager adress - * @param {String} metadataUpdater User adress which is going to have Metadata Updater permission - * @return {Promise} trxReceipt - */ - public async addMetadataUpdater( - nftAddress: string, - address: string, - metadataUpdater: string - ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - - // if ((await this.getNFTPermissions(nftAddress, address)).manager !== true) { - // throw new Error(`Caller is not Manager`) - // } - - const estGas = await this.estGasAddMetadataUpdater( - nftAddress, - address, - metadataUpdater, - nftContract - ) - - // Call addToMetadataList function of the contract - const trxReceipt = await nftContract.methods.addToMetadataList(metadataUpdater).send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - return trxReceipt - } - - /** - * Estimate gas cost for removeFromMetadataList method - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Manager adress - * @param {String} metadataUpdater Address of the user to be revoked Metadata updater Permission - * @param {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async esGasRemoveMetadataUpdater( - nftAddress: string, - address: string, - metadataUpdater: string, - 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 - .removeFromMetadataList(metadataUpdater) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas - } - - /** - * Remove Metadata Updater permission - only Manager can succeed - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Manager adress - * @param {String} metadataUpdater Address of the user to be revoked Metadata updater Permission - * @return {Promise} trxReceipt - */ - public async removeMetadataUpdater( - nftAddress: string, - address: string, - metadataUpdater: string - ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - - // if ((await this.getNFTPermissions(nftAddress, address)).manager !== true) { - // throw new Error(`Caller is not Manager`) - // } - - const estGas = await this.esGasRemoveMetadataUpdater( - nftAddress, - address, - metadataUpdater, - nftContract - ) - - // Call removeFromMetadataList function of the contract - const trxReceipt = await nftContract.methods - .removeFromMetadataList(metadataUpdater) - .send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - return trxReceipt - } - - /** - * Estimate gas cost for addTo725StoreList method - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Manager adress - * @param {String} storeUpdater User adress which is going to have Store Updater permission - * @param {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async estGasAddStoreUpdater( - nftAddress: string, - address: string, - storeUpdater: string, - 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 - .addTo725StoreList(storeUpdater) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas - } - - /** - * Add Store Updater permission - only Manager can succeed - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Manager adress - * @param {String} storeUpdater User adress which is going to have Store Updater permission - * @return {Promise} trxReceipt - */ - public async addStoreUpdater( - nftAddress: string, - address: string, - storeUpdater: string - ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - - // if ((await this.getNFTPermissions(nftAddress, address)).manager !== true) { - // throw new Error(`Caller is not Manager`) - // } - - const estGas = await this.estGasAddStoreUpdater( - nftAddress, - address, - storeUpdater, - nftContract - ) - - // Call addTo725StoreList function of the contract - const trxReceipt = await nftContract.methods.addTo725StoreList(storeUpdater).send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - return trxReceipt - } - - /** - * Estimate gas cost for removeFrom725StoreList method - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Manager adress - * @param {String} storeUpdater Address of the user to be revoked Store Updater Permission - * @param {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async estGasRemoveStoreUpdater( - nftAddress: string, - address: string, - storeUpdater: string, - 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 - .removeFrom725StoreList(storeUpdater) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas - } - - /** - * Remove Store Updater permission - only Manager can succeed - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Manager adress - * @param {String} storeUpdater Address of the user to be revoked Store Updater Permission - * @return {Promise} trxReceipt - */ - public async removeStoreUpdater( - nftAddress: string, - address: string, - storeUpdater: string - ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - - // if ((await this.getNFTPermissions(nftAddress, address)).manager !== true) { - // throw new Error(`Caller is not Manager`) - // } - - const estGas = await this.estGasRemoveStoreUpdater( - nftAddress, - address, - storeUpdater, - nftContract - ) - - // Call removeFrom725StoreList function of the contract - const trxReceipt = await nftContract.methods - .removeFrom725StoreList(storeUpdater) - .send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - return trxReceipt - } - - /** - * Estimate gas cost for cleanPermissions method - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Owner adress - * @param {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async estGasCleanPermissions( - nftAddress: string, - address: string, - 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 - .cleanPermissions() - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas - } - - /** - * This function allows to remove all ROLES at erc721 level: Managers, ERC20Deployer, MetadataUpdater, StoreUpdater - * Even NFT Owner has to readd himself as Manager - * Permissions at erc20 level stay. - * Only NFT Owner can call it. - * @param {String} nftAddress erc721 contract adress - * @param {String} address NFT Owner adress - * @return {Promise} trxReceipt - */ - - public async cleanPermissions( - nftAddress: string, - address: string - ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - - if ((await this.getNftOwner(nftAddress)) !== address) { - throw new Error(`Caller is not NFT Owner`) - } - - const estGas = await this.estGasCleanPermissions(nftAddress, address, nftContract) - - // Call cleanPermissions function of the contract - const trxReceipt = await nftContract.methods.cleanPermissions().send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - return trxReceipt - } - - /** - * Estimate gas cost for transfer NFT 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 {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async estGasTransferNft( - nftAddress: string, - nftOwner: string, - nftReceiver: string, - tokenId: 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 - .transferFrom(nftOwner, nftReceiver, tokenId) - .estimateGas({ from: nftOwner }, (err, estGas) => - err ? gasLimitDefault : estGas - ) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas - } - - /** - * Transfers the NFT - * will clean all permissions both on erc721 and erc20 level. - * @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 - * @return {Promise} trxReceipt - */ - public async transferNft( - nftAddress: string, - nftOwner: string, - nftReceiver: string, - tokenId?: number - ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - - if ((await this.getNftOwner(nftAddress)) !== nftOwner) { - throw new Error(`Caller is not NFT Owner`) - } - - const tokenIdentifier = tokenId || 1 - - const estGas = await this.estGasTransferNft( - nftAddress, - nftOwner, - nftReceiver, - tokenIdentifier, - nftContract - ) - - // Call transferFrom function of the contract - const trxReceipt = await nftContract.methods - .transferFrom(nftOwner, nftReceiver, tokenIdentifier) - .send({ - from: nftOwner, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - return trxReceipt - } - - /** - * Estimate gas cost for safeTransfer NFT 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 {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async estGasSafeTransferNft( - nftAddress: string, - nftOwner: string, - nftReceiver: string, - tokenId: 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 - .safeTransferFrom(nftOwner, nftReceiver, tokenId) - .estimateGas({ from: nftOwner }, (err, estGas) => - err ? gasLimitDefault : estGas - ) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas - } - - /** - * safeTransferNFT Used for transferring the NFT, can be used by an approved relayer - * will clean all permissions both on erc721 and erc20 level. - * @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 - * @return {Promise} trxReceipt - */ - public async safeTransferNft( - nftAddress: string, - nftOwner: string, - nftReceiver: string, - tokenId?: number - ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - - if ((await this.getNftOwner(nftAddress)) !== nftOwner) { - throw new Error(`Caller is not NFT Owner`) - } - - const tokenIdentifier = tokenId || 1 - - const estGas = await this.estGasSafeTransferNft( - nftAddress, - nftOwner, - nftReceiver, - tokenIdentifier, - nftContract - ) - - // Call transferFrom function of the contract - const trxReceipt = await nftContract.methods - .safeTransferFrom(nftOwner, nftReceiver, tokenIdentifier) - .send({ - from: nftOwner, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - return trxReceipt - } - - /** - * 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 {Contract} nftContract optional contract instance - * @return {Promise} - */ - public async estGasSetMetadata( - nftAddress: string, - nftOwner: string, - metadataState: number, - metaDataDecryptorUrl: string, - metaDataDecryptorAddress: string, - flags: string, - data: string, - metadataHash: string, - 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 - .setMetaData( - metadataState, - metaDataDecryptorUrl, - metaDataDecryptorAddress, - flags, - data, - metadataHash - ) - .estimateGas({ from: nftOwner }, (err, estGas) => - err ? gasLimitDefault : estGas - ) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas - } - - /** - * safeTransferNFT Used for transferring the NFT, can be used by an approved relayer - * 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( - nftAddress: string, - address: string, - metadataState: number, - metaDataDecryptorUrl: string, - metaDataDecryptorAddress: string, - flags: string, - data: string, - metadataHash: string - ): 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`) - // } - - const estGas = await this.estGasSetMetadata( - nftAddress, - address, - metadataState, - metaDataDecryptorUrl, - metaDataDecryptorAddress, - flags, - data, - metadataHash, - nftContract - ) - - // Call transferFrom function of the contract - const trxReceipt = await nftContract.methods - .setMetaData( - metadataState, - metaDataDecryptorUrl, - metaDataDecryptorAddress, - flags, - data, - metadataHash - ) - .send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) - }) - - return trxReceipt - } - - /** Get Owner - * @param {String} nftAddress erc721 contract adress - * @return {Promise} string - */ - public async getNftOwner(nftAddress: string): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - const trxReceipt = await nftContract.methods.ownerOf(1).call() - return trxReceipt - } - - /** Get users NFT Permissions - * @param {String} nftAddress erc721 contract adress - * @param {String} address user adress - * @return {Promise} - */ - public async getNftPermissions(nftAddress: string, address: string): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - const roles = await nftContract.methods.getPermissions(address).call() - return roles - } - - /** Get users ERC20Deployer role - * @param {String} nftAddress erc721 contract adress - * @param {String} address user adress - * @return {Promise} - */ - public async isErc20Deployer(nftAddress: string, address: string): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - const isERC20Deployer = await nftContract.methods.isERC20Deployer(address).call() - return isERC20Deployer - } - - /** Gets data at a given `key` - * @param {String} nftAddress erc721 contract adress - * @param {String} key the key which value to retrieve - * @return {Promise} The data stored at the key - */ - public async getData(nftAddress: string, key: string): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) - const data = await nftContract.methods.getData(key).call() - return data - } -}