mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Merge branch 'v4main' into issue-1350-rewrite-dispenser-test
This commit is contained in:
commit
9ec4b081d7
2822
CHANGELOG.md
2822
CHANGELOG.md
File diff suppressed because it is too large
Load Diff
2486
package-lock.json
generated
2486
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
16
package.json
16
package.json
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@oceanprotocol/lib",
|
||||
"source": "./src/index.ts",
|
||||
"version": "1.0.0-next.37",
|
||||
"version": "1.0.0-next.42",
|
||||
"description": "JavaScript client library for Ocean Protocol",
|
||||
"main": "./dist/lib.js",
|
||||
"umd:main": "dist/lib.umd.js",
|
||||
@ -58,7 +58,7 @@
|
||||
"web3": "^1.7.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"@oceanprotocol/contracts": "1.0.0-alpha.28",
|
||||
"@oceanprotocol/contracts": "^1.0.0-alpha.32",
|
||||
"bignumber.js": "^9.0.2",
|
||||
"cross-fetch": "^3.1.5",
|
||||
"crypto-js": "^4.1.1",
|
||||
@ -68,12 +68,12 @@
|
||||
"web3-eth-contract": "^1.7.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@truffle/hdwallet-provider": "^2.0.7",
|
||||
"@truffle/hdwallet-provider": "^2.0.8",
|
||||
"@types/chai": "^4.3.1",
|
||||
"@types/chai-spies": "^1.0.3",
|
||||
"@types/crypto-js": "^4.1.1",
|
||||
"@types/mocha": "^9.1.1",
|
||||
"@types/node": "^17.0.29",
|
||||
"@types/node": "^17.0.32",
|
||||
"@types/node-fetch": "^3.0.3",
|
||||
"@typescript-eslint/eslint-plugin": "^4.33.0",
|
||||
"@typescript-eslint/parser": "^4.33.0",
|
||||
@ -86,18 +86,18 @@
|
||||
"eslint-config-prettier": "^8.5.0",
|
||||
"eslint-plugin-prettier": "^4.0.0",
|
||||
"fs": "0.0.1-security",
|
||||
"microbundle": "^0.15.0",
|
||||
"mocha": "^9.2.2",
|
||||
"microbundle": "0.14.2",
|
||||
"mocha": "^10.0.0",
|
||||
"mock-local-storage": "^1.1.21",
|
||||
"nyc": "^15.1.0",
|
||||
"ora": "5.4.1",
|
||||
"prettier": "^2.6.2",
|
||||
"release-it": "^14.14.2",
|
||||
"release-it": "^15.0.0",
|
||||
"source-map-support": "^0.5.19",
|
||||
"ts-node": "^10.7.0",
|
||||
"ts-node-register": "^1.0.0",
|
||||
"typedoc": "0.22.15",
|
||||
"typescript": "^4.6.3"
|
||||
"typescript": "^4.6.4"
|
||||
},
|
||||
"nyc": {
|
||||
"include": [
|
||||
|
@ -23,6 +23,8 @@ export interface TokenInOutMarket {
|
||||
tokenIn: string
|
||||
tokenOut: string
|
||||
marketFeeAddress: string
|
||||
tokenInDecimals?: number
|
||||
tokenOutDecimals?: number
|
||||
}
|
||||
|
||||
export interface AmountsInMaxFee {
|
||||
|
@ -15,3 +15,14 @@ export interface ProviderInitialize {
|
||||
computeAddress: string
|
||||
providerFee: ProviderFees
|
||||
}
|
||||
|
||||
export interface ProviderComputeInitialize {
|
||||
datatoken?: string
|
||||
validOrder?: string
|
||||
providerFee?: ProviderFees
|
||||
}
|
||||
|
||||
export interface ProviderComputeInitializeResults {
|
||||
algorithm?: ProviderComputeInitialize
|
||||
datasets?: ProviderComputeInitialize[]
|
||||
}
|
||||
|
@ -10,9 +10,9 @@ import {
|
||||
getFreCreationParams,
|
||||
getErcCreationParams,
|
||||
getPoolCreationParams,
|
||||
configHelperNetworks,
|
||||
setContractDefaults,
|
||||
ZERO_ADDRESS
|
||||
ZERO_ADDRESS,
|
||||
ConfigHelper
|
||||
} from '../utils'
|
||||
import { Config } from '../models/index.js'
|
||||
import {
|
||||
@ -66,13 +66,14 @@ export class NftFactory {
|
||||
constructor(
|
||||
factory721Address: string,
|
||||
web3: Web3,
|
||||
network?: string | number,
|
||||
factory721Abi?: AbiItem | AbiItem[],
|
||||
config?: Config
|
||||
) {
|
||||
this.factory721Address = factory721Address
|
||||
this.factory721Abi = factory721Abi || (defaultFactory721Abi.abi as AbiItem[])
|
||||
this.web3 = web3
|
||||
this.config = config || configHelperNetworks[0]
|
||||
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
|
||||
this.factory721 = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.factory721Abi, this.factory721Address),
|
||||
this.config
|
||||
|
@ -3,7 +3,7 @@ import Web3 from 'web3'
|
||||
import { TransactionReceipt } from 'web3-core'
|
||||
import { AbiItem } from 'web3-utils'
|
||||
import defaultRouter from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json'
|
||||
import { getFairGasPrice, setContractDefaults, configHelperNetworks } from '../utils'
|
||||
import { getFairGasPrice, setContractDefaults, ConfigHelper } from '../utils'
|
||||
import { Operation } from '../@types/Router'
|
||||
import { Config } from '../models/index.js'
|
||||
|
||||
@ -27,13 +27,14 @@ export class Router {
|
||||
constructor(
|
||||
routerAddress: string,
|
||||
web3: Web3,
|
||||
network?: string | number,
|
||||
RouterAbi?: AbiItem | AbiItem[],
|
||||
config?: Config
|
||||
) {
|
||||
this.routerAddress = routerAddress
|
||||
this.RouterAbi = RouterAbi || (defaultRouter.abi as AbiItem[])
|
||||
this.web3 = web3
|
||||
this.config = config || configHelperNetworks[0]
|
||||
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
|
||||
this.router = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.RouterAbi, this.routerAddress),
|
||||
this.config
|
||||
|
@ -4,11 +4,11 @@ import { TransactionReceipt } from 'web3-core'
|
||||
import { Contract } from 'web3-eth-contract'
|
||||
import {
|
||||
getFairGasPrice,
|
||||
configHelperNetworks,
|
||||
setContractDefaults,
|
||||
unitsToAmount,
|
||||
amountToUnits,
|
||||
LoggerInstance
|
||||
LoggerInstance,
|
||||
ConfigHelper
|
||||
} from '../../utils'
|
||||
import BigNumber from 'bignumber.js'
|
||||
import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json'
|
||||
@ -40,11 +40,32 @@ export class Pool {
|
||||
public GASLIMIT_DEFAULT = 1000000
|
||||
private config: Config
|
||||
|
||||
constructor(web3: Web3, poolAbi: AbiItem | AbiItem[] = null, config?: Config) {
|
||||
constructor(
|
||||
web3: Web3,
|
||||
network?: string | number,
|
||||
poolAbi: AbiItem | AbiItem[] = null,
|
||||
config?: Config
|
||||
) {
|
||||
if (poolAbi) this.poolAbi = poolAbi
|
||||
else this.poolAbi = PoolTemplate.abi as AbiItem[]
|
||||
this.web3 = web3
|
||||
this.config = config || configHelperNetworks[0]
|
||||
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
|
||||
}
|
||||
|
||||
async amountToUnits(
|
||||
token: string,
|
||||
amount: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
return amountToUnits(this.web3, token, amount, tokenDecimals)
|
||||
}
|
||||
|
||||
async unitsToAmount(
|
||||
token: string,
|
||||
amount: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
return unitsToAmount(this.web3, token, amount, tokenDecimals)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -362,9 +383,14 @@ export class Pool {
|
||||
* Returns the current token reserve amount
|
||||
* @param {String} poolAddress
|
||||
* @param {String} token Address of the token to be checked
|
||||
* @param {number} tokenDecimals optional number of decimals of the token
|
||||
* @return {String}
|
||||
*/
|
||||
async getReserve(poolAddress: string, token: string): Promise<string> {
|
||||
async getReserve(
|
||||
poolAddress: string,
|
||||
token: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
let amount = null
|
||||
try {
|
||||
const pool = setContractDefaults(
|
||||
@ -372,7 +398,7 @@ export class Pool {
|
||||
this.config
|
||||
)
|
||||
const result = await pool.methods.getBalance(token).call()
|
||||
amount = await unitsToAmount(this.web3, token, result)
|
||||
amount = await this.unitsToAmount(token, result, tokenDecimals)
|
||||
} catch (e) {
|
||||
LoggerInstance.error(`ERROR: Failed to get how many tokens \
|
||||
are in the pool: ${e.message}`)
|
||||
@ -498,9 +524,14 @@ export class Pool {
|
||||
* Get Market Fees available to be collected for a specific token
|
||||
* @param {String} poolAddress
|
||||
* @param {String} token token we want to check fees
|
||||
* @param {number} tokenDecimals optional number of decimals of the token
|
||||
* @return {String}
|
||||
*/
|
||||
async getMarketFees(poolAddress: string, token: string): Promise<string> {
|
||||
async getMarketFees(
|
||||
poolAddress: string,
|
||||
token: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
const pool = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi, poolAddress),
|
||||
this.config
|
||||
@ -508,7 +539,7 @@ export class Pool {
|
||||
let weight = null
|
||||
try {
|
||||
const result = await pool.methods.publishMarketFees(token).call()
|
||||
weight = await unitsToAmount(this.web3, token, result)
|
||||
weight = await this.unitsToAmount(token, result, tokenDecimals)
|
||||
} catch (e) {
|
||||
LoggerInstance.error(`ERROR: Failed to get market fees for a token: ${e.message}`)
|
||||
}
|
||||
@ -557,9 +588,14 @@ export class Pool {
|
||||
* Get Community Fees available to be collected for a specific token
|
||||
* @param {String} poolAddress
|
||||
* @param {String} token token we want to check fees
|
||||
* @param {number} tokenDecimals optional number of decimals of the token
|
||||
* @return {String}
|
||||
*/
|
||||
async getCommunityFees(poolAddress: string, token: string): Promise<string> {
|
||||
async getCommunityFees(
|
||||
poolAddress: string,
|
||||
token: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
const pool = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi, poolAddress),
|
||||
this.config
|
||||
@ -567,7 +603,7 @@ export class Pool {
|
||||
let weight = null
|
||||
try {
|
||||
const result = await pool.methods.communityFees(token).call()
|
||||
weight = await unitsToAmount(this.web3, token, result)
|
||||
weight = await this.unitsToAmount(token, result, tokenDecimals)
|
||||
} catch (e) {
|
||||
LoggerInstance.error(
|
||||
`ERROR: Failed to get community fees for a token: ${e.message}`
|
||||
@ -802,21 +838,20 @@ export class Pool {
|
||||
this.config
|
||||
)
|
||||
|
||||
const tokenAmountIn = await amountToUnits(
|
||||
this.web3,
|
||||
const tokenAmountIn = await this.amountToUnits(
|
||||
tokenInOutMarket.tokenIn,
|
||||
amountsInOutMaxFee.tokenAmountIn
|
||||
amountsInOutMaxFee.tokenAmountIn,
|
||||
tokenInOutMarket.tokenInDecimals
|
||||
)
|
||||
|
||||
const minAmountOut = await amountToUnits(
|
||||
this.web3,
|
||||
const minAmountOut = await this.amountToUnits(
|
||||
tokenInOutMarket.tokenOut,
|
||||
amountsInOutMaxFee.minAmountOut
|
||||
amountsInOutMaxFee.minAmountOut,
|
||||
tokenInOutMarket.tokenOutDecimals
|
||||
)
|
||||
|
||||
const maxPrice = amountsInOutMaxFee.maxPrice
|
||||
? amountToUnits(
|
||||
this.web3,
|
||||
? this.amountToUnits(
|
||||
await this.getBaseToken(poolAddress),
|
||||
amountsInOutMaxFee.maxPrice
|
||||
)
|
||||
@ -882,23 +917,22 @@ export class Pool {
|
||||
amountsInOutMaxFee
|
||||
)
|
||||
|
||||
const tokenAmountIn = await amountToUnits(
|
||||
this.web3,
|
||||
const tokenAmountIn = await this.amountToUnits(
|
||||
tokenInOutMarket.tokenIn,
|
||||
amountsInOutMaxFee.tokenAmountIn
|
||||
amountsInOutMaxFee.tokenAmountIn,
|
||||
tokenInOutMarket.tokenInDecimals
|
||||
)
|
||||
|
||||
const minAmountOut = await amountToUnits(
|
||||
this.web3,
|
||||
const minAmountOut = await this.amountToUnits(
|
||||
tokenInOutMarket.tokenOut,
|
||||
amountsInOutMaxFee.minAmountOut
|
||||
amountsInOutMaxFee.minAmountOut,
|
||||
tokenInOutMarket.tokenOutDecimals
|
||||
)
|
||||
|
||||
let result = null
|
||||
|
||||
const maxPrice = amountsInOutMaxFee.maxPrice
|
||||
? await amountToUnits(
|
||||
this.web3,
|
||||
? await this.amountToUnits(
|
||||
await this.getBaseToken(poolAddress),
|
||||
amountsInOutMaxFee.maxPrice
|
||||
)
|
||||
@ -956,21 +990,20 @@ export class Pool {
|
||||
|
||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||
|
||||
const maxAmountIn = await amountToUnits(
|
||||
this.web3,
|
||||
const maxAmountIn = await this.amountToUnits(
|
||||
tokenInOutMarket.tokenIn,
|
||||
amountsInOutMaxFee.maxAmountIn
|
||||
amountsInOutMaxFee.maxAmountIn,
|
||||
tokenInOutMarket.tokenInDecimals
|
||||
)
|
||||
|
||||
const tokenAmountOut = await amountToUnits(
|
||||
this.web3,
|
||||
const tokenAmountOut = await this.amountToUnits(
|
||||
tokenInOutMarket.tokenOut,
|
||||
amountsInOutMaxFee.tokenAmountOut
|
||||
amountsInOutMaxFee.tokenAmountOut,
|
||||
tokenInOutMarket.tokenOutDecimals
|
||||
)
|
||||
|
||||
const maxPrice = amountsInOutMaxFee.maxPrice
|
||||
? await amountToUnits(
|
||||
this.web3,
|
||||
? await this.amountToUnits(
|
||||
await this.getBaseToken(poolAddress),
|
||||
amountsInOutMaxFee.maxPrice
|
||||
)
|
||||
@ -1031,21 +1064,20 @@ export class Pool {
|
||||
amountsInOutMaxFee
|
||||
)
|
||||
|
||||
const maxAmountIn = await amountToUnits(
|
||||
this.web3,
|
||||
const maxAmountIn = await this.amountToUnits(
|
||||
tokenInOutMarket.tokenIn,
|
||||
amountsInOutMaxFee.maxAmountIn
|
||||
amountsInOutMaxFee.maxAmountIn,
|
||||
tokenInOutMarket.tokenInDecimals
|
||||
)
|
||||
|
||||
const tokenAmountOut = await amountToUnits(
|
||||
this.web3,
|
||||
const tokenAmountOut = await this.amountToUnits(
|
||||
tokenInOutMarket.tokenOut,
|
||||
amountsInOutMaxFee.tokenAmountOut
|
||||
amountsInOutMaxFee.tokenAmountOut,
|
||||
tokenInOutMarket.tokenOutDecimals
|
||||
)
|
||||
|
||||
const maxPrice = amountsInOutMaxFee.maxPrice
|
||||
? amountToUnits(
|
||||
this.web3,
|
||||
? this.amountToUnits(
|
||||
await this.getBaseToken(poolAddress),
|
||||
amountsInOutMaxFee.maxPrice
|
||||
)
|
||||
@ -1077,177 +1109,6 @@ export class Pool {
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimate gas cost for joinPool method
|
||||
* @param {String} address
|
||||
* @param {String} poolAddress
|
||||
* @param {String} poolAmountOut expected number of pool shares that you will get
|
||||
* @param {String[]} maxAmountsIn array with maxium amounts spent
|
||||
* @param {Contract} contractInstance optional contract instance
|
||||
* @return {Promise<number>}
|
||||
*/
|
||||
public async estJoinPool(
|
||||
address: string,
|
||||
poolAddress: string,
|
||||
poolAmountOut: string,
|
||||
maxAmountsIn: string[],
|
||||
contractInstance?: Contract
|
||||
): Promise<number> {
|
||||
const poolContract =
|
||||
contractInstance ||
|
||||
setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress),
|
||||
this.config
|
||||
)
|
||||
|
||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||
let estGas
|
||||
try {
|
||||
estGas = await poolContract.methods
|
||||
.joinPool(poolAmountOut, maxAmountsIn)
|
||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||
} catch (e) {
|
||||
estGas = gasLimitDefault
|
||||
}
|
||||
return estGas
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds dual side liquidity to the pool (both datatoken and basetoken)
|
||||
* This will pull some of each of the currently trading tokens in the pool,
|
||||
* meaning you must have called approve for each token for this pool.
|
||||
* These values are limited by the array of maxAmountsIn in the order of the pool tokens.
|
||||
* @param {String} address
|
||||
* @param {String} poolAddress
|
||||
* @param {String} poolAmountOut expected number of pool shares that you will get
|
||||
* @param {String[]} maxAmountsIn array with maxium amounts spent
|
||||
* @return {TransactionReceipt}
|
||||
*/
|
||||
async joinPool(
|
||||
address: string,
|
||||
poolAddress: string,
|
||||
poolAmountOut: string,
|
||||
maxAmountsIn: string[]
|
||||
): Promise<TransactionReceipt> {
|
||||
const pool = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi, poolAddress),
|
||||
this.config
|
||||
)
|
||||
const weiMaxAmountsIn = []
|
||||
const tokens = await this.getFinalTokens(poolAddress)
|
||||
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const amount = await amountToUnits(this.web3, tokens[i], maxAmountsIn[i])
|
||||
weiMaxAmountsIn.push(amount)
|
||||
}
|
||||
|
||||
let result = null
|
||||
|
||||
const estGas = await this.estJoinPool(
|
||||
address,
|
||||
poolAddress,
|
||||
this.web3.utils.toWei(poolAmountOut),
|
||||
weiMaxAmountsIn
|
||||
)
|
||||
|
||||
try {
|
||||
result = await pool.methods
|
||||
.joinPool(this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn)
|
||||
.send({
|
||||
from: address,
|
||||
gas: estGas + 1,
|
||||
gasPrice: await getFairGasPrice(this.web3, this.config)
|
||||
})
|
||||
} catch (e) {
|
||||
LoggerInstance.error(`ERROR: Failed to join pool: ${e.message}`)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimate gas cost for exitPool
|
||||
* @param {String} address
|
||||
* @param {String} poolAddress
|
||||
``* @param {String} poolAmountIn amount of pool shares spent
|
||||
* @param {String[]} minAmountsOut aarray with minimum amount of tokens expected
|
||||
* @param {Contract} contractInstance optional contract instance
|
||||
* @return {Promise<number>}
|
||||
*/
|
||||
public async estExitPool(
|
||||
address: string,
|
||||
poolAddress: string,
|
||||
poolAmountIn: string,
|
||||
minAmountsOut: string[],
|
||||
contractInstance?: Contract
|
||||
): Promise<number> {
|
||||
const poolContract =
|
||||
contractInstance ||
|
||||
setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress),
|
||||
this.config
|
||||
)
|
||||
|
||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||
let estGas
|
||||
try {
|
||||
estGas = await poolContract.methods
|
||||
.exitPool(poolAmountIn, minAmountsOut)
|
||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||
} catch (e) {
|
||||
estGas = gasLimitDefault
|
||||
}
|
||||
return estGas
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes dual side liquidity from the pool (both datatoken and basetoken)
|
||||
* Exit the pool, paying poolAmountIn pool tokens and getting some of each of the currently trading tokens in return.
|
||||
* These values are limited by the array of minAmountsOut in the order of the pool tokens.
|
||||
* @param {String} account
|
||||
* @param {String} poolAddress
|
||||
* @param {String} poolAmountIn amount of pool shares spent
|
||||
* @param {String[]} minAmountsOut array with minimum amount of tokens expected
|
||||
* @return {TransactionReceipt}
|
||||
*/
|
||||
async exitPool(
|
||||
account: string,
|
||||
poolAddress: string,
|
||||
poolAmountIn: string,
|
||||
minAmountsOut: string[]
|
||||
): Promise<TransactionReceipt> {
|
||||
const pool = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi, poolAddress),
|
||||
this.config
|
||||
)
|
||||
const weiMinAmountsOut = []
|
||||
const tokens = await this.getFinalTokens(poolAddress)
|
||||
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const amount = await amountToUnits(this.web3, tokens[i], minAmountsOut[i])
|
||||
weiMinAmountsOut.push(amount)
|
||||
}
|
||||
let result = null
|
||||
const estGas = await this.estExitPool(
|
||||
account,
|
||||
poolAddress,
|
||||
this.web3.utils.toWei(poolAmountIn),
|
||||
weiMinAmountsOut
|
||||
)
|
||||
|
||||
try {
|
||||
result = await pool.methods
|
||||
.exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut)
|
||||
.send({
|
||||
from: account,
|
||||
gas: estGas,
|
||||
gasPrice: await getFairGasPrice(this.web3, this.config)
|
||||
})
|
||||
} catch (e) {
|
||||
LoggerInstance.error(`ERROR: Failed to exit pool: ${e.message}`)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimate gas cost for joinswapExternAmountIn
|
||||
* @param {String} address
|
||||
@ -1292,13 +1153,15 @@ export class Pool {
|
||||
* @param {String} poolAddress
|
||||
* @param {String} tokenAmountIn exact number of base tokens to spend
|
||||
* @param {String} minPoolAmountOut minimum of pool shares expectex
|
||||
* @param {number} tokenInDecimals optional number of decimals of the token
|
||||
* @return {TransactionReceipt}
|
||||
*/
|
||||
async joinswapExternAmountIn(
|
||||
account: string,
|
||||
poolAddress: string,
|
||||
tokenAmountIn: string,
|
||||
minPoolAmountOut: string
|
||||
minPoolAmountOut: string,
|
||||
tokenInDecimals?: number
|
||||
): Promise<TransactionReceipt> {
|
||||
const pool = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi, poolAddress),
|
||||
@ -1311,7 +1174,11 @@ export class Pool {
|
||||
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
|
||||
}
|
||||
|
||||
const amountInFormatted = await amountToUnits(this.web3, tokenIn, tokenAmountIn)
|
||||
const amountInFormatted = await this.amountToUnits(
|
||||
tokenIn,
|
||||
tokenAmountIn,
|
||||
tokenInDecimals
|
||||
)
|
||||
const estGas = await this.estJoinswapExternAmountIn(
|
||||
account,
|
||||
poolAddress,
|
||||
@ -1380,13 +1247,15 @@ export class Pool {
|
||||
* @param {String} poolAddress
|
||||
* @param {String} poolAmountIn exact number of pool shares to spend
|
||||
* @param {String} minTokenAmountOut minimum amount of basetokens expected
|
||||
* @param {number} poolDecimals optional number of decimals of the poool
|
||||
* @return {TransactionReceipt}
|
||||
*/
|
||||
async exitswapPoolAmountIn(
|
||||
account: string,
|
||||
poolAddress: string,
|
||||
poolAmountIn: string,
|
||||
minTokenAmountOut: string
|
||||
minTokenAmountOut: string,
|
||||
poolDecimals?: number
|
||||
): Promise<TransactionReceipt> {
|
||||
const pool = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi, poolAddress),
|
||||
@ -1406,10 +1275,10 @@ export class Pool {
|
||||
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
|
||||
}
|
||||
|
||||
const minTokenOutFormatted = await amountToUnits(
|
||||
this.web3,
|
||||
const minTokenOutFormatted = await this.amountToUnits(
|
||||
await this.getBaseToken(poolAddress),
|
||||
minTokenAmountOut
|
||||
minTokenAmountOut,
|
||||
poolDecimals
|
||||
)
|
||||
const estGas = await this.estExitswapPoolAmountIn(
|
||||
account,
|
||||
@ -1506,13 +1375,17 @@ export class Pool {
|
||||
* @param tokenOut token to get
|
||||
* @param tokenAmountOut exact amount of tokenOut
|
||||
* @param swapMarketFee consume market swap fee
|
||||
* @param {number} tokenInDecimals optional number of decimals of the token to be swaped
|
||||
* @param {number} tokenOutDecimals optional number of decimals of the token to get
|
||||
*/
|
||||
public async getAmountInExactOut(
|
||||
poolAddress: string,
|
||||
tokenIn: string,
|
||||
tokenOut: string,
|
||||
tokenAmountOut: string,
|
||||
swapMarketFee: string
|
||||
swapMarketFee: string,
|
||||
tokenInDecimals?: number,
|
||||
tokenOutDecimals?: number
|
||||
): Promise<PoolPriceAndFees> {
|
||||
const pool = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi, poolAddress),
|
||||
@ -1525,7 +1398,11 @@ export class Pool {
|
||||
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
|
||||
}
|
||||
|
||||
const amountOutFormatted = await amountToUnits(this.web3, tokenOut, tokenAmountOut)
|
||||
const amountOutFormatted = await this.amountToUnits(
|
||||
tokenOut,
|
||||
tokenAmountOut,
|
||||
tokenOutDecimals
|
||||
)
|
||||
|
||||
let amount = null
|
||||
|
||||
@ -1539,22 +1416,30 @@ export class Pool {
|
||||
)
|
||||
.call()
|
||||
amount = {
|
||||
tokenAmount: await unitsToAmount(this.web3, tokenOut, result.tokenAmountIn),
|
||||
liquidityProviderSwapFeeAmount: await unitsToAmount(
|
||||
this.web3,
|
||||
tokenIn,
|
||||
result.lpFeeAmount
|
||||
tokenAmount: await this.unitsToAmount(
|
||||
tokenOut,
|
||||
result.tokenAmountIn,
|
||||
tokenOutDecimals
|
||||
),
|
||||
oceanFeeAmount: await unitsToAmount(this.web3, tokenIn, result.oceanFeeAmount),
|
||||
publishMarketSwapFeeAmount: await unitsToAmount(
|
||||
this.web3,
|
||||
liquidityProviderSwapFeeAmount: await this.unitsToAmount(
|
||||
tokenIn,
|
||||
result.publishMarketSwapFeeAmount
|
||||
result.lpFeeAmount,
|
||||
tokenInDecimals
|
||||
),
|
||||
consumeMarketSwapFeeAmount: await unitsToAmount(
|
||||
this.web3,
|
||||
oceanFeeAmount: await this.unitsToAmount(
|
||||
tokenIn,
|
||||
result.consumeMarketSwapFeeAmount
|
||||
result.oceanFeeAmount,
|
||||
tokenInDecimals
|
||||
),
|
||||
publishMarketSwapFeeAmount: await this.unitsToAmount(
|
||||
tokenIn,
|
||||
result.publishMarketSwapFeeAmount,
|
||||
tokenInDecimals
|
||||
),
|
||||
consumeMarketSwapFeeAmount: await this.unitsToAmount(
|
||||
tokenIn,
|
||||
result.consumeMarketSwapFeeAmount,
|
||||
tokenInDecimals
|
||||
)
|
||||
}
|
||||
} catch (e) {
|
||||
@ -1568,15 +1453,19 @@ export class Pool {
|
||||
* Returns: tokenAmountOut, LPFee, opcFee , publishMarketSwapFee, consumeMarketSwapFee
|
||||
* @param tokenIn token to be swaped
|
||||
* @param tokenOut token to get
|
||||
* @param tokenAmountOut exact amount of tokenOut
|
||||
* @param _consumeMarketSwapFee consume market swap fee
|
||||
* @param tokenAmountIn exact amount of tokenIn
|
||||
* @param swapMarketFee
|
||||
* @param {number} tokenInDecimals optional number of decimals of the token to be swaped
|
||||
* @param {number} tokenOutDecimals optional number of decimals of the token to get
|
||||
*/
|
||||
public async getAmountOutExactIn(
|
||||
poolAddress: string,
|
||||
tokenIn: string,
|
||||
tokenOut: string,
|
||||
tokenAmountIn: string,
|
||||
swapMarketFee: string
|
||||
swapMarketFee: string,
|
||||
tokenInDecimals?: number,
|
||||
tokenOutDecimals?: number
|
||||
): Promise<PoolPriceAndFees> {
|
||||
const pool = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi, poolAddress),
|
||||
@ -1588,7 +1477,11 @@ export class Pool {
|
||||
throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`)
|
||||
}
|
||||
|
||||
const amountInFormatted = await amountToUnits(this.web3, tokenIn, tokenAmountIn)
|
||||
const amountInFormatted = await this.amountToUnits(
|
||||
tokenIn,
|
||||
tokenAmountIn,
|
||||
tokenInDecimals
|
||||
)
|
||||
|
||||
let amount = null
|
||||
|
||||
@ -1603,22 +1496,30 @@ export class Pool {
|
||||
.call()
|
||||
|
||||
amount = {
|
||||
tokenAmount: await unitsToAmount(this.web3, tokenOut, result.tokenAmountOut),
|
||||
liquidityProviderSwapFeeAmount: await unitsToAmount(
|
||||
this.web3,
|
||||
tokenIn,
|
||||
result.lpFeeAmount
|
||||
tokenAmount: await this.unitsToAmount(
|
||||
tokenOut,
|
||||
result.tokenAmountOut,
|
||||
tokenOutDecimals
|
||||
),
|
||||
oceanFeeAmount: await unitsToAmount(this.web3, tokenIn, result.oceanFeeAmount),
|
||||
publishMarketSwapFeeAmount: await unitsToAmount(
|
||||
this.web3,
|
||||
liquidityProviderSwapFeeAmount: await this.unitsToAmount(
|
||||
tokenIn,
|
||||
result.publishMarketSwapFeeAmount
|
||||
result.lpFeeAmount,
|
||||
tokenInDecimals
|
||||
),
|
||||
consumeMarketSwapFeeAmount: await unitsToAmount(
|
||||
this.web3,
|
||||
oceanFeeAmount: await this.unitsToAmount(
|
||||
tokenIn,
|
||||
result.consumeMarketSwapFeeAmount
|
||||
result.oceanFeeAmount,
|
||||
tokenInDecimals
|
||||
),
|
||||
publishMarketSwapFeeAmount: await this.unitsToAmount(
|
||||
tokenIn,
|
||||
result.publishMarketSwapFeeAmount,
|
||||
tokenInDecimals
|
||||
),
|
||||
consumeMarketSwapFeeAmount: await this.unitsToAmount(
|
||||
tokenIn,
|
||||
result.consumeMarketSwapFeeAmount,
|
||||
tokenInDecimals
|
||||
)
|
||||
}
|
||||
} catch (e) {
|
||||
@ -1631,11 +1532,15 @@ export class Pool {
|
||||
* Returns number of poolshares obtain by staking exact tokenAmountIn tokens
|
||||
* @param tokenIn tokenIn
|
||||
* @param tokenAmountIn exact number of tokens staked
|
||||
* @param {number} poolDecimals optional number of decimals of the poool
|
||||
* @param {number} tokenInDecimals optional number of decimals of the token
|
||||
*/
|
||||
public async calcPoolOutGivenSingleIn(
|
||||
poolAddress: string,
|
||||
tokenIn: string,
|
||||
tokenAmountIn: string
|
||||
tokenAmountIn: string,
|
||||
poolDecimals?: number,
|
||||
tokenInDecimals?: number
|
||||
): Promise<string> {
|
||||
const pool = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi, poolAddress),
|
||||
@ -1647,11 +1552,11 @@ export class Pool {
|
||||
const result = await pool.methods
|
||||
.calcPoolOutSingleIn(
|
||||
tokenIn,
|
||||
await amountToUnits(this.web3, tokenIn, tokenAmountIn)
|
||||
await this.amountToUnits(tokenIn, tokenAmountIn, tokenInDecimals)
|
||||
)
|
||||
.call()
|
||||
|
||||
amount = await unitsToAmount(this.web3, poolAddress, result)
|
||||
amount = await this.unitsToAmount(poolAddress, result, poolDecimals)
|
||||
} catch (e) {
|
||||
LoggerInstance.error(
|
||||
`ERROR: Failed to calculate PoolOutGivenSingleIn : ${e.message}`
|
||||
@ -1664,25 +1569,32 @@ export class Pool {
|
||||
* Returns number of tokens to be staked to the pool in order to get an exact number of poolshares
|
||||
* @param tokenIn tokenIn
|
||||
* @param poolAmountOut expected amount of pool shares
|
||||
* @param {number} poolDecimals optional number of decimals of the pool
|
||||
* @param {number} tokenInDecimals optional number of decimals of the token
|
||||
*/
|
||||
public async calcSingleInGivenPoolOut(
|
||||
poolAddress: string,
|
||||
tokenIn: string,
|
||||
poolAmountOut: string
|
||||
poolAmountOut: string,
|
||||
poolDecimals?: number,
|
||||
tokenInDecimals?: number
|
||||
): Promise<string> {
|
||||
const pool = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi, poolAddress),
|
||||
this.config
|
||||
)
|
||||
let amount = null
|
||||
const amountFormatted = await amountToUnits(this.web3, poolAddress, poolAmountOut)
|
||||
const amountFormatted = await this.amountToUnits(
|
||||
poolAddress,
|
||||
poolAmountOut,
|
||||
poolDecimals
|
||||
)
|
||||
try {
|
||||
const result = await pool.methods
|
||||
.calcSingleInPoolOut(tokenIn, amountFormatted)
|
||||
|
||||
.call()
|
||||
|
||||
amount = await unitsToAmount(this.web3, tokenIn, result)
|
||||
amount = await this.unitsToAmount(tokenIn, result, tokenInDecimals)
|
||||
} catch (e) {
|
||||
LoggerInstance.error(
|
||||
`ERROR: Failed to calculate SingleInGivenPoolOut : ${e.message}`
|
||||
@ -1695,11 +1607,15 @@ export class Pool {
|
||||
* Returns expected amount of tokenOut for removing exact poolAmountIn pool shares from the pool
|
||||
* @param tokenOut tokenOut
|
||||
* @param poolAmountIn amount of shares spent
|
||||
* @param {number} poolDecimals optional number of decimals of the pool
|
||||
* @param {number} tokenOutDecimals optional number of decimals of the token
|
||||
*/
|
||||
public async calcSingleOutGivenPoolIn(
|
||||
poolAddress: string,
|
||||
tokenOut: string,
|
||||
poolAmountIn: string
|
||||
poolAmountIn: string,
|
||||
poolDecimals?: number,
|
||||
tokenOutDecimals?: number
|
||||
): Promise<string> {
|
||||
const pool = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi, poolAddress),
|
||||
@ -1711,10 +1627,10 @@ export class Pool {
|
||||
const result = await pool.methods
|
||||
.calcSingleOutPoolIn(
|
||||
tokenOut,
|
||||
await amountToUnits(this.web3, poolAddress, poolAmountIn)
|
||||
await this.amountToUnits(poolAddress, poolAmountIn, poolDecimals)
|
||||
)
|
||||
.call()
|
||||
amount = await unitsToAmount(this.web3, tokenOut, result)
|
||||
amount = await this.unitsToAmount(tokenOut, result, tokenOutDecimals)
|
||||
} catch (e) {
|
||||
LoggerInstance.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${e}`)
|
||||
}
|
||||
@ -1725,11 +1641,15 @@ export class Pool {
|
||||
* Returns number of poolshares needed to withdraw exact tokenAmountOut tokens
|
||||
* @param tokenOut tokenOut
|
||||
* @param tokenAmountOut expected amount of tokensOut
|
||||
* @param {number} poolDecimals optional number of decimals of the pool
|
||||
* @param {number} tokenOutDecimals optional number of decimals of the token
|
||||
*/
|
||||
public async calcPoolInGivenSingleOut(
|
||||
poolAddress: string,
|
||||
tokenOut: string,
|
||||
tokenAmountOut: string
|
||||
tokenAmountOut: string,
|
||||
poolDecimals?: number,
|
||||
tokenOutDecimals?: number
|
||||
): Promise<string> {
|
||||
const pool = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.poolAbi, poolAddress),
|
||||
@ -1741,11 +1661,11 @@ export class Pool {
|
||||
const result = await pool.methods
|
||||
.calcPoolInSingleOut(
|
||||
tokenOut,
|
||||
await amountToUnits(this.web3, tokenOut, tokenAmountOut)
|
||||
await this.amountToUnits(tokenOut, tokenAmountOut, tokenOutDecimals)
|
||||
)
|
||||
.call()
|
||||
|
||||
amount = await unitsToAmount(this.web3, poolAddress, result)
|
||||
amount = await this.unitsToAmount(poolAddress, result, poolDecimals)
|
||||
} catch (e) {
|
||||
LoggerInstance.error(
|
||||
`ERROR: Failed to calculate PoolInGivenSingleOut : ${e.message}`
|
||||
|
@ -7,8 +7,8 @@ import defaultDispenserAbi from '@oceanprotocol/contracts/artifacts/contracts/po
|
||||
import {
|
||||
LoggerInstance as logger,
|
||||
getFairGasPrice,
|
||||
configHelperNetworks,
|
||||
setContractDefaults
|
||||
setContractDefaults,
|
||||
ConfigHelper
|
||||
} from '../../utils/'
|
||||
import { Datatoken } from '../../tokens'
|
||||
import { Config } from '../../models/index.js'
|
||||
@ -39,6 +39,7 @@ export class Dispenser {
|
||||
*/
|
||||
constructor(
|
||||
web3: Web3,
|
||||
network?: string | number,
|
||||
dispenserAddress: string = null,
|
||||
dispenserAbi: AbiItem | AbiItem[] = null,
|
||||
config?: Config
|
||||
@ -46,7 +47,7 @@ export class Dispenser {
|
||||
this.web3 = web3
|
||||
this.dispenserAddress = dispenserAddress
|
||||
this.dispenserAbi = dispenserAbi || (defaultDispenserAbi.abi as AbiItem[])
|
||||
this.config = config || configHelperNetworks[0]
|
||||
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
|
||||
if (web3)
|
||||
this.dispenserContract = setContractDefaults(
|
||||
new this.web3.eth.Contract(this.dispenserAbi, this.dispenserAddress),
|
||||
|
@ -6,11 +6,11 @@ import Web3 from 'web3'
|
||||
import {
|
||||
LoggerInstance,
|
||||
getFairGasPrice,
|
||||
configHelperNetworks,
|
||||
setContractDefaults,
|
||||
amountToUnits,
|
||||
unitsToAmount,
|
||||
ZERO_ADDRESS
|
||||
ZERO_ADDRESS,
|
||||
ConfigHelper
|
||||
} from '../../utils'
|
||||
import { Config } from '../../models/index.js'
|
||||
import { PriceAndFees } from '../..'
|
||||
@ -75,12 +75,13 @@ export class FixedRateExchange {
|
||||
constructor(
|
||||
web3: Web3,
|
||||
fixedRateAddress: string,
|
||||
network?: string | number,
|
||||
fixedRateExchangeAbi: AbiItem | AbiItem[] = null,
|
||||
oceanAddress: string = null,
|
||||
config?: Config
|
||||
) {
|
||||
this.web3 = web3
|
||||
this.config = config || configHelperNetworks[0]
|
||||
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
|
||||
this.fixedRateExchangeAbi =
|
||||
fixedRateExchangeAbi || (defaultFixedRateExchangeAbi.abi as AbiItem[])
|
||||
this.oceanAddress = oceanAddress
|
||||
@ -91,12 +92,20 @@ export class FixedRateExchange {
|
||||
)
|
||||
}
|
||||
|
||||
async amountToUnits(token: string, amount: string): Promise<string> {
|
||||
return amountToUnits(this.web3, token, amount)
|
||||
async amountToUnits(
|
||||
token: string,
|
||||
amount: string,
|
||||
tokenDecimals: number
|
||||
): Promise<string> {
|
||||
return amountToUnits(this.web3, token, amount, tokenDecimals)
|
||||
}
|
||||
|
||||
async unitsToAmount(token: string, amount: string): Promise<string> {
|
||||
return unitsToAmount(this.web3, token, amount)
|
||||
async unitsToAmount(
|
||||
token: string,
|
||||
amount: string,
|
||||
tokenDecimals: number
|
||||
): Promise<string> {
|
||||
return unitsToAmount(this.web3, token, amount, tokenDecimals)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -172,11 +181,13 @@ export class FixedRateExchange {
|
||||
const consumeMarketFeeFormatted = this.web3.utils.toWei(consumeMarketFee)
|
||||
const dtAmountFormatted = await this.amountToUnits(
|
||||
exchange.datatoken,
|
||||
datatokenAmount
|
||||
datatokenAmount,
|
||||
+exchange.dtDecimals
|
||||
)
|
||||
const maxBtFormatted = await this.amountToUnits(
|
||||
exchange.baseToken,
|
||||
maxBaseTokenAmount
|
||||
maxBaseTokenAmount,
|
||||
+exchange.btDecimals
|
||||
)
|
||||
|
||||
const estGas = await this.estBuyDT(
|
||||
@ -268,11 +279,13 @@ export class FixedRateExchange {
|
||||
const consumeMarketFeeFormatted = this.web3.utils.toWei(consumeMarketFee)
|
||||
const dtAmountFormatted = await this.amountToUnits(
|
||||
exchange.datatoken,
|
||||
datatokenAmount
|
||||
datatokenAmount,
|
||||
+exchange.dtDecimals
|
||||
)
|
||||
const minBtFormatted = await this.amountToUnits(
|
||||
exchange.baseToken,
|
||||
minBaseTokenAmount
|
||||
minBaseTokenAmount,
|
||||
+exchange.btDecimals
|
||||
)
|
||||
const estGas = await this.estBuyDT(
|
||||
address,
|
||||
@ -530,12 +543,8 @@ export class FixedRateExchange {
|
||||
*/
|
||||
public async getDTSupply(exchangeId: string): Promise<string> {
|
||||
const dtSupply = await this.contract.methods.getDTSupply(exchangeId).call()
|
||||
return await this.unitsToAmount(
|
||||
(
|
||||
await this.getExchange(exchangeId)
|
||||
).datatoken,
|
||||
dtSupply
|
||||
)
|
||||
const exchange = await this.getExchange(exchangeId)
|
||||
return await this.unitsToAmount(exchange.datatoken, dtSupply, +exchange.dtDecimals)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -545,12 +554,8 @@ export class FixedRateExchange {
|
||||
*/
|
||||
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
|
||||
)
|
||||
const exchange = await this.getExchange(exchangeId)
|
||||
return await this.unitsToAmount(exchange.baseToken, btSupply, +exchange.btDecimals)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -578,7 +583,11 @@ export class FixedRateExchange {
|
||||
const result = await this.contract.methods
|
||||
.calcBaseInGivenOutDT(
|
||||
exchangeId,
|
||||
await this.amountToUnits(fixedRateExchange.datatoken, datatokenAmount),
|
||||
await this.amountToUnits(
|
||||
fixedRateExchange.datatoken,
|
||||
datatokenAmount,
|
||||
+fixedRateExchange.dtDecimals
|
||||
),
|
||||
this.web3.utils.toWei(consumeMarketFee)
|
||||
)
|
||||
.call()
|
||||
@ -586,19 +595,23 @@ export class FixedRateExchange {
|
||||
const priceAndFees = {
|
||||
baseTokenAmount: await this.unitsToAmount(
|
||||
fixedRateExchange.baseToken,
|
||||
result.baseTokenAmount
|
||||
result.baseTokenAmount,
|
||||
+fixedRateExchange.btDecimals
|
||||
),
|
||||
marketFeeAmount: await this.unitsToAmount(
|
||||
fixedRateExchange.baseToken,
|
||||
result.marketFeeAmount
|
||||
result.marketFeeAmount,
|
||||
+fixedRateExchange.btDecimals
|
||||
),
|
||||
oceanFeeAmount: await this.unitsToAmount(
|
||||
fixedRateExchange.baseToken,
|
||||
result.oceanFeeAmount
|
||||
result.oceanFeeAmount,
|
||||
+fixedRateExchange.btDecimals
|
||||
),
|
||||
consumeMarketFeeAmount: await this.unitsToAmount(
|
||||
fixedRateExchange.baseToken,
|
||||
result.consumeMarketFeeAmount
|
||||
result.consumeMarketFeeAmount,
|
||||
+fixedRateExchange.btDecimals
|
||||
)
|
||||
} as PriceAndFees
|
||||
return priceAndFees
|
||||
@ -620,17 +633,16 @@ export class FixedRateExchange {
|
||||
const result = await this.contract.methods
|
||||
.calcBaseOutGivenInDT(
|
||||
exchangeId,
|
||||
await this.amountToUnits(exchange.datatoken, datatokenAmount),
|
||||
await this.amountToUnits(
|
||||
exchange.datatoken,
|
||||
datatokenAmount,
|
||||
+exchange.dtDecimals
|
||||
),
|
||||
this.web3.utils.toWei(consumeMarketFee)
|
||||
)
|
||||
.call()
|
||||
|
||||
return await this.unitsToAmount(
|
||||
(
|
||||
await this.getExchange(exchangeId)
|
||||
).baseToken,
|
||||
result[0]
|
||||
)
|
||||
return await this.unitsToAmount(exchange.baseToken, result[0], +exchange.btDecimals)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -644,10 +656,26 @@ export class FixedRateExchange {
|
||||
.call()
|
||||
result.dtDecimals = result.dtDecimals.toString()
|
||||
result.btDecimals = result.btDecimals.toString()
|
||||
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.dtBalance = await this.unitsToAmount(
|
||||
result.datatoken,
|
||||
result.dtBalance,
|
||||
+result.dtDecimals
|
||||
)
|
||||
result.btBalance = await this.unitsToAmount(
|
||||
result.baseToken,
|
||||
result.btBalance,
|
||||
+result.btDecimals
|
||||
)
|
||||
result.dtSupply = await this.unitsToAmount(
|
||||
result.datatoken,
|
||||
result.dtSupply,
|
||||
+result.dtDecimals
|
||||
)
|
||||
result.btSupply = await this.unitsToAmount(
|
||||
result.baseToken,
|
||||
result.btSupply,
|
||||
+result.btDecimals
|
||||
)
|
||||
result.fixedRate = this.web3.utils.fromWei(result.fixedRate)
|
||||
result.exchangeId = exchangeId
|
||||
return result
|
||||
@ -663,17 +691,16 @@ export class FixedRateExchange {
|
||||
result.opcFee = this.web3.utils.fromWei(result.opcFee.toString())
|
||||
result.marketFee = this.web3.utils.fromWei(result.marketFee.toString())
|
||||
|
||||
const exchange = await this.getExchange(exchangeId)
|
||||
result.marketFeeAvailable = await this.unitsToAmount(
|
||||
(
|
||||
await this.getExchange(exchangeId)
|
||||
).baseToken,
|
||||
result.marketFeeAvailable
|
||||
exchange.baseToken,
|
||||
result.marketFeeAvailable,
|
||||
+exchange.btDecimals
|
||||
)
|
||||
result.oceanFeeAvailable = await this.unitsToAmount(
|
||||
(
|
||||
await this.getExchange(exchangeId)
|
||||
).baseToken,
|
||||
result.oceanFeeAvailable
|
||||
exchange.baseToken,
|
||||
result.oceanFeeAvailable,
|
||||
+exchange.btDecimals
|
||||
)
|
||||
|
||||
result.exchangeId = exchangeId
|
||||
@ -821,7 +848,11 @@ export class FixedRateExchange {
|
||||
const fixedrate: FixedPriceExchange = await this.contract.methods
|
||||
.getExchange(exchangeId)
|
||||
.call()
|
||||
const amountWei = await this.amountToUnits(fixedrate.baseToken, amount)
|
||||
const amountWei = await this.amountToUnits(
|
||||
fixedrate.baseToken,
|
||||
amount,
|
||||
+fixedrate.btDecimals
|
||||
)
|
||||
try {
|
||||
estGas = await fixedRate.methods
|
||||
.collectBT(exchangeId, amountWei)
|
||||
@ -851,7 +882,11 @@ export class FixedRateExchange {
|
||||
const fixedrate: FixedPriceExchange = await this.contract.methods
|
||||
.getExchange(exchangeId)
|
||||
.call()
|
||||
const amountWei = await this.amountToUnits(fixedrate.baseToken, amount)
|
||||
const amountWei = await this.amountToUnits(
|
||||
fixedrate.baseToken,
|
||||
amount,
|
||||
+fixedrate.btDecimals
|
||||
)
|
||||
const trxReceipt = await this.contract.methods.collectBT(exchangeId, amountWei).send({
|
||||
from: address,
|
||||
gas: estGas + 1,
|
||||
@ -880,7 +915,11 @@ export class FixedRateExchange {
|
||||
const fixedrate: FixedPriceExchange = await this.contract.methods
|
||||
.getExchange(exchangeId)
|
||||
.call()
|
||||
const amountWei = await this.amountToUnits(fixedrate.datatoken, amount)
|
||||
const amountWei = await this.amountToUnits(
|
||||
fixedrate.datatoken,
|
||||
amount,
|
||||
+fixedrate.dtDecimals
|
||||
)
|
||||
try {
|
||||
estGas = await fixedRate.methods
|
||||
.collectDT(exchangeId, amountWei)
|
||||
@ -910,7 +949,11 @@ export class FixedRateExchange {
|
||||
const fixedrate: FixedPriceExchange = await this.contract.methods
|
||||
.getExchange(exchangeId)
|
||||
.call()
|
||||
const amountWei = await this.amountToUnits(fixedrate.datatoken, amount)
|
||||
const amountWei = await this.amountToUnits(
|
||||
fixedrate.datatoken,
|
||||
amount,
|
||||
+fixedrate.dtDecimals
|
||||
)
|
||||
const trxReceipt = await this.contract.methods.collectDT(exchangeId, amountWei).send({
|
||||
from: address,
|
||||
gas: estGas + 1,
|
||||
|
@ -2,10 +2,8 @@ import Web3 from 'web3'
|
||||
import { AbiItem } from 'web3-utils/types'
|
||||
import { TransactionReceipt } from 'web3-core'
|
||||
import { Contract } from 'web3-eth-contract'
|
||||
import { LoggerInstance, getFairGasPrice, configHelperNetworks } from '../../utils'
|
||||
import BigNumber from 'bignumber.js'
|
||||
import { LoggerInstance, getFairGasPrice, ConfigHelper, unitsToAmount } from '../../utils'
|
||||
import SideStakingTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json'
|
||||
import defaultErc20Abi from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
|
||||
import { Config } from '../../models'
|
||||
|
||||
export class SideStaking {
|
||||
@ -14,45 +12,24 @@ export class SideStaking {
|
||||
public GASLIMIT_DEFAULT = 1000000
|
||||
public config: Config
|
||||
|
||||
constructor(web3: Web3, ssAbi: AbiItem | AbiItem[] = null, config?: Config) {
|
||||
constructor(
|
||||
web3: Web3,
|
||||
network?: string | number,
|
||||
ssAbi: AbiItem | AbiItem[] = null,
|
||||
config?: Config
|
||||
) {
|
||||
if (ssAbi) this.ssAbi = ssAbi
|
||||
else this.ssAbi = SideStakingTemplate.abi as AbiItem[]
|
||||
this.web3 = web3
|
||||
this.config = config || configHelperNetworks[0]
|
||||
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
|
||||
}
|
||||
|
||||
async amountToUnits(token: string, amount: string): Promise<string> {
|
||||
let decimals = 18
|
||||
const tokenContract = new this.web3.eth.Contract(
|
||||
defaultErc20Abi.abi as AbiItem[],
|
||||
token
|
||||
)
|
||||
try {
|
||||
decimals = await tokenContract.methods.decimals().call()
|
||||
} catch (e) {
|
||||
LoggerInstance.error('ERROR: FAILED TO CALL DECIMALS(), USING 18')
|
||||
}
|
||||
|
||||
const amountFormatted = new BigNumber(parseInt(amount) * 10 ** decimals)
|
||||
|
||||
return amountFormatted.toString()
|
||||
}
|
||||
|
||||
async unitsToAmount(token: string, amount: string): Promise<string> {
|
||||
let decimals = 18
|
||||
const tokenContract = new this.web3.eth.Contract(
|
||||
defaultErc20Abi.abi as AbiItem[],
|
||||
token
|
||||
)
|
||||
try {
|
||||
decimals = await tokenContract.methods.decimals().call()
|
||||
} catch (e) {
|
||||
LoggerInstance.error('ERROR: FAILED TO CALL DECIMALS(), USING 18')
|
||||
}
|
||||
|
||||
const amountFormatted = new BigNumber(parseInt(amount) / 10 ** decimals)
|
||||
|
||||
return amountFormatted.toString()
|
||||
async unitsToAmount(
|
||||
token: string,
|
||||
amount: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
return unitsToAmount(this.web3, token, amount, tokenDecimals)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -178,11 +155,13 @@ export class SideStaking {
|
||||
* Get dt balance in the staking contract available for being added as liquidity
|
||||
* @param {String} ssAddress side staking contract address
|
||||
* @param {String} datatokenAddress datatokenAddress
|
||||
* @param {number} tokenDecimals optional number of decimals of the token
|
||||
* @return {String}
|
||||
*/
|
||||
async getDatatokenBalance(
|
||||
ssAddress: string,
|
||||
datatokenAddress: string
|
||||
datatokenAddress: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
const sideStaking = new this.web3.eth.Contract(this.ssAbi, ssAddress)
|
||||
let result = null
|
||||
@ -191,7 +170,7 @@ export class SideStaking {
|
||||
} catch (e) {
|
||||
LoggerInstance.error(`ERROR: Failed to get: ${e.message}`)
|
||||
}
|
||||
result = await this.unitsToAmount(datatokenAddress, result)
|
||||
result = await this.unitsToAmount(datatokenAddress, result, tokenDecimals)
|
||||
return result
|
||||
}
|
||||
|
||||
@ -216,9 +195,14 @@ export class SideStaking {
|
||||
* Get total amount vesting
|
||||
* @param {String} ssAddress side staking contract address
|
||||
* @param {String} datatokenAddress datatokenAddress
|
||||
* @param {number} tokenDecimals optional number of decimals of the token
|
||||
* @return {String}
|
||||
*/
|
||||
async getvestingAmount(ssAddress: string, datatokenAddress: string): Promise<string> {
|
||||
async getvestingAmount(
|
||||
ssAddress: string,
|
||||
datatokenAddress: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
const sideStaking = new this.web3.eth.Contract(this.ssAbi, ssAddress)
|
||||
let result = null
|
||||
try {
|
||||
@ -226,7 +210,7 @@ export class SideStaking {
|
||||
} catch (e) {
|
||||
LoggerInstance.error(`ERROR: Failed to get: ${e.message}`)
|
||||
}
|
||||
result = await this.unitsToAmount(datatokenAddress, result)
|
||||
result = await this.unitsToAmount(datatokenAddress, result, tokenDecimals)
|
||||
return result
|
||||
}
|
||||
|
||||
@ -254,11 +238,13 @@ export class SideStaking {
|
||||
* Get how much has been taken from the vesting amount
|
||||
* @param {String} ssAddress side staking contract address
|
||||
* @param {String} datatokenAddress datatokenAddress
|
||||
* @param {number} tokenDecimals optional number of decimals of the token
|
||||
* @return {String}
|
||||
*/
|
||||
async getvestingAmountSoFar(
|
||||
ssAddress: string,
|
||||
datatokenAddress: string
|
||||
datatokenAddress: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
const sideStaking = new this.web3.eth.Contract(this.ssAbi, ssAddress)
|
||||
let result = null
|
||||
@ -267,7 +253,7 @@ export class SideStaking {
|
||||
} catch (e) {
|
||||
LoggerInstance.error(`ERROR: Failed to get: ${e.message}`)
|
||||
}
|
||||
result = await this.unitsToAmount(datatokenAddress, result)
|
||||
result = await this.unitsToAmount(datatokenAddress, result, tokenDecimals)
|
||||
return result
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,8 @@ import {
|
||||
ComputeAlgorithm,
|
||||
ComputeAsset,
|
||||
ComputeEnvironment,
|
||||
ProviderInitialize
|
||||
ProviderInitialize,
|
||||
ProviderComputeInitializeResults
|
||||
} from '../@types/'
|
||||
import { noZeroX } from '../utils/ConversionTypeHelper'
|
||||
import fetch from 'cross-fetch'
|
||||
@ -327,6 +328,60 @@ export class Provider {
|
||||
}
|
||||
}
|
||||
|
||||
/** Initialize a compute request.
|
||||
* @param {ComputeAsset} assets
|
||||
* @param {ComputeAlgorithmber} algorithm
|
||||
* @param {string} computeEnv
|
||||
* @param {number} validUntil
|
||||
* @param {string} providerUri Identifier of the asset to be registered in ocean
|
||||
* @param {string} accountId
|
||||
* @param {AbortSignal} signal abort signal
|
||||
* @return {Promise<ProviderComputeInitialize>} ProviderComputeInitialize data
|
||||
*/
|
||||
public async initializeCompute(
|
||||
assets: ComputeAsset[],
|
||||
algorithm: ComputeAlgorithm,
|
||||
computeEnv: string,
|
||||
validUntil: number,
|
||||
providerUri: string,
|
||||
accountId: string,
|
||||
signal?: AbortSignal
|
||||
): Promise<ProviderComputeInitializeResults> {
|
||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||
const serviceEndpoints = await this.getServiceEndpoints(
|
||||
providerUri,
|
||||
providerEndpoints
|
||||
)
|
||||
const providerData = {
|
||||
datasets: assets,
|
||||
algorithm: algorithm,
|
||||
compute: {
|
||||
env: computeEnv,
|
||||
validUntil: validUntil
|
||||
},
|
||||
consumerAddress: accountId
|
||||
}
|
||||
const initializeUrl = this.getEndpointURL(serviceEndpoints, 'initializeCompute')
|
||||
? this.getEndpointURL(serviceEndpoints, 'initializeCompute').urlPath
|
||||
: null
|
||||
if (!initializeUrl) return null
|
||||
try {
|
||||
const response = await fetch(initializeUrl, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(providerData),
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
signal: signal
|
||||
})
|
||||
const results = await response.json()
|
||||
return results
|
||||
} catch (e) {
|
||||
LoggerInstance.error(e)
|
||||
throw new Error('ComputeJob cannot be initialized')
|
||||
}
|
||||
}
|
||||
|
||||
/** Gets fully signed URL for download
|
||||
* @param {string} did
|
||||
* @param {string} accountId
|
||||
|
@ -9,9 +9,9 @@ import {
|
||||
LoggerInstance,
|
||||
getFairGasPrice,
|
||||
setContractDefaults,
|
||||
configHelperNetworks,
|
||||
getFreOrderParams,
|
||||
ZERO_ADDRESS
|
||||
ZERO_ADDRESS,
|
||||
ConfigHelper
|
||||
} from '../utils'
|
||||
import {
|
||||
ConsumeMarketFee,
|
||||
@ -61,6 +61,7 @@ export class Datatoken {
|
||||
*/
|
||||
constructor(
|
||||
web3: Web3,
|
||||
network?: string | number,
|
||||
datatokensAbi?: AbiItem | AbiItem[],
|
||||
datatokensEnterpriseAbi?: AbiItem | AbiItem[],
|
||||
config?: Config
|
||||
@ -69,7 +70,7 @@ export class Datatoken {
|
||||
this.datatokensAbi = datatokensAbi || (defaultDatatokensAbi.abi as AbiItem[])
|
||||
this.datatokensEnterpriseAbi =
|
||||
datatokensEnterpriseAbi || (defaultDatatokensEnterpriseAbi.abi as AbiItem[])
|
||||
this.config = config || configHelperNetworks[0]
|
||||
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
|
||||
this.nft = new Nft(this.web3)
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@ import {
|
||||
getFairGasPrice,
|
||||
generateDtName,
|
||||
setContractDefaults,
|
||||
configHelperNetworks
|
||||
ConfigHelper
|
||||
} from '../utils'
|
||||
import { Contract } from 'web3-eth-contract'
|
||||
import { MetadataProof } from '../../src/@types'
|
||||
@ -33,10 +33,15 @@ export class Nft {
|
||||
public startBlock: number
|
||||
public config: Config
|
||||
|
||||
constructor(web3: Web3, nftAbi?: AbiItem | AbiItem[], config?: Config) {
|
||||
constructor(
|
||||
web3: Web3,
|
||||
network?: string | number,
|
||||
nftAbi?: AbiItem | AbiItem[],
|
||||
config?: Config
|
||||
) {
|
||||
this.nftAbi = nftAbi || (defaultNftAbi.abi as AbiItem[])
|
||||
this.web3 = web3
|
||||
this.config = config || configHelperNetworks[0]
|
||||
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,11 +120,12 @@ export async function getPoolCreationParams(
|
||||
export async function unitsToAmount(
|
||||
web3: Web3,
|
||||
token: string,
|
||||
amount: string
|
||||
amount: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
try {
|
||||
const tokenContract = new web3.eth.Contract(minAbi, token)
|
||||
let decimals = await tokenContract.methods.decimals().call()
|
||||
let decimals = tokenDecimals || (await tokenContract.methods.decimals().call())
|
||||
if (decimals === '0') {
|
||||
decimals = 18
|
||||
}
|
||||
@ -143,11 +144,12 @@ export async function unitsToAmount(
|
||||
export async function amountToUnits(
|
||||
web3: Web3,
|
||||
token: string,
|
||||
amount: string
|
||||
amount: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
try {
|
||||
const tokenContract = new web3.eth.Contract(minAbi, token)
|
||||
let decimals = await tokenContract.methods.decimals().call()
|
||||
let decimals = tokenDecimals || (await tokenContract.methods.decimals().call())
|
||||
if (decimals === '0') {
|
||||
decimals = 18
|
||||
}
|
||||
|
@ -46,7 +46,8 @@ export async function estApprove(
|
||||
* @param {String} tokenAddress
|
||||
* @param {String} spender
|
||||
* @param {String} amount (always expressed as wei)
|
||||
* @param {String} force if true, will overwrite any previous allowence. Else, will check if allowence is enough and will not send a transaction if it's not needed
|
||||
* @param {boolean} force if true, will overwrite any previous allowence. Else, will check if allowence is enough and will not send a transaction if it's not needed
|
||||
* @param {number} tokenDecimals optional number of decimals of the token
|
||||
*/
|
||||
export async function approve(
|
||||
web3: Web3,
|
||||
@ -54,7 +55,8 @@ export async function approve(
|
||||
tokenAddress: string,
|
||||
spender: string,
|
||||
amount: string,
|
||||
force = false
|
||||
force = false,
|
||||
tokenDecimals?: number
|
||||
): Promise<TransactionReceipt | string> {
|
||||
const tokenContract = new web3.eth.Contract(minAbi, tokenAddress)
|
||||
if (!force) {
|
||||
@ -64,7 +66,7 @@ export async function approve(
|
||||
}
|
||||
}
|
||||
let result = null
|
||||
const amountFormatted = await amountToUnits(web3, tokenAddress, amount)
|
||||
const amountFormatted = await amountToUnits(web3, tokenAddress, amount, tokenDecimals)
|
||||
const estGas = await estApprove(
|
||||
web3,
|
||||
account,
|
||||
@ -94,17 +96,19 @@ export async function approve(
|
||||
* @param {String } tokenAdress
|
||||
* @param {String} account
|
||||
* @param {String} spender
|
||||
* @param {number} tokenDecimals optional number of decimals of the token
|
||||
*/
|
||||
export async function allowance(
|
||||
web3: Web3,
|
||||
tokenAddress: string,
|
||||
account: string,
|
||||
spender: string
|
||||
spender: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
const tokenContract = new web3.eth.Contract(minAbi, tokenAddress)
|
||||
const trxReceipt = await tokenContract.methods.allowance(account, spender).call()
|
||||
|
||||
return await unitsToAmount(web3, tokenAddress, trxReceipt)
|
||||
return await unitsToAmount(web3, tokenAddress, trxReceipt, tokenDecimals)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -113,14 +117,16 @@ export async function allowance(
|
||||
* @param {String} tokenAdress
|
||||
* @param {String} owner
|
||||
* @param {String} spender
|
||||
* @param {number} tokenDecimals optional number of decimals of the token
|
||||
*/
|
||||
export async function balance(
|
||||
web3: Web3,
|
||||
tokenAddress: string,
|
||||
account: string
|
||||
account: string,
|
||||
tokenDecimals?: number
|
||||
): Promise<string> {
|
||||
const tokenContract = new web3.eth.Contract(minAbi, tokenAddress)
|
||||
const trxReceipt = await tokenContract.methods.balanceOf(account).call()
|
||||
|
||||
return await unitsToAmount(web3, tokenAddress, trxReceipt)
|
||||
return await unitsToAmount(web3, tokenAddress, trxReceipt, tokenDecimals)
|
||||
}
|
||||
|
@ -8,11 +8,39 @@ import {
|
||||
NftFactory,
|
||||
NftCreateData,
|
||||
Datatoken,
|
||||
getHash,
|
||||
Nft,
|
||||
sleep
|
||||
sleep,
|
||||
ZERO_ADDRESS
|
||||
} from '../../src'
|
||||
import { ProviderFees, Erc20CreateParams, ComputeJob, Asset } from '../../src/@types'
|
||||
import {
|
||||
Erc20CreateParams,
|
||||
ComputeJob,
|
||||
ComputeAsset,
|
||||
ComputeAlgorithm,
|
||||
ProviderComputeInitialize,
|
||||
ConsumeMarketFee
|
||||
} from '../../src/@types'
|
||||
|
||||
let config: Config
|
||||
|
||||
let aquarius: Aquarius
|
||||
let datatoken: Datatoken
|
||||
let providerUrl: string
|
||||
let consumerAccount: string
|
||||
let publisherAccount: string
|
||||
let computeJobId: string
|
||||
let providerInitializeComputeResults
|
||||
let computeEnvs
|
||||
let addresses: any
|
||||
let ddoWith1mTimeoutId
|
||||
let ddoWithNoTimeoutId
|
||||
let algoDdoWith1mTimeoutId
|
||||
let algoDdoWithNoTimeoutId
|
||||
|
||||
let resolvedDdoWith1mTimeout
|
||||
let resolvedDdoWithNoTimeout
|
||||
let resolvedAlgoDdoWith1mTimeout
|
||||
let resolvedAlgoDdoWithNoTimeout
|
||||
|
||||
const assetUrl = [
|
||||
{
|
||||
@ -21,7 +49,7 @@ const assetUrl = [
|
||||
method: 'GET'
|
||||
}
|
||||
]
|
||||
const ddo = {
|
||||
const ddoWithNoTimeout = {
|
||||
'@context': ['https://w3id.org/did/v1'],
|
||||
id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c',
|
||||
version: '4.0.0',
|
||||
@ -52,13 +80,44 @@ const ddo = {
|
||||
publisherTrustedAlgorithmPublishers: [],
|
||||
publisherTrustedAlgorithms: [],
|
||||
allowRawAlgorithm: true,
|
||||
allowNetworkAccess: true,
|
||||
namespace: 'ocean-compute',
|
||||
cpus: 2,
|
||||
gpus: 4,
|
||||
gpuType: 'NVIDIA Tesla V100 GPU',
|
||||
memory: '128M',
|
||||
volumeSize: '2G'
|
||||
allowNetworkAccess: true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
const ddoWith1mTimeout = {
|
||||
'@context': ['https://w3id.org/did/v1'],
|
||||
id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c',
|
||||
version: '4.0.0',
|
||||
chainId: 4,
|
||||
nftAddress: '0x0',
|
||||
metadata: {
|
||||
created: '2021-12-20T14:35:20Z',
|
||||
updated: '2021-12-20T14:35:20Z',
|
||||
type: 'dataset',
|
||||
name: 'dfgdfgdg',
|
||||
description: 'd dfgd fgd dfg dfgdfgd dfgdf',
|
||||
tags: [''],
|
||||
author: 'dd',
|
||||
license: 'https://market.oceanprotocol.com/terms',
|
||||
additionalInformation: {
|
||||
termsAndConditions: true
|
||||
}
|
||||
},
|
||||
services: [
|
||||
{
|
||||
id: 'notAnId',
|
||||
type: 'compute',
|
||||
files: '',
|
||||
datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3',
|
||||
serviceEndpoint: 'https://providerv4.rinkeby.oceanprotocol.com',
|
||||
timeout: 60,
|
||||
compute: {
|
||||
publisherTrustedAlgorithmPublishers: [],
|
||||
publisherTrustedAlgorithms: [],
|
||||
allowRawAlgorithm: true,
|
||||
allowNetworkAccess: true
|
||||
}
|
||||
}
|
||||
]
|
||||
@ -70,7 +129,7 @@ const algoAssetUrl = [
|
||||
method: 'GET'
|
||||
}
|
||||
]
|
||||
const algoDdo = {
|
||||
const algoDdoWithNoTimeout = {
|
||||
'@context': ['https://w3id.org/did/v1'],
|
||||
id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c',
|
||||
version: '4.0.0',
|
||||
@ -110,144 +169,246 @@ const algoDdo = {
|
||||
}
|
||||
]
|
||||
}
|
||||
let providerUrl: string
|
||||
let consumerAccount: string
|
||||
let computeJobId: string
|
||||
let resolvedDDOAsset: Asset
|
||||
|
||||
const algoDdoWith1mTimeout = {
|
||||
'@context': ['https://w3id.org/did/v1'],
|
||||
id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c',
|
||||
version: '4.0.0',
|
||||
chainId: 4,
|
||||
nftAddress: '0x0',
|
||||
metadata: {
|
||||
created: '2021-12-20T14:35:20Z',
|
||||
updated: '2021-12-20T14:35:20Z',
|
||||
type: 'algorithm',
|
||||
name: 'dfgdfgdg',
|
||||
description: 'd dfgd fgd dfg dfgdfgd dfgdf',
|
||||
tags: [''],
|
||||
author: 'dd',
|
||||
license: 'https://market.oceanprotocol.com/terms',
|
||||
additionalInformation: {
|
||||
termsAndConditions: true
|
||||
},
|
||||
algorithm: {
|
||||
language: 'Node.js',
|
||||
version: '1.0.0',
|
||||
container: {
|
||||
entrypoint: 'node $ALGO',
|
||||
image: 'ubuntu',
|
||||
tag: 'latest',
|
||||
checksum: '44e10daa6637893f4276bb8d7301eb35306ece50f61ca34dcab550'
|
||||
}
|
||||
}
|
||||
},
|
||||
services: [
|
||||
{
|
||||
id: 'notAnId',
|
||||
type: 'access',
|
||||
files: '',
|
||||
datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3',
|
||||
serviceEndpoint: 'https://providerv4.rinkeby.oceanprotocol.com',
|
||||
timeout: 60
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
async function createAsset(
|
||||
name: string,
|
||||
symbol: string,
|
||||
owner: string,
|
||||
assetUrl: any,
|
||||
ddo: any,
|
||||
providerUrl: string
|
||||
) {
|
||||
const nft = new Nft(web3)
|
||||
const Factory = new NftFactory(addresses.ERC721Factory, web3)
|
||||
|
||||
const chain = await web3.eth.getChainId()
|
||||
ddo.chainId = parseInt(chain.toString(10))
|
||||
const nftParamsAsset: NftCreateData = {
|
||||
name: name,
|
||||
symbol: symbol,
|
||||
templateIndex: 1,
|
||||
tokenURI: 'aaa',
|
||||
transferable: true,
|
||||
owner: owner
|
||||
}
|
||||
const erc20ParamsAsset: Erc20CreateParams = {
|
||||
templateIndex: 1,
|
||||
cap: '100000',
|
||||
feeAmount: '0',
|
||||
paymentCollector: ZERO_ADDRESS,
|
||||
feeToken: ZERO_ADDRESS,
|
||||
minter: owner,
|
||||
mpFeeAddress: ZERO_ADDRESS
|
||||
}
|
||||
|
||||
const result = await Factory.createNftWithErc20(owner, nftParamsAsset, erc20ParamsAsset)
|
||||
|
||||
const erc721AddressAsset = result.events.NFTCreated.returnValues[0]
|
||||
const datatokenAddressAsset = result.events.TokenCreated.returnValues[0]
|
||||
// create the files encrypted string
|
||||
let providerResponse = await ProviderInstance.encrypt(assetUrl, providerUrl)
|
||||
ddo.services[0].files = await providerResponse
|
||||
ddo.services[0].datatokenAddress = datatokenAddressAsset
|
||||
ddo.services[0].serviceEndpoint = providerUrl
|
||||
// update ddo and set the right did
|
||||
ddo.nftAddress = web3.utils.toChecksumAddress(erc721AddressAsset)
|
||||
ddo.id =
|
||||
'did:op:' +
|
||||
SHA256(web3.utils.toChecksumAddress(erc721AddressAsset) + chain.toString(10))
|
||||
providerResponse = await ProviderInstance.encrypt(ddo, providerUrl)
|
||||
const encryptedResponse = await providerResponse
|
||||
const validateResult = await aquarius.validate(ddo)
|
||||
assert(validateResult.valid, 'Could not validate metadata')
|
||||
await nft.setMetadata(
|
||||
erc721AddressAsset,
|
||||
owner,
|
||||
0,
|
||||
providerUrl,
|
||||
'',
|
||||
'0x2',
|
||||
encryptedResponse,
|
||||
validateResult.hash
|
||||
)
|
||||
return ddo.id
|
||||
}
|
||||
|
||||
async function handleOrder(
|
||||
order: ProviderComputeInitialize,
|
||||
datatokenAddress: string,
|
||||
payerAccount: string,
|
||||
consumerAccount: string,
|
||||
serviceIndex: number,
|
||||
consumeMarkerFee?: ConsumeMarketFee
|
||||
) {
|
||||
/* We do have 3 possible situations:
|
||||
- have validOrder and no providerFees -> then order is valid, providerFees are valid, just use it in startCompute
|
||||
- have validOrder and providerFees -> then order is valid but providerFees are not valid, we need to call reuseOrder and pay only providerFees
|
||||
- no validOrder -> we need to call startOrder, to pay 1 DT & providerFees
|
||||
*/
|
||||
if (order.providerFee && order.providerFee.providerFeeAmount) {
|
||||
await datatoken.approve(
|
||||
order.providerFee.providerFeeToken,
|
||||
datatokenAddress,
|
||||
order.providerFee.providerFeeAmount,
|
||||
payerAccount
|
||||
)
|
||||
}
|
||||
if (order.validOrder) {
|
||||
if (!order.providerFee) return order.validOrder
|
||||
const tx = await datatoken.reuseOrder(
|
||||
datatokenAddress,
|
||||
payerAccount,
|
||||
order.validOrder,
|
||||
order.providerFee
|
||||
)
|
||||
return tx.transactionHash
|
||||
}
|
||||
const tx = await datatoken.startOrder(
|
||||
datatokenAddress,
|
||||
payerAccount,
|
||||
consumerAccount,
|
||||
serviceIndex,
|
||||
order.providerFee,
|
||||
consumeMarkerFee
|
||||
)
|
||||
return tx.transactionHash
|
||||
}
|
||||
|
||||
describe('Simple compute tests', async () => {
|
||||
let config: Config
|
||||
let addresses: any
|
||||
let aquarius: Aquarius
|
||||
|
||||
before(async () => {
|
||||
config = await getTestConfig(web3)
|
||||
addresses = getAddresses()
|
||||
aquarius = new Aquarius(config.metadataCacheUri)
|
||||
providerUrl = config.providerUri
|
||||
datatoken = new Datatoken(web3)
|
||||
})
|
||||
|
||||
it('should publish a dataset, algorithm and start a compute job', async () => {
|
||||
const nft = new Nft(web3)
|
||||
const datatoken = new Datatoken(web3)
|
||||
const Factory = new NftFactory(addresses.ERC721Factory, web3)
|
||||
it('should publish datasets and algorithms', async () => {
|
||||
const accounts = await web3.eth.getAccounts()
|
||||
const publisherAccount = accounts[0]
|
||||
publisherAccount = accounts[0]
|
||||
consumerAccount = accounts[1]
|
||||
const chain = await web3.eth.getChainId()
|
||||
const nftParamsAsset: NftCreateData = {
|
||||
name: 'testNFT',
|
||||
symbol: 'TST',
|
||||
templateIndex: 1,
|
||||
tokenURI: 'aaa',
|
||||
transferable: true,
|
||||
owner: publisherAccount
|
||||
}
|
||||
const erc20ParamsAsset: Erc20CreateParams = {
|
||||
templateIndex: 1,
|
||||
cap: '100000',
|
||||
feeAmount: '0',
|
||||
paymentCollector: '0x0000000000000000000000000000000000000000',
|
||||
feeToken: '0x0000000000000000000000000000000000000000',
|
||||
minter: publisherAccount,
|
||||
mpFeeAddress: '0x0000000000000000000000000000000000000000'
|
||||
}
|
||||
|
||||
const result = await Factory.createNftWithErc20(
|
||||
ddoWith1mTimeoutId = await createAsset(
|
||||
'D1Min',
|
||||
'D1M',
|
||||
publisherAccount,
|
||||
nftParamsAsset,
|
||||
erc20ParamsAsset
|
||||
assetUrl,
|
||||
ddoWith1mTimeout,
|
||||
providerUrl
|
||||
)
|
||||
ddoWithNoTimeoutId = await createAsset(
|
||||
'D1Min',
|
||||
'D1M',
|
||||
publisherAccount,
|
||||
assetUrl,
|
||||
ddoWithNoTimeout,
|
||||
providerUrl
|
||||
)
|
||||
algoDdoWith1mTimeoutId = await createAsset(
|
||||
'A1Min',
|
||||
'A1M',
|
||||
publisherAccount,
|
||||
algoAssetUrl,
|
||||
algoDdoWith1mTimeout,
|
||||
providerUrl
|
||||
)
|
||||
|
||||
const erc721AddressAsset = result.events.NFTCreated.returnValues[0]
|
||||
const datatokenAddressAsset = result.events.TokenCreated.returnValues[0]
|
||||
// create the files encrypted string
|
||||
let providerResponse = await ProviderInstance.encrypt(assetUrl, providerUrl)
|
||||
ddo.services[0].files = await providerResponse
|
||||
ddo.services[0].datatokenAddress = datatokenAddressAsset
|
||||
// update ddo and set the right did
|
||||
ddo.nftAddress = erc721AddressAsset
|
||||
ddo.id =
|
||||
'did:op:' +
|
||||
SHA256(web3.utils.toChecksumAddress(erc721AddressAsset) + chain.toString(10))
|
||||
|
||||
providerResponse = await ProviderInstance.encrypt(ddo, providerUrl)
|
||||
|
||||
let encryptedResponse = await providerResponse
|
||||
let metadataHash = getHash(JSON.stringify(ddo))
|
||||
let res = await nft.setMetadata(
|
||||
erc721AddressAsset,
|
||||
algoDdoWithNoTimeoutId = await createAsset(
|
||||
'A1Min',
|
||||
'A1M',
|
||||
publisherAccount,
|
||||
0,
|
||||
providerUrl,
|
||||
'',
|
||||
'0x2',
|
||||
encryptedResponse,
|
||||
'0x' + metadataHash
|
||||
algoAssetUrl,
|
||||
algoDdoWithNoTimeout,
|
||||
providerUrl
|
||||
)
|
||||
// let's publish the algorithm as well
|
||||
const nftParamsAlgo: NftCreateData = {
|
||||
name: 'testNFT',
|
||||
symbol: 'TST',
|
||||
templateIndex: 1,
|
||||
tokenURI: '',
|
||||
transferable: true,
|
||||
owner: publisherAccount
|
||||
}
|
||||
const erc20ParamsAlgo: Erc20CreateParams = {
|
||||
templateIndex: 1,
|
||||
cap: '100000',
|
||||
feeAmount: '0',
|
||||
paymentCollector: '0x0000000000000000000000000000000000000000',
|
||||
feeToken: '0x0000000000000000000000000000000000000000',
|
||||
minter: publisherAccount,
|
||||
mpFeeAddress: '0x0000000000000000000000000000000000000000'
|
||||
}
|
||||
const resultAlgo = await Factory.createNftWithErc20(
|
||||
})
|
||||
|
||||
it('should resolve published datasets and algorithms', async () => {
|
||||
resolvedDdoWith1mTimeout = await aquarius.waitForAqua(ddoWith1mTimeoutId)
|
||||
assert(resolvedDdoWith1mTimeout, 'Cannot fetch DDO from Aquarius')
|
||||
resolvedDdoWithNoTimeout = await aquarius.waitForAqua(ddoWithNoTimeoutId)
|
||||
assert(resolvedDdoWithNoTimeout, 'Cannot fetch DDO from Aquarius')
|
||||
resolvedAlgoDdoWith1mTimeout = await aquarius.waitForAqua(algoDdoWith1mTimeoutId)
|
||||
assert(resolvedAlgoDdoWith1mTimeout, 'Cannot fetch DDO from Aquarius')
|
||||
resolvedAlgoDdoWithNoTimeout = await aquarius.waitForAqua(algoDdoWithNoTimeoutId)
|
||||
assert(resolvedAlgoDdoWithNoTimeout, 'Cannot fetch DDO from Aquarius')
|
||||
})
|
||||
|
||||
it('should send DT to consumer', async () => {
|
||||
const datatoken = new Datatoken(web3)
|
||||
await datatoken.mint(
|
||||
resolvedDdoWith1mTimeout.services[0].datatokenAddress,
|
||||
publisherAccount,
|
||||
nftParamsAlgo,
|
||||
erc20ParamsAlgo
|
||||
'10',
|
||||
consumerAccount
|
||||
)
|
||||
const erc721AddressAlgo = resultAlgo.events.NFTCreated.returnValues[0]
|
||||
const datatokenAddressAlgo = resultAlgo.events.TokenCreated.returnValues[0]
|
||||
|
||||
// create the files encrypted string
|
||||
providerResponse = await ProviderInstance.encrypt(algoAssetUrl, providerUrl)
|
||||
algoDdo.services[0].files = await providerResponse
|
||||
algoDdo.services[0].datatokenAddress = datatokenAddressAlgo
|
||||
// update ddo and set the right did
|
||||
algoDdo.nftAddress = erc721AddressAlgo
|
||||
|
||||
algoDdo.id =
|
||||
'did:op:' +
|
||||
SHA256(web3.utils.toChecksumAddress(erc721AddressAlgo) + chain.toString(10))
|
||||
|
||||
providerResponse = await ProviderInstance.encrypt(algoDdo, providerUrl)
|
||||
encryptedResponse = await providerResponse
|
||||
metadataHash = getHash(JSON.stringify(algoDdo))
|
||||
res = await nft.setMetadata(
|
||||
erc721AddressAlgo,
|
||||
await datatoken.mint(
|
||||
resolvedDdoWithNoTimeout.services[0].datatokenAddress,
|
||||
publisherAccount,
|
||||
0,
|
||||
providerUrl,
|
||||
'',
|
||||
'0x2',
|
||||
encryptedResponse,
|
||||
'0x' + metadataHash
|
||||
'10',
|
||||
consumerAccount
|
||||
)
|
||||
await datatoken.mint(
|
||||
resolvedAlgoDdoWith1mTimeout.services[0].datatokenAddress,
|
||||
publisherAccount,
|
||||
'10',
|
||||
consumerAccount
|
||||
)
|
||||
await datatoken.mint(
|
||||
resolvedAlgoDdoWithNoTimeout.services[0].datatokenAddress,
|
||||
publisherAccount,
|
||||
'10',
|
||||
consumerAccount
|
||||
)
|
||||
})
|
||||
|
||||
// let's wait
|
||||
resolvedDDOAsset = await aquarius.waitForAqua(ddo.id)
|
||||
assert(resolvedDDOAsset, 'Cannot fetch DDO from Aquarius')
|
||||
const resolvedDDOAlgo = await aquarius.waitForAqua(algoDdo.id)
|
||||
assert(resolvedDDOAlgo, 'Cannot fetch DDO from Aquarius')
|
||||
// mint 1 ERC20 and send it to the consumer
|
||||
await datatoken.mint(datatokenAddressAsset, publisherAccount, '1', consumerAccount)
|
||||
await datatoken.mint(datatokenAddressAlgo, publisherAccount, '1', consumerAccount)
|
||||
|
||||
it('should fetch compute environments from provider', async () => {
|
||||
// get compute environments
|
||||
const computeEnvs = await ProviderInstance.getComputeEnvironments(providerUrl)
|
||||
computeEnvs = await ProviderInstance.getComputeEnvironments(providerUrl)
|
||||
assert(computeEnvs, 'No Compute environments found')
|
||||
})
|
||||
|
||||
it('should start a computeJob', async () => {
|
||||
// we choose the first env
|
||||
const computeEnv = computeEnvs[0].id
|
||||
const computeConsumerAddress = computeEnvs[0].consumerAddress
|
||||
@ -255,89 +416,53 @@ describe('Simple compute tests', async () => {
|
||||
const mytime = new Date()
|
||||
mytime.setMinutes(mytime.getMinutes() + 19)
|
||||
const computeValidUntil = Math.floor(mytime.getTime() / 1000)
|
||||
// initialize provider orders for algo
|
||||
const initializeDataAlgo = await ProviderInstance.initialize(
|
||||
resolvedDDOAlgo.id,
|
||||
resolvedDDOAlgo.services[0].id,
|
||||
0,
|
||||
consumerAccount,
|
||||
providerUrl,
|
||||
null,
|
||||
null,
|
||||
computeEnv,
|
||||
computeValidUntil
|
||||
)
|
||||
const providerAlgoFees: ProviderFees = {
|
||||
providerFeeAddress: initializeDataAlgo.providerFee.providerFeeAddress,
|
||||
providerFeeToken: initializeDataAlgo.providerFee.providerFeeToken,
|
||||
providerFeeAmount: initializeDataAlgo.providerFee.providerFeeAmount,
|
||||
v: initializeDataAlgo.providerFee.v,
|
||||
r: initializeDataAlgo.providerFee.r,
|
||||
s: initializeDataAlgo.providerFee.s,
|
||||
providerData: initializeDataAlgo.providerFee.providerData,
|
||||
validUntil: initializeDataAlgo.providerFee.validUntil
|
||||
const assets: ComputeAsset[] = [
|
||||
{
|
||||
documentId: resolvedDdoWith1mTimeout.id,
|
||||
serviceId: resolvedDdoWith1mTimeout.services[0].id
|
||||
}
|
||||
]
|
||||
const dtAddressArray = [resolvedDdoWith1mTimeout.services[0].datatokenAddress]
|
||||
const algo: ComputeAlgorithm = {
|
||||
documentId: resolvedAlgoDdoWith1mTimeout.id,
|
||||
serviceId: resolvedAlgoDdoWith1mTimeout.services[0].id
|
||||
}
|
||||
|
||||
// make the payment
|
||||
const txidAlgo = await datatoken.startOrder(
|
||||
datatokenAddressAlgo,
|
||||
consumerAccount,
|
||||
computeConsumerAddress, // this is important because the c2d is the consumer, and user is the payer. Otherwise, c2d will have no access to the asset
|
||||
0,
|
||||
providerAlgoFees
|
||||
)
|
||||
assert(txidAlgo, 'Failed to order algo')
|
||||
|
||||
const providerValidUntil = new Date()
|
||||
providerValidUntil.setHours(providerValidUntil.getHours() + 1)
|
||||
|
||||
// initialize provider orders for asset
|
||||
const initializeData = await ProviderInstance.initialize(
|
||||
resolvedDDOAsset.id,
|
||||
resolvedDDOAsset.services[0].id,
|
||||
0,
|
||||
consumerAccount,
|
||||
providerUrl,
|
||||
null,
|
||||
null,
|
||||
providerInitializeComputeResults = await ProviderInstance.initializeCompute(
|
||||
assets,
|
||||
algo,
|
||||
computeEnv,
|
||||
computeValidUntil
|
||||
computeValidUntil,
|
||||
providerUrl,
|
||||
consumerAccount
|
||||
)
|
||||
const providerDatasetFees: ProviderFees = {
|
||||
providerFeeAddress: initializeData.providerFee.providerFeeAddress,
|
||||
providerFeeToken: initializeData.providerFee.providerFeeToken,
|
||||
providerFeeAmount: initializeData.providerFee.providerFeeAmount,
|
||||
v: initializeData.providerFee.v,
|
||||
r: initializeData.providerFee.r,
|
||||
s: initializeData.providerFee.s,
|
||||
providerData: initializeData.providerFee.providerData,
|
||||
validUntil: initializeData.providerFee.validUntil
|
||||
}
|
||||
// make the payment
|
||||
const txidAsset = await datatoken.startOrder(
|
||||
datatokenAddressAsset,
|
||||
assert(
|
||||
!('error' in providerInitializeComputeResults.algorithm),
|
||||
'Cannot order algorithm'
|
||||
)
|
||||
algo.transferTxId = await handleOrder(
|
||||
providerInitializeComputeResults.algorithm,
|
||||
resolvedAlgoDdoWith1mTimeout.services[0].datatokenAddress,
|
||||
consumerAccount,
|
||||
computeConsumerAddress, // this is important because the c2d is the consumer, and user is the payer. Otherwise, c2d will have no access to the asset
|
||||
0,
|
||||
providerDatasetFees
|
||||
computeConsumerAddress,
|
||||
0
|
||||
)
|
||||
assert(txidAsset, 'Failed to order algo')
|
||||
// start the compute job
|
||||
for (let i = 0; i < providerInitializeComputeResults.datasets.length; i++) {
|
||||
assets[i].transferTxId = await handleOrder(
|
||||
providerInitializeComputeResults.datasets[i],
|
||||
dtAddressArray[i],
|
||||
consumerAccount,
|
||||
computeConsumerAddress,
|
||||
0
|
||||
)
|
||||
}
|
||||
const computeJobs = await ProviderInstance.computeStart(
|
||||
providerUrl,
|
||||
web3,
|
||||
consumerAccount,
|
||||
computeEnv,
|
||||
{
|
||||
documentId: resolvedDDOAsset.id,
|
||||
serviceId: resolvedDDOAsset.services[0].id,
|
||||
transferTxId: txidAsset.transactionHash
|
||||
},
|
||||
{
|
||||
documentId: resolvedDDOAlgo.id,
|
||||
serviceId: resolvedDDOAlgo.services[0].id,
|
||||
transferTxId: txidAlgo.transactionHash
|
||||
}
|
||||
assets[0],
|
||||
algo
|
||||
)
|
||||
assert(computeJobs, 'Cannot start compute job')
|
||||
computeJobId = computeJobs[0].jobId
|
||||
@ -348,7 +473,7 @@ describe('Simple compute tests', async () => {
|
||||
providerUrl,
|
||||
consumerAccount,
|
||||
computeJobId,
|
||||
resolvedDDOAsset.id
|
||||
resolvedDdoWith1mTimeout.id
|
||||
)) as ComputeJob
|
||||
assert(jobStatus, 'Cannot retrieve compute status!')
|
||||
})
|
||||
|
@ -32,4 +32,9 @@ describe('Provider tests', async () => {
|
||||
)
|
||||
assert(fileinfo[0].valid === true, 'Sent file is not valid')
|
||||
})
|
||||
|
||||
it('Alice tests compute environments', async () => {
|
||||
const computeEnvs = await providerInstance.getComputeEnvironments(config.providerUri)
|
||||
assert(computeEnvs, 'No Compute environments found')
|
||||
})
|
||||
})
|
||||
|
@ -54,7 +54,7 @@ describe('Pool unit test', () => {
|
||||
contracts = await deployContracts(web3, factoryOwner)
|
||||
|
||||
// initialize Pool instance
|
||||
pool = new Pool(web3, PoolTemplate.abi as AbiItem[])
|
||||
pool = new Pool(web3, 8996, PoolTemplate.abi as AbiItem[])
|
||||
assert(pool != null)
|
||||
|
||||
daiContract = new web3.eth.Contract(MockERC20.abi as AbiItem[], contracts.daiAddress)
|
||||
@ -143,6 +143,7 @@ describe('Pool unit test', () => {
|
||||
const nftFactory = new NftFactory(
|
||||
contracts.erc721FactoryAddress,
|
||||
web3,
|
||||
8996,
|
||||
ERC721Factory.abi as AbiItem[]
|
||||
)
|
||||
|
||||
@ -294,21 +295,6 @@ describe('Pool unit test', () => {
|
||||
assert(tx != null)
|
||||
})
|
||||
|
||||
it('#joinPool- user2 should add liquidity, receiving LP tokens', async () => {
|
||||
const BPTAmountOut = '0.01'
|
||||
const maxAmountsIn = [
|
||||
'50', // Amounts IN
|
||||
'50' // Amounts IN
|
||||
]
|
||||
|
||||
await approve(web3, user2, erc20Token, poolAddress, '50')
|
||||
await approve(web3, user2, contracts.daiAddress, poolAddress, '50')
|
||||
const tx = await pool.joinPool(user2, poolAddress, BPTAmountOut, maxAmountsIn)
|
||||
assert(tx != null)
|
||||
expect(await pool.sharesBalance(user2, poolAddress)).to.equal(BPTAmountOut)
|
||||
expect(tx.events.LOG_JOIN.event === 'LOG_JOIN')
|
||||
expect(tx.events.LOG_BPT.event === 'LOG_BPT')
|
||||
})
|
||||
it('#joinswapExternAmountIn- user2 should add liquidity, receiving LP tokens', async () => {
|
||||
const daiAmountIn = '100'
|
||||
const minBPTOut = '0.1'
|
||||
@ -333,21 +319,6 @@ describe('Pool unit test', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('#exitPool- user2 exit the pool receiving both tokens, burning LP', async () => {
|
||||
const BPTAmountIn = '0.5'
|
||||
const minAmountOut = [
|
||||
'1', // min amount out for OCEAN AND DT
|
||||
'1'
|
||||
]
|
||||
|
||||
const tx = await pool.exitPool(user2, poolAddress, BPTAmountIn, minAmountOut)
|
||||
|
||||
assert(tx != null)
|
||||
|
||||
expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(erc20Token)
|
||||
expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(contracts.daiAddress)
|
||||
})
|
||||
|
||||
it('#exitswapPoolAmountIn- user2 exit the pool receiving only DAI', async () => {
|
||||
const BPTAmountIn = '0.5'
|
||||
const minDAIOut = '0.5'
|
||||
@ -602,6 +573,7 @@ describe('Pool unit test', () => {
|
||||
const nftFactory = new NftFactory(
|
||||
contracts.erc721FactoryAddress,
|
||||
web3,
|
||||
8996,
|
||||
ERC721Factory.abi as AbiItem[]
|
||||
)
|
||||
|
||||
@ -826,25 +798,6 @@ describe('Pool unit test', () => {
|
||||
// console.log(tx.events)
|
||||
})
|
||||
|
||||
it('#joinPool- user2 should add liquidity, receiving LP tokens', async () => {
|
||||
const BPTAmountOut = '0.01'
|
||||
const maxAmountsIn = [
|
||||
'50', // Amounts IN
|
||||
'50' // Amounts IN
|
||||
]
|
||||
|
||||
await approve(web3, user2, erc20Token, poolAddress, '50')
|
||||
await approve(web3, user2, contracts.usdcAddress, poolAddress, '50')
|
||||
const tx = await pool.joinPool(user2, poolAddress, BPTAmountOut, maxAmountsIn)
|
||||
assert(tx != null)
|
||||
expect(await pool.sharesBalance(user2, poolAddress)).to.equal(BPTAmountOut)
|
||||
expect(tx.events.LOG_JOIN.event === 'LOG_JOIN')
|
||||
expect(tx.events.LOG_BPT.event === 'LOG_BPT')
|
||||
|
||||
// console.log(tx)
|
||||
// console.log(tx.events.LOG_JOIN)
|
||||
// console.log(tx.events.LOG_BPT)
|
||||
})
|
||||
it('#joinswapExternAmountIn- user2 should add liquidity, receiving LP tokens', async () => {
|
||||
const usdcAmountIn = '100'
|
||||
const minBPTOut = '0.1'
|
||||
@ -867,21 +820,6 @@ describe('Pool unit test', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('#exitPool- user2 exit the pool receiving both tokens, burning LP', async () => {
|
||||
const BPTAmountIn = '0.5'
|
||||
const minAmountOut = [
|
||||
'1', // min amount out for USDC AND DT
|
||||
'1'
|
||||
]
|
||||
|
||||
const tx = await pool.exitPool(user2, poolAddress, BPTAmountIn, minAmountOut)
|
||||
|
||||
assert(tx != null)
|
||||
|
||||
expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(erc20Token)
|
||||
expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(contracts.usdcAddress)
|
||||
})
|
||||
|
||||
it('#exitswapPoolAmountIn- user2 exit the pool receiving only USDC', async () => {
|
||||
const BPTAmountIn = '0.5'
|
||||
const minUSDCOut = '0.5'
|
||||
|
@ -59,7 +59,7 @@ describe('Dispenser flow', () => {
|
||||
})
|
||||
|
||||
it('should initialize Dispenser class', async () => {
|
||||
DispenserClass = new Dispenser(web3, contracts.dispenserAddress)
|
||||
DispenserClass = new Dispenser(web3, 8996, contracts.dispenserAddress)
|
||||
assert(DispenserClass !== null)
|
||||
})
|
||||
|
||||
@ -78,10 +78,10 @@ describe('Dispenser flow', () => {
|
||||
dtAddress = txReceipt.events.TokenCreated.returnValues.newTokenAddress
|
||||
})
|
||||
|
||||
it('Make user1 minter', async () => {
|
||||
datatoken = new Datatoken(web3)
|
||||
await datatoken.addMinter(dtAddress, factoryOwner, user1)
|
||||
assert((await datatoken.getDTPermissions(dtAddress, user1)).minter === true)
|
||||
it('Make user2 minter', async () => {
|
||||
datatoken = new Datatoken(web3, 8996)
|
||||
await datatoken.addMinter(dtAddress, factoryOwner, user2)
|
||||
assert((await datatoken.getDTPermissions(dtAddress, user2)).minter === true)
|
||||
})
|
||||
|
||||
it('Create dispenser', async () => {
|
||||
|
@ -118,6 +118,7 @@ describe('Fixed Rate unit test', () => {
|
||||
fixedRate = new FixedRateExchange(
|
||||
web3,
|
||||
fixedRateAddress,
|
||||
8996,
|
||||
FixedRate.abi as AbiItem[],
|
||||
contracts.oceanAddress
|
||||
)
|
||||
@ -438,6 +439,7 @@ describe('Fixed Rate unit test', () => {
|
||||
fixedRate = new FixedRateExchange(
|
||||
web3,
|
||||
fixedRateAddress,
|
||||
8996,
|
||||
FixedRate.abi as AbiItem[],
|
||||
contracts.oceanAddress
|
||||
)
|
||||
|
@ -59,10 +59,10 @@ describe('SideStaking unit test', () => {
|
||||
sideStakingAddress = contracts.sideStakingAddress
|
||||
|
||||
// initialize Pool instance
|
||||
pool = new Pool(web3, PoolTemplate.abi as AbiItem[])
|
||||
pool = new Pool(web3, 8996, PoolTemplate.abi as AbiItem[])
|
||||
assert(pool != null)
|
||||
//
|
||||
sideStaking = new SideStaking(web3, SSContract.abi as AbiItem[])
|
||||
sideStaking = new SideStaking(web3, 8996, SSContract.abi as AbiItem[])
|
||||
assert(sideStaking != null)
|
||||
|
||||
daiContract = new web3.eth.Contract(MockERC20.abi as AbiItem[], contracts.daiAddress)
|
||||
@ -219,7 +219,7 @@ describe('SideStaking unit test', () => {
|
||||
|
||||
it('#getvestingAmount ', async () => {
|
||||
expect(await sideStaking.getvestingAmount(sideStakingAddress, erc20Token)).to.equal(
|
||||
'10000'
|
||||
'0'
|
||||
)
|
||||
})
|
||||
it('#getvestingLastBlock ', async () => {
|
||||
@ -228,38 +228,12 @@ describe('SideStaking unit test', () => {
|
||||
).to.equal(initialBlock.toString())
|
||||
})
|
||||
|
||||
it('#getvestingEndBlock ', async () => {
|
||||
expect(
|
||||
await sideStaking.getvestingEndBlock(sideStakingAddress, erc20Token)
|
||||
).to.equal((initialBlock + vestedBlocks).toString())
|
||||
})
|
||||
it('#getvestingAmountSoFar ', async () => {
|
||||
expect(
|
||||
await sideStaking.getvestingAmountSoFar(sideStakingAddress, erc20Token)
|
||||
).to.equal('0')
|
||||
})
|
||||
|
||||
it('#getVesting ', async () => {
|
||||
expect(await erc20Contract.methods.balanceOf(factoryOwner).call()).to.equal('0')
|
||||
|
||||
const tx = await sideStaking.getVesting(
|
||||
factoryOwner,
|
||||
sideStakingAddress,
|
||||
erc20Token
|
||||
)
|
||||
const collector = await erc20Contract.methods.getPaymentCollector().call()
|
||||
expect(
|
||||
await sideStaking.unitsToAmount(
|
||||
erc20Token,
|
||||
await erc20Contract.methods.balanceOf(collector).call()
|
||||
)
|
||||
).to.equal(await sideStaking.getvestingAmountSoFar(sideStakingAddress, erc20Token))
|
||||
|
||||
expect(
|
||||
await sideStaking.getvestingLastBlock(sideStakingAddress, erc20Token)
|
||||
).to.equal((await web3.eth.getBlockNumber()).toString())
|
||||
})
|
||||
|
||||
it('#swapExactAmountIn - should swap', async () => {
|
||||
await daiContract.methods
|
||||
.transfer(user2, web3.utils.toWei('1000'))
|
||||
@ -428,7 +402,7 @@ describe('SideStaking unit test', () => {
|
||||
|
||||
it('#getvestingAmount ', async () => {
|
||||
expect(await sideStaking.getvestingAmount(sideStakingAddress, erc20Token)).to.equal(
|
||||
'10000'
|
||||
'0'
|
||||
)
|
||||
})
|
||||
it('#getvestingLastBlock ', async () => {
|
||||
@ -437,38 +411,12 @@ describe('SideStaking unit test', () => {
|
||||
).to.equal(initialBlock.toString())
|
||||
})
|
||||
|
||||
it('#getvestingEndBlock ', async () => {
|
||||
expect(
|
||||
await sideStaking.getvestingEndBlock(sideStakingAddress, erc20Token)
|
||||
).to.equal((initialBlock + vestedBlocks).toString())
|
||||
})
|
||||
it('#getvestingAmountSoFar ', async () => {
|
||||
expect(
|
||||
await sideStaking.getvestingAmountSoFar(sideStakingAddress, erc20Token)
|
||||
).to.equal('0')
|
||||
})
|
||||
|
||||
it('#getVesting ', async () => {
|
||||
expect(await erc20Contract.methods.balanceOf(factoryOwner).call()).to.equal('0')
|
||||
|
||||
const tx = await sideStaking.getVesting(
|
||||
factoryOwner,
|
||||
sideStakingAddress,
|
||||
erc20Token
|
||||
)
|
||||
const collector = await erc20Contract.methods.getPaymentCollector().call()
|
||||
expect(
|
||||
await sideStaking.unitsToAmount(
|
||||
erc20Token,
|
||||
await erc20Contract.methods.balanceOf(collector).call()
|
||||
)
|
||||
).to.equal(await sideStaking.getvestingAmountSoFar(sideStakingAddress, erc20Token))
|
||||
|
||||
expect(
|
||||
await sideStaking.getvestingLastBlock(sideStakingAddress, erc20Token)
|
||||
).to.equal((await web3.eth.getBlockNumber()).toString())
|
||||
})
|
||||
|
||||
it('#swapExactAmountIn - should swap', async () => {
|
||||
const transferAmount = await amountToUnits(web3, contracts.usdcAddress, '1000') // 1000 USDC
|
||||
await usdcContract.methods
|
||||
|
@ -62,6 +62,7 @@ describe('Datatoken', () => {
|
||||
nftFactory = new NftFactory(
|
||||
contracts.erc721FactoryAddress,
|
||||
web3,
|
||||
8996,
|
||||
ERC721Factory.abi as AbiItem[]
|
||||
)
|
||||
const nftData: NftCreateData = {
|
||||
@ -74,7 +75,7 @@ describe('Datatoken', () => {
|
||||
}
|
||||
|
||||
nftAddress = await nftFactory.createNFT(nftOwner, nftData)
|
||||
nftDatatoken = new Nft(web3, ERC721Template.abi as AbiItem[])
|
||||
nftDatatoken = new Nft(web3, 8996, ERC721Template.abi as AbiItem[])
|
||||
})
|
||||
|
||||
it('#createERC20 - should create a new ERC20 DT from NFT contract', async () => {
|
||||
@ -98,6 +99,7 @@ describe('Datatoken', () => {
|
||||
it('should initialize DT20 Instance', async () => {
|
||||
datatoken = new Datatoken(
|
||||
web3,
|
||||
8996,
|
||||
ERC20Template.abi as AbiItem[],
|
||||
ERC20TemplateEnterprise.abi as AbiItem[]
|
||||
)
|
||||
|
@ -39,6 +39,7 @@ describe('NFT', () => {
|
||||
nftFactory = new NftFactory(
|
||||
contracts.erc721FactoryAddress,
|
||||
web3,
|
||||
8996,
|
||||
ERC721Factory.abi as AbiItem[]
|
||||
)
|
||||
const nftData: NftCreateData = {
|
||||
@ -51,7 +52,7 @@ describe('NFT', () => {
|
||||
}
|
||||
|
||||
nftAddress = await nftFactory.createNFT(nftOwner, nftData)
|
||||
nftDatatoken = new Nft(web3, ERC721Template.abi as AbiItem[])
|
||||
nftDatatoken = new Nft(web3, 8996, ERC721Template.abi as AbiItem[])
|
||||
})
|
||||
it('#getTokenURI', async () => {
|
||||
const tokenURI = await nftDatatoken.getTokenURI(nftAddress, 1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user