From bbd465992b25e824d5c8b8934ce2fa61629ed8dc Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 19 Aug 2020 12:56:48 +0200 Subject: [PATCH] lots of ABI typings --- package.json | 1 - src/balancer/OceanPool.ts | 13 +++++-- src/balancer/Pool.ts | 65 +++++++++++++++++---------------- src/balancer/PoolFactory.ts | 15 ++++---- src/datatokens/Datatokens.ts | 20 +++++++--- src/ocean/Account.ts | 1 - src/ocean/Ocean.ts | 6 --- test/BalancerContractHandler.ts | 10 +++-- test/TestContractHandler.ts | 10 +++-- 9 files changed, 76 insertions(+), 65 deletions(-) diff --git a/package.json b/package.json index 387936ad..39b812ee 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "dependencies": { "@ethereum-navigator/navigator": "^0.5.0", "@oceanprotocol/contracts": "^0.3.4", - "bignumber.js": "^9.0.0", "decimal.js": "^10.2.0", "fs": "0.0.1-security", "node-fetch": "^2.6.0", diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index e69bcfc4..68622432 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -1,19 +1,23 @@ import Web3 from 'web3' +import { AbiItem } from 'web3-utils/types' import { Pool } from './Pool' +/** + * Ocean Pools submodule exposed under ocean.pool + */ export class OceanPool extends Pool { public oceanAddress: string = null public dtAddress: string = null constructor( web3: Web3, - FactoryABI: any = null, - PoolABI: any = null, + factoryABI: AbiItem | AbiItem[] = null, + poolABI: AbiItem | AbiItem[] = null, factoryAddress: string = null, oceanAddress: string = null, gaslimit?: number ) { - super(web3, FactoryABI, PoolABI, factoryAddress, gaslimit) + super(web3, factoryABI, poolABI, factoryAddress, gaslimit) if (oceanAddress) { this.oceanAddress = oceanAddress } @@ -76,6 +80,7 @@ export class OceanPool extends Pool { const tokens = await this.getCurrentTokens(account, poolAddress) let token for (token of tokens) { + // TODO: Potential timing attack, left side: true if (token !== this.oceanAddress) this.dtAddress = token } return this.dtAddress @@ -314,7 +319,7 @@ export class OceanPool extends Pool { */ public async searchPoolforDT(account: string, dtAddress: string): Promise { const result: string[] = [] - const factory = new this.web3.eth.Contract(this.FactoryABI, this.factoryAddress, { + const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { from: account }) const events = await factory.getPastEvents('SPoolRegistered', { diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 35c5cbf0..083dcca2 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -1,6 +1,7 @@ import Web3 from 'web3' +import { AbiItem } from 'web3-utils/types' import Decimal from 'decimal.js' -import * as jsonPoolABI from '@oceanprotocol/contracts/artifacts/SPool.json' +import * as jsonpoolABI from '@oceanprotocol/contracts/artifacts/SPool.json' import { PoolFactory } from './PoolFactory' /** @@ -13,18 +14,18 @@ export interface TokensToAdd { } export class Pool extends PoolFactory { - private PoolABI: any + private poolABI: AbiItem | AbiItem[] constructor( web3: Web3, - FactoryABI: any = null, - PoolABI: any = null, + factoryABI: AbiItem | AbiItem[] = null, + poolABI: AbiItem | AbiItem[] = null, factoryAddress: string = null, gaslimit?: number ) { - super(web3, FactoryABI, factoryAddress, gaslimit) - if (PoolABI) this.PoolABI = PoolABI - else this.PoolABI = jsonPoolABI.abi + super(web3, factoryABI, factoryAddress, gaslimit) + if (poolABI) this.poolABI = poolABI + else this.poolABI = jsonpoolABI.abi } /** @@ -142,7 +143,7 @@ export class Pool extends PoolFactory { poolAddress: string, tokens: TokensToAdd[] ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) @@ -176,7 +177,7 @@ export class Pool extends PoolFactory { * @param {String} fee (will be converted to wei) */ async setSwapFee(account: string, poolAddress: string, fee: string): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -196,7 +197,7 @@ export class Pool extends PoolFactory { * @param {String} poolAddress */ async finalize(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -216,7 +217,7 @@ export class Pool extends PoolFactory { * @param {String} poolAddress */ async getNumTokens(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -235,7 +236,7 @@ export class Pool extends PoolFactory { * @return {String[]} */ async getCurrentTokens(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -254,7 +255,7 @@ export class Pool extends PoolFactory { * @return {String[]} */ async getFinalTokens(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -273,7 +274,7 @@ export class Pool extends PoolFactory { * @return {String} */ async getController(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -297,7 +298,7 @@ export class Pool extends PoolFactory { poolAddress: string, controllerAddress: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -319,7 +320,7 @@ export class Pool extends PoolFactory { * @return {Boolean} */ async isBound(account: string, poolAddress: string, token: string): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -339,7 +340,7 @@ export class Pool extends PoolFactory { * @return {String} */ async getReserve(account: string, poolAddress: string, token: string): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let amount = null @@ -359,7 +360,7 @@ export class Pool extends PoolFactory { * @return {Boolean} */ async isFinalized(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -378,7 +379,7 @@ export class Pool extends PoolFactory { * @return {String} Swap fee in wei */ async getSwapFee(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let fee = null @@ -403,7 +404,7 @@ export class Pool extends PoolFactory { poolAddress: string, token: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let weight = null @@ -428,7 +429,7 @@ export class Pool extends PoolFactory { poolAddress: string, token: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let weight = null @@ -451,7 +452,7 @@ export class Pool extends PoolFactory { account: string, poolAddress: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let weight = null @@ -484,7 +485,7 @@ export class Pool extends PoolFactory { minAmountOut: string, maxPrice: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -524,7 +525,7 @@ export class Pool extends PoolFactory { minAmountOut: string, maxPrice: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -558,7 +559,7 @@ export class Pool extends PoolFactory { poolAmountOut: string, maxAmountsIn: string[] ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) const weiMaxAmountsIn = [] @@ -595,7 +596,7 @@ export class Pool extends PoolFactory { poolAmountIn: string, minAmountsOut: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) const weiMinAmountsOut = [] @@ -630,7 +631,7 @@ export class Pool extends PoolFactory { tokenAmountIn: string, minPoolAmountOut: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -664,7 +665,7 @@ export class Pool extends PoolFactory { poolAmountOut: string, maxAmountIn: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -698,7 +699,7 @@ export class Pool extends PoolFactory { poolAmountIn: string, minTokenAmountOut: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -732,7 +733,7 @@ export class Pool extends PoolFactory { tokenAmountOut: string, maxPoolAmountIn: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let result = null @@ -764,7 +765,7 @@ export class Pool extends PoolFactory { tokenIn: string, tokenOut: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let price = null @@ -791,7 +792,7 @@ export class Pool extends PoolFactory { tokenIn: string, tokenOut: string ): Promise { - const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) let price = null diff --git a/src/balancer/PoolFactory.ts b/src/balancer/PoolFactory.ts index 809bf247..73243bc5 100644 --- a/src/balancer/PoolFactory.ts +++ b/src/balancer/PoolFactory.ts @@ -1,22 +1,23 @@ import Web3 from 'web3' +import { AbiItem } from 'web3-utils/types' import * as jsonFactoryABI from '@oceanprotocol/contracts/artifacts/SFactory.json' export class PoolFactory { public GASLIMIT_DEFAULT = 5000000 - public web3: any = null - public FactoryABI: any - public factoryAddress: any + public web3: Web3 = null + public factoryABI: AbiItem | AbiItem[] + public factoryAddress: string constructor( web3: Web3, - FactoryABI: any = null, + factoryABI: AbiItem | AbiItem[] = null, factoryAddress: string = null, gaslimit?: number ) { this.web3 = web3 - if (FactoryABI) this.FactoryABI = FactoryABI - else this.FactoryABI = jsonFactoryABI.abi + if (factoryABI) this.factoryABI = factoryABI + else this.factoryABI = jsonFactoryABI.abi if (factoryAddress) { this.factoryAddress = factoryAddress } @@ -37,7 +38,7 @@ export class PoolFactory { return null } - const factory = new this.web3.eth.Contract(this.FactoryABI, this.factoryAddress, { + const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { from: account }) diff --git a/src/datatokens/Datatokens.ts b/src/datatokens/Datatokens.ts index acb8b510..f6305da8 100644 --- a/src/datatokens/Datatokens.ts +++ b/src/datatokens/Datatokens.ts @@ -1,14 +1,17 @@ -const defaultFactoryABI = require('@oceanprotocol/contracts/artifacts/DTFactory.json') -const defaultDatatokensABI = require('@oceanprotocol/contracts/artifacts/DataTokenTemplate.json') +import Web3 from 'web3' +import { AbiItem } from 'web3-utils/types' + +import defaultFactoryABI from '@oceanprotocol/contracts/artifacts/DTFactory.json' +import defaultDatatokensABI from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' /** * Provides a interface to DataTokens */ export class DataTokens { public factoryAddress: string - public factoryABI: any - public datatokensABI: any - public web3: any + public factoryABI: AbiItem | AbiItem[] + public datatokensABI: AbiItem | AbiItem[] + public web3: Web3 /** * Instantiate DataTokens (independently of Ocean). @@ -18,7 +21,12 @@ export class DataTokens { * @param {any} web3 */ - constructor(factoryAddress: string, factoryABI: any, datatokensABI: any, web3: any) { + constructor( + factoryAddress: string, + factoryABI: AbiItem | AbiItem[], + datatokensABI: AbiItem | AbiItem[], + web3: Web3 + ) { this.factoryAddress = factoryAddress this.factoryABI = factoryABI || defaultFactoryABI.abi this.datatokensABI = datatokensABI || defaultDatatokensABI.abi diff --git a/src/ocean/Account.ts b/src/ocean/Account.ts index d112d28f..80f00139 100644 --- a/src/ocean/Account.ts +++ b/src/ocean/Account.ts @@ -1,4 +1,3 @@ -import BigNumber from 'bignumber.js' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' /** diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 307a2c37..78e0aa8b 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -1,18 +1,12 @@ import { Accounts } from './Accounts' - import { Assets } from './Assets' - -// import { Compute } from './Compute' - import { Versions } from './Versions' import { OceanUtils } from './utils/Utils' - import { MetadataStore } from '../metadatastore/MetadataStore' import { Provider } from '../provider/Provider' import { DataTokens } from '../datatokens/Datatokens' import { Network } from '../datatokens/Network' import { Config } from '../models/Config' - import { Instantiable, generateIntantiableConfigFromConfig diff --git a/test/BalancerContractHandler.ts b/test/BalancerContractHandler.ts index e182941b..0e1f3339 100644 --- a/test/BalancerContractHandler.ts +++ b/test/BalancerContractHandler.ts @@ -1,3 +1,5 @@ +import Web3 from 'web3' +import { AbiItem } from 'web3-utils/types' import { Contract } from 'web3-eth-contract' export class BalancerContractHandler { @@ -8,14 +10,14 @@ export class BalancerContractHandler { public factoryBytecode: string public factoryAddress: string public poolAddress: string - public web3: any + public web3: Web3 constructor( - factoryABI: Contract, + factoryABI: AbiItem | AbiItem[], factoryBytecode: string, - poolABI: Contract, + poolABI: AbiItem | AbiItem[], poolBytecode: string, - web3: any + web3: Web3 ) { this.web3 = web3 this.factory = new this.web3.eth.Contract(factoryABI) diff --git a/test/TestContractHandler.ts b/test/TestContractHandler.ts index ce7db03f..6a4cf090 100644 --- a/test/TestContractHandler.ts +++ b/test/TestContractHandler.ts @@ -1,4 +1,6 @@ +import Web3 from 'web3' import { Contract } from 'web3-eth-contract' +import { AbiItem } from 'web3-utils/types' export class TestContractHandler { public factory: Contract @@ -8,14 +10,14 @@ export class TestContractHandler { public factoryBytecode: string public factoryAddress: string public templateAddress: string - public web3: any + public web3: Web3 constructor( - factoryABI: Contract, - datatokensABI: Contract, + factoryABI: AbiItem | AbiItem[], + datatokensABI: AbiItem | AbiItem[], templateBytecode: string, factoryBytecode: string, - web3: any + web3: Web3 ) { this.web3 = web3 this.factory = new this.web3.eth.Contract(factoryABI)