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

Merge branch 'v4main' into issue-1425-use-proper-config-in-constructors

This commit is contained in:
mihaisc 2022-05-03 13:01:01 +03:00
commit c7dfe50b92
No known key found for this signature in database
GPG Key ID: 4FB0C2329B4C6E29
7 changed files with 3349 additions and 2115 deletions

View File

@ -58,6 +58,8 @@ jobs:
working-directory: ${{ github.workspace }}/barge
run: |
bash -x start_ocean.sh --no-aquarius --no-elasticsearch --no-provider --no-dashboard 2>&1 > start_ocean.log &
env:
CONTRACTS_VERSION: v1.0.0-alpha.31
- run: npm ci
- name: Wait for contracts deployment
working-directory: ${{ github.workspace }}/barge
@ -115,6 +117,8 @@ jobs:
working-directory: ${{ github.workspace }}/barge
run: |
bash -x start_ocean.sh --with-provider2 --no-dashboard --with-c2d 2>&1 > start_ocean.log &
env:
CONTRACTS_VERSION: v1.0.0-alpha.31
- run: npm ci
- run: npm run build:metadata

File diff suppressed because it is too large Load Diff

2372
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
{
"name": "@oceanprotocol/lib",
"source": "./src/index.ts",
"version": "1.0.0-next.37",
"version": "1.0.0-next.38",
"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.31",
"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.31",
"@types/node-fetch": "^3.0.3",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
@ -87,17 +87,17 @@
"eslint-plugin-prettier": "^4.0.0",
"fs": "0.0.1-security",
"microbundle": "^0.15.0",
"mocha": "^9.2.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": [

View File

@ -1082,177 +1082,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

View File

@ -294,21 +294,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 +318,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'
@ -826,25 +796,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 +818,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'

View File

@ -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