mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
add more functions and tests
This commit is contained in:
parent
21a60ebf23
commit
8f43ae058f
@ -4,11 +4,11 @@ import { TransactionReceipt } from 'web3-core'
|
|||||||
import { Contract } from 'web3-eth-contract'
|
import { Contract } from 'web3-eth-contract'
|
||||||
import { Logger, getFairGasPrice } from '../../utils'
|
import { Logger, getFairGasPrice } from '../../utils'
|
||||||
import BigNumber from 'bignumber.js'
|
import BigNumber from 'bignumber.js'
|
||||||
const BN = require('bn.js');
|
|
||||||
import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json'
|
import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json'
|
||||||
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'
|
||||||
|
const BN = require('bn.js')
|
||||||
|
|
||||||
const MaxUint256 =
|
const MaxUint256 =
|
||||||
'115792089237316195423570985008687907853269984665640564039457584007913129639934'
|
'115792089237316195423570985008687907853269984665640564039457584007913129639934'
|
||||||
@ -132,10 +132,9 @@ export class Pool {
|
|||||||
const amountFormatted = await this.amountToUnits(tokenAddress, amount)
|
const amountFormatted = await this.amountToUnits(tokenAddress, amount)
|
||||||
const estGas = await this.estApprove(account, tokenAddress, spender, amountFormatted)
|
const estGas = await this.estApprove(account, tokenAddress, spender, amountFormatted)
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = await token.methods
|
result = await token.methods
|
||||||
.approve(spender,new BigNumber(await this.amountToUnits(tokenAddress, amount)))
|
.approve(spender, new BigNumber(await this.amountToUnits(tokenAddress, amount)))
|
||||||
.send({
|
.send({
|
||||||
from: account,
|
from: account,
|
||||||
gas: estGas + 1,
|
gas: estGas + 1,
|
||||||
@ -165,8 +164,7 @@ export class Pool {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
/**
|
|
||||||
* Estimate gas cost for setSwapFee
|
* Estimate gas cost for setSwapFee
|
||||||
* @param {String} account
|
* @param {String} account
|
||||||
* @param {String} tokenAddress
|
* @param {String} tokenAddress
|
||||||
@ -176,30 +174,27 @@ export class Pool {
|
|||||||
* @param {Contract} contractInstance optional contract instance
|
* @param {Contract} contractInstance optional contract instance
|
||||||
* @return {Promise<number>}
|
* @return {Promise<number>}
|
||||||
*/
|
*/
|
||||||
public async estSetSwapFee(
|
public async estSetSwapFee(
|
||||||
account: string,
|
account: string,
|
||||||
poolAddress: string,
|
poolAddress: string,
|
||||||
fee: string,
|
fee: string,
|
||||||
contractInstance?: Contract
|
contractInstance?: Contract
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
const poolContract =
|
const poolContract =
|
||||||
contractInstance ||
|
contractInstance ||
|
||||||
new this.web3.eth.Contract(defaultERC20ABI.abi as AbiItem[], poolAddress)
|
new this.web3.eth.Contract(defaultERC20ABI.abi as AbiItem[], poolAddress)
|
||||||
|
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||||
let estGas
|
let estGas
|
||||||
try {
|
try {
|
||||||
estGas = await poolContract.methods
|
estGas = await poolContract.methods
|
||||||
.setSwapFee(fee)
|
.setSwapFee(fee)
|
||||||
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
estGas = gasLimitDefault
|
estGas = gasLimitDefault
|
||||||
|
|
||||||
}
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
return estGas
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set pool fee
|
* Set pool fee
|
||||||
@ -216,7 +211,7 @@ export class Pool {
|
|||||||
from: account
|
from: account
|
||||||
})
|
})
|
||||||
let result = null
|
let result = null
|
||||||
const estGas = await this.estSetSwapFee(account,poolAddress,fee)
|
const estGas = await this.estSetSwapFee(account, poolAddress, fee)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = await pool.methods.setSwapFee(this.web3.utils.toWei(fee)).send({
|
result = await pool.methods.setSwapFee(this.web3.utils.toWei(fee)).send({
|
||||||
@ -746,7 +741,7 @@ export class Pool {
|
|||||||
tokenAmountIn,
|
tokenAmountIn,
|
||||||
tokenOut,
|
tokenOut,
|
||||||
minAmountOut,
|
minAmountOut,
|
||||||
maxPrice ? maxPrice : MaxUint256
|
maxPrice || MaxUint256
|
||||||
)
|
)
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -827,7 +822,7 @@ export class Pool {
|
|||||||
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
|
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
|
||||||
)
|
)
|
||||||
|
|
||||||
//console.log(minAmountOutFormatted, 'minamoutnoutformatted')
|
// console.log(minAmountOutFormatted, 'minamoutnoutformatted')
|
||||||
try {
|
try {
|
||||||
result = await pool.methods
|
result = await pool.methods
|
||||||
.swapExactAmountIn(
|
.swapExactAmountIn(
|
||||||
@ -884,7 +879,7 @@ export class Pool {
|
|||||||
maxAmountIn,
|
maxAmountIn,
|
||||||
tokenOut,
|
tokenOut,
|
||||||
amountOut,
|
amountOut,
|
||||||
maxPrice ? maxPrice : MaxUint256
|
maxPrice || MaxUint256
|
||||||
)
|
)
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -10,15 +10,30 @@ import { Logger, getFairGasPrice } from '../../utils'
|
|||||||
const MAX_AWAIT_PROMISES = 10
|
const MAX_AWAIT_PROMISES = 10
|
||||||
|
|
||||||
export interface FixedPriceExchange {
|
export interface FixedPriceExchange {
|
||||||
exchangeID?: string
|
active: boolean
|
||||||
exchangeOwner: string
|
exchangeOwner: string
|
||||||
dataToken: string
|
dataToken: string
|
||||||
baseToken: string
|
baseToken: string
|
||||||
fixedRate: string
|
fixedRate: string
|
||||||
active: boolean
|
dtDecimals: string
|
||||||
supply: string
|
btDecimals: string
|
||||||
|
dtBalance: string
|
||||||
|
btBalance: string
|
||||||
|
dtSupply: string
|
||||||
|
btSupply: string
|
||||||
|
withMint: boolean
|
||||||
|
allowedSwapper: string
|
||||||
|
exchangeID?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface FeesInfo {
|
||||||
|
opfFee: string
|
||||||
|
marketFee: string
|
||||||
|
marketFeeCollector: string
|
||||||
|
marketFeeAvailable: string
|
||||||
|
oceanFeeAvailable: string
|
||||||
|
exchangeID: string
|
||||||
|
}
|
||||||
export interface FixedPriceSwap {
|
export interface FixedPriceSwap {
|
||||||
exchangeID: string
|
exchangeID: string
|
||||||
caller: string
|
caller: string
|
||||||
@ -35,9 +50,9 @@ export class FixedRateExchange {
|
|||||||
public GASLIMIT_DEFAULT = 1000000
|
public GASLIMIT_DEFAULT = 1000000
|
||||||
/** Ocean related functions */
|
/** Ocean related functions */
|
||||||
public oceanAddress: string = null
|
public oceanAddress: string = null
|
||||||
public fixedRateAddress:string
|
public fixedRateAddress: string
|
||||||
public fixedRateExchangeABI: AbiItem | AbiItem[]
|
public fixedRateExchangeABI: AbiItem | AbiItem[]
|
||||||
public fixedRateContract:Contract
|
public fixedRateContract: Contract
|
||||||
public web3: Web3
|
public web3: Web3
|
||||||
public contract: Contract = null
|
public contract: Contract = null
|
||||||
private logger: Logger
|
private logger: Logger
|
||||||
@ -45,8 +60,6 @@ export class FixedRateExchange {
|
|||||||
public startBlock: number
|
public startBlock: number
|
||||||
public ssABI: AbiItem | AbiItem[]
|
public ssABI: AbiItem | AbiItem[]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate FixedRateExchange
|
* Instantiate FixedRateExchange
|
||||||
* @param {any} web3
|
* @param {any} web3
|
||||||
@ -75,13 +88,14 @@ export class FixedRateExchange {
|
|||||||
|
|
||||||
this.logger = logger
|
this.logger = logger
|
||||||
}
|
}
|
||||||
|
|
||||||
async amountToUnits(token: string, amount: string): Promise<string> {
|
async amountToUnits(token: string, amount: string): Promise<string> {
|
||||||
let decimals = 18
|
let decimals = 18
|
||||||
const tokenContract = new this.web3.eth.Contract(
|
const tokenContract = new this.web3.eth.Contract(
|
||||||
defaultERC20ABI.abi as AbiItem[],
|
defaultERC20ABI.abi as AbiItem[],
|
||||||
token
|
token
|
||||||
)
|
)
|
||||||
console.log('1')
|
|
||||||
try {
|
try {
|
||||||
decimals = await tokenContract.methods.decimals().call()
|
decimals = await tokenContract.methods.decimals().call()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -110,20 +124,23 @@ export class FixedRateExchange {
|
|||||||
return amountFormatted.toString()
|
return amountFormatted.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates unique exchange identifier.
|
* Creates unique exchange identifier.
|
||||||
* @param {String} dataToken Data Token Contract Address
|
* @param {String} dataToken Data Token Contract Address
|
||||||
* @param {String} owner Owner of the exchange
|
* @param {String} owner Owner of the exchange
|
||||||
* @return {Promise<string>} exchangeId
|
* @return {Promise<string>} exchangeId
|
||||||
*/
|
*/
|
||||||
public async generateExchangeId(dataToken: string, owner: string): Promise<string> {
|
public async generateExchangeId(
|
||||||
|
basetoken: string,
|
||||||
|
dataToken: string,
|
||||||
|
owner: string
|
||||||
|
): Promise<string> {
|
||||||
const exchangeId = await this.contract.methods
|
const exchangeId = await this.contract.methods
|
||||||
.generateExchangeId(this.oceanAddress, dataToken, owner)
|
.generateExchangeId(basetoken, dataToken, owner)
|
||||||
.call()
|
.call()
|
||||||
return exchangeId
|
return exchangeId
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Estimate gas cost for buyDT
|
* Estimate gas cost for buyDT
|
||||||
* @param {String} account
|
* @param {String} account
|
||||||
@ -132,26 +149,26 @@ export class FixedRateExchange {
|
|||||||
* @param {Contract} contractInstance optional contract instance
|
* @param {Contract} contractInstance optional contract instance
|
||||||
* @return {Promise<number>}
|
* @return {Promise<number>}
|
||||||
*/
|
*/
|
||||||
public async estBuyDT(
|
public async estBuyDT(
|
||||||
account: string,
|
account: string,
|
||||||
datatokenAddress: string,
|
datatokenAddress: string,
|
||||||
dtAmount:string,
|
dtAmount: string,
|
||||||
maxBasetokenAmount: string,
|
maxBasetokenAmount: string,
|
||||||
contractInstance?: Contract
|
contractInstance?: Contract
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
const fixedRate =
|
const fixedRate = contractInstance || this.fixedRateContract
|
||||||
contractInstance || this.fixedRateContract
|
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||||
let estGas
|
let estGas
|
||||||
try {
|
try {
|
||||||
estGas = await fixedRate.methods
|
estGas = await fixedRate.methods
|
||||||
.buyDT(datatokenAddress,dtAmount.toString(),maxBasetokenAmount.toString())
|
.buyDT(datatokenAddress, dtAmount.toString(), maxBasetokenAmount.toString())
|
||||||
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
estGas = gasLimitDefault
|
estGas = gasLimitDefault
|
||||||
}
|
}
|
||||||
return estGas
|
return estGas
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Atomic swap
|
* Atomic swap
|
||||||
* @param {String} exchangeId ExchangeId
|
* @param {String} exchangeId ExchangeId
|
||||||
@ -164,15 +181,30 @@ export class FixedRateExchange {
|
|||||||
address: string,
|
address: string,
|
||||||
exchangeId: string,
|
exchangeId: string,
|
||||||
datatokenAmount: string,
|
datatokenAmount: string,
|
||||||
maxBasetokenAmount:string
|
maxBasetokenAmount: string
|
||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
const dtAmountFormatted = await this.amountToUnits(((await this.getExchange(exchangeId)).dataToken),datatokenAmount)
|
const dtAmountFormatted = await this.amountToUnits(
|
||||||
const maxBtFormatted = await this.amountToUnits(((await this.getExchange(exchangeId)).baseToken),maxBasetokenAmount)
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).dataToken,
|
||||||
|
datatokenAmount
|
||||||
|
)
|
||||||
|
const maxBtFormatted = await this.amountToUnits(
|
||||||
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).baseToken,
|
||||||
|
maxBasetokenAmount
|
||||||
|
)
|
||||||
|
|
||||||
const estGas = await this.estBuyDT(address,exchangeId,dtAmountFormatted,maxBtFormatted)
|
const estGas = await this.estBuyDT(
|
||||||
|
address,
|
||||||
|
exchangeId,
|
||||||
|
dtAmountFormatted,
|
||||||
|
maxBtFormatted
|
||||||
|
)
|
||||||
try {
|
try {
|
||||||
const trxReceipt = await this.contract.methods
|
const trxReceipt = await this.contract.methods
|
||||||
.buyDT(exchangeId, dtAmountFormatted,maxBtFormatted)
|
.buyDT(exchangeId, dtAmountFormatted, maxBtFormatted)
|
||||||
.send({
|
.send({
|
||||||
from: address,
|
from: address,
|
||||||
gas: estGas + 1,
|
gas: estGas + 1,
|
||||||
@ -193,26 +225,26 @@ export class FixedRateExchange {
|
|||||||
* @param {Contract} contractInstance optional contract instance
|
* @param {Contract} contractInstance optional contract instance
|
||||||
* @return {Promise<number>}
|
* @return {Promise<number>}
|
||||||
*/
|
*/
|
||||||
public async estSellDT(
|
public async estSellDT(
|
||||||
account: string,
|
account: string,
|
||||||
datatokenAddress: string,
|
datatokenAddress: string,
|
||||||
dtAmount:string,
|
dtAmount: string,
|
||||||
maxBasetokenAmount: string,
|
maxBasetokenAmount: string,
|
||||||
contractInstance?: Contract
|
contractInstance?: Contract
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
const fixedRate =
|
const fixedRate = contractInstance || this.fixedRateContract
|
||||||
contractInstance || this.fixedRateContract
|
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||||
let estGas
|
let estGas
|
||||||
try {
|
try {
|
||||||
estGas = await fixedRate.methods
|
estGas = await fixedRate.methods
|
||||||
.sellDT(datatokenAddress,dtAmount,maxBasetokenAmount)
|
.sellDT(datatokenAddress, dtAmount, maxBasetokenAmount)
|
||||||
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
estGas = gasLimitDefault
|
estGas = gasLimitDefault
|
||||||
}
|
}
|
||||||
return estGas
|
return estGas
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Atomic swap
|
* Atomic swap
|
||||||
* @param {String} exchangeId ExchangeId
|
* @param {String} exchangeId ExchangeId
|
||||||
@ -225,15 +257,29 @@ export class FixedRateExchange {
|
|||||||
address: string,
|
address: string,
|
||||||
exchangeId: string,
|
exchangeId: string,
|
||||||
datatokenAmount: string,
|
datatokenAmount: string,
|
||||||
minBasetokenAmount:string
|
minBasetokenAmount: string
|
||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
|
const dtAmountFormatted = await this.amountToUnits(
|
||||||
const dtAmountFormatted = await this.amountToUnits((await this.getExchange(exchangeId)).dataToken,datatokenAmount)
|
(
|
||||||
const minBtFormatted = await this.amountToUnits((await this.getExchange(exchangeId)).baseToken,minBasetokenAmount)
|
await this.getExchange(exchangeId)
|
||||||
const estGas = await this.estBuyDT(address,exchangeId,dtAmountFormatted,minBtFormatted)
|
).dataToken,
|
||||||
|
datatokenAmount
|
||||||
|
)
|
||||||
|
const minBtFormatted = await this.amountToUnits(
|
||||||
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).baseToken,
|
||||||
|
minBasetokenAmount
|
||||||
|
)
|
||||||
|
const estGas = await this.estBuyDT(
|
||||||
|
address,
|
||||||
|
exchangeId,
|
||||||
|
dtAmountFormatted,
|
||||||
|
minBtFormatted
|
||||||
|
)
|
||||||
try {
|
try {
|
||||||
const trxReceipt = await this.contract.methods
|
const trxReceipt = await this.contract.methods
|
||||||
.sellDT(exchangeId, dtAmountFormatted,minBtFormatted)
|
.sellDT(exchangeId, dtAmountFormatted, minBtFormatted)
|
||||||
.send({
|
.send({
|
||||||
from: address,
|
from: address,
|
||||||
gas: estGas + 1,
|
gas: estGas + 1,
|
||||||
@ -265,19 +311,18 @@ export class FixedRateExchange {
|
|||||||
* @param {Contract} contractInstance optional contract instance
|
* @param {Contract} contractInstance optional contract instance
|
||||||
* @return {Promise<number>}
|
* @return {Promise<number>}
|
||||||
*/
|
*/
|
||||||
public async estSetRate(
|
public async estSetRate(
|
||||||
account: string,
|
account: string,
|
||||||
exchangeId: string,
|
exchangeId: string,
|
||||||
newRate:string,
|
newRate: string,
|
||||||
contractInstance?: Contract
|
contractInstance?: Contract
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
const fixedRate =
|
const fixedRate = contractInstance || this.fixedRateContract
|
||||||
contractInstance || this.fixedRateContract
|
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||||
let estGas
|
let estGas
|
||||||
try {
|
try {
|
||||||
estGas = await fixedRate.methods
|
estGas = await fixedRate.methods
|
||||||
.setRate(exchangeId,newRate)
|
.setRate(exchangeId, newRate)
|
||||||
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
estGas = gasLimitDefault
|
estGas = gasLimitDefault
|
||||||
@ -295,13 +340,15 @@ export class FixedRateExchange {
|
|||||||
public async setRate(
|
public async setRate(
|
||||||
address: string,
|
address: string,
|
||||||
exchangeId: string,
|
exchangeId: string,
|
||||||
newRate: number,
|
newRate: string
|
||||||
|
|
||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
|
const estGas = await this.estSetRate(
|
||||||
const estGas = await this.estSetRate(address,exchangeId,this.web3.utils.toWei(String(newRate)))
|
address,
|
||||||
|
exchangeId,
|
||||||
|
this.web3.utils.toWei(String(newRate))
|
||||||
|
)
|
||||||
const trxReceipt = await this.contract.methods
|
const trxReceipt = await this.contract.methods
|
||||||
.setRate(exchangeId, this.web3.utils.toWei(String(newRate)))
|
.setRate(exchangeId, this.web3.utils.toWei(newRate))
|
||||||
.send({
|
.send({
|
||||||
from: address,
|
from: address,
|
||||||
gas: estGas + 1,
|
gas: estGas + 1,
|
||||||
@ -310,31 +357,80 @@ export class FixedRateExchange {
|
|||||||
return trxReceipt
|
return trxReceipt
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Estimate gas cost for setRate
|
||||||
|
* @param {String} account
|
||||||
|
* @param {String} exchangeId ExchangeId
|
||||||
|
* @param {String} newAllowedSwapper new allowed swapper address
|
||||||
|
* @param {Contract} contractInstance optional contract instance
|
||||||
|
* @return {Promise<number>}
|
||||||
|
*/
|
||||||
|
public async estSetAllowedSwapper(
|
||||||
|
account: string,
|
||||||
|
exchangeId: string,
|
||||||
|
newAllowedSwapper: string,
|
||||||
|
contractInstance?: Contract
|
||||||
|
): Promise<number> {
|
||||||
|
const fixedRate = contractInstance || this.fixedRateContract
|
||||||
|
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||||
|
let estGas
|
||||||
|
try {
|
||||||
|
estGas = await fixedRate.methods
|
||||||
|
.setRate(exchangeId, newAllowedSwapper)
|
||||||
|
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||||
|
} catch (e) {
|
||||||
|
estGas = gasLimitDefault
|
||||||
|
}
|
||||||
|
return estGas
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set new rate
|
||||||
|
* @param {String} exchangeId ExchangeId
|
||||||
|
* @param {String} newAllowedSwapper newAllowedSwapper (set address zero if we want to remove allowed swapper)
|
||||||
|
* @param {String} address User account
|
||||||
|
* @return {Promise<TransactionReceipt>} transaction receipt
|
||||||
|
*/
|
||||||
|
public async setAllowedSwapper(
|
||||||
|
address: string,
|
||||||
|
exchangeId: string,
|
||||||
|
newAllowedSwapper: string
|
||||||
|
): Promise<TransactionReceipt> {
|
||||||
|
const estGas = await this.estSetAllowedSwapper(address, exchangeId, newAllowedSwapper)
|
||||||
|
const trxReceipt = await this.contract.methods
|
||||||
|
.setAllowedSwapper(exchangeId, newAllowedSwapper)
|
||||||
|
.send({
|
||||||
|
from: address,
|
||||||
|
gas: estGas + 1,
|
||||||
|
gasPrice: await getFairGasPrice(this.web3)
|
||||||
|
})
|
||||||
|
return trxReceipt
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* Estimate gas cost for activate
|
* Estimate gas cost for activate
|
||||||
* @param {String} account
|
* @param {String} account
|
||||||
* @param {String} exchangeId ExchangeId
|
* @param {String} exchangeId ExchangeId
|
||||||
* @param {Contract} contractInstance optional contract instance
|
* @param {Contract} contractInstance optional contract instance
|
||||||
* @return {Promise<number>}
|
* @return {Promise<number>}
|
||||||
*/
|
*/
|
||||||
public async estActivate(
|
public async estActivate(
|
||||||
account: string,
|
account: string,
|
||||||
exchangeId: string,
|
exchangeId: string,
|
||||||
contractInstance?: Contract
|
contractInstance?: Contract
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
const fixedRate =
|
const fixedRate = contractInstance || this.fixedRateContract
|
||||||
contractInstance || this.fixedRateContract
|
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
let estGas
|
||||||
let estGas
|
try {
|
||||||
try {
|
estGas = await fixedRate.methods
|
||||||
estGas = await fixedRate.methods
|
|
||||||
.toggleExchangeState(exchangeId)
|
.toggleExchangeState(exchangeId)
|
||||||
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
estGas = gasLimitDefault
|
estGas = gasLimitDefault
|
||||||
}
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
return estGas
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Activate an exchange
|
* Activate an exchange
|
||||||
@ -351,7 +447,7 @@ export class FixedRateExchange {
|
|||||||
if (exchange.active === true) return null
|
if (exchange.active === true) return null
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||||
|
|
||||||
const estGas = await this.estActivate(address,exchangeId)
|
const estGas = await this.estActivate(address, exchangeId)
|
||||||
const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({
|
const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({
|
||||||
from: address,
|
from: address,
|
||||||
gas: estGas + 1,
|
gas: estGas + 1,
|
||||||
@ -360,33 +456,30 @@ export class FixedRateExchange {
|
|||||||
return trxReceipt
|
return trxReceipt
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Estimate gas cost for deactivate
|
* Estimate gas cost for deactivate
|
||||||
* @param {String} account
|
* @param {String} account
|
||||||
* @param {String} exchangeId ExchangeId
|
* @param {String} exchangeId ExchangeId
|
||||||
* @param {Contract} contractInstance optional contract instance
|
* @param {Contract} contractInstance optional contract instance
|
||||||
* @return {Promise<number>}
|
* @return {Promise<number>}
|
||||||
*/
|
*/
|
||||||
public async estDeactivate(
|
public async estDeactivate(
|
||||||
account: string,
|
account: string,
|
||||||
exchangeId: string,
|
exchangeId: string,
|
||||||
contractInstance?: Contract
|
contractInstance?: Contract
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
const fixedRate =
|
const fixedRate = contractInstance || this.fixedRateContract
|
||||||
contractInstance || this.fixedRateContract
|
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
let estGas
|
||||||
let estGas
|
try {
|
||||||
try {
|
estGas = await fixedRate.methods
|
||||||
estGas = await fixedRate.methods
|
.toggleExchangeState(exchangeId)
|
||||||
.toggleExchangeState(exchangeId)
|
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||||
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
} catch (e) {
|
||||||
} catch (e) {
|
estGas = gasLimitDefault
|
||||||
estGas = gasLimitDefault
|
}
|
||||||
}
|
return estGas
|
||||||
return estGas
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deactivate an exchange
|
* Deactivate an exchange
|
||||||
@ -397,18 +490,19 @@ export class FixedRateExchange {
|
|||||||
public async deactivate(
|
public async deactivate(
|
||||||
address: string,
|
address: string,
|
||||||
exchangeId: string
|
exchangeId: string
|
||||||
|
|
||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
const exchange = await this.getExchange(exchangeId)
|
const exchange = await this.getExchange(exchangeId)
|
||||||
if (!exchange) return null
|
if (!exchange) return null
|
||||||
if (exchange.active === false) return null
|
if (exchange.active === false) return null
|
||||||
|
|
||||||
const estGas = await this.estDeactivate(address,exchangeId)
|
const estGas = await this.estDeactivate(address, exchangeId)
|
||||||
|
|
||||||
const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({
|
const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({
|
||||||
from: address,
|
from: address,
|
||||||
gas: estGas + 1,
|
gas: estGas + 1,
|
||||||
gasPrice: await getFairGasPrice(this.web3)
|
gasPrice: await getFairGasPrice(this.web3)
|
||||||
})
|
})
|
||||||
|
|
||||||
return trxReceipt
|
return trxReceipt
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -423,29 +517,101 @@ export class FixedRateExchange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Supply
|
* Get Datatoken Supply in the exchange
|
||||||
* @param {String} exchangeId ExchangeId
|
* @param {String} exchangeId ExchangeId
|
||||||
* @return {Promise<string>} Rate (converted from wei)
|
* @return {Promise<string>} dt supply formatted
|
||||||
*/
|
*/
|
||||||
public async getSupply(exchangeId: string): Promise<string> {
|
public async getDTSupply(exchangeId: string): Promise<string> {
|
||||||
const weiRate = await this.contract.methods.getSupply(exchangeId).call()
|
const dtSupply = await this.contract.methods.getDTSupply(exchangeId).call()
|
||||||
return this.web3.utils.fromWei(weiRate)
|
return await this.unitsToAmount(
|
||||||
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).dataToken,
|
||||||
|
dtSupply
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* getOceanNeeded
|
* Get Basetoken Supply in the exchange
|
||||||
* @param {String} exchangeId ExchangeId
|
* @param {String} exchangeId ExchangeId
|
||||||
* @param {Number} dataTokenAmount Amount of Data Tokens
|
* @return {Promise<string>} dt supply formatted
|
||||||
* @return {Promise<string>} Ocean amount needed
|
|
||||||
*/
|
*/
|
||||||
public async getOceanNeeded(
|
public async getBTSupply(exchangeId: string): Promise<string> {
|
||||||
|
const btSupply = await this.contract.methods.getBTSupply(exchangeId).call()
|
||||||
|
return await this.unitsToAmount(
|
||||||
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).baseToken,
|
||||||
|
btSupply
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Allower Swapper (if set this is the only account which can use this exchange, else is set at address(0))
|
||||||
|
* @param {String} exchangeId ExchangeId
|
||||||
|
* @return {Promise<string>} address of allowedSwapper
|
||||||
|
*/
|
||||||
|
public async getAllowedSwapper(exchangeId: string): Promise<string> {
|
||||||
|
return await this.contract.methods.getAllowedSwapper(exchangeId).call()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getBTNeeded - returns amount in basetoken that user will pay for dataTokenAmount
|
||||||
|
* @param {String} exchangeId ExchangeId
|
||||||
|
* @param {Number} dataTokenAmount Amount of Data Tokens user wants to buy
|
||||||
|
* @return {Promise<string>} Amount of basetoken needed for buying
|
||||||
|
*/
|
||||||
|
public async getAmountBTIn(
|
||||||
exchangeId: string,
|
exchangeId: string,
|
||||||
dataTokenAmount: string
|
dataTokenAmount: string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const weiRate = await this.contract.methods
|
const result = await this.contract.methods
|
||||||
.calcBaseInGivenOutDT(exchangeId, this.web3.utils.toWei(dataTokenAmount))
|
.calcBaseInGivenOutDT(
|
||||||
|
exchangeId,
|
||||||
|
await this.amountToUnits(
|
||||||
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).dataToken,
|
||||||
|
dataTokenAmount
|
||||||
|
)
|
||||||
|
)
|
||||||
.call()
|
.call()
|
||||||
return this.web3.utils.fromWei(weiRate)
|
return await this.unitsToAmount(
|
||||||
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).baseToken,
|
||||||
|
result.baseTokenAmount
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getBTOut - returns amount in basetoken that user will receive for dataTokenAmount sold
|
||||||
|
* @param {String} exchangeId ExchangeId
|
||||||
|
* @param {Number} dataTokenAmount Amount of Data Tokens
|
||||||
|
* @return {Promise<string>} Amount of basetokens user will receive
|
||||||
|
*/
|
||||||
|
public async getAmountBTOut(
|
||||||
|
exchangeId: string,
|
||||||
|
dataTokenAmount: string
|
||||||
|
): Promise<string> {
|
||||||
|
const result = await this.contract.methods
|
||||||
|
.calcBaseOutGivenInDT(
|
||||||
|
exchangeId,
|
||||||
|
await this.amountToUnits(
|
||||||
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).dataToken,
|
||||||
|
dataTokenAmount
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.call()
|
||||||
|
|
||||||
|
return await this.unitsToAmount(
|
||||||
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).baseToken,
|
||||||
|
result.baseTokenAmount
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -457,9 +623,50 @@ export class FixedRateExchange {
|
|||||||
const result: FixedPriceExchange = await this.contract.methods
|
const result: FixedPriceExchange = await this.contract.methods
|
||||||
.getExchange(exchangeId)
|
.getExchange(exchangeId)
|
||||||
.call()
|
.call()
|
||||||
// result.fixedRate = this.web3.utils.fromWei(result.fixedRate)
|
result.dtDecimals = result.dtDecimals.toString()
|
||||||
// result.supply = this.web3.utils.fromWei(result.supply)
|
result.btDecimals = result.btDecimals.toString()
|
||||||
// result.exchangeID = exchangeId
|
result.dtBalance = await this.unitsToAmount(result.dataToken, result.dtBalance)
|
||||||
|
result.btBalance = await this.unitsToAmount(result.baseToken, result.btBalance)
|
||||||
|
result.dtSupply = await this.unitsToAmount(result.dataToken, result.dtSupply)
|
||||||
|
result.btSupply = await this.unitsToAmount(result.baseToken, result.btSupply)
|
||||||
|
result.fixedRate = this.web3.utils.fromWei(result.fixedRate)
|
||||||
|
result.exchangeID = exchangeId
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get fee details for an exchange
|
||||||
|
* @param {String} exchangeId ExchangeId
|
||||||
|
* @return {Promise<FixedPricedExchange>} Exchange details
|
||||||
|
*/
|
||||||
|
public async getFeesInfo(exchangeId: string): Promise<FeesInfo> {
|
||||||
|
const result: FeesInfo = await this.contract.methods.getFeesInfo(exchangeId).call()
|
||||||
|
result.opfFee = await this.unitsToAmount(
|
||||||
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).baseToken,
|
||||||
|
result.opfFee
|
||||||
|
)
|
||||||
|
result.marketFee = await this.unitsToAmount(
|
||||||
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).baseToken,
|
||||||
|
result.marketFee
|
||||||
|
)
|
||||||
|
result.marketFeeAvailable = await this.unitsToAmount(
|
||||||
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).baseToken,
|
||||||
|
result.marketFeeAvailable
|
||||||
|
)
|
||||||
|
result.oceanFeeAvailable = await this.unitsToAmount(
|
||||||
|
(
|
||||||
|
await this.getExchange(exchangeId)
|
||||||
|
).baseToken,
|
||||||
|
result.oceanFeeAvailable
|
||||||
|
)
|
||||||
|
|
||||||
|
result.exchangeID = exchangeId
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -482,7 +689,106 @@ export class FixedRateExchange {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Estimate gas cost for activate
|
||||||
|
* @param {String} account
|
||||||
|
* @param {String} exchangeId ExchangeId
|
||||||
|
* @param {Contract} contractInstance optional contract instance
|
||||||
|
* @return {Promise<number>}
|
||||||
|
*/
|
||||||
|
public async estActivateMint(
|
||||||
|
account: string,
|
||||||
|
exchangeId: string,
|
||||||
|
contractInstance?: Contract
|
||||||
|
): Promise<number> {
|
||||||
|
const fixedRate = contractInstance || this.fixedRateContract
|
||||||
|
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||||
|
let estGas
|
||||||
|
try {
|
||||||
|
estGas = await fixedRate.methods
|
||||||
|
.toggleMintState(exchangeId, true)
|
||||||
|
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||||
|
} catch (e) {
|
||||||
|
estGas = gasLimitDefault
|
||||||
|
}
|
||||||
|
return estGas
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Activate minting option for fixed rate contract
|
||||||
|
* @param {String} exchangeId ExchangeId
|
||||||
|
* @param {String} address User address
|
||||||
|
* @return {Promise<TransactionReceipt>} transaction receipt
|
||||||
|
*/
|
||||||
|
public async activateMint(
|
||||||
|
address: string,
|
||||||
|
exchangeId: string
|
||||||
|
): Promise<TransactionReceipt> {
|
||||||
|
const exchange = await this.getExchange(exchangeId)
|
||||||
|
if (!exchange) return null
|
||||||
|
if (exchange.withMint === true) return null
|
||||||
|
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||||
|
|
||||||
|
const estGas = await this.estActivateMint(address, exchangeId)
|
||||||
|
const trxReceipt = await this.contract.methods
|
||||||
|
.toggleMintState(exchangeId, true)
|
||||||
|
.send({
|
||||||
|
from: address,
|
||||||
|
gas: estGas + 1,
|
||||||
|
gasPrice: await getFairGasPrice(this.web3)
|
||||||
|
})
|
||||||
|
return trxReceipt
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Estimate gas cost for deactivate
|
||||||
|
* @param {String} account
|
||||||
|
* @param {String} exchangeId ExchangeId
|
||||||
|
* @param {Contract} contractInstance optional contract instance
|
||||||
|
* @return {Promise<number>}
|
||||||
|
*/
|
||||||
|
public async estDeactivateMint(
|
||||||
|
account: string,
|
||||||
|
exchangeId: string,
|
||||||
|
contractInstance?: Contract
|
||||||
|
): Promise<number> {
|
||||||
|
const fixedRate = contractInstance || this.fixedRateContract
|
||||||
|
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||||
|
let estGas
|
||||||
|
try {
|
||||||
|
estGas = await fixedRate.methods
|
||||||
|
.toggleMintState(exchangeId)
|
||||||
|
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||||
|
} catch (e) {
|
||||||
|
estGas = gasLimitDefault
|
||||||
|
}
|
||||||
|
return estGas
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deactivate minting for fixed rate
|
||||||
|
* @param {String} exchangeId ExchangeId
|
||||||
|
* @param {String} address User address
|
||||||
|
* @return {Promise<TransactionReceipt>} transaction receipt
|
||||||
|
*/
|
||||||
|
public async deactivateMint(
|
||||||
|
address: string,
|
||||||
|
exchangeId: string
|
||||||
|
): Promise<TransactionReceipt> {
|
||||||
|
const exchange = await this.getExchange(exchangeId)
|
||||||
|
if (!exchange) return null
|
||||||
|
if (exchange.withMint === false) return null
|
||||||
|
|
||||||
|
const estGas = await this.estDeactivate(address, exchangeId)
|
||||||
|
|
||||||
|
const trxReceipt = await this.contract.methods
|
||||||
|
.toggleMintState(exchangeId, false)
|
||||||
|
.send({
|
||||||
|
from: address,
|
||||||
|
gas: estGas + 1,
|
||||||
|
gasPrice: await getFairGasPrice(this.web3)
|
||||||
|
})
|
||||||
|
|
||||||
|
return trxReceipt
|
||||||
|
}
|
||||||
}
|
}
|
@ -175,7 +175,7 @@ export class SideStaking {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`ERROR: Failed to get: ${e.message}`)
|
this.logger.error(`ERROR: Failed to get: ${e.message}`)
|
||||||
}
|
}
|
||||||
result = await this.unitsToAmount(datatokenAddress,result)
|
result = await this.unitsToAmount(datatokenAddress, result)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,7 +210,7 @@ export class SideStaking {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`ERROR: Failed to get: ${e.message}`)
|
this.logger.error(`ERROR: Failed to get: ${e.message}`)
|
||||||
}
|
}
|
||||||
result = await this.unitsToAmount(datatokenAddress,result)
|
result = await this.unitsToAmount(datatokenAddress, result)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -251,7 +251,7 @@ export class SideStaking {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`ERROR: Failed to get: ${e.message}`)
|
this.logger.error(`ERROR: Failed to get: ${e.message}`)
|
||||||
}
|
}
|
||||||
result = await this.unitsToAmount(datatokenAddress,result)
|
result = await this.unitsToAmount(datatokenAddress, result)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,7 +284,7 @@ export class SideStaking {
|
|||||||
return estGas
|
return estGas
|
||||||
}
|
}
|
||||||
|
|
||||||
/**Send vested tokens available to the publisher address, can be called by anyone
|
/** Send vested tokens available to the publisher address, can be called by anyone
|
||||||
*
|
*
|
||||||
* @param {String} account
|
* @param {String} account
|
||||||
* @param {String} ssAddress side staking contract address
|
* @param {String} ssAddress side staking contract address
|
||||||
|
@ -4,6 +4,7 @@ import { TestContractHandler } from '../../../TestContractHandler'
|
|||||||
import { Contract } from 'web3-eth-contract'
|
import { Contract } from 'web3-eth-contract'
|
||||||
import Web3 from 'web3'
|
import Web3 from 'web3'
|
||||||
import BigNumber from 'bignumber.js'
|
import BigNumber from 'bignumber.js'
|
||||||
|
import BN from 'bn.js'
|
||||||
import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json'
|
import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json'
|
||||||
import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json'
|
import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json'
|
||||||
import SSContract from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json'
|
import SSContract from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json'
|
||||||
@ -32,6 +33,8 @@ describe('Fixed Rate unit test', () => {
|
|||||||
let user3: string
|
let user3: string
|
||||||
let initialBlock: number
|
let initialBlock: number
|
||||||
let fixedRateAddress: string
|
let fixedRateAddress: string
|
||||||
|
let daiAddress: string
|
||||||
|
let usdcAddress: string
|
||||||
let exchangeId: string
|
let exchangeId: string
|
||||||
let contracts: TestContractHandler
|
let contracts: TestContractHandler
|
||||||
let fixedRate: FixedRateExchange
|
let fixedRate: FixedRateExchange
|
||||||
@ -41,7 +44,7 @@ describe('Fixed Rate unit test', () => {
|
|||||||
let daiContract: Contract
|
let daiContract: Contract
|
||||||
let usdcContract: Contract
|
let usdcContract: Contract
|
||||||
const vestedBlocks = 2500000
|
const vestedBlocks = 2500000
|
||||||
|
const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000'
|
||||||
it('should deploy contracts', async () => {
|
it('should deploy contracts', async () => {
|
||||||
contracts = new TestContractHandler(
|
contracts = new TestContractHandler(
|
||||||
web3,
|
web3,
|
||||||
@ -72,12 +75,12 @@ describe('Fixed Rate unit test', () => {
|
|||||||
user2 = contracts.accounts[3]
|
user2 = contracts.accounts[3]
|
||||||
user3 = contracts.accounts[4]
|
user3 = contracts.accounts[4]
|
||||||
exchangeOwner = contracts.accounts[0]
|
exchangeOwner = contracts.accounts[0]
|
||||||
|
|
||||||
await contracts.deployContracts(factoryOwner, FactoryRouter.abi as AbiItem[])
|
await contracts.deployContracts(factoryOwner, FactoryRouter.abi as AbiItem[])
|
||||||
|
|
||||||
// initialize fixed rate
|
// initialize fixed rate
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
daiContract = new web3.eth.Contract(
|
daiContract = new web3.eth.Contract(
|
||||||
contracts.MockERC20.options.jsonInterface,
|
contracts.MockERC20.options.jsonInterface,
|
||||||
contracts.daiAddress
|
contracts.daiAddress
|
||||||
@ -88,13 +91,10 @@ describe('Fixed Rate unit test', () => {
|
|||||||
contracts.usdcAddress
|
contracts.usdcAddress
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
await usdcContract.methods.decimals().call(),
|
await usdcContract.methods.decimals().call(),
|
||||||
'USDC DECIMALS IN THIS TEST'
|
'USDC DECIMALS IN THIS TEST'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Test a Fixed Rate Exchange with DAI (18 Decimals)', () => {
|
describe('Test a Fixed Rate Exchange with DAI (18 Decimals)', () => {
|
||||||
@ -110,21 +110,16 @@ describe('Fixed Rate unit test', () => {
|
|||||||
const ercData = {
|
const ercData = {
|
||||||
templateIndex: 1,
|
templateIndex: 1,
|
||||||
strings: ['ERC20B1', 'ERC20DT1Symbol'],
|
strings: ['ERC20B1', 'ERC20DT1Symbol'],
|
||||||
addresses: [
|
addresses: [contracts.accounts[0], user3, contracts.accounts[0], ADDRESS_ZERO],
|
||||||
contracts.accounts[0],
|
|
||||||
user3,
|
|
||||||
contracts.accounts[0],
|
|
||||||
'0x0000000000000000000000000000000000000000'
|
|
||||||
],
|
|
||||||
uints: [web3.utils.toWei('1000000'), 0],
|
uints: [web3.utils.toWei('1000000'), 0],
|
||||||
bytess: []
|
bytess: []
|
||||||
}
|
}
|
||||||
|
|
||||||
//[baseToken,owner,marketFeeCollector,allowedSwapper]
|
// [baseToken,owner,marketFeeCollector,allowedSwapper]
|
||||||
const fixedRateData = {
|
const fixedRateData = {
|
||||||
fixedPriceAddress:contracts.fixedRateAddress,
|
fixedPriceAddress: contracts.fixedRateAddress,
|
||||||
addresses:[contracts.daiAddress,exchangeOwner,user3, '0x0000000000000000000000000000000000000000'],
|
addresses: [contracts.daiAddress, exchangeOwner, user3, ADDRESS_ZERO],
|
||||||
uints:[18,18,web3.utils.toWei('1'),1e15,0]
|
uints: [18, 18, web3.utils.toWei('1'), 1e15, 0]
|
||||||
}
|
}
|
||||||
|
|
||||||
const nftFactory = new NFTFactory(contracts.factory721Address, web3, LoggerInstance)
|
const nftFactory = new NFTFactory(contracts.factory721Address, web3, LoggerInstance)
|
||||||
@ -145,47 +140,169 @@ describe('Fixed Rate unit test', () => {
|
|||||||
expect(await dtContract.methods.balanceOf(user2).call()).to.equal('0')
|
expect(await dtContract.methods.balanceOf(user2).call()).to.equal('0')
|
||||||
|
|
||||||
fixedRateAddress = contracts.fixedRateAddress
|
fixedRateAddress = contracts.fixedRateAddress
|
||||||
fixedRate = new FixedRateExchange(web3, LoggerInstance, fixedRateAddress, FixedRate.abi as AbiItem[],contracts.oceanAddress)
|
fixedRate = new FixedRateExchange(
|
||||||
|
web3,
|
||||||
|
LoggerInstance,
|
||||||
|
fixedRateAddress,
|
||||||
|
FixedRate.abi as AbiItem[],
|
||||||
|
contracts.oceanAddress
|
||||||
|
)
|
||||||
assert(fixedRate != null)
|
assert(fixedRate != null)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('#isActive - should return true if exchange is active', async () => {
|
it('#isActive - should return true if exchange is active', async () => {
|
||||||
expect(await fixedRate.isActive(exchangeId)).to.equal(
|
expect(await fixedRate.isActive(exchangeId)).to.equal(true)
|
||||||
true
|
expect(await fixedRate.isActive('0x00')).to.equal(false)
|
||||||
)
|
})
|
||||||
expect(await fixedRate.isActive('0x00')).to.equal(
|
|
||||||
false
|
it('#deactivate - should deactivate an exchange if exchangeOwner', async () => {
|
||||||
)
|
expect(await fixedRate.isActive(exchangeId)).to.equal(true)
|
||||||
|
await fixedRate.deactivate(exchangeOwner, exchangeId)
|
||||||
|
|
||||||
|
expect(await fixedRate.isActive(exchangeId)).to.equal(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#activate - should activate an exchange if exchangeOwner', async () => {
|
||||||
|
expect(await fixedRate.isActive(exchangeId)).to.equal(false)
|
||||||
|
await fixedRate.activate(exchangeOwner, exchangeId)
|
||||||
|
expect(await fixedRate.isActive(exchangeId)).to.equal(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#activateMint - should activate Mint(allows fixed rate contract to mint dts if required), if exchangeOwner', async () => {
|
||||||
|
expect((await fixedRate.getExchange(exchangeId)).withMint).to.equal(false)
|
||||||
|
await fixedRate.activateMint(exchangeOwner, exchangeId)
|
||||||
|
expect((await fixedRate.getExchange(exchangeId)).withMint).to.equal(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#dectivateMint - should deactivate Mint if exchangeOwner', async () => {
|
||||||
|
expect((await fixedRate.getExchange(exchangeId)).withMint).to.equal(true)
|
||||||
|
await fixedRate.deactivateMint(exchangeOwner, exchangeId)
|
||||||
|
expect((await fixedRate.getExchange(exchangeId)).withMint).to.equal(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#generate exchangeId - should generate a specific exchangeId', async () => {
|
||||||
|
expect(
|
||||||
|
await fixedRate.generateExchangeId(contracts.daiAddress, dtAddress, exchangeOwner)
|
||||||
|
).to.equal(exchangeId)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#getNumberOfExchanges - should return total number of exchanges', async () => {
|
||||||
|
expect(await fixedRate.getNumberOfExchanges()).to.equal('1')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#getExchanges - should return all exchanges ids', async () => {
|
||||||
|
const exchangeIds = await fixedRate.getExchanges()
|
||||||
|
expect(exchangeIds[0]).to.equal(exchangeId)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#getRate - should return rate', async () => {
|
||||||
|
expect(await fixedRate.getRate(exchangeId)).to.equal('1')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#setRate - set new rate if exchangeOwner', async () => {
|
||||||
|
await fixedRate.setRate(exchangeOwner, exchangeId, '2')
|
||||||
|
expect(await fixedRate.getRate(exchangeId)).to.equal('2')
|
||||||
|
await fixedRate.setRate(exchangeOwner, exchangeId, '1')
|
||||||
|
expect(await fixedRate.getRate(exchangeId)).to.equal('1')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#getDTSupply - should get the dt supply in the exchange', async () => {
|
||||||
|
expect(await fixedRate.getDTSupply(exchangeId)).to.equal('0')
|
||||||
|
})
|
||||||
|
it('#getBTSupply - should get the bt supply in the exchange', async () => {
|
||||||
|
expect(await fixedRate.getBTSupply(exchangeId)).to.equal('0')
|
||||||
|
})
|
||||||
|
it('#getBTNeeded - should get bt amount for a specific dt amount', async () => {
|
||||||
|
console.log(await fixedRate.getAmountBTIn(exchangeId, '100'))
|
||||||
|
})
|
||||||
|
it('#getBTNeeded - should get bt amount for a specific dt amount', async () => {
|
||||||
|
console.log(await fixedRate.getAmountBTOut(exchangeId, '100'))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('#buyDT - user2 should buy some dt', async () => {
|
it('#buyDT - user2 should buy some dt', async () => {
|
||||||
await dtContract.methods.mint(exchangeOwner,web3.utils.toWei('1000')).send({from:exchangeOwner})
|
await dtContract.methods
|
||||||
await dtContract.methods.approve(fixedRateAddress,web3.utils.toWei('1000')).send({from:exchangeOwner})
|
.mint(exchangeOwner, web3.utils.toWei('1000'))
|
||||||
await daiContract.methods.transfer(user2,web3.utils.toWei('100')).send({from:exchangeOwner})
|
.send({ from: exchangeOwner })
|
||||||
await daiContract.methods.approve(fixedRateAddress,web3.utils.toWei('100')).send({from:user2})
|
await dtContract.methods
|
||||||
const tx = await fixedRate.buyDT(user2,exchangeId,'10','11')
|
.approve(fixedRateAddress, web3.utils.toWei('1000'))
|
||||||
// console.log(tx.events.Swapped.returnValues)
|
.send({ from: exchangeOwner })
|
||||||
|
await daiContract.methods
|
||||||
|
.transfer(user2, web3.utils.toWei('100'))
|
||||||
|
.send({ from: exchangeOwner })
|
||||||
|
await daiContract.methods
|
||||||
|
.approve(fixedRateAddress, web3.utils.toWei('100'))
|
||||||
|
.send({ from: user2 })
|
||||||
|
expect(await dtContract.methods.balanceOf(user2).call()).to.equal('0')
|
||||||
|
const daiBalanceBefore = new BN(await daiContract.methods.balanceOf(user2).call())
|
||||||
|
expect(daiBalanceBefore.toString()).to.equal(web3.utils.toWei('100'))
|
||||||
|
const tx = await fixedRate.buyDT(user2, exchangeId, '10', '11')
|
||||||
|
// console.log(tx.events.Swapped.returnValues)
|
||||||
assert(tx.events.Swapped != null)
|
assert(tx.events.Swapped != null)
|
||||||
const args = tx.events.Swapped.returnValues
|
const args = tx.events.Swapped.returnValues
|
||||||
expect(args.exchangeId).to.equal(exchangeId)
|
expect(args.exchangeId).to.equal(exchangeId)
|
||||||
expect(args.by).to.equal(user2)
|
expect(args.by).to.equal(user2)
|
||||||
expect(args.dataTokenSwappedAmount).to.equal(web3.utils.toWei('10'))
|
expect(args.dataTokenSwappedAmount).to.equal(web3.utils.toWei('10'))
|
||||||
expect(args.tokenOutAddress).to.equal(dtAddress)
|
expect(args.tokenOutAddress).to.equal(dtAddress)
|
||||||
|
expect(await dtContract.methods.balanceOf(user2).call()).to.equal(
|
||||||
|
args.dataTokenSwappedAmount
|
||||||
|
)
|
||||||
|
expect(
|
||||||
|
daiBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString()
|
||||||
|
).to.equal(await daiContract.methods.balanceOf(user2).call())
|
||||||
})
|
})
|
||||||
|
|
||||||
it('#sellDT - user2 should sell some dt', async () => {
|
it('#sellDT - user2 should sell some dt', async () => {
|
||||||
|
await dtContract.methods
|
||||||
await dtContract.methods.approve(fixedRateAddress,web3.utils.toWei('10')).send({from:user2})
|
.approve(fixedRateAddress, web3.utils.toWei('10'))
|
||||||
const tx = await fixedRate.sellDT(user2,exchangeId,'10','9')
|
.send({ from: user2 })
|
||||||
// console.log(tx.events.Swapped.returnValues)
|
const daiBalanceBefore = new BN(await daiContract.methods.balanceOf(user2).call())
|
||||||
|
const tx = await fixedRate.sellDT(user2, exchangeId, '10', '9')
|
||||||
|
// console.log(tx.events.Swapped.returnValues)
|
||||||
assert(tx.events.Swapped != null)
|
assert(tx.events.Swapped != null)
|
||||||
const args = tx.events.Swapped.returnValues
|
const args = tx.events.Swapped.returnValues
|
||||||
expect(args.exchangeId).to.equal(exchangeId)
|
expect(args.exchangeId).to.equal(exchangeId)
|
||||||
expect(args.by).to.equal(user2)
|
expect(args.by).to.equal(user2)
|
||||||
expect(args.dataTokenSwappedAmount).to.equal(web3.utils.toWei('10'))
|
expect(args.dataTokenSwappedAmount).to.equal(web3.utils.toWei('10'))
|
||||||
expect(args.tokenOutAddress).to.equal(contracts.daiAddress)
|
expect(args.tokenOutAddress).to.equal(contracts.daiAddress)
|
||||||
|
expect(await dtContract.methods.balanceOf(user2).call()).to.equal('0')
|
||||||
|
expect(
|
||||||
|
daiBalanceBefore.add(new BN(args.baseTokenSwappedAmount)).toString()
|
||||||
|
).to.equal(await daiContract.methods.balanceOf(user2).call())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('#getExchange - should return exchange details', async () => {
|
||||||
|
const result = await fixedRate.getExchange(exchangeId)
|
||||||
|
expect(result.active).to.equal(true)
|
||||||
|
expect(result.btDecimals).to.equal('18')
|
||||||
|
expect(result.dtDecimals).to.equal('18')
|
||||||
|
expect(result.baseToken).to.equal(contracts.daiAddress)
|
||||||
|
expect(result.dataToken).to.equal(dtAddress)
|
||||||
|
expect(result.exchangeOwner).to.equal(exchangeOwner)
|
||||||
|
expect(result.withMint).to.equal(false)
|
||||||
|
expect(result.dtBalance).to.equal('10')
|
||||||
|
expect(result.btBalance).to.equal('0')
|
||||||
|
expect(result.dtSupply).to.equal('1000')
|
||||||
|
expect(result.btSupply).to.equal('0')
|
||||||
|
expect(result.fixedRate).to.equal('1')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#getFeesInfo - should return exchange fee details', async () => {
|
||||||
|
const result = await fixedRate.getFeesInfo(exchangeId)
|
||||||
|
expect(result.marketFee).to.equal('0.001')
|
||||||
|
// we made 2 swaps for 10 DT at rate 1, the fee is 0.1% for market and always in basetoken so it's 0.01 DAI
|
||||||
|
// we made 2 swaps for 10 DT at rate 1, the fee is 0.1% for ocean community and always in basetoken so it's 0.01 DAI
|
||||||
|
expect(result.marketFeeAvailable).to.equal('0.02') // formatted for basetoken decimals
|
||||||
|
expect(result.oceanFeeAvailable).to.equal('0.02') // formatted for basetoken decimals
|
||||||
|
expect(result.marketFeeCollector).to.equal(user3)
|
||||||
|
expect(result.opfFee).to.equal('0.001')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#getAllowedSwapper- should return address(0) if not set, if exchangeOwner', async () => {
|
||||||
|
expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ADDRESS_ZERO)
|
||||||
|
})
|
||||||
|
it('#setAllowedSwapper- should return address(0) if not set, if exchangeOwner', async () => {
|
||||||
|
await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, user2)
|
||||||
|
expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(user2)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -262,13 +262,15 @@ describe('SideStaking unit test', () => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
await sideStaking.unitsToAmount(erc20Token,await erc20Contract.methods.balanceOf(contracts.accounts[0]).call() )
|
await sideStaking.unitsToAmount(
|
||||||
|
erc20Token,
|
||||||
|
await erc20Contract.methods.balanceOf(contracts.accounts[0]).call()
|
||||||
|
)
|
||||||
).to.equal(await sideStaking.getvestingAmountSoFar(sideStakingAddress, erc20Token))
|
).to.equal(await sideStaking.getvestingAmountSoFar(sideStakingAddress, erc20Token))
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
await sideStaking.getvestingLastBlock(sideStakingAddress, erc20Token)
|
await sideStaking.getvestingLastBlock(sideStakingAddress, erc20Token)
|
||||||
).to.equal((await web3.eth.getBlockNumber()).toString())
|
).to.equal((await web3.eth.getBlockNumber()).toString())
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('#swapExactAmountIn - should swap', async () => {
|
it('#swapExactAmountIn - should swap', async () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user