diff --git a/src/@types/Asset.ts b/src/@types/Asset.ts index b3ecc835..3d679280 100644 --- a/src/@types/Asset.ts +++ b/src/@types/Asset.ts @@ -1,4 +1,4 @@ -import { DDO } from './DDO/DDO' +import { DDO } from '.' export interface AssetNft { /** diff --git a/src/@types/Compute.ts b/src/@types/Compute.ts index 39b8edf1..81142c7c 100644 --- a/src/@types/Compute.ts +++ b/src/@types/Compute.ts @@ -1,4 +1,4 @@ -import { Metadata, MetadataAlgorithm } from './DDO/Metadata' +import { Metadata, MetadataAlgorithm } from '.' export type ComputeResultType = | 'algorithmLog' diff --git a/src/@types/DDO/DDO.ts b/src/@types/DDO/DDO.ts index 7c88def6..0a6d0e24 100644 --- a/src/@types/DDO/DDO.ts +++ b/src/@types/DDO/DDO.ts @@ -1,7 +1,4 @@ -import { Service } from './Service' -import { Metadata } from './Metadata' -import { Credentials } from './Credentials' -import { Event } from './Event' +import { Service, Metadata, Credentials, Event } from '..' /** * DID Descriptor Object. diff --git a/src/@types/Dispenser.ts b/src/@types/Dispenser.ts index 6919ef22..02749dfb 100644 --- a/src/@types/Dispenser.ts +++ b/src/@types/Dispenser.ts @@ -5,3 +5,13 @@ export interface DispenserCreationParams { withMint?: boolean // true if we want to allow the dispenser to be a minter allowedSwapper?: string // only account that can ask tokens. set address(0) if not required } + +export interface DispenserToken { + active: boolean + owner: string + maxTokens: string + maxBalance: string + balance: string + isMinter: boolean + allowedSwapper: string +} diff --git a/src/@types/Erc20.ts b/src/@types/Erc20.ts index 19643b47..96cdb2bb 100644 --- a/src/@types/Erc20.ts +++ b/src/@types/Erc20.ts @@ -1,3 +1,5 @@ +import { ProviderFees } from '.' + export interface Erc20CreateParams { templateIndex: number minter: string @@ -21,3 +23,22 @@ export interface PublishingMarketFee { publishMarketFeeToken: string publishMarketFeeAmount: string } + +export interface DatatokenRoles { + minter: boolean + paymentManager: boolean +} + +export interface OrderParams { + consumer: string + serviceIndex: number + _providerFee: ProviderFees + _consumeMarketFee: ConsumeMarketFee +} + +export interface DispenserParams { + maxTokens: string + maxBalance: string + withMint?: boolean // true if we want to allow the dispenser to be a minter + allowedSwapper?: string // only account that can ask tokens. set address(0) if not required +} diff --git a/src/@types/Erc721.ts b/src/@types/Erc721.ts index 5b2289e3..4ba47066 100644 --- a/src/@types/Erc721.ts +++ b/src/@types/Erc721.ts @@ -11,3 +11,10 @@ export interface MetadataAndTokenURI { tokenURI: string metadataProofs?: MetadataProof[] } + +export interface NftRoles { + manager: boolean + deployERC20: boolean + updateMetadata: boolean + store: boolean +} diff --git a/src/@types/FixedPrice.ts b/src/@types/FixedPrice.ts index 3fe6ea43..8527a7f0 100644 --- a/src/@types/FixedPrice.ts +++ b/src/@types/FixedPrice.ts @@ -25,3 +25,29 @@ export interface PriceAndFees { marketFeeAmount: string consumeMarketFeeAmount: string } + +export interface FixedPriceExchange { + active: boolean + exchangeOwner: string + datatoken: string + baseToken: string + fixedRate: string + dtDecimals: string + btDecimals: string + dtBalance: string + btBalance: string + dtSupply: string + btSupply: string + withMint: boolean + allowedSwapper: string + exchangeId?: string +} + +export interface FeesInfo { + opcFee: string + marketFee: string + marketFeeCollector: string + marketFeeAvailable: string + oceanFeeAvailable: string + exchangeId: string +} diff --git a/src/@types/NFTFactory.ts b/src/@types/NFTFactory.ts new file mode 100644 index 00000000..5e20ac9d --- /dev/null +++ b/src/@types/NFTFactory.ts @@ -0,0 +1,23 @@ +import { ConsumeMarketFee, ProviderFees } from '.' + +export interface Template { + templateAddress: string + isActive: boolean +} + +export interface TokenOrder { + tokenAddress: string + consumer: string + serviceIndex: number + _providerFee: ProviderFees + _consumeMarketFee: ConsumeMarketFee +} + +export interface NftCreateData { + name: string + symbol: string + templateIndex: number + tokenURI: string + transferable: boolean + owner: string +} diff --git a/src/@types/Provider.ts b/src/@types/Provider.ts index a8be432c..1b2e64c6 100644 --- a/src/@types/Provider.ts +++ b/src/@types/Provider.ts @@ -26,3 +26,12 @@ export interface ProviderComputeInitializeResults { algorithm?: ProviderComputeInitialize datasets?: ProviderComputeInitialize[] } + +export interface ServiceEndpoint { + serviceName: string + method: string + urlPath: string +} +export interface UserCustomParameters { + [key: string]: any +} diff --git a/src/@types/index.ts b/src/@types/index.ts index 7e03cb56..7ff08ef4 100644 --- a/src/@types/index.ts +++ b/src/@types/index.ts @@ -1,14 +1,17 @@ -export * from './DDO/DDO' -export * from './Asset' -export * from './DDO/Service' export * from './DDO/Credentials' +export * from './DDO/DDO' +export * from './DDO/Event' export * from './DDO/Metadata' -export * from './FileMetadata' +export * from './DDO/Service' +export * from './Asset' export * from './Compute' -export * from './Provider' -export * from './FixedPrice' -export * from './Pool' +export * from './Dispenser' +export * from './DownloadResponse' export * from './Erc20' export * from './Erc721' -export * from './Dispenser' +export * from './FileMetadata' +export * from './FixedPrice' +export * from './NFTFactory' +export * from './Pool' +export * from './Provider' export * from './Router' diff --git a/src/models/Config.ts b/src/config/Config.ts similarity index 98% rename from src/models/Config.ts rename to src/config/Config.ts index 7b6c12bb..d10aad7b 100644 --- a/src/models/Config.ts +++ b/src/config/Config.ts @@ -1,5 +1,5 @@ -import { LogLevel } from '../utils/Logger' import { AbiItem } from 'web3-utils/types' +import { LogLevel } from '../utils' export class Config { /** @@ -195,5 +195,3 @@ export class Config { */ gasFeeMultiplier: number } - -export default Config diff --git a/src/utils/ConfigHelper.ts b/src/config/ConfigHelper.ts similarity index 98% rename from src/utils/ConfigHelper.ts rename to src/config/ConfigHelper.ts index cc01d00c..dfb96477 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/config/ConfigHelper.ts @@ -1,7 +1,7 @@ -import Config from '../models/Config' // eslint-disable-next-line import/no-named-default import { default as DefaultContractsAddresses } from '@oceanprotocol/contracts/addresses/address.json' -import LoggerInstance from './Logger' +import { Config } from '.' +import { LoggerInstance } from '../utils' const configHelperNetworksBase: Config = { chainId: null, diff --git a/src/config/index.ts b/src/config/index.ts new file mode 100644 index 00000000..3ac42305 --- /dev/null +++ b/src/config/index.ts @@ -0,0 +1,2 @@ +export * from './Config' +export * from './ConfigHelper' diff --git a/src/contracts/SmartContract.ts b/src/contracts/SmartContract.ts new file mode 100644 index 00000000..fb3114a0 --- /dev/null +++ b/src/contracts/SmartContract.ts @@ -0,0 +1,46 @@ +import Web3 from 'web3' +import { AbiItem } from 'web3-utils' +import { Config, ConfigHelper } from '../config' +import { amountToUnits, unitsToAmount } from '../utils' + +export abstract class SmartContract { + public web3: Web3 + public config: Config + public abi: AbiItem | AbiItem[] + + abstract getDefaultAbi(): AbiItem | AbiItem[] + + /** + * Instantiate the smart contract. + * @param {Web3} web3 + * @param {string | number} network Network id or name + * @param {Config} config Configutation of the smart contract + * @param {AbiItem | AbiItem[]} abi ABI of the smart contract + */ + constructor( + web3: Web3, + network?: string | number, + config?: Config, + abi?: AbiItem | AbiItem[] + ) { + this.web3 = web3 + this.config = config || new ConfigHelper().getConfig(network || 'unknown') + this.abi = abi || (this.getDefaultAbi() as AbiItem[]) + } + + async amountToUnits( + token: string, + amount: string, + tokenDecimals?: number + ): Promise { + return amountToUnits(this.web3, token, amount, tokenDecimals) + } + + async unitsToAmount( + token: string, + amount: string, + tokenDecimals?: number + ): Promise { + return unitsToAmount(this.web3, token, amount, tokenDecimals) + } +} diff --git a/src/contracts/SmartContractWithAddress.ts b/src/contracts/SmartContractWithAddress.ts new file mode 100644 index 00000000..a1f56392 --- /dev/null +++ b/src/contracts/SmartContractWithAddress.ts @@ -0,0 +1,34 @@ +import Web3 from 'web3' +import { Contract } from 'web3-eth-contract' +import { AbiItem } from 'web3-utils' +import { Config } from '../config' +import { setContractDefaults } from '../utils' +import { SmartContract } from '.' + +export abstract class SmartContractWithAddress extends SmartContract { + public address: string + public contract: Contract + + /** + * Instantiate the smart contract. + * @param {string} address Address of the smart contract + * @param {Web3} web3 + * @param {string | number} network Network id or name + * @param {Config} config Configutation of the smart contract + * @param {AbiItem | AbiItem[]} abi ABI of the smart contract + */ + constructor( + address: string, + web3: Web3, + network?: string | number, + config?: Config, + abi?: AbiItem | AbiItem[] + ) { + super(web3, network, config, abi) + this.address = address + this.contract = setContractDefaults( + new this.web3.eth.Contract(this.getDefaultAbi(), this.address), + this.config + ) + } +} diff --git a/src/factories/NFTFactory.ts b/src/contracts/factories/NFTFactory.ts similarity index 82% rename from src/factories/NFTFactory.ts rename to src/contracts/factories/NFTFactory.ts index 9b697282..4a69f7a1 100644 --- a/src/factories/NFTFactory.ts +++ b/src/contracts/factories/NFTFactory.ts @@ -1,8 +1,7 @@ -import { Contract } from 'web3-eth-contract' import Web3 from 'web3' import { TransactionReceipt } from 'web3-core' import { AbiItem } from 'web3-utils' -import defaultFactory721Abi from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json' +import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json' import { LoggerInstance, getFairGasPrice, @@ -10,74 +9,26 @@ import { getFreCreationParams, getErcCreationParams, getPoolCreationParams, - setContractDefaults, estimateGas, - ZERO_ADDRESS, - ConfigHelper -} from '../utils' -import { Config } from '../models/index.js' + ZERO_ADDRESS +} from '../../utils' import { - ProviderFees, FreCreationParams, Erc20CreateParams, PoolCreationParams, DispenserCreationParams, - ConsumeMarketFee -} from '../@types/index.js' - -interface Template { - templateAddress: string - isActive: boolean -} - -export interface TokenOrder { - tokenAddress: string - consumer: string - serviceIndex: number - _providerFee: ProviderFees - _consumeMarketFee: ConsumeMarketFee -} - -export interface NftCreateData { - name: string - symbol: string - templateIndex: number - tokenURI: string - transferable: boolean - owner: string -} + NftCreateData, + Template, + TokenOrder +} from '../../@types' +import { SmartContractWithAddress } from '..' /** * Provides an interface for NFT Factory contract */ -export class NftFactory { - public factory721Address: string - public factory721Abi: AbiItem | AbiItem[] - public web3: Web3 - public config: Config - public factory721: Contract - - /** - * Instantiate Datatokens. - * @param {String} factory721Address - * @param {AbiItem | AbiItem[]} factory721ABI - * @param {Web3} web3 - */ - constructor( - factory721Address: string, - web3: Web3, - network?: string | number, - factory721Abi?: AbiItem | AbiItem[], - config?: Config - ) { - this.factory721Address = factory721Address - this.factory721Abi = factory721Abi || (defaultFactory721Abi.abi as AbiItem[]) - this.web3 = web3 - this.config = config || new ConfigHelper().getConfig(network || 'unknown') - this.factory721 = setContractDefaults( - new this.web3.eth.Contract(this.factory721Abi, this.factory721Address), - this.config - ) +export class NftFactory extends SmartContractWithAddress { + getDefaultAbi(): AbiItem | AbiItem[] { + return ERC721Factory.abi as AbiItem[] } /** @@ -89,7 +40,7 @@ export class NftFactory { public async estGasCreateNFT(address: string, nftData: NftCreateData): Promise { return estimateGas( address, - this.factory721.methods.deployERC721Contract, + this.contract.methods.deployERC721Contract, nftData.name, nftData.symbol, nftData.templateIndex, @@ -127,7 +78,7 @@ export class NftFactory { } const estGas = await estimateGas( address, - this.factory721.methods.deployERC721Contract, + this.contract.methods.deployERC721Contract, nftData.name, nftData.symbol, nftData.templateIndex, @@ -139,7 +90,7 @@ export class NftFactory { ) // Invoke createToken function of the contract - const trxReceipt = await this.factory721.methods + const trxReceipt = await this.contract.methods .deployERC721Contract( nftData.name, nftData.symbol, @@ -169,7 +120,7 @@ export class NftFactory { * @return {Promise} Number of NFT created from this factory */ public async getCurrentNFTCount(): Promise { - const trxReceipt = await this.factory721.methods.getCurrentNFTCount().call() + const trxReceipt = await this.contract.methods.getCurrentNFTCount().call() return trxReceipt } @@ -177,7 +128,7 @@ export class NftFactory { * @return {Promise} Number of DTs created from this factory */ public async getCurrentTokenCount(): Promise { - const trxReceipt = await this.factory721.methods.getCurrentTokenCount().call() + const trxReceipt = await this.contract.methods.getCurrentTokenCount().call() return trxReceipt } @@ -185,7 +136,7 @@ export class NftFactory { * @return {Promise} Factory Owner address */ public async getOwner(): Promise { - const trxReceipt = await this.factory721.methods.owner().call() + const trxReceipt = await this.contract.methods.owner().call() return trxReceipt } @@ -193,7 +144,7 @@ export class NftFactory { * @return {Promise} Number of NFT Template added to this factory */ public async getCurrentNFTTemplateCount(): Promise { - const count = await this.factory721.methods.getCurrentNFTTemplateCount().call() + const count = await this.contract.methods.getCurrentNFTTemplateCount().call() return count } @@ -201,7 +152,7 @@ export class NftFactory { * @return {Promise} Number of ERC20 Template added to this factory */ public async getCurrentTokenTemplateCount(): Promise { - const count = await this.factory721.methods.getCurrentTemplateCount().call() + const count = await this.contract.methods.getCurrentTemplateCount().call() return count } @@ -217,7 +168,7 @@ export class NftFactory { if (index === 0) { throw new Error(`Template index cannot be ZERO`) } - const template = await this.factory721.methods.getNFTTemplate(index).call() + const template = await this.contract.methods.getNFTTemplate(index).call() return template } @@ -226,7 +177,7 @@ export class NftFactory { * @return {Promise