1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

inherit NFT from SmartContract

This commit is contained in:
Miquel A. Cabot 2022-06-06 17:28:43 +02:00
parent b89a121997
commit 3b56218935

View File

@ -1,7 +1,6 @@
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 ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json'
import {
LoggerInstance,
getFairGasPrice,
@ -11,24 +10,11 @@ import {
} from '../../utils'
import { Contract } from 'web3-eth-contract'
import { MetadataProof, MetadataAndTokenURI, NftRoles } from '../../@types'
import { Config, ConfigHelper } from '../../config'
import { SmartContract } from '../SmartContract'
export class Nft {
public factory721Address: string
public nftAbi: AbiItem | AbiItem[]
public web3: Web3
public startBlock: number
public config: Config
constructor(
web3: Web3,
network?: string | number,
nftAbi?: AbiItem | AbiItem[],
config?: Config
) {
this.nftAbi = nftAbi || (defaultNftAbi.abi as AbiItem[])
this.web3 = web3
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
export class Nft extends SmartContract {
getDefaultAbi(): AbiItem | AbiItem[] {
return ERC721Template.abi as AbiItem[]
}
/**
@ -63,10 +49,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(
address,
nftContract.methods.createERC20,
@ -118,7 +101,7 @@ export class Nft {
// Create 721contract object
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -172,10 +155,7 @@ export class Nft {
) {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(address, nftContract.methods.addManager, manager)
}
@ -189,7 +169,7 @@ export class Nft {
*/
public async addManager(nftAddress: string, address: string, manager: string) {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -225,10 +205,7 @@ export class Nft {
) {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(address, nftContract.methods.removeManager, manager)
}
@ -241,7 +218,7 @@ export class Nft {
*/
public async removeManager(nftAddress: string, address: string, manager: string) {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -277,10 +254,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(address, nftContract.methods.addToCreateERC20List, erc20Deployer)
}
@ -297,7 +271,7 @@ export class Nft {
erc20Deployer: string
): Promise<TransactionReceipt> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -340,10 +314,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(
address,
@ -365,7 +336,7 @@ export class Nft {
erc20Deployer: string
): Promise<TransactionReceipt> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -410,10 +381,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(address, nftContract.methods.addToMetadataList, metadataUpdater)
}
@ -431,7 +399,7 @@ export class Nft {
metadataUpdater: string
): Promise<TransactionReceipt> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -471,10 +439,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(
address,
@ -496,7 +461,7 @@ export class Nft {
metadataUpdater: string
): Promise<TransactionReceipt> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -543,10 +508,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(address, nftContract.methods.addTo725StoreList, storeUpdater)
}
@ -564,7 +526,7 @@ export class Nft {
storeUpdater: string
): Promise<TransactionReceipt> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -604,10 +566,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(address, nftContract.methods.removeFrom725StoreList, storeUpdater)
}
@ -625,7 +584,7 @@ export class Nft {
storeUpdater: string
): Promise<TransactionReceipt> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -669,10 +628,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(address, nftContract.methods.cleanPermissions)
}
@ -692,7 +648,7 @@ export class Nft {
address: string
): Promise<TransactionReceipt> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -730,10 +686,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(
nftOwner,
@ -760,7 +713,7 @@ export class Nft {
tokenId?: number
): Promise<TransactionReceipt> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -808,10 +761,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(
nftOwner,
@ -838,7 +788,7 @@ export class Nft {
tokenId?: number
): Promise<TransactionReceipt> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -893,10 +843,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
if (!metadataProofs) metadataProofs = []
return estimateGas(
metadataUpdater,
@ -930,7 +877,7 @@ export class Nft {
metadataProofs?: MetadataProof[]
): Promise<TransactionReceipt> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
if (!metadataProofs) metadataProofs = []
@ -983,10 +930,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
const sanitizedMetadataAndTokenURI = {
...metadataAndTokenURI,
metadataProofs: metadataAndTokenURI.metadataProofs || []
@ -1011,7 +955,7 @@ export class Nft {
metadataAndTokenURI: MetadataAndTokenURI
): Promise<TransactionReceipt> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
if (!(await this.getNftPermissions(nftAddress, metadataUpdater)).updateMetadata) {
@ -1053,10 +997,7 @@ export class Nft {
): Promise<any> {
const nftContract =
contractInstance ||
setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
setContractDefaults(new this.web3.eth.Contract(this.abi, nftAddress), this.config)
return estimateGas(
metadataUpdater,
@ -1078,7 +1019,7 @@ export class Nft {
metadataState: number
): Promise<TransactionReceipt> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -1114,7 +1055,7 @@ export class Nft {
data: string
): Promise<any> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -1133,7 +1074,7 @@ export class Nft {
data: string
): Promise<any> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
@ -1152,7 +1093,7 @@ export class Nft {
*/
public async getNftOwner(nftAddress: string): Promise<string> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
const trxReceipt = await nftContract.methods.ownerOf(1).call()
@ -1166,7 +1107,7 @@ export class Nft {
*/
public async getNftPermissions(nftAddress: string, address: string): Promise<NftRoles> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
const roles = await nftContract.methods.getPermissions(address).call()
@ -1179,7 +1120,7 @@ export class Nft {
*/
public async getMetadata(nftAddress: string): Promise<Object> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
return await nftContract.methods.getMetaData().call()
@ -1192,7 +1133,7 @@ export class Nft {
*/
public async isErc20Deployer(nftAddress: string, address: string): Promise<boolean> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
const isERC20Deployer = await nftContract.methods.isERC20Deployer(address).call()
@ -1206,7 +1147,7 @@ export class Nft {
*/
public async getData(nftAddress: string, key: string): Promise<string> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
const data = await nftContract.methods.getData(key).call()
@ -1220,7 +1161,7 @@ export class Nft {
*/
public async getTokenURI(nftAddress: string, id: number): Promise<string> {
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
new this.web3.eth.Contract(this.abi, nftAddress),
this.config
)
const data = await nftContract.methods.tokenURI(id).call()