mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Merge pull request #230 from oceanprotocol/feature/setup-pool
feature/setup-pool
This commit is contained in:
commit
b9f32775d7
@ -40,7 +40,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@ethereum-navigator/navigator": "^0.5.0",
|
"@ethereum-navigator/navigator": "^0.5.0",
|
||||||
"@oceanprotocol/contracts": "^0.3.4",
|
"@oceanprotocol/contracts": "^0.3.5",
|
||||||
"decimal.js": "^10.2.0",
|
"decimal.js": "^10.2.0",
|
||||||
"fs": "0.0.1-security",
|
"fs": "0.0.1-security",
|
||||||
"node-fetch": "^2.6.0",
|
"node-fetch": "^2.6.0",
|
||||||
|
@ -26,9 +26,10 @@ export class OceanPool extends Pool {
|
|||||||
/**
|
/**
|
||||||
* Create DataToken pool
|
* Create DataToken pool
|
||||||
@param {String} account
|
@param {String} account
|
||||||
* @param {String} token Data Token address
|
* @param {String} token DataToken address
|
||||||
* @param {String} amount Data Token amount
|
* @param {String} amount DataToken amount
|
||||||
* @param {String} weight Data Token weight
|
* @param {String} weight DataToken weight
|
||||||
|
* @param {String} fee Swap fee (as float)
|
||||||
* @return {String}
|
* @return {String}
|
||||||
*/
|
*/
|
||||||
public async createDTPool(
|
public async createDTPool(
|
||||||
@ -36,8 +37,7 @@ export class OceanPool extends Pool {
|
|||||||
token: string,
|
token: string,
|
||||||
amount: string,
|
amount: string,
|
||||||
weight: string,
|
weight: string,
|
||||||
fee: string,
|
fee: string
|
||||||
finalize = true
|
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
if (this.oceanAddress == null) {
|
if (this.oceanAddress == null) {
|
||||||
console.error('oceanAddress is not defined')
|
console.error('oceanAddress is not defined')
|
||||||
@ -50,22 +50,33 @@ export class OceanPool extends Pool {
|
|||||||
const address = await super.createPool(account)
|
const address = await super.createPool(account)
|
||||||
const oceanWeight = 10 - parseFloat(weight)
|
const oceanWeight = 10 - parseFloat(weight)
|
||||||
const oceanAmount = (parseFloat(amount) * oceanWeight) / parseFloat(weight)
|
const oceanAmount = (parseFloat(amount) * oceanWeight) / parseFloat(weight)
|
||||||
const tokens = [
|
|
||||||
{
|
|
||||||
address: token,
|
|
||||||
amount: String(amount),
|
|
||||||
weight: String(weight)
|
|
||||||
},
|
|
||||||
{
|
|
||||||
address: this.oceanAddress,
|
|
||||||
amount: String(oceanAmount),
|
|
||||||
weight: String(oceanWeight)
|
|
||||||
}
|
|
||||||
]
|
|
||||||
this.dtAddress = token
|
this.dtAddress = token
|
||||||
await super.addToPool(account, address, tokens)
|
|
||||||
await super.setSwapFee(account, address, fee)
|
await this.approve(
|
||||||
if (finalize) await super.finalize(account, address)
|
account,
|
||||||
|
token,
|
||||||
|
address,
|
||||||
|
this.web3.utils.toWei(String(amount)) as any
|
||||||
|
)
|
||||||
|
await this.approve(
|
||||||
|
account,
|
||||||
|
this.oceanAddress,
|
||||||
|
address,
|
||||||
|
this.web3.utils.toWei(String(oceanAmount)) as any
|
||||||
|
)
|
||||||
|
|
||||||
|
await super.setup(
|
||||||
|
account,
|
||||||
|
address,
|
||||||
|
token,
|
||||||
|
this.web3.utils.toWei(String(amount)),
|
||||||
|
this.web3.utils.toWei(String(weight)),
|
||||||
|
this.oceanAddress,
|
||||||
|
this.web3.utils.toWei(String(oceanAmount)),
|
||||||
|
this.web3.utils.toWei(String(oceanWeight)),
|
||||||
|
this.web3.utils.toWei(fee)
|
||||||
|
)
|
||||||
|
|
||||||
return address
|
return address
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,8 +32,54 @@ export class Pool extends PoolFactory {
|
|||||||
* Creates a new pool
|
* Creates a new pool
|
||||||
*/
|
*/
|
||||||
async createPool(account: string): Promise<string> {
|
async createPool(account: string): Promise<string> {
|
||||||
const pooladdress = await super.createPool(account)
|
return await super.createPool(account)
|
||||||
return pooladdress
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup a new pool by setting datatoken, base token, swap fee and
|
||||||
|
* finalizing the pool to make it public.
|
||||||
|
*
|
||||||
|
* @param {String} account ethereum address to use for sending this transaction
|
||||||
|
* @param {String} poolAddress address of new Balancer Pool
|
||||||
|
* @param {String} dataToken address of datatoken ERC20 contract
|
||||||
|
* @param {String} dataTokenAmount in wei
|
||||||
|
* @param {String} dataTokenWeight in wei
|
||||||
|
* @param {String} baseToken address of base token ERC20 contract
|
||||||
|
* @param {String} baseTokenAmount in wei
|
||||||
|
* @param {String} baseTokenWeight in wei
|
||||||
|
* @param {String} swapFee in wei
|
||||||
|
*/
|
||||||
|
async setup(
|
||||||
|
account: string,
|
||||||
|
poolAddress: string,
|
||||||
|
dataToken: string,
|
||||||
|
dataTokenAmount: string,
|
||||||
|
dataTokenWeight: string,
|
||||||
|
baseToken: string,
|
||||||
|
baseTokenAmount: string,
|
||||||
|
baseTokenWeight: string,
|
||||||
|
swapFee: string
|
||||||
|
): Promise<string> {
|
||||||
|
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, {
|
||||||
|
from: account
|
||||||
|
})
|
||||||
|
let result = null
|
||||||
|
try {
|
||||||
|
result = await pool.methods
|
||||||
|
.setup(
|
||||||
|
dataToken,
|
||||||
|
dataTokenAmount,
|
||||||
|
dataTokenWeight,
|
||||||
|
baseToken,
|
||||||
|
baseTokenAmount,
|
||||||
|
baseTokenWeight,
|
||||||
|
swapFee
|
||||||
|
)
|
||||||
|
.send({ from: account, gas: this.GASLIMIT_DEFAULT })
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Pool.setup failed:' + e)
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -105,11 +105,15 @@ describe('Balancer flow', () => {
|
|||||||
await oceandatatoken.mint(oceanTokenAddress, alice, tokenAmount)
|
await oceandatatoken.mint(oceanTokenAddress, alice, tokenAmount)
|
||||||
})
|
})
|
||||||
it('Alice transfers 200 ocean token to Bob', async () => {
|
it('Alice transfers 200 ocean token to Bob', async () => {
|
||||||
const ts = await datatoken.transfer(oceanTokenAddress, bob, transferAmount, alice)
|
await datatoken.transfer(oceanTokenAddress, bob, transferAmount, alice)
|
||||||
})
|
})
|
||||||
it('Alice creates a new OceanPool pool', async () => {
|
it('Alice creates a new OceanPool pool', async () => {
|
||||||
/// new pool with total DT = 45 , dt weight=90% with swap fee 2%
|
/// new pool with total DT = 45 , dt weight=90% with swap fee 2%
|
||||||
alicePoolAddress = await Pool.createDTPool(alice, tokenAddress, 45, 9, '0.02')
|
alicePoolAddress = await Pool.createDTPool(alice, tokenAddress, '45', '9', '0.02')
|
||||||
|
const s = await Pool.totalSupply(alicePoolAddress)
|
||||||
|
assert(String(s) === '100', 'totalSupply does not match: ' + s)
|
||||||
|
const n = await Pool.getNumTokens(alice, alicePoolAddress)
|
||||||
|
assert(String(n) === '2', 'unexpected num tokens: ' + n)
|
||||||
})
|
})
|
||||||
it('Get pool information', async () => {
|
it('Get pool information', async () => {
|
||||||
const currentTokens = await Pool.getCurrentTokens(alice, alicePoolAddress)
|
const currentTokens = await Pool.getCurrentTokens(alice, alicePoolAddress)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user