From fa2a1f8944b5a158a142929bf89e09e21c169b8f Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 30 Sep 2020 23:10:53 -0700 Subject: [PATCH] remove need of an account for some calls --- src/balancer/OceanPool.ts | 46 +++++---------- src/balancer/Pool.ts | 86 +++++++---------------------- src/datatokens/Datatokens.ts | 28 +++------- test/unit/Datatokens.test.ts | 4 +- test/unit/balancer/Balancer.test.ts | 32 +++++------ 5 files changed, 60 insertions(+), 136 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 0a7ac1f6..db10fdfd 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -118,27 +118,25 @@ export class OceanPool extends Pool { /** * Get Ocean Token balance of a pool - * @param {String} account * @param {String} poolAddress * @return {String} */ - public async getOceanReserve(account: string, poolAddress: string): Promise { + public async getOceanReserve(poolAddress: string): Promise { if (this.oceanAddress == null) { console.error('oceanAddress is not defined') return null } - return super.getReserve(account, poolAddress, this.oceanAddress) + return super.getReserve(poolAddress, this.oceanAddress) } /** * Get Data Token balance of a pool - * @param {String} account * @param {String} poolAddress * @return {String} */ - public async getDTReserve(account: string, poolAddress: string): Promise { + public async getDTReserve(poolAddress: string): Promise { await this.getDTAddress(poolAddress) - return super.getReserve(account, poolAddress, this.dtAddress) + return super.getReserve(poolAddress, this.dtAddress) } /** @@ -332,12 +330,12 @@ export class OceanPool extends Pool { * @param {String} poolAddress * @return {String} */ - public async getDTPrice(account: string, poolAddress: string): Promise { + public async getDTPrice(poolAddress: string): Promise { if (this.oceanAddress == null) { console.error('oceanAddress is not defined') return null } - return this.getOceanNeeded(account, poolAddress, '1') + return this.getOceanNeeded(poolAddress, '1') } /** @@ -346,11 +344,9 @@ export class OceanPool extends Pool { * @param {String} dtAddress * @return {String[]} */ - public async searchPoolforDT(account: string, dtAddress: string): Promise { + public async searchPoolforDT(dtAddress: string): Promise { const result: string[] = [] - const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { - from: account - }) + const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress) const events = await factory.getPastEvents('BPoolRegistered', { filter: {}, fromBlock: 0, @@ -363,25 +359,13 @@ export class OceanPool extends Pool { return result } - public async getOceanNeeded( - account: string, - poolAddress: string, - dtRequired: string - ): Promise { + public async getOceanNeeded(poolAddress: string, dtRequired: string): Promise { await this.getDTAddress(poolAddress) - const tokenBalanceIn = await this.getReserve(account, poolAddress, this.oceanAddress) - const tokenWeightIn = await this.getDenormalizedWeight( - account, - poolAddress, - this.oceanAddress - ) - const tokenBalanceOut = await this.getReserve(account, poolAddress, this.dtAddress) - const tokenWeightOut = await this.getDenormalizedWeight( - account, - poolAddress, - this.dtAddress - ) - const swapFee = await this.getSwapFee(account, poolAddress) + const tokenBalanceIn = await this.getReserve(poolAddress, this.oceanAddress) + const tokenWeightIn = await this.getDenormalizedWeight(poolAddress, this.oceanAddress) + const tokenBalanceOut = await this.getReserve(poolAddress, this.dtAddress) + const tokenWeightOut = await this.getDenormalizedWeight(poolAddress, this.dtAddress) + const swapFee = await this.getSwapFee(poolAddress) return super.calcInGivenOut( tokenBalanceIn, tokenWeightIn, @@ -428,7 +412,7 @@ export class OceanPool extends Pool { /** * Get all actions from a pool (join,exit,swap) * @param {String} poolAddress Pool address - * @param {String} account + * @param {String} account Optional, filter for this address * @return {PoolTransaction[]} */ public async getPoolLogs( diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index cb930518..3df7309e 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -256,14 +256,11 @@ export class Pool extends PoolFactory { /** * Get number of tokens composing this pool - * @param {String} account * @param {String} poolAddress * @return {String} */ - async getNumTokens(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + async getNumTokens(poolAddress: string): Promise { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let result = null try { result = await pool.methods.getNumTokens().call() @@ -307,14 +304,11 @@ export class Pool extends PoolFactory { /** * Get controller address of this pool - * @param {String} account * @param {String} poolAddress * @return {String} */ - async getController(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + async getController(poolAddress: string): Promise { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let result = null try { result = await pool.methods.getController().call() @@ -352,15 +346,12 @@ export class Pool extends PoolFactory { /** * Get if a token is bounded to a pool - * @param {String} account * @param {String} poolAddress * @param {String} token Address of the token * @return {Boolean} */ - async isBound(account: string, poolAddress: string, token: string): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + async isBound(poolAddress: string, token: string): Promise { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let result = null try { result = await pool.methods.isBound(token).call() @@ -372,15 +363,12 @@ export class Pool extends PoolFactory { /** * Get how many tokens are in the pool - * @param {String} account * @param {String} poolAddress * @param {String} token Address of the token * @return {String} */ - async getReserve(account: string, poolAddress: string, token: string): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + async getReserve(poolAddress: string, token: string): Promise { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let amount = null try { const result = await pool.methods.getBalance(token).call() @@ -393,14 +381,11 @@ export class Pool extends PoolFactory { /** * Get if a pool is finalized - * @param {String} account * @param {String} poolAddress * @return {Boolean} */ - async isFinalized(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + async isFinalized(poolAddress: string): Promise { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let result = null try { result = await pool.methods.isFinalized().call() @@ -412,14 +397,11 @@ export class Pool extends PoolFactory { /** * Get pool fee - * @param {String} account * @param {String} poolAddress * @return {String} Swap fee in wei */ - async getSwapFee(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + async getSwapFee(poolAddress: string): Promise { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let fee = null try { const result = await pool.methods.getSwapFee().call() @@ -432,19 +414,12 @@ export class Pool extends PoolFactory { /** * The normalized weight of a token. The combined normalized weights of all tokens will sum up to 1. (Note: the actual sum may be 1 plus or minus a few wei due to division precision loss) - * @param {String} account * @param {String} poolAddress * @param {String} token * @return {String} */ - async getNormalizedWeight( - account: string, - poolAddress: string, - token: string - ): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + async getNormalizedWeight(poolAddress: string, token: string): Promise { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let weight = null try { const result = await pool.methods.getNormalizedWeight(token).call() @@ -457,19 +432,12 @@ export class Pool extends PoolFactory { /** * getDenormalizedWeight of a token in pool - * @param {String} account * @param {String} poolAddress * @param {String} token * @return {String} */ - async getDenormalizedWeight( - account: string, - poolAddress: string, - token: string - ): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + async getDenormalizedWeight(poolAddress: string, token: string): Promise { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let weight = null try { const result = await pool.methods.getDenormalizedWeight(token).call() @@ -482,17 +450,11 @@ export class Pool extends PoolFactory { /** * getTotalDenormalizedWeight in pool - * @param {String} account * @param {String} poolAddress * @return {String} */ - async getTotalDenormalizedWeight( - account: string, - poolAddress: string - ): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + async getTotalDenormalizedWeight(poolAddress: string): Promise { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let weight = null try { const result = await pool.methods.getTotalDenormalizedWeight().call() @@ -792,21 +754,17 @@ export class Pool extends PoolFactory { /** * Get Spot Price of swaping tokenIn to tokenOut - * @param {String} account * @param {String} poolAddress * @param {String} tokenIn * @param {String} tokenOut * @return {String} */ async getSpotPrice( - account: string, poolAddress: string, tokenIn: string, tokenOut: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let price = null try { const result = await pool.methods.getSpotPrice(tokenIn, tokenOut).call() @@ -819,21 +777,17 @@ export class Pool extends PoolFactory { /** * Get Spot Price of swaping tokenIn to tokenOut without fees - * @param {String} account * @param {String} poolAddress * @param {String} tokenIn * @param {String} tokenOut * @return {String} */ async getSpotPriceSansFee( - account: string, poolAddress: string, tokenIn: string, tokenOut: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let price = null try { const result = await pool.methods.getSpotPriceSansFee(tokenIn, tokenOut).call() diff --git a/src/datatokens/Datatokens.ts b/src/datatokens/Datatokens.ts index cde50a1c..c66ed354 100644 --- a/src/datatokens/Datatokens.ts +++ b/src/datatokens/Datatokens.ts @@ -287,52 +287,40 @@ export class DataTokens { /** Get Blob * @param {String} dataTokenAddress - * @param {String} address * @return {Promise} string */ - public async getBlob(dataTokenAddress: string, address: string): Promise { - const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress, { - from: address - }) + public async getBlob(dataTokenAddress: string): Promise { + const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress) const trxReceipt = await datatoken.methods.blob().call() return trxReceipt } /** Get Name * @param {String} dataTokenAddress - * @param {String} address * @return {Promise} string */ - public async getName(dataTokenAddress: string, address: string): Promise { - const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress, { - from: address - }) + public async getName(dataTokenAddress: string): Promise { + const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress) const trxReceipt = await datatoken.methods.name().call() return trxReceipt } /** Get Symbol * @param {String} dataTokenAddress - * @param {String} address * @return {Promise} string */ - public async getSymbol(dataTokenAddress: string, address: string): Promise { - const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress, { - from: address - }) + public async getSymbol(dataTokenAddress: string): Promise { + const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress) const trxReceipt = await datatoken.methods.symbol().call() return trxReceipt } /** Get Cap * @param {String} dataTokenAddress - * @param {String} address * @return {Promise} string */ - public async getCap(dataTokenAddress: string, address: string): Promise { - const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress, { - from: address - }) + public async getCap(dataTokenAddress: string): Promise { + const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress) const trxReceipt = await datatoken.methods.cap().call() return this.web3.utils.fromWei(trxReceipt) } diff --git a/test/unit/Datatokens.test.ts b/test/unit/Datatokens.test.ts index 177aa5e5..f760f04c 100644 --- a/test/unit/Datatokens.test.ts +++ b/test/unit/Datatokens.test.ts @@ -52,8 +52,8 @@ describe('DataTokens', () => { tokenAddress = await datatoken.create(blob, minter) assert(tokenAddress !== null) - const tokenName = await datatoken.getName(tokenAddress, minter) - const tokenSymbol = await datatoken.getSymbol(tokenAddress, minter) + const tokenName = await datatoken.getName(tokenAddress) + const tokenSymbol = await datatoken.getSymbol(tokenAddress) assert(tokenName !== null || tokenName !== '') assert(tokenSymbol !== null || tokenSymbol !== '') }) diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 063c56b7..e166088b 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -118,7 +118,7 @@ describe('Balancer flow', () => { 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) + const n = await Pool.getNumTokens(alicePoolAddress) assert(String(n) === '2', 'unexpected num tokens: ' + n) }) it('Get pool information', async () => { @@ -129,13 +129,12 @@ describe('Balancer flow', () => { }) it('Get pool swap fee', async () => { - const currentSwapFee = await Pool.getSwapFee(alice, alicePoolAddress) + const currentSwapFee = await Pool.getSwapFee(alicePoolAddress) assert(currentSwapFee === '0.02') }) it('Get spot price for swapping', async () => { const spotPrice = await Pool.getSpotPrice( - alice, alicePoolAddress, tokenAddress, oceanTokenAddress @@ -145,7 +144,6 @@ describe('Balancer flow', () => { it('Get spot price for swapping without fees', async () => { const spotPrice = await Pool.getSpotPriceSansFee( - alice, alicePoolAddress, tokenAddress, oceanTokenAddress @@ -154,15 +152,15 @@ describe('Balancer flow', () => { }) it('Get dtPrice from the pool ', async () => { - currentDtPrice = await Pool.getDTPrice(alice, alicePoolAddress) + currentDtPrice = await Pool.getDTPrice(alicePoolAddress) assert(Number(currentDtPrice) > 0) }) it('Get dtToken pool reserve ', async () => { - const currentDtReserve = await Pool.getDTReserve(alice, alicePoolAddress) + const currentDtReserve = await Pool.getDTReserve(alicePoolAddress) assert(Number(currentDtReserve) > 0) }) it('Get Ocean pool reserve ', async () => { - const currentOceanReserve = await Pool.getOceanReserve(alice, alicePoolAddress) + const currentOceanReserve = await Pool.getOceanReserve(alicePoolAddress) assert(Number(currentOceanReserve) > 0) }) it('Get total supply of pool tokens', async () => { @@ -170,12 +168,12 @@ describe('Balancer flow', () => { assert(Number(totalSupply) > 0) }) it('Get amount of Ocean needed to buy 1 dtToken', async () => { - const requiredOcean = await Pool.getOceanNeeded(alice, alicePoolAddress, '1') + const requiredOcean = await Pool.getOceanNeeded(alicePoolAddress, '1') assert(Number(requiredOcean) > 0) }) it('Bob should search for pools with this DT', async () => { - const pools = await Pool.searchPoolforDT(bob, tokenAddress) + const pools = await Pool.searchPoolforDT(tokenAddress) assert(pools.length > 0) greatPool = pools[0] }) @@ -188,7 +186,7 @@ describe('Balancer flow', () => { assert(Number(bobOceanBalance) > 0) }) it('Bob should add DT liquidity to pool ', async () => { - const currentDtReserve = await Pool.getDTReserve(bob, greatPool) + const currentDtReserve = await Pool.getDTReserve(greatPool) if (consoleDebug) console.log('currentDtReserve:' + currentDtReserve) const bobDtBalance = await datatoken.balance(tokenAddress, bob) if (consoleDebug) console.log('BOB DT Balance:' + bobDtBalance) @@ -196,7 +194,7 @@ describe('Balancer flow', () => { const newbobDtBalance = await datatoken.balance(tokenAddress, bob) - const newDtReserve = await Pool.getDTReserve(bob, greatPool) + const newDtReserve = await Pool.getDTReserve(greatPool) const sharesBalance = await Pool.sharesBalance(bob, greatPool) if (consoleDebug) console.log('newDtReserve:' + newDtReserve) @@ -208,7 +206,7 @@ describe('Balancer flow', () => { }) it('Bob should remove DT liquidity from pool ', async () => { - const currentDtReserve = await Pool.getDTReserve(bob, greatPool) + const currentDtReserve = await Pool.getDTReserve(greatPool) if (consoleDebug) console.log('currentDtReserve:' + currentDtReserve) const bobDtBalance = await datatoken.balance(tokenAddress, bob) if (consoleDebug) console.log('bobDtBalance:' + bobDtBalance) @@ -216,7 +214,7 @@ describe('Balancer flow', () => { if (consoleDebug) console.log('poolShares:' + poolShares) await Pool.removeDTLiquidity(bob, greatPool, '0.75', poolShares) - const newDtReserve = await Pool.getDTReserve(bob, greatPool) + const newDtReserve = await Pool.getDTReserve(greatPool) if (consoleDebug) console.log('newDtReserve:' + newDtReserve) const newbobDtBalance = await datatoken.balance(tokenAddress, bob) if (consoleDebug) console.log('newbobDtBalance:' + newbobDtBalance) @@ -228,7 +226,7 @@ describe('Balancer flow', () => { }) it('Bob should add Ocean liquidity to pool ', async () => { - const currentDtReserve = await Pool.getOceanReserve(bob, greatPool) + const currentDtReserve = await Pool.getOceanReserve(greatPool) const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob) if (consoleDebug) console.log('currentDtReserve:' + currentDtReserve) if (consoleDebug) console.log('bobDtBalance:' + bobDtBalance) @@ -237,7 +235,7 @@ describe('Balancer flow', () => { const newbobDtBalance = await datatoken.balance(oceanTokenAddress, bob) - const newDtReserve = await Pool.getOceanReserve(bob, greatPool) + const newDtReserve = await Pool.getOceanReserve(greatPool) const sharesBalance = await Pool.sharesBalance(bob, greatPool) if (consoleDebug) console.log('newDtReserve:' + newDtReserve) @@ -249,7 +247,7 @@ describe('Balancer flow', () => { }) it('Bob should remove Ocean liquidity from pool ', async () => { - const currentDtReserve = await Pool.getOceanReserve(bob, greatPool) + const currentDtReserve = await Pool.getOceanReserve(greatPool) const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob) const poolShares = await Pool.sharesBalance(bob, greatPool) @@ -259,7 +257,7 @@ describe('Balancer flow', () => { await Pool.removeOceanLiquidity(bob, greatPool, '0.75', poolShares) - const newDtReserve = await Pool.getOceanReserve(bob, greatPool) + const newDtReserve = await Pool.getOceanReserve(greatPool) const newbobDtBalance = await datatoken.balance(oceanTokenAddress, bob) const newpoolShares = await Pool.sharesBalance(bob, greatPool)