1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00
ocean.js/src/contracts/Router.ts
Alex Coseru 9bf71ba4f0
Features/ethers (#1696)
* add ethers

* global updates

* ve updates

* ve tests

* contract updates

* first test passing

* downgrade to ethers 5.7.2

* add log

* add tx wait

* full NftFactory.test

* add wait

* add Router tests

* fix dispenser

* Nft test

* add dispenser tests

* add fre tests part 1

* WIP datatoken unit tests

* add DFRewards test

* increase gas estimate

* increase gas estimate

* Work datatoken unit tests

* datatoken test more tests

* finished datatoken tests

* fix nft get data

* fix nft transfer tests

* Provider int tests

* Updating CodeExamples.md

* update provider & fix publish flow int test

* wip publish edit consume integration test

* more work on  publish edit consume integration test

* fix edit  publish edit consume integration test

* add 3 int tests

* Updating ComputeExamples.md

* fix signature and download

* fix compute flow integration test

* udapte handleComputeOrder helper

* update datatoken instance

* update datatoken global variable

* mint ocean tokens to consumer as well and added logs

* update compute exammples

* Updating ComputeExamples.md

* wip code examples readme

* update code examples readme

* Updating CodeExamples.md

* run all tests

* update dep in readme

* update readme

* code examples update metadata flags

* update dt instance in code examples md

* set metadata updates

* Updating CodeExamples.md

* update code examples readme structure

* Updating CodeExamples.md

* update readmes table links

* Updating CodeExamples.md

* Updating ComputeExamples.md

* clean-up

* Updating CodeExamples.md

* added missing unit tests for usdc fixed rate exchange tests

* more cleanup and jsdoc updates

* more  jsdoc updates

* donw with jsdoc updates

* handle provider errors

* add missing error handling file

* adds most of the provider errors

* update get compute env return type

* Release 3.0.0-next.0

* adding Typedoc to ethers branch

* update provider signature message

* fix lint

* Release 3.0.0-next.1

* fix lint

* adding Typedoc to ethers branch

* Updating CI to build and commit the documentation

* Updating documentation

* Updating script permissions

* fix todos add missing logic to send tx

* npm package cleanups

* Release 3.0.0-next.2

* update log messages for errors

* Release 3.0.0-next.3

* fix gasFee issue on sendTx

* Release 3.0.0-next.4

* add consume params typings (#1731)

* fix gas fee estimate

* remove comments

* add some delays before resolving datasets

* adds delay to Publish flow tests

* Release 3.0.0-next.5

---------

Co-authored-by: Bogdan Fazakas <bogdan.fazakas@gmail.com>
Co-authored-by: GitHub Actions Bot <>
Co-authored-by: Jamie Hewitt <jamie.hewitt15@gmail.com>
Co-authored-by: Jamie Hewitt <jamie@oceanprotocol.com>
2023-05-29 11:20:38 +03:00

310 lines
9.5 KiB
TypeScript

import FactoryRouter from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json'
import { sendTx } from '../utils'
import { Operation, ReceiptOrEstimate, AbiItem } from '../@types'
import { SmartContractWithAddress } from './SmartContractWithAddress'
/**
* Provides an interface for FactoryRouter contract
*/
export class Router extends SmartContractWithAddress {
getDefaultAbi() {
return FactoryRouter.abi as AbiItem[]
}
/**
* * Buys a batch of datatokens.
* one single call to buy multiple DT for multiple assets.
* require tokenIn approvals for router from user. (except for dispenser operations)
* @param {Operation[]} operations - The operations to execute.
* @param {boolean} [estimateGas=false] - Whether to return only the estimate gas or not.
* @return {Promise<ReceiptOrEstimate>} Transaction receipt
*/
public async buyDatatokenBatch<G extends boolean = false>(
operations: Operation[],
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
const estGas = await this.contract.estimateGas.buyDTBatch(operations)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
const trxReceipt = await sendTx(
estGas,
this.signer,
this.config?.gasFeeMultiplier,
this.contract.buyDTBatch,
operations
)
return <ReceiptOrEstimate<G>>trxReceipt
}
/**
* Checks if a token is on approved tokens list,
* if true opfFee is lower in pools with that token/DT
* @param {string} address - The address of the token to check.
* @return {Promise<boolean>} true if is on the list.
*/
public async isApprovedToken(address: string): Promise<boolean> {
return await this.contract.isApprovedToken(address)
}
/**
* Check if an address is a Fixed Rate contract.
* @param {string} address - The address of the fixed rate exchange to check.
* @return {Promise<boolean>} true if is a Fixed Rate contract
*/
public async isFixedPrice(address: string): Promise<boolean> {
return await this.contract.isFixedRateContract(address)
}
/**
* Get Router Owner
* @return {Promise<string>} Router Owner address
*/
public async getOwner(): Promise<string> {
return await this.contract.routerOwner()
}
/**
* Get NFT Factory address
* @return {Promise<string>} NFT Factory address
*/
public async getNFTFactory(): Promise<string> {
return await this.contract.factory()
}
/**
* Adds a token to the list of tokens with reduced fees
* @param {String} address caller address
* @param {String} tokenAddress token address to add
* @param {Boolean} [estimateGas] if True, return gas estimate
* @return {Promise<ReceiptOrEstimate>}
*/
public async addApprovedToken<G extends boolean = false>(
address: string,
tokenAddress: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
if ((await this.getOwner()) !== address) {
throw new Error(`Caller is not Router Owner`)
}
const estGas = await this.contract.estimateGas.addApprovedToken(tokenAddress)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
const trxReceipt = await sendTx(
estGas,
this.signer,
this.config?.gasFeeMultiplier,
this.contract.addApprovedToken,
tokenAddress
)
return <ReceiptOrEstimate<G>>trxReceipt
}
/**
* Removes a token if exists from the list of tokens with reduced fees
* @param {String} address caller address
* @param {String} tokenAddress token address to remove
* @param {Boolean} [estimateGas] if True, return gas estimate
* @return {Promise<ReceiptOrEstimate>}
*/
public async removeApprovedToken<G extends boolean = false>(
address: string,
tokenAddress: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
if ((await this.getOwner()) !== address) {
throw new Error(`Caller is not Router Owner`)
}
const estGas = await this.contract.estimateGas.removeApprovedToken(tokenAddress)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
const trxReceipt = await sendTx(
estGas,
this.signer,
this.config?.gasFeeMultiplier,
this.contract.removeApprovedToken,
tokenAddress
)
return <ReceiptOrEstimate<G>>trxReceipt
}
/**
* Adds an address to the list of fixed rate contracts
* @param {String} address caller address
* @param {String} tokenAddress contract address to add
* @param {Boolean} [estimateGas] if True, return gas estimate
* @return {Promise<ReceiptOrEstimate>}
*/
public async addFixedRateContract<G extends boolean = false>(
address: string,
tokenAddress: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
if ((await this.getOwner()) !== address) {
throw new Error(`Caller is not Router Owner`)
}
const estGas = await this.contract.estimateGas.addFixedRateContract(tokenAddress)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
const trxReceipt = await sendTx(
estGas,
this.signer,
this.config?.gasFeeMultiplier,
this.contract.addFixedRateContract,
tokenAddress
)
return <ReceiptOrEstimate<G>>trxReceipt
}
/**
* Removes an address from the list of fixed rate contracts
* @param {String} address caller address
* @param {String} tokenAddress contract address to add
* @param {Boolean} [estimateGas] if True, return gas estimate
* @return {Promise<ReceiptOrEstimate>}
*/
public async removeFixedRateContract<G extends boolean = false>(
address: string,
tokenAddress: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
if ((await this.getOwner()) !== address) {
throw new Error(`Caller is not Router Owner`)
}
const estGas = await this.contract.estimateGas.removeFixedRateContract(tokenAddress)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
const trxReceipt = await sendTx(
estGas,
this.signer,
this.config?.gasFeeMultiplier,
this.contract.removeFixedRateContract,
tokenAddress
)
return <ReceiptOrEstimate<G>>trxReceipt
}
/**
* Adds an address to the list of dispensers
* @param {String} address caller address
* @param {String} tokenAddress contract address to add
* @param {Boolean} estimateGas if True, return gas estimate
* @return {Promise<ReceiptOrEstimate>}
*/
public async addDispenserContract<G extends boolean = false>(
address: string,
tokenAddress: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
if ((await this.getOwner()) !== address) {
throw new Error(`Caller is not Router Owner`)
}
const estGas = await this.contract.estimateGas.addDispenserContract(tokenAddress)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
const trxReceipt = await sendTx(
estGas,
this.signer,
this.config?.gasFeeMultiplier,
this.contract.addDispenserContract,
tokenAddress
)
return <ReceiptOrEstimate<G>>trxReceipt
}
/**
* Removes an address from the list of dispensers
* @param {String} address caller address
* @param {String} tokenAddress address Contract to be removed
* @param {Boolean} estimateGas if True, return gas estimate
* @return {Promise<ReceiptOrEstimate>}
*/
public async removeDispenserContract<G extends boolean = false>(
address: string,
tokenAddress: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
if ((await this.getOwner()) !== address) {
throw new Error(`Caller is not Router Owner`)
}
const estGas = await this.contract.estimateGas.removeDispenserContract(tokenAddress)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
const trxReceipt = await sendTx(
estGas,
this.signer,
this.config?.gasFeeMultiplier,
this.contract.removeDispenserContract,
tokenAddress
)
return <ReceiptOrEstimate<G>>trxReceipt
}
/** Get OPF Fee per token
* @return {Promise<number>} OPC fee for a specific baseToken
*/
public async getOPCFee(baseToken: string): Promise<number> {
return await this.contract.getOPCFee(baseToken)
}
/** Get Current OPF Fee
* @return {Promise<number>} OPF fee
*/
public async getCurrentOPCFee(): Promise<number> {
return await this.contract.swapOceanFee()
}
/**
* Updates OP Community Fees
* @param {String} address caller address
* @param {number} newSwapOceanFee Amount charged for swapping with ocean approved tokens
* @param {number} newSwapNonOceanFee Amount charged for swapping with non ocean approved tokens
* @param {number} newConsumeFee Amount charged from consumeFees
* @param {number} newProviderFee Amount charged for providerFees
* @param {Boolean} estimateGas if True, return gas estimate
* @return {Promise<ReceiptOrEstimate>}
*/
public async updateOPCFee<G extends boolean = false>(
address: string,
newSwapOceanFee: number,
newSwapNonOceanFee: number,
newConsumeFee: number,
newProviderFee: number,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
if ((await this.getOwner()) !== address) {
throw new Error(`Caller is not Router Owner`)
}
const estGas = await this.contract.estimateGas.updateOPCFee(
newSwapOceanFee,
newSwapNonOceanFee,
newConsumeFee,
newProviderFee
)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
const trxReceipt = await sendTx(
estGas,
this.signer,
this.config?.gasFeeMultiplier,
this.contract.updateOPCFee,
newSwapOceanFee,
newSwapNonOceanFee,
newConsumeFee,
newProviderFee
)
return <ReceiptOrEstimate<G>>trxReceipt
}
}