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

updated contract fees strategy wip unit tests

This commit is contained in:
Bogdan Fazakas 2021-12-06 19:04:17 +02:00
parent 9d2c0d86ed
commit 96ec95cfa2
5 changed files with 148 additions and 130 deletions

View File

@ -20,9 +20,6 @@ export interface OrderParams {
consumer: string consumer: string
amount: string amount: string
serviceIndex: number serviceIndex: number
consumeFeeAddress: string
consumeFeeToken: string
consumeFeeAmount: string
} }
export interface DispenserParams { export interface DispenserParams {
@ -831,9 +828,6 @@ export class Datatoken {
consumer: string, consumer: string,
amount: string, amount: string,
serviceIndex: number, serviceIndex: number,
mpFeeAddress: string,
feeToken: string,
feeAmount: string,
contractInstance?: Contract contractInstance?: Contract
): Promise<any> { ): Promise<any> {
const dtContract = const dtContract =
@ -844,14 +838,7 @@ export class Datatoken {
let estGas let estGas
try { try {
estGas = await dtContract.methods estGas = await dtContract.methods
.startOrder( .startOrder(consumer, this.web3.utils.toWei(amount), serviceIndex)
consumer,
this.web3.utils.toWei(amount),
serviceIndex,
mpFeeAddress,
feeToken,
this.web3.utils.toWei(feeAmount)
)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) { } catch (e) {
estGas = gasLimitDefault estGas = gasLimitDefault
@ -875,13 +862,9 @@ export class Datatoken {
address: string, address: string,
consumer: string, consumer: string,
amount: string, amount: string,
serviceIndex: number, serviceIndex: number
mpFeeAddress: string,
feeToken: string,
feeAmount: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const dtContract = new this.web3.eth.Contract(this.datatokensABI, dtAddress) const dtContract = new this.web3.eth.Contract(this.datatokensABI, dtAddress)
if (!mpFeeAddress) mpFeeAddress = '0x0000000000000000000000000000000000000000'
try { try {
const estGas = await this.estGasStartOrder( const estGas = await this.estGasStartOrder(
dtAddress, dtAddress,
@ -889,21 +872,11 @@ export class Datatoken {
consumer, consumer,
amount, amount,
serviceIndex, serviceIndex,
mpFeeAddress,
feeToken,
feeAmount,
dtContract dtContract
) )
const trxReceipt = await dtContract.methods const trxReceipt = await dtContract.methods
.startOrder( .startOrder(consumer, this.web3.utils.toWei(amount), serviceIndex)
consumer,
this.web3.utils.toWei(amount),
serviceIndex,
mpFeeAddress,
feeToken,
this.web3.utils.toWei(feeAmount)
)
.send({ .send({
from: address, from: address,
gas: estGas + 1, gas: estGas + 1,

View File

@ -23,9 +23,6 @@ export interface TokenOrder {
consumer: string consumer: string
amount: string | number amount: string | number
serviceIndex: number serviceIndex: number
consumeFeeAddress: string
consumeFeeToken: string // address of the token marketplace wants to add fee on top
consumeFeeAmount: number
} }
export interface NFTCreateData { export interface NFTCreateData {

View File

@ -15,4 +15,6 @@ export interface FreOrderParams {
exchangeContract: string exchangeContract: string
exchangeId: string exchangeId: string
maxBaseTokenAmount: string maxBaseTokenAmount: string
swapMarketFee: number
marketFeeAddress: string
} }

View File

@ -18,3 +18,23 @@ export interface CurrentFees {
tokens: string[] tokens: string[]
amounts: string[] amounts: string[]
} }
export interface TokenInOutMarket {
tokenIn: string
tokenOut: string
marketFeeAddress: string
}
export interface AmountsInMaxFee {
tokenAmountIn: string
minAmountOut: string
maxPrice: string
swapMarketFee: string
}
export interface AmountsOutMaxFee {
tokenAmountOut: string
maxAmountIn: string
maxPrice: string
swapMarketFee: string
}

View File

@ -8,7 +8,13 @@ import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/bal
import defaultPool from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' import defaultPool from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json'
import defaultERC20ABI from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' import defaultERC20ABI from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
import Decimal from 'decimal.js' import Decimal from 'decimal.js'
import { CurrentFees } from '../../interfaces' import {
CurrentFees,
TokenInOutMarket,
AmountsInOutMaxFee,
AmountsInMaxFee,
AmountsOutMaxFee
} from '../../interfaces'
const BN = require('bn.js') const BN = require('bn.js')
const MaxUint256 = const MaxUint256 =
@ -639,7 +645,7 @@ export class Pool {
} }
/** /**
* collectOPF - collect market fees - can be called by the marketFeeCollector * collectOPF - collect market fees - can be called by the publishMarketCollector
* @param {String} address * @param {String} address
* @param {String} poolAddress * @param {String} poolAddress
* @param {String} to address that will receive fees * @param {String} to address that will receive fees
@ -733,50 +739,6 @@ export class Pool {
return result return result
} }
/**
* Estimate gas cost for swapExactAmountIn
* @param {String} address
* @param {String} poolAddress
* @param {String} tokenIn
* @param {String} tokenAmountIn will be converted to wei
* @param {String} tokenOut
* @param {String} minAmountOut will be converted to wei
* @param {String} maxPrice will be converted to wei
* @param {Contract} contractInstance optional contract instance
* @return {Promise<number>}
*/
public async estSwapExactAmountIn(
address: string,
poolAddress: string,
tokenIn: string,
tokenAmountIn: string,
tokenOut: string,
minAmountOut: string,
maxPrice: string,
contractInstance?: Contract
): Promise<number> {
const poolContract =
contractInstance ||
new this.web3.eth.Contract(this.poolABI as AbiItem[], poolAddress)
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await poolContract.methods
.swapExactAmountIn(
tokenIn,
tokenAmountIn,
tokenOut,
minAmountOut,
maxPrice || MaxUint256
)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
}
async amountToUnits(token: string, amount: string): Promise<string> { async amountToUnits(token: string, amount: string): Promise<string> {
try { try {
const tokenContract = new this.web3.eth.Contract( const tokenContract = new this.web3.eth.Contract(
@ -812,6 +774,53 @@ export class Pool {
} }
} }
/**
* Estimate gas cost for swapExactAmountIn
* @param {String} address
* @param {String} poolAddress
* @param {String} tokenIn
* @param {String} tokenAmountIn will be converted to wei
* @param {String} tokenOut
* @param {String} minAmountOut will be converted to wei
* @param {String} maxPrice will be converted to wei
* @param {Contract} contractInstance optional contract instance
* @return {Promise<number>}
*/
public async estSwapExactAmountIn(
address: string,
poolAddress: string,
tokenInOutMarket: TokenInOutMarket,
amountsInOutMaxFee: AmountsInMaxFee,
contractInstance?: Contract
): Promise<number> {
const poolContract =
contractInstance ||
new this.web3.eth.Contract(this.poolABI as AbiItem[], poolAddress)
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await poolContract.methods
.swapExactAmountIn(
[
tokenInOutMarket.tokenIn,
tokenInOutMarket.tokenOut,
tokenInOutMarket.marketFeeAddress
],
[
amountsInOutMaxFee.tokenAmountIn,
amountsInOutMaxFee.minAmountOut,
this.web3.utils.toWei(amountsInOutMaxFee.maxPrice),
amountsInOutMaxFee.swapMarketFee
]
)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
}
/** /**
* swapExactAmountIn - Trades an exact tokenAmountIn of tokenIn taken from the caller by the pool, in exchange for at least minAmountOut of tokenOut given to the caller from the pool, with a maximum marginal price of maxPrice. Returns (tokenAmountOut, spotPriceAfter), where tokenAmountOut is the amount of token that came out of the pool, and spotPriceAfter is the new marginal spot price, ie, the result of getSpotPrice after the call. (These values are what are limited by the arguments; you are guaranteed tokenAmountOut >= minAmountOut and spotPriceAfter <= maxPrice). * swapExactAmountIn - Trades an exact tokenAmountIn of tokenIn taken from the caller by the pool, in exchange for at least minAmountOut of tokenOut given to the caller from the pool, with a maximum marginal price of maxPrice. Returns (tokenAmountOut, spotPriceAfter), where tokenAmountOut is the amount of token that came out of the pool, and spotPriceAfter is the new marginal spot price, ie, the result of getSpotPrice after the call. (These values are what are limited by the arguments; you are guaranteed tokenAmountOut >= minAmountOut and spotPriceAfter <= maxPrice).
* @param {String} address * @param {String} address
@ -826,39 +835,44 @@ export class Pool {
async swapExactAmountIn( async swapExactAmountIn(
address: string, address: string,
poolAddress: string, poolAddress: string,
tokenIn: string, tokenInOutMarket: TokenInOutMarket,
tokenAmountIn: string, amountsInOutMaxFee: AmountsInOutMaxFee
tokenOut: string,
minAmountOut: string,
maxPrice?: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
const amountInFormatted = await this.amountToUnits(tokenIn, tokenAmountIn) amountsInOutMaxFee.tokenAmountIn = await this.amountToUnits(
tokenInOutMarket.tokenIn,
amountsInOutMaxFee.tokenAmountIn
)
const minAmountOutFormatted = await this.amountToUnits(tokenOut, minAmountOut) amountsInOutMaxFee.minAmountOut = await this.amountToUnits(
tokenInOutMarket.tokenOut,
amountsInOutMaxFee.minAmountOut
)
let result = null let result = null
const estGas = await this.estSwapExactAmountIn( const estGas = await this.estSwapExactAmountIn(
address, address,
poolAddress, poolAddress,
tokenIn, tokenInOutMarket,
amountInFormatted, amountsInOutMaxFee
tokenOut,
minAmountOutFormatted.toString(),
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
) )
// console.log(minAmountOutFormatted, 'minamoutnoutformatted')
try { try {
result = await pool.methods result = await pool.methods
.swapExactAmountIn( .swapExactAmountIn(
tokenIn, [
amountInFormatted, tokenInOutMarket.tokenIn,
tokenOut, tokenInOutMarket.tokenOut,
minAmountOutFormatted, tokenInOutMarket.marketFeeAddress
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 ],
[
amountsInOutMaxFee.tokenAmountIn,
amountsInOutMaxFee.minAmountOut,
this.web3.utils.toWei(amountsInOutMaxFee.maxPrice),
amountsInOutMaxFee.swapMarketFee
]
) )
.send({ .send({
from: address, from: address,
@ -887,11 +901,8 @@ export class Pool {
public async estSwapExactAmountOut( public async estSwapExactAmountOut(
address: string, address: string,
poolAddress: string, poolAddress: string,
tokenIn: string, tokenInOutMarket: TokenInOutMarket,
maxAmountIn: string, amountsInOutMaxFee: AmountsOutMaxFee,
tokenOut: string,
amountOut: string,
maxPrice?: string,
contractInstance?: Contract contractInstance?: Contract
): Promise<number> { ): Promise<number> {
const poolContract = const poolContract =
@ -903,11 +914,17 @@ export class Pool {
try { try {
estGas = await poolContract.methods estGas = await poolContract.methods
.swapExactAmountOut( .swapExactAmountOut(
tokenIn, [
maxAmountIn, tokenInOutMarket.tokenIn,
tokenOut, tokenInOutMarket.tokenOut,
amountOut, tokenInOutMarket.marketFeeAddress
maxPrice || MaxUint256 ],
[
amountsInOutMaxFee.maxAmountIn,
amountsInOutMaxFee.tokenAmountOut,
this.web3.utils.toWei(amountsInOutMaxFee.maxPrice),
amountsInOutMaxFee.swapMarketFee
]
) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) { } catch (e) {
@ -930,35 +947,41 @@ export class Pool {
async swapExactAmountOut( async swapExactAmountOut(
account: string, account: string,
poolAddress: string, poolAddress: string,
tokenIn: string, tokenInOutMarket: TokenInOutMarket,
maxAmountIn: string, amountsInOutMaxFee: AmountsOutMaxFee
tokenOut: string,
amountOut: string,
maxPrice?: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
let result = null let result = null
const maxAmountInFormatted = await this.amountToUnits(tokenIn, maxAmountIn) amountsInOutMaxFee.maxAmountIn = await this.amountToUnits(
const amountOutFormatted = await this.amountToUnits(tokenOut, amountOut) tokenInOutMarket.tokenIn,
amountsInOutMaxFee.maxAmountIn
)
amountsInOutMaxFee.tokenAmountOut = await this.amountToUnits(
tokenInOutMarket.tokenOut,
amountsInOutMaxFee.tokenAmountOut
)
const estGas = await this.estSwapExactAmountOut( const estGas = await this.estSwapExactAmountOut(
account, account,
poolAddress, poolAddress,
tokenIn, tokenInOutMarket,
maxAmountInFormatted, amountsInOutMaxFee
tokenOut,
amountOutFormatted,
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
) )
try { try {
result = await pool.methods result = await pool.methods
.swapExactAmountOut( .swapExactAmountOut(
tokenIn, [
maxAmountInFormatted, tokenInOutMarket.tokenIn,
tokenOut, tokenInOutMarket.tokenOut,
amountOutFormatted, tokenInOutMarket.marketFeeAddress
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 ],
[
amountsInOutMaxFee.maxAmountIn,
amountsInOutMaxFee.tokenAmountOut,
this.web3.utils.toWei(amountsInOutMaxFee.maxPrice),
amountsInOutMaxFee.swapMarketFee
]
) )
.send({ .send({
from: account, from: account,
@ -1452,7 +1475,8 @@ export class Pool {
async getSpotPrice( async getSpotPrice(
poolAddress: string, poolAddress: string,
tokenIn: string, tokenIn: string,
tokenOut: string tokenOut: string,
swapMarketFe: number
): Promise<string> { ): Promise<string> {
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
let decimalsTokenIn = 18 let decimalsTokenIn = 18
@ -1479,7 +1503,7 @@ export class Pool {
let price = null let price = null
try { try {
price = await pool.methods.getSpotPrice(tokenIn, tokenOut).call() price = await pool.methods.getSpotPrice(tokenIn, tokenOut, swapMarketFe).call()
price = new BigNumber(price.toString()) price = new BigNumber(price.toString())
} catch (e) { } catch (e) {
this.logger.error('ERROR: Failed to get spot price of swapping tokenIn to tokenOut') this.logger.error('ERROR: Failed to get spot price of swapping tokenIn to tokenOut')
@ -1506,7 +1530,8 @@ export class Pool {
poolAddress: string, poolAddress: string,
tokenIn: string, tokenIn: string,
tokenOut: string, tokenOut: string,
tokenAmountOut: string tokenAmountOut: string,
swapMarketFee: string
): Promise<string> { ): Promise<string> {
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
@ -1516,7 +1541,7 @@ export class Pool {
try { try {
const result = await pool.methods const result = await pool.methods
.getAmountInExactOut(tokenIn, tokenOut, amountOutFormatted) .getAmountInExactOut(tokenIn, tokenOut, amountOutFormatted, swapMarketFee)
.call() .call()
amount = await this.unitsToAmount(tokenIn, result) amount = await this.unitsToAmount(tokenIn, result)
} catch (e) { } catch (e) {
@ -1529,7 +1554,8 @@ export class Pool {
poolAddress: string, poolAddress: string,
tokenIn: string, tokenIn: string,
tokenOut: string, tokenOut: string,
tokenAmountIn: string tokenAmountIn: string,
swapMarketFee: string
): Promise<string> { ): Promise<string> {
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
@ -1539,7 +1565,7 @@ export class Pool {
try { try {
const result = await pool.methods const result = await pool.methods
.getAmountOutExactIn(tokenIn, tokenOut, amountInFormatted) .getAmountOutExactIn(tokenIn, tokenOut, amountInFormatted, swapMarketFee)
.call() .call()
amount = await this.unitsToAmount(tokenOut, result) amount = await this.unitsToAmount(tokenOut, result)