Skip to content

Commit

Permalink
Merge pull request #1881 from oceanprotocol/issue-1872-circular-deps
Browse files Browse the repository at this point in the history
Remove circular dependencies, fix imports and improve build
  • Loading branch information
paulo-ocean authored Dec 12, 2024
2 parents 1d4291c + 7a7e27b commit 0bc6f0b
Show file tree
Hide file tree
Showing 24 changed files with 141 additions and 135 deletions.
2 changes: 1 addition & 1 deletion src/config/ConfigHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { default as DefaultContractsAddresses } from '@oceanprotocol/contracts/addresses/address.json'
import fs from 'fs'
import { Config } from '.'
import { LoggerInstance } from '../utils'
import { LoggerInstance } from '../utils/Logger'

const configHelperNetworksBase: Config = {
chainId: null,
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/AccessList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Signer } from 'ethers'
import AccessList from '@oceanprotocol/contracts/artifacts/contracts/accesslists/AccessList.sol/AccessList.json'
import { sendTx } from '../utils'
import { sendTx } from '../utils/ContractUtils'
import { AbiItem, ReceiptOrEstimate } from '../@types'
import { Config } from '../config'
import { SmartContractWithAddress } from './SmartContractWithAddress'
Expand Down
4 changes: 3 additions & 1 deletion src/contracts/AccessListFactory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { BigNumber, Signer } from 'ethers'
import { Config } from '../config'
import AccessListFactory from '@oceanprotocol/contracts/artifacts/contracts/accesslists/AccessListFactory.sol/AccessListFactory.json'
import { generateDtName, sendTx, getEventFromTx, ZERO_ADDRESS } from '../utils'
import { sendTx, getEventFromTx } from '../utils/ContractUtils'
import { ZERO_ADDRESS } from '../utils/Constants'
import { AbiItem, ReceiptOrEstimate } from '../@types'
import { SmartContractWithAddress } from './SmartContractWithAddress'
import { generateDtName } from '../utils/DatatokenName'

/**
* Provides an interface for Access List Factory contract
Expand Down
3 changes: 2 additions & 1 deletion src/contracts/Datatoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { ethers, Signer } from 'ethers'
import Decimal from 'decimal.js'
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
import ERC20TemplateEnterprise from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20TemplateEnterprise.sol/ERC20TemplateEnterprise.json'
import { amountToUnits, sendTx, ZERO_ADDRESS } from '../utils'
import { amountToUnits, sendTx } from '../utils/ContractUtils'
import { ZERO_ADDRESS } from '../utils/Constants'
import {
AbiItem,
ConsumeMarketFee,
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/Datatoken4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ERC20Template4 from '@oceanprotocol/contracts/artifacts/contracts/templat
import { AbiItem, ReceiptOrEstimate } from '../@types'
import { AccessListContract } from './AccessList'
import { Config } from '../config'
import { sendTx } from '../utils'
import { sendTx } from '../utils/ContractUtils'

export class Datatoken4 extends Datatoken {
public accessList: AccessListContract
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/Dispenser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Decimal from 'decimal.js'
import DispenserAbi from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json'
import { sendTx } from '../utils'
import { Datatoken } from './Datatoken'
import { SmartContractWithAddress } from './SmartContractWithAddress'
import { DispenserToken, ReceiptOrEstimate, AbiItem } from '../@types'
import { sendTx } from '../utils/ContractUtils'

export class Dispenser extends SmartContractWithAddress {
getDefaultAbi() {
Expand Down
3 changes: 2 additions & 1 deletion src/contracts/FixedRateExchange.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import FixedRateExchangeAbi from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json'
import { sendTx, ZERO_ADDRESS } from '../utils'
import { sendTx } from '../utils/ContractUtils'
import {
PriceAndFees,
FeesInfo,
Expand All @@ -8,6 +8,7 @@ import {
AbiItem
} from '../@types'
import { SmartContractWithAddress } from './SmartContractWithAddress'
import { ZERO_ADDRESS } from '../utils/Constants'

export class FixedRateExchange extends SmartContractWithAddress {
getDefaultAbi() {
Expand Down
7 changes: 5 additions & 2 deletions src/contracts/NFT.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BigNumber, ethers } from 'ethers'
import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json'
import { generateDtName, sendTx, getEventFromTx, ZERO_ADDRESS } from '../utils'
import {
MetadataProof,
MetadataAndTokenURI,
Expand All @@ -9,10 +8,14 @@ import {
AbiItem
} from '../@types'
import { SmartContract } from './SmartContract'

import { generateDtName } from '../utils/DatatokenName'
import { ZERO_ADDRESS } from '../utils/Constants'
import { getEventFromTx, sendTx } from '../utils/ContractUtils'
import {
calculateActiveTemplateIndex,
getOceanArtifactsAdressesByChainId
} from '../utils/Assets'
} from '../utils/Adresses'

export class Nft extends SmartContract {
getDefaultAbi() {
Expand Down
12 changes: 4 additions & 8 deletions src/contracts/NFTFactory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { BigNumber, ethers } from 'ethers'
import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json'
import {
generateDtName,
ZERO_ADDRESS,
sendTx,
getEventFromTx,
getTokenDecimals,
LoggerInstance
} from '../utils'
import {
AbiItem,
FreCreationParams,
Expand All @@ -19,6 +11,10 @@ import {
ReceiptOrEstimate
} from '../@types'
import { SmartContractWithAddress } from './SmartContractWithAddress'
import { generateDtName } from '../utils/DatatokenName'
import { ZERO_ADDRESS } from '../utils/Constants'
import { getEventFromTx, getTokenDecimals, sendTx } from '../utils/ContractUtils'
import { LoggerInstance } from '../utils/Logger'

/**
* Provides an interface for NFT Factory contract
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/Router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import FactoryRouter from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json'
import { sendTx } from '../utils'
import { sendTx } from '../utils/ContractUtils'
import { Operation, ReceiptOrEstimate, AbiItem } from '../@types'
import { SmartContractWithAddress } from './SmartContractWithAddress'

Expand Down
4 changes: 2 additions & 2 deletions src/contracts/SmartContract.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ethers, Signer, Contract } from 'ethers'
import { AbiItem } from '../@types'
import { Config, ConfigHelper } from '../config'
import * as sapphire from '@oasisprotocol/sapphire-paratime'
import {
amountToUnits,
getFairGasPrice,
setContractDefaults,
unitsToAmount
} from '../utils'
import * as sapphire from '@oasisprotocol/sapphire-paratime'
} from '../utils/ContractUtils'

export abstract class SmartContract {
public signer: Signer
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/df/DfRewards.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dfRewardsABI from '@oceanprotocol/contracts/artifacts/contracts/df/DFRewards.sol/DFRewards.json'
import { sendTx } from '../../utils'
import { sendTx } from '../../utils/ContractUtils'
import { SmartContractWithAddress } from '../SmartContractWithAddress'
import { AbiItem, ReceiptOrEstimate } from '../../@types'

Expand Down
2 changes: 1 addition & 1 deletion src/contracts/df/DfStrategyV1.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dfStrategyV1ABI from '@oceanprotocol/contracts/artifacts/contracts/df/DFStrategyV1.sol/DFStrategyV1.json'
import { sendTx } from '../../utils'
import { sendTx } from '../../utils/ContractUtils'
import { SmartContractWithAddress } from '../SmartContractWithAddress'
import { ReceiptOrEstimate, AbiItem } from '../../@types'
/**
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/ve/VeAllocate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import veAllocateABI from '@oceanprotocol/contracts/artifacts/contracts/ve/veAllocate.sol/veAllocate.json'
import { sendTx } from '../../utils'
import { sendTx } from '../../utils/ContractUtils'
import { SmartContractWithAddress } from '../SmartContractWithAddress'
import { ReceiptOrEstimate, AbiItem } from '../../@types'
/**
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/ve/VeFeeDistributor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import veFeeABI from '@oceanprotocol/contracts/artifacts/contracts/ve/veFeeDistributor.vy/veFeeDistributor.json'
import { sendTx } from '../../utils'
import { sendTx } from '../../utils/ContractUtils'
import { SmartContractWithAddress } from '../SmartContractWithAddress'
import { ReceiptOrEstimate, AbiItem } from '../../@types'
/**
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/ve/VeOcean.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import veOceanABI from '@oceanprotocol/contracts/artifacts/contracts/ve/veOCEAN.vy/veOCEAN.json'
import { sendTx } from '../../utils'
import { sendTx } from '../../utils/ContractUtils'
import { SmartContractWithAddress } from '../SmartContractWithAddress'
import { ReceiptOrEstimate, AbiItem } from '../../@types'
/**
Expand Down
3 changes: 2 additions & 1 deletion src/services/Aquarius.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fetch from 'cross-fetch'
import { LoggerInstance, sleep } from '../utils'
import { LoggerInstance } from '../utils/Logger'
import { Asset, DDO, ValidateMetadata } from '../@types'
import { sleep } from '../utils/General'

export interface SearchQuery {
from?: number
Expand Down
2 changes: 1 addition & 1 deletion src/services/Provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fetch from 'cross-fetch'
import { ethers, Signer, providers } from 'ethers'
import { LoggerInstance } from '../utils'
import { LoggerInstance } from '../utils/Logger'
import {
Arweave,
FileInfo,
Expand Down
90 changes: 90 additions & 0 deletions src/utils/Adresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { ethers, Signer } from 'ethers'
import { NftFactory } from '../contracts/NFTFactory'
import fs from 'fs'
// eslint-disable-next-line import/no-named-default
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/interfaces/IERC20Template.sol/IERC20Template.json'
// eslint-disable-next-line import/no-named-default
import { default as Addresses } from '@oceanprotocol/contracts/addresses/address.json'
/**
* Get the artifacts address from the address.json file
* either from the env or from the ocean-contracts dir
* @returns data or null
*/
export function getOceanArtifactsAdresses(): any {
try {
if (process.env.ADDRESS_FILE) {
// eslint-disable-next-line security/detect-non-literal-fs-filename
const data = fs.readFileSync(process.env.ADDRESS_FILE, 'utf8')
return JSON.parse(data)
}
return Addresses
} catch (error) {
return Addresses
}
}

/**
* Get the artifacts address from the address.json file, for the given chain
* either from the env or from the ocean-contracts dir, safer than above, because sometimes the network name
* is mispeled, best example "optimism_sepolia" vs "optimism-sepolia"
* @returns data or null
*/
export function getOceanArtifactsAdressesByChainId(chain: number): any {
try {
// eslint-disable-next-line security/detect-non-literal-fs-filename
const data = getOceanArtifactsAdresses()
if (data) {
const networks = Object.keys(data)
for (const network of networks) {
if (data[network].chainId === chain) {
return data[network]
}
}
}
} catch (error) {
console.error(error)
}
return null
}
/**
* Use this function to accurately calculate the template index, and also checking if the template is active
* @param owner the signer account
* @param nftContractAddress the nft contract address, usually artifactsAddresses.ERC721Factory
* @param template the template ID or template address (from smart contract getId() function)
* @returns index of the template on the list
*/
export async function calculateActiveTemplateIndex(
owner: Signer,
nftContractAddress: string, // addresses.ERC721Factory,
template: string | number
): Promise<number> {
// is an ID number?
const isTemplateID = typeof template === 'number'

const factoryERC721 = new NftFactory(nftContractAddress, owner)
const currentTokenCount = await factoryERC721.getCurrentTokenTemplateCount()
for (let i = 1; i <= currentTokenCount; i++) {
const tokenTemplate = await factoryERC721.getTokenTemplate(i)

const erc20Template = new ethers.Contract(
tokenTemplate.templateAddress,
ERC20Template.abi,
owner
)

// check for ID
if (isTemplateID) {
const id = await erc20Template.connect(owner).getId()
if (tokenTemplate.isActive && id.toString() === template.toString()) {
return i
}
} else if (
tokenTemplate.isActive &&
tokenTemplate.templateAddress === template.toString()
) {
return i
}
}
// if nothing is found it returns -1
return -1
}
Loading

0 comments on commit 0bc6f0b

Please sign in to comment.