From cc6cf435fe7ae052467e2b69c3ecf8429b66bc99 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 27 Aug 2020 03:29:00 -0700 Subject: [PATCH 01/11] first cut, tests are failing --- package.json | 2 +- src/exchange/FixRateExchange.ts | 233 ++++++++++++++ src/models/Config.ts | 12 + src/ocean/Ocean.ts | 14 + src/utils/ConfigHelper.ts | 9 +- test/FixedPriceContractHandler.ts | 58 ++++ .../unit/exchanges/FixedPriceExchange.test.ts | 293 ++++++++++++++++++ 7 files changed, 617 insertions(+), 4 deletions(-) create mode 100644 src/exchange/FixRateExchange.ts create mode 100644 test/FixedPriceContractHandler.ts create mode 100644 test/unit/exchanges/FixedPriceExchange.test.ts diff --git a/package.json b/package.json index 93b9cb70..d4952dc6 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@ethereum-navigator/navigator": "^0.5.0", - "@oceanprotocol/contracts": "^0.3.4", + "@oceanprotocol/contracts": "^0.3.5", "decimal.js": "^10.2.0", "fs": "0.0.1-security", "node-fetch": "^2.6.0", diff --git a/src/exchange/FixRateExchange.ts b/src/exchange/FixRateExchange.ts new file mode 100644 index 00000000..837a4e10 --- /dev/null +++ b/src/exchange/FixRateExchange.ts @@ -0,0 +1,233 @@ +const defaultFixedRateExchangeABI = require('@oceanprotocol/contracts/artifacts/FixedRateExchange.json') + +export interface FixedPricedExchange { + exchangeOwner: string + dataToken: string + baseToken: string + fixedRate: number + active: boolean +} + +export class OceanFixedRateExchange { + /** Ocean related functions */ + public oceanAddress: string = null + public fixedRateExchangeAddress: string + public fixedRateExchangeABI: any + public web3: any + public contract: any = null + + /** + * Instantiate FixedRateExchange + * @param {any} web3 + * @param {String} fixedRateExchangeAddress + * @param {any} fixedRateExchangeABI + * @param {String} oceanAddress + */ + constructor( + web3: any, + fixedRateExchangeAddress: string = null, + fixedRateExchangeABI: any = null, + oceanAddress: string = null + ) { + this.web3 = web3 + this.fixedRateExchangeAddress = fixedRateExchangeAddress + this.fixedRateExchangeABI = fixedRateExchangeABI || defaultFixedRateExchangeABI.abi + this.oceanAddress = oceanAddress + if (web3) + this.contract = new this.web3.eth.Contract( + this.fixedRateExchangeABI, + this.fixedRateExchangeAddress + ) + } + + /** + * Creates new exchange pair between Ocean Token and data token. + * @param {String} dataToken Data Token Contract Address + * @param {Number} rate exchange rate + * @param {String} address User address + * @return {Promise} exchangeId + */ + public async create(dataToken: string, rate: string, address: string): Promise { + const estGas = await this.contract.methods + .create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate)) + .estimateGas(function (err, estGas) { + if (err) console.log('FixedPriceExchange: ' + err) + return estGas + }) + const trxReceipt = await this.contract.methods + .create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate)) + .send({ + from: address, + gas: estGas + 1 + }) + + let exchangeId = null + try { + exchangeId = trxReceipt.events.ExchangeCreated.returnValues[0] + } catch (e) { + console.error(e) + } + return exchangeId + } + + /** + * Creates unique exchange identifier. + * @param {String} dataToken Data Token Contract Address + * @param {String} owner Owner of the exchange + * @return {Promise} exchangeId + */ + public async generateExchangeId(dataToken: string, owner: string): Promise { + const exchangeId = await this.contract.methods + .generateExchangeId(this.oceanAddress, dataToken, owner) + .call() + return exchangeId + } + + /** + * Atomic swap + * @param {String} exchangeId ExchangeId + * @param {Number} dataTokenAmount Amount of Data Tokens + * @param {String} address User address + * @return {Promise} transaction receipt + */ + public async swap( + exchangeId: string, + dataTokenAmount: string, + address: string + ): Promise { + const estGas = await this.contract.methods + .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) + .estimateGas(function (err, estGas) { + if (err) console.log('FixedPriceExchange: ' + err) + return estGas + }) + + const trxReceipt = await this.contract.methods + .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) + .send({ + from: address, + gas: estGas + 1 + }) + return trxReceipt + } + + /** + * Gets total number of exchanges + * @param {String} exchangeId ExchangeId + * @param {Number} dataTokenAmount Amount of Data Tokens + * @return {Promise} no of available exchanges + */ + public async getNumberOfExchanges(): Promise { + const numExchanges = await this.contract.methods.getNumberOfExchanges().call() + return numExchanges + } + + /** + * Set new rate + * @param {String} exchangeId ExchangeId + * @param {Number} newRate New rate + * @param {String} address User account + * @return {Promise} transaction receipt + */ + public async setRate( + exchangeId: string, + newRate: number, + address: string + ): Promise { + const estGas = await this.contract.methods + .setRate(exchangeId, this.web3.utils.toWei(String(newRate))) + .estimateGas(function (err, estGas) { + if (err) console.log('FixedPriceExchange: ' + err) + return estGas + }) + const trxReceipt = await this.contract.methods + .setRate(exchangeId, this.web3.utils.toWei(String(newRate))) + .send({ + from: address, + gas: estGas + 1 + }) + return trxReceipt + } + + /** + * Activate an exchange + * @param {String} exchangeId ExchangeId + * @param {String} address User address + * @return {Promise} transaction receipt + */ + public async activate(exchangeId: string, address: string): Promise { + const estGas = await this.contract.methods + .activate(exchangeId) + .estimateGas(function (err, estGas) { + if (err) console.log('FixedPriceExchange: ' + err) + return estGas + }) + const trxReceipt = await this.contract.methods.activate(exchangeId).send({ + from: address, + gas: estGas + 1 + }) + return trxReceipt + } + + /** + * Deactivate an exchange + * @param {String} exchangeId ExchangeId + * @param {String} address User address + * @return {Promise} transaction receipt + */ + public async deactivate(exchangeId: string, address: string): Promise { + const estGas = await this.contract.methods + .deactivate(exchangeId) + .estimateGas(function (err, estGas) { + if (err) console.log('FixedPriceExchange: ' + err) + return estGas + }) + const trxReceipt = await this.contract.methods.deactivate(exchangeId).send({ + from: address, + gas: estGas + 1 + }) + return trxReceipt + } + + /** + * Get Rate + * @param {String} exchangeId ExchangeId + * @return {Promise} Rate (converted from wei) + */ + public async getRate(exchangeId: string): Promise { + const weiRate = await this.contract.methods.getRate(exchangeId).call() + return this.web3.utils.fromWei(weiRate) + } + + /** + * Get exchange details + * @param {String} exchangeId ExchangeId + * @return {Promise} Exchange details + */ + public async getExchange(exchangeId: string): Promise { + const result: FixedPricedExchange = await this.contract.methods + .getExchange(exchangeId) + .call() + return result + } + + /** + * Get all exchanges + * @param {String} exchangeId ExchangeId + * @return {Promise} Exchanges list + */ + public async getExchanges(): Promise { + const result = await this.contract.methods.getExchanges().call() + return result + } + + /** + * Check if an exchange is active + * @param {String} exchangeId ExchangeId + * @return {Promise} Result + */ + public async isActive(exchangeId: string): Promise { + const result = await this.contract.methods.isActive(exchangeId).call() + return result + } +} diff --git a/src/models/Config.ts b/src/models/Config.ts index bc6bdfdf..cd40bd43 100644 --- a/src/models/Config.ts +++ b/src/models/Config.ts @@ -68,12 +68,24 @@ export class Config { */ public poolFactoryABI?: any + /** * Pool ABI * @type {string} */ public poolABI?: any + /** + * FixedRateExchangeAddress + * @type {string} + */ + public fixedRateExchangeAddress?: string + + /** + * FixedRateExchangeAddressABI + * @type {any} + */ + public fixedRateExchangeAddressABI?: any /** * Log level. * @type {boolean | LogLevel} diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 78e0aa8b..eef02a8d 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -13,6 +13,7 @@ import { } from '../Instantiable.abstract' import { Compute } from './Compute' import { OceanPool } from '../balancer/OceanPool' +import {OceanFixedRateExchange, FixedPricedExchange} from '../exchange/FixRateExchange' /** * Main interface for Ocean Protocol. @@ -57,6 +58,13 @@ export class Ocean extends Instantiable { instanceConfig.config.poolFactoryAddress, instanceConfig.config.oceanTokenAddress ) + instance.fixedRateExchange = new OceanFixedRateExchange( + instanceConfig.config.web3Provider, + instanceConfig.config.fixedRateExchangeAddress, + instanceConfig.config.fixedRateExchangeAddressABI, + instanceConfig.config.oceanTokenAddress + + ) instance.versions = await Versions.getInstance(instanceConfig) instance.network = new Network() return instance @@ -123,6 +131,12 @@ export class Ocean extends Instantiable { */ public pool: OceanPool + /** + * Ocean FixedRateExchange submodule + * @type {OceanFixedRateExchange} + */ + public fixedRateExchange: OceanFixedRateExchange + /** * Ocean tokens submodule * @type {OceanTokens} diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index c7fb6e06..b7954579 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -19,7 +19,8 @@ const configs = [ factoryAddress: null, metadataStoreUri: 'http://127.0.0.1:5000', providerUri: 'http://127.0.0.1:8030', - poolFactoryAddress: null + poolFactoryAddress: null, + fixedRateExchangeAddress: null }, { chainId: 4, @@ -29,7 +30,8 @@ const configs = [ oceanTokenAddress: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07', metadataStoreUri: 'https://aquarius.rinkeby.v3.dev-ocean.com', providerUri: 'https://provider.rinkeby.v3.dev-ocean.com', - poolFactoryAddress: '0xA4531C624A3D88323a1e178DABe1233AF178701B' + poolFactoryAddress: '0xA4531C624A3D88323a1e178DABe1233AF178701B', + fixedRateExchangeAddress: '0x7219AfFc1C2b474830D9d9b0423ecf47073C5488' }, { chainId: 1, @@ -39,7 +41,8 @@ const configs = [ oceanTokenAddress: '0x985dd3d42de1e256d09e1c10f112bccb8015ad41', metadataStoreUri: null, providerUri: null, - poolFactoryAddress: null + poolFactoryAddress: null, + fixedRateExchangeAddress: null } ] diff --git a/test/FixedPriceContractHandler.ts b/test/FixedPriceContractHandler.ts new file mode 100644 index 00000000..ea131bd4 --- /dev/null +++ b/test/FixedPriceContractHandler.ts @@ -0,0 +1,58 @@ +import Web3 from 'web3' +import { Contract } from 'web3-eth-contract' +import { AbiItem } from 'web3-utils/types' + +export class FixedPricedContractHandler { + public contract: Contract + public accounts: string[] + public contractBytecode: string + public contractAddress: string + public web3: Web3 + + constructor( + contractABI: AbiItem | AbiItem[], + contractBytecode: string, + web3: Web3 + ) { + this.web3 = web3 + this.contract = new this.web3.eth.Contract(contractABI) + this.contractBytecode = contractBytecode + + } + + public async getAccounts() { + this.accounts = await this.web3.eth.getAccounts() + } + + public async deployContracts() { + let estGas + + await this.getAccounts() + // get est gascost + estGas = await this.contract + .deploy({ + data: this.contractBytecode, + arguments: [] + }) + .estimateGas(function (err, estGas) { + if (err) console.log('DeployContracts: ' + err) + return estGas + }) + // deploy the contract and get it's address + this.contractAddress = await this.contract + .deploy({ + data: this.contractBytecode, + arguments: [] + }) + .send({ + from: this.accounts[0], + gas: estGas + 1, + gasPrice: '3000000000' + }) + .then(function (contract) { + return contract.options.address + }) + + + } +} diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts new file mode 100644 index 00000000..5acdf021 --- /dev/null +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -0,0 +1,293 @@ +import { assert } from 'chai' +import { AbiItem } from 'web3-utils/types' +import { TestContractHandler } from '../../TestContractHandler' +import { FixedPricedContractHandler } from '../../FixedPriceContractHandler' +import { DataTokens } from '../../../src/datatokens/Datatokens' +import { + OceanFixedRateExchange, + FixedPricedExchange +} from '../../../src/exchange/FixRateExchange' + +import Web3 from 'web3' +import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json' +import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' +import FixedRateExchangeContract = require('@oceanprotocol/contracts/artifacts/FixedRateExchange.json') + +import BigNumber from 'bignumber.js' +const web3 = new Web3('http://127.0.0.1:8545') + +describe('FixedRateExchange flow', () => { + let oceanTokenAddress + let FixedRateExchangeAddress + let FixedRateClass + let oceandatatoken + let aliceExchangeId + let bob + let alice + let datatoken + let tokenAddress + + let alicePoolAddress + let currentDtPrice + let owner + let contracts + + let consoleDebug: false + let greatPool + const tokenAmount = '1000' + const transferAmount = '200' + const blob = 'http://localhost:8030/api/v1/services/consume' + describe('#test', () => { + before(async () => { + // deploy SFactory + const Contracts = new FixedPricedContractHandler( + FixedRateExchangeContract.abi as AbiItem[], + FixedRateExchangeContract.bytecode, + web3 + ) + await Contracts.getAccounts() + owner = Contracts.accounts[0] + + await Contracts.deployContracts() + FixedRateExchangeAddress = Contracts.contractAddress + assert(FixedRateExchangeAddress !== null) + + // deploy DT Factory + contracts = new TestContractHandler( + factory.abi as AbiItem[], + datatokensTemplate.abi as AbiItem[], + datatokensTemplate.bytecode, + factory.bytecode, + web3 + ) + await contracts.getAccounts() + owner = contracts.accounts[0] + alice = contracts.accounts[1] + bob = contracts.accounts[2] + await contracts.deployContracts(owner) + + // initialize DataTokens + datatoken = new DataTokens( + contracts.factoryAddress, + factory.abi as AbiItem[], + datatokensTemplate.abi as AbiItem[], + web3 + ) + assert(datatoken !== null) + }) + + it('should create datatokens smart contract', async () => { + tokenAddress = await datatoken.create(blob, alice) + assert(tokenAddress !== null) + console.log('data Token address:' + tokenAddress) + }) + it('Create a dummy OceanToken', async () => { + // Bob creates a Datatoken + oceandatatoken = new DataTokens( + contracts.factoryAddress, + factory.abi as AbiItem[], + datatokensTemplate.abi as AbiItem[], + web3 + ) + oceanTokenAddress = await oceandatatoken.create(blob, bob) + console.log('oceanTokenAddress:' + oceanTokenAddress) + }) + + it('should initialize FixedExchangeRate class', async () => { + FixedRateClass = new OceanFixedRateExchange( + web3, + FixedRateExchangeAddress, + FixedRateExchangeContract.abi, + oceanTokenAddress + ) + assert(FixedRateClass !== null) + }) + + it('Alice mints 1000 tokens', async () => { + await datatoken.mint(tokenAddress, alice, tokenAmount) + }) + it('Bob mints 1000 Ocean tokens', async () => { + await oceandatatoken.mint(oceanTokenAddress, bob, tokenAmount) + }) + it('Alice should have 1000 tokens', async () => { + const balance = await datatoken.balance(tokenAddress, alice) + console.log(balance) + }) + it('Bob should have 1000 ocean tokens', async () => { + const balance = await oceandatatoken.balance(oceanTokenAddress, bob) + console.log(balance) + }) + it('Alice creates a new FixedRate Exchange with a rate of 0.5', async () => { + aliceExchangeId = await FixedRateClass.create(tokenAddress, '0.1', alice) + console.log(aliceExchangeId) + }) + it('Alice allows Exchange to spend 1000 data tokens', async () => { + await datatoken.approve(tokenAddress, FixedRateExchangeAddress, tokenAmount, alice) + }) + it('Bob allows Exchange to spend 1000 ocean tokens', async () => { + await oceandatatoken.approve( + oceanTokenAddress, + FixedRateExchangeAddress, + tokenAmount, + bob + ) + }) + it('Alice should aproved speding datatokens', async () => { + const balance = await datatoken.allowance( + tokenAddress, + alice, + FixedRateExchangeAddress + ) + console.log(balance) + }) + it('Bob should aproved speding oceantokens', async () => { + const balance = await oceandatatoken.allowance( + oceanTokenAddress, + bob, + FixedRateExchangeAddress + ) + console.log(balance) + }) + it('Bob should get the exchange details', async () => { + const exchangeDetails = await FixedRateClass.getExchange(aliceExchangeId) + console.log(exchangeDetails) + }) + it('Bob should swap 1 DataToken', async () => { + const swapResult = await FixedRateClass.swap(aliceExchangeId, '1', bob) + console.log(swapResult) + }) + /* + it('Alice creates a new OceanPool pool', async () => { + /// new pool with total DT = 45 , dt weight=90% with swap fee 2% + alicePoolAddress = await Pool.createDTPool(alice, tokenAddress, 45, 9, '0.02') + }) + it('Get pool information', async () => { + const currentTokens = await Pool.getCurrentTokens(alice, alicePoolAddress) + assert(currentTokens.length === 2) + assert(currentTokens.includes(tokenAddress)) + assert(currentTokens.includes(oceanTokenAddress)) + }) + it('Get pool swap fee', async () => { + const currentSwapFee = await Pool.getSwapFee(alice, alicePoolAddress) + assert(currentSwapFee === '0.02') + }) + it('Get dtPrice from the pool ', async () => { + currentDtPrice = await Pool.getDTPrice(alice, alicePoolAddress) + assert(currentDtPrice > 0) + }) + it('Get dtToken pool reserve ', async () => { + const currentDtReserve = await Pool.getDTReserve(alice, alicePoolAddress) + assert(currentDtReserve > 0) + }) + it('Get Ocean pool reserve ', async () => { + const currentOceanReserve = await Pool.getOceanReserve(alice, alicePoolAddress) + assert(currentOceanReserve > 0) + }) + it('Get total supply of pool tokens', async () => { + const totalSupply = await Pool.totalSupply(alicePoolAddress) + assert(totalSupply > 0) + }) + it('Get amount of Ocean needed to buy 1 dtToken', async () => { + const requiredOcean = await Pool.getOceanNeeded(alice, alicePoolAddress, '1') + assert(requiredOcean > 0) + }) + + it('Bob should search for pools with this DT', async () => { + const pools = await Pool.searchPoolforDT(bob, tokenAddress) + assert(pools.length > 0) + greatPool = pools[0] + }) + it('Bob should buy a DT ', async () => { + const maxPrice = parseFloat(currentDtPrice) * 2 + await Pool.buyDT(bob, greatPool, '1', '2', String(maxPrice)) + const bobDtBalance = await datatoken.balance(tokenAddress, bob) + const bobOceanBalance = await datatoken.balance(oceanTokenAddress, bob) + assert(bobDtBalance > 0) + assert(bobOceanBalance > 0) + }) + it('Bob should add DT liquidity to pool ', async () => { + const currentDtReserve = await Pool.getDTReserve(bob, greatPool) + if (consoleDebug) console.log('currentDtReserve:' + currentDtReserve) + const bobDtBalance = await datatoken.balance(tokenAddress, bob) + if (consoleDebug) console.log('BOB DT Balance:' + bobDtBalance) + await Pool.addDTLiquidity(bob, greatPool, bobDtBalance) + + const newbobDtBalance = await datatoken.balance(tokenAddress, bob) + + const newDtReserve = await Pool.getDTReserve(bob, greatPool) + + const sharesBalance = await Pool.sharesBalance(bob, greatPool) + if (consoleDebug) console.log('newDtReserve:' + newDtReserve) + if (consoleDebug) console.log('newbobDtBalance:' + newbobDtBalance) + if (consoleDebug) console.log('sharesBalance:' + sharesBalance) + assert(parseFloat(newbobDtBalance) < parseFloat(bobDtBalance)) + assert(parseFloat(newDtReserve) > parseFloat(currentDtReserve)) + assert(parseFloat(sharesBalance) > 0) + }) + + it('Bob should remove DT liquidity from pool ', async () => { + const currentDtReserve = await Pool.getDTReserve(bob, greatPool) + if (consoleDebug) console.log('currentDtReserve:' + currentDtReserve) + const bobDtBalance = await datatoken.balance(tokenAddress, bob) + if (consoleDebug) console.log('bobDtBalance:' + bobDtBalance) + const poolShares = await Pool.sharesBalance(bob, greatPool) + if (consoleDebug) console.log('poolShares:' + poolShares) + await Pool.removeDTLiquidity(bob, greatPool, '0.75', poolShares) + + const newDtReserve = await Pool.getDTReserve(bob, greatPool) + if (consoleDebug) console.log('newDtReserve:' + newDtReserve) + const newbobDtBalance = await datatoken.balance(tokenAddress, bob) + if (consoleDebug) console.log('newbobDtBalance:' + newbobDtBalance) + const newpoolShares = await Pool.sharesBalance(bob, greatPool) + if (consoleDebug) console.log('newpoolShares:' + newpoolShares) + assert(parseFloat(newDtReserve) < parseFloat(currentDtReserve)) + assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance)) + assert(parseFloat(poolShares) > parseFloat(newpoolShares)) + }) + + it('Bob should add Ocean liquidity to pool ', async () => { + const currentDtReserve = await Pool.getOceanReserve(bob, greatPool) + const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob) + if (consoleDebug) console.log('currentDtReserve:' + currentDtReserve) + if (consoleDebug) console.log('bobDtBalance:' + bobDtBalance) + + await Pool.addOceanLiquidity(bob, greatPool, '1') + + const newbobDtBalance = await datatoken.balance(oceanTokenAddress, bob) + + const newDtReserve = await Pool.getOceanReserve(bob, greatPool) + + const sharesBalance = await Pool.sharesBalance(bob, greatPool) + if (consoleDebug) console.log('newDtReserve:' + newDtReserve) + if (consoleDebug) console.log('newbobDtBalance:' + newbobDtBalance) + if (consoleDebug) console.log('sharesBalance:' + sharesBalance) + assert(parseFloat(newbobDtBalance) < parseFloat(bobDtBalance)) + assert(parseFloat(newDtReserve) > parseFloat(currentDtReserve)) + assert(parseFloat(sharesBalance) > 0) + }) + + it('Bob should remove Ocean liquidity from pool ', async () => { + const currentDtReserve = await Pool.getOceanReserve(bob, greatPool) + const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob) + + const poolShares = await Pool.sharesBalance(bob, greatPool) + if (consoleDebug) console.log('currentDtReserve:' + currentDtReserve) + if (consoleDebug) console.log('bobDtBalance:' + bobDtBalance) + if (consoleDebug) console.log('poolShares:' + poolShares) + + await Pool.removeOceanLiquidity(bob, greatPool, '0.75', poolShares) + + const newDtReserve = await Pool.getOceanReserve(bob, greatPool) + const newbobDtBalance = await datatoken.balance(oceanTokenAddress, bob) + const newpoolShares = await Pool.sharesBalance(bob, greatPool) + + if (consoleDebug) console.log('newDtReserve:' + newDtReserve) + if (consoleDebug) console.log('newbobDtBalance:' + newbobDtBalance) + if (consoleDebug) console.log('newpoolShares:' + newpoolShares) + assert(parseFloat(newDtReserve) < parseFloat(currentDtReserve)) + assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance)) + assert(parseFloat(poolShares) > parseFloat(newpoolShares)) + }) + */ + }) +}) From 205a763d97f16c74fde31c0b9e627bcb7ab9250b Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 27 Aug 2020 03:33:27 -0700 Subject: [PATCH 02/11] fix lint --- src/exchange/FixRateExchange.ts | 4 ++-- src/models/Config.ts | 1 - src/ocean/Ocean.ts | 3 +-- test/FixedPriceContractHandler.ts | 13 ++----------- test/unit/exchanges/FixedPriceExchange.test.ts | 2 +- 5 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/exchange/FixRateExchange.ts b/src/exchange/FixRateExchange.ts index 837a4e10..e90f9d32 100644 --- a/src/exchange/FixRateExchange.ts +++ b/src/exchange/FixRateExchange.ts @@ -1,4 +1,4 @@ -const defaultFixedRateExchangeABI = require('@oceanprotocol/contracts/artifacts/FixedRateExchange.json') +import defaultFixedRateExchangeABI from '@oceanprotocol/contracts/artifacts/FixedRateExchange.json' export interface FixedPricedExchange { exchangeOwner: string @@ -101,7 +101,7 @@ export class OceanFixedRateExchange { if (err) console.log('FixedPriceExchange: ' + err) return estGas }) - + const trxReceipt = await this.contract.methods .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) .send({ diff --git a/src/models/Config.ts b/src/models/Config.ts index cd40bd43..53dc0297 100644 --- a/src/models/Config.ts +++ b/src/models/Config.ts @@ -68,7 +68,6 @@ export class Config { */ public poolFactoryABI?: any - /** * Pool ABI * @type {string} diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index eef02a8d..ad7c7a4a 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -13,7 +13,7 @@ import { } from '../Instantiable.abstract' import { Compute } from './Compute' import { OceanPool } from '../balancer/OceanPool' -import {OceanFixedRateExchange, FixedPricedExchange} from '../exchange/FixRateExchange' +import { OceanFixedRateExchange, FixedPricedExchange } from '../exchange/FixRateExchange' /** * Main interface for Ocean Protocol. @@ -63,7 +63,6 @@ export class Ocean extends Instantiable { instanceConfig.config.fixedRateExchangeAddress, instanceConfig.config.fixedRateExchangeAddressABI, instanceConfig.config.oceanTokenAddress - ) instance.versions = await Versions.getInstance(instanceConfig) instance.network = new Network() diff --git a/test/FixedPriceContractHandler.ts b/test/FixedPriceContractHandler.ts index ea131bd4..724454a6 100644 --- a/test/FixedPriceContractHandler.ts +++ b/test/FixedPriceContractHandler.ts @@ -9,15 +9,10 @@ export class FixedPricedContractHandler { public contractAddress: string public web3: Web3 - constructor( - contractABI: AbiItem | AbiItem[], - contractBytecode: string, - web3: Web3 - ) { + constructor(contractABI: AbiItem | AbiItem[], contractBytecode: string, web3: Web3) { this.web3 = web3 this.contract = new this.web3.eth.Contract(contractABI) this.contractBytecode = contractBytecode - } public async getAccounts() { @@ -25,11 +20,9 @@ export class FixedPricedContractHandler { } public async deployContracts() { - let estGas - await this.getAccounts() // get est gascost - estGas = await this.contract + const estGas = await this.contract .deploy({ data: this.contractBytecode, arguments: [] @@ -52,7 +45,5 @@ export class FixedPricedContractHandler { .then(function (contract) { return contract.options.address }) - - } } diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index 5acdf021..cf009acb 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -11,9 +11,9 @@ import { import Web3 from 'web3' import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json' import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' -import FixedRateExchangeContract = require('@oceanprotocol/contracts/artifacts/FixedRateExchange.json') import BigNumber from 'bignumber.js' +import FixedRateExchangeContract = require('@oceanprotocol/contracts/artifacts/FixedRateExchange.json') const web3 = new Web3('http://127.0.0.1:8545') describe('FixedRateExchange flow', () => { From 0792f66abecacc60e598b1e86081d457497beb59 Mon Sep 17 00:00:00 2001 From: ssallam Date: Thu, 27 Aug 2020 17:01:31 +0200 Subject: [PATCH 03/11] Fix failing test by handling error when estimating gas. --- src/exchange/FixRateExchange.ts | 23 +++++++++---- .../unit/exchanges/FixedPriceExchange.test.ts | 34 +++++++++++-------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/exchange/FixRateExchange.ts b/src/exchange/FixRateExchange.ts index e90f9d32..a0f6dc43 100644 --- a/src/exchange/FixRateExchange.ts +++ b/src/exchange/FixRateExchange.ts @@ -95,13 +95,24 @@ export class OceanFixedRateExchange { dataTokenAmount: string, address: string ): Promise { - const estGas = await this.contract.methods - .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) - .estimateGas(function (err, estGas) { - if (err) console.log('FixedPriceExchange: ' + err) - return estGas - }) + let estGas + try { + estGas = await this.contract.methods + .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) + .estimateGas(function (err, g) { + if (err) { + console.log('FixedPriceExchange: ' + err) + return 200000 + } else { + return g + } + }) + } catch (e) { + console.log('FixedPriceExchange: ' + e) + estGas = 200000 + } + console.log('estGas: ' + estGas) const trxReceipt = await this.contract.methods .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) .send({ diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index cf009acb..e14da41a 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -34,7 +34,8 @@ describe('FixedRateExchange flow', () => { let consoleDebug: false let greatPool - const tokenAmount = '1000' + const tokenAmount = '777' + const oceanAmount = '499' const transferAmount = '200' const blob = 'http://localhost:8030/api/v1/services/consume' describe('#test', () => { @@ -103,11 +104,11 @@ describe('FixedRateExchange flow', () => { assert(FixedRateClass !== null) }) - it('Alice mints 1000 tokens', async () => { - await datatoken.mint(tokenAddress, alice, tokenAmount) + it('Alice mints datatokens', async () => { + const r = await datatoken.mint(tokenAddress, alice, tokenAmount) }) - it('Bob mints 1000 Ocean tokens', async () => { - await oceandatatoken.mint(oceanTokenAddress, bob, tokenAmount) + it('Bob mints Ocean tokens', async () => { + const r = await oceandatatoken.mint(oceanTokenAddress, bob, oceanAmount) }) it('Alice should have 1000 tokens', async () => { const balance = await datatoken.balance(tokenAddress, alice) @@ -118,19 +119,14 @@ describe('FixedRateExchange flow', () => { console.log(balance) }) it('Alice creates a new FixedRate Exchange with a rate of 0.5', async () => { - aliceExchangeId = await FixedRateClass.create(tokenAddress, '0.1', alice) + aliceExchangeId = await FixedRateClass.create(tokenAddress, '0.2', alice) console.log(aliceExchangeId) }) it('Alice allows Exchange to spend 1000 data tokens', async () => { - await datatoken.approve(tokenAddress, FixedRateExchangeAddress, tokenAmount, alice) + const r = await datatoken.approve(tokenAddress, FixedRateExchangeAddress, '77', alice) }) it('Bob allows Exchange to spend 1000 ocean tokens', async () => { - await oceandatatoken.approve( - oceanTokenAddress, - FixedRateExchangeAddress, - tokenAmount, - bob - ) + const r = await oceandatatoken.approve(oceanTokenAddress, FixedRateExchangeAddress, '49', bob) }) it('Alice should aproved speding datatokens', async () => { const balance = await datatoken.allowance( @@ -153,8 +149,16 @@ describe('FixedRateExchange flow', () => { console.log(exchangeDetails) }) it('Bob should swap 1 DataToken', async () => { - const swapResult = await FixedRateClass.swap(aliceExchangeId, '1', bob) - console.log(swapResult) + const swapResult = await FixedRateClass.swap(aliceExchangeId, '10', bob) + console.log('swap receipt status: ' + swapResult.status) + console.log(await datatoken.balance(tokenAddress, alice)) + console.log(await datatoken.balance(tokenAddress, bob)) + console.log(await datatoken.allowance(tokenAddress, alice, FixedRateExchangeAddress)) + + console.log(await datatoken.balance(oceanTokenAddress, bob)) + console.log(await datatoken.balance(oceanTokenAddress, alice)) + console.log(await datatoken.allowance(oceanTokenAddress, bob, FixedRateExchangeAddress)) + }) /* it('Alice creates a new OceanPool pool', async () => { From 22ab44713d21d92eaf9319be26af254c165af6b5 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Fri, 28 Aug 2020 01:47:25 -0700 Subject: [PATCH 04/11] add more tests --- src/exchange/FixRateExchange.ts | 183 ++++++++++++++---- .../unit/exchanges/FixedPriceExchange.test.ts | 102 ++++++++-- 2 files changed, 231 insertions(+), 54 deletions(-) diff --git a/src/exchange/FixRateExchange.ts b/src/exchange/FixRateExchange.ts index e90f9d32..333a9b15 100644 --- a/src/exchange/FixRateExchange.ts +++ b/src/exchange/FixRateExchange.ts @@ -1,11 +1,14 @@ import defaultFixedRateExchangeABI from '@oceanprotocol/contracts/artifacts/FixedRateExchange.json' +import BigNumber from 'bignumber.js' export interface FixedPricedExchange { + exchangeID?: string exchangeOwner: string dataToken: string baseToken: string - fixedRate: number + fixedRate: string active: boolean + supply: string } export class OceanFixedRateExchange { @@ -90,25 +93,47 @@ export class OceanFixedRateExchange { * @param {String} address User address * @return {Promise} transaction receipt */ - public async swap( + public async buyDT( exchangeId: string, dataTokenAmount: string, address: string ): Promise { - const estGas = await this.contract.methods - .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) - .estimateGas(function (err, estGas) { - if (err) console.log('FixedPriceExchange: ' + err) - return estGas - }) - - const trxReceipt = await this.contract.methods - .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) - .send({ - from: address, - gas: estGas + 1 - }) - return trxReceipt + let estGas + try { + estGas = await this.contract.methods + .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) + .estimateGas(function (err, g) { + if (err) { + // console.log('FixedPriceExchange: ' + err) + return 200000 + } else { + return g + } + }) + } catch (e) { + // console.log('FixedPriceExchange: ' + e) + estGas = 200000 + } + // console.log('estGas: ' + estGas) + /* const estGas = await this.contract.methods + .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) + .estimateGas(function (err, estGas) { + if (err) console.log('FixedPriceExchange: ' + err) + return estGas + }) + */ + try { + const trxReceipt = await this.contract.methods + .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) + .send({ + from: address, + gas: estGas + 1 + }) + return trxReceipt + } catch (e) { + console.error(e) + return null + } } /** @@ -134,12 +159,20 @@ export class OceanFixedRateExchange { newRate: number, address: string ): Promise { - const estGas = await this.contract.methods - .setRate(exchangeId, this.web3.utils.toWei(String(newRate))) - .estimateGas(function (err, estGas) { - if (err) console.log('FixedPriceExchange: ' + err) - return estGas - }) + let estGas + try { + estGas = await this.contract.methods + .setRate(exchangeId, this.web3.utils.toWei(String(newRate))) + .estimateGas(function (err, estGas) { + if (err) { + // console.log('FixedPriceExchange: ' + err) + return 200000 + } + return estGas + }) + } catch (e) { + estGas = 200000 + } const trxReceipt = await this.contract.methods .setRate(exchangeId, this.web3.utils.toWei(String(newRate))) .send({ @@ -156,12 +189,20 @@ export class OceanFixedRateExchange { * @return {Promise} transaction receipt */ public async activate(exchangeId: string, address: string): Promise { - const estGas = await this.contract.methods - .activate(exchangeId) - .estimateGas(function (err, estGas) { - if (err) console.log('FixedPriceExchange: ' + err) - return estGas - }) + let estGas + try { + estGas = await this.contract.methods + .activate(exchangeId) + .estimateGas(function (err, estGas) { + if (err) { + // console.log('FixedPriceExchange: ' + err) + estGas = 200000 + } + return estGas + }) + } catch (e) { + estGas = 200000 + } const trxReceipt = await this.contract.methods.activate(exchangeId).send({ from: address, gas: estGas + 1 @@ -176,12 +217,20 @@ export class OceanFixedRateExchange { * @return {Promise} transaction receipt */ public async deactivate(exchangeId: string, address: string): Promise { - const estGas = await this.contract.methods - .deactivate(exchangeId) - .estimateGas(function (err, estGas) { - if (err) console.log('FixedPriceExchange: ' + err) - return estGas - }) + let estGas + try { + estGas = await this.contract.methods + .deactivate(exchangeId) + .estimateGas(function (err, estGas) { + if (err) { + // console.log('FixedPriceExchange: ' + err) + estGas = 200000 + } + return estGas + }) + } catch (e) { + estGas = 200000 + } const trxReceipt = await this.contract.methods.deactivate(exchangeId).send({ from: address, gas: estGas + 1 @@ -199,6 +248,32 @@ export class OceanFixedRateExchange { return this.web3.utils.fromWei(weiRate) } + /** + * Get Supply + * @param {String} exchangeId ExchangeId + * @return {Promise} Rate (converted from wei) + */ + public async getSupply(exchangeId: string): Promise { + const weiRate = await this.contract.methods.getSupply(exchangeId).call() + return this.web3.utils.fromWei(weiRate) + } + + /** + * getOceanNeeded + * @param {String} exchangeId ExchangeId + * @param {Number} dataTokenAmount Amount of Data Tokens + * @return {Promise} Ocean amount needed + */ + public async getOceanNeeded( + exchangeId: string, + dataTokenAmount: string + ): Promise { + const weiRate = await this.contract.methods + .CalcInGivenOut(exchangeId, this.web3.utils.toWei(dataTokenAmount)) + .call() + return this.web3.utils.fromWei(weiRate) + } + /** * Get exchange details * @param {String} exchangeId ExchangeId @@ -230,4 +305,44 @@ export class OceanFixedRateExchange { const result = await this.contract.methods.isActive(exchangeId).call() return result } + + /** + * Calculates how many basetokens are needed to get specifyed amount of datatokens + * @param {String} exchangeId ExchangeId + * @param {String} dataTokenAmount dataTokenAmount + * @return {Promise} Result + */ + public async CalcInGivenOut( + exchangeId: string, + dataTokenAmount: string + ): Promise { + const result = await this.contract.methods + .CalcInGivenOut(exchangeId, this.web3.utils.toWei(dataTokenAmount)) + .call() + return this.web3.utils.fromWei(result) + } + + public async searchforDT( + dataTokenAddress: string, + minSupply: string + ): Promise { + const result: FixedPricedExchange[] = [] + const events = await this.contract.getPastEvents('ExchangeCreated', { + filter: { datatoken: dataTokenAddress }, + fromBlock: 0, + toBlock: 'latest' + }) + for (let i = 0; i < events.length; i++) { + const constituents = await this.getExchange(events[i].returnValues[0]) + constituents.exchangeID = events[i].returnValues[0] + if (constituents.active === true) { + const supply = new BigNumber(this.web3.utils.fromWei(constituents.supply)) + const required = new BigNumber(minSupply) + if (supply >= required) { + result.push(constituents) + } + } + } + return result + } } diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index cf009acb..d3081b56 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -32,10 +32,12 @@ describe('FixedRateExchange flow', () => { let owner let contracts - let consoleDebug: false + const consoleDebug = false let greatPool - const tokenAmount = '1000' - const transferAmount = '200' + const tokenAmount = '1000000000000000000000000000000000' + const fixedPriceRate = '0.5' + const updatedPriceRate = '0.5' + const swapAmount = '1' const blob = 'http://localhost:8030/api/v1/services/consume' describe('#test', () => { before(async () => { @@ -79,7 +81,8 @@ describe('FixedRateExchange flow', () => { it('should create datatokens smart contract', async () => { tokenAddress = await datatoken.create(blob, alice) assert(tokenAddress !== null) - console.log('data Token address:' + tokenAddress) + if (consoleDebug) console.log("Alice's address:" + alice) + if (consoleDebug) console.log('data Token address:' + tokenAddress) }) it('Create a dummy OceanToken', async () => { // Bob creates a Datatoken @@ -90,7 +93,8 @@ describe('FixedRateExchange flow', () => { web3 ) oceanTokenAddress = await oceandatatoken.create(blob, bob) - console.log('oceanTokenAddress:' + oceanTokenAddress) + if (consoleDebug) console.log("Bob's address:" + bob) + if (consoleDebug) console.log('oceanTokenAddress:' + oceanTokenAddress) }) it('should initialize FixedExchangeRate class', async () => { @@ -104,33 +108,40 @@ describe('FixedRateExchange flow', () => { }) it('Alice mints 1000 tokens', async () => { - await datatoken.mint(tokenAddress, alice, tokenAmount) + const txid = await datatoken.mint(tokenAddress, alice, tokenAmount) + if (consoleDebug) console.log(txid) + assert(txid !== null) }) it('Bob mints 1000 Ocean tokens', async () => { - await oceandatatoken.mint(oceanTokenAddress, bob, tokenAmount) + const txid = await oceandatatoken.mint(oceanTokenAddress, bob, tokenAmount) + if (consoleDebug) console.log(txid) + assert(txid !== null) }) it('Alice should have 1000 tokens', async () => { const balance = await datatoken.balance(tokenAddress, alice) - console.log(balance) + if (consoleDebug) console.log("Alice's datatoke balance:" + balance) }) it('Bob should have 1000 ocean tokens', async () => { const balance = await oceandatatoken.balance(oceanTokenAddress, bob) - console.log(balance) - }) - it('Alice creates a new FixedRate Exchange with a rate of 0.5', async () => { - aliceExchangeId = await FixedRateClass.create(tokenAddress, '0.1', alice) - console.log(aliceExchangeId) + if (consoleDebug) console.log("Bob's ocean balance:" + balance) }) it('Alice allows Exchange to spend 1000 data tokens', async () => { - await datatoken.approve(tokenAddress, FixedRateExchangeAddress, tokenAmount, alice) + const txid = await datatoken.approve( + tokenAddress, + FixedRateExchangeAddress, + tokenAmount, + alice + ) + if (consoleDebug) console.log(txid) }) it('Bob allows Exchange to spend 1000 ocean tokens', async () => { - await oceandatatoken.approve( + const txid = await oceandatatoken.approve( oceanTokenAddress, FixedRateExchangeAddress, tokenAmount, bob ) + if (consoleDebug) console.log(txid) }) it('Alice should aproved speding datatokens', async () => { const balance = await datatoken.allowance( @@ -138,7 +149,7 @@ describe('FixedRateExchange flow', () => { alice, FixedRateExchangeAddress ) - console.log(balance) + if (consoleDebug) console.log('Alice datatoken allowance:' + balance) }) it('Bob should aproved speding oceantokens', async () => { const balance = await oceandatatoken.allowance( @@ -146,16 +157,67 @@ describe('FixedRateExchange flow', () => { bob, FixedRateExchangeAddress ) - console.log(balance) + if (consoleDebug) console.log('Bob ocean allowance:' + balance) + }) + it('Alice creates a new FixedRate Exchange with a rate of 0.5', async () => { + aliceExchangeId = await FixedRateClass.create(tokenAddress, fixedPriceRate, alice) + if (consoleDebug) console.log('aliceExchangeId:' + aliceExchangeId) + }) + it('Bob should find the exchange', async () => { + const exchangeDetails = await FixedRateClass.searchforDT(tokenAddress, '0') + assert(exchangeDetails[0].exchangeID === aliceExchangeId) }) it('Bob should get the exchange details', async () => { const exchangeDetails = await FixedRateClass.getExchange(aliceExchangeId) - console.log(exchangeDetails) + if (consoleDebug) console.log(exchangeDetails) + }) + + it('Bob should get the amount of Ocean needed', async () => { + const OceansNeeded = await FixedRateClass.CalcInGivenOut( + aliceExchangeId, + swapAmount + ) + if (consoleDebug) console.log('Oceans needed:' + OceansNeeded) + assert(OceansNeeded !== null) }) it('Bob should swap 1 DataToken', async () => { - const swapResult = await FixedRateClass.swap(aliceExchangeId, '1', bob) - console.log(swapResult) + const swapResult = await FixedRateClass.buyDT(aliceExchangeId, swapAmount, bob) + if (consoleDebug) console.log(swapResult) + assert(swapResult !== null) }) + it('Alice datatoken balance after swap', async () => { + const balance = await datatoken.balance(tokenAddress, alice) + if (consoleDebug) console.log('Alice datatoken balance:' + balance) + }) + it('Alice ocean balance after swap', async () => { + const balance = await oceandatatoken.balance(oceanTokenAddress, alice) + if (consoleDebug) console.log('Alice ocean balance:' + balance) + }) + it('Bob datatoken balance after swap', async () => { + const balance = await datatoken.balance(tokenAddress, bob) + if (consoleDebug) console.log('Bob datatoken balance:' + balance) + }) + it('Bob ocean balance after swap', async () => { + const balance = await oceandatatoken.balance(oceanTokenAddress, bob) + if (consoleDebug) console.log('Bob ocean balance:' + balance) + }) + it('Alice should update the rate', async () => { + const tx = await FixedRateClass.setRate(aliceExchangeId, updatedPriceRate, alice) + assert(tx !== null) + }) + it('Alice should be able to deactivate the exchange', async () => { + const tx = await FixedRateClass.deactivate(aliceExchangeId, alice) + assert(tx !== null) + const exchangeDetails = await FixedRateClass.getExchange(aliceExchangeId) + assert(exchangeDetails.active === false) + }) + it('Alice should be able to activate the exchange', async () => { + const tx = await FixedRateClass.activate(aliceExchangeId, alice) + assert(tx !== null) + const exchangeDetails = await FixedRateClass.getExchange(aliceExchangeId) + assert(exchangeDetails.active === true) + }) + /* it('Alice creates a new OceanPool pool', async () => { /// new pool with total DT = 45 , dt weight=90% with swap fee 2% From 220261cada63aedc5e7f71fb41f17a73838fd657 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Sun, 30 Aug 2020 23:56:25 -0700 Subject: [PATCH 05/11] update to contracts 0.4.0 --- package-lock.json | 6 ++--- package.json | 2 +- src/balancer/OceanPool.ts | 2 +- src/balancer/Pool.ts | 2 +- src/balancer/PoolFactory.ts | 6 ++--- src/datatokens/Datatokens.ts | 25 +++++++++++++------ src/ocean/Assets.ts | 16 ++++++++++-- test/TestContractHandler.ts | 10 ++++---- test/integration/ComputeFlow.test.ts | 8 +++++- test/integration/Marketplaceflow.test.ts | 8 +++++- test/integration/Simpleflow.test.ts | 2 +- test/unit/Datatokens.test.ts | 2 +- test/unit/balancer/Balancer.test.ts | 14 ++++++++--- .../unit/exchanges/FixedPriceExchange.test.ts | 16 ++++++++++-- 14 files changed, 86 insertions(+), 33 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7b74d69e..59001c18 100644 --- a/package-lock.json +++ b/package-lock.json @@ -880,9 +880,9 @@ } }, "@oceanprotocol/contracts": { - "version": "0.3.5", - "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-0.3.5.tgz", - "integrity": "sha512-z7ziNbRwsPrJi+zGyokgUEKivD90a5/9jjV+WLj1q5U96g60rd5rxox4EKNPNGlHx/m5rWBJhHBV4rseJjtFjg==" + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-0.4.0.tgz", + "integrity": "sha512-K7enUvVKexr5USpzZC0GRG+RCNH8A9A1bwbKKi4+YR2ClXHfiLqPak2Gf+McrfsOhlPEr7fnYACITgbs0CwrBQ==" }, "@octokit/auth-token": { "version": "2.4.2", diff --git a/package.json b/package.json index d4952dc6..6df9d8fe 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@ethereum-navigator/navigator": "^0.5.0", - "@oceanprotocol/contracts": "^0.3.5", + "@oceanprotocol/contracts": "^0.4.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 a9cb2493..b4ec95cd 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -321,7 +321,7 @@ export class OceanPool extends Pool { const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { from: account }) - const events = await factory.getPastEvents('SPoolRegistered', { + const events = await factory.getPastEvents('BPoolRegistered', { filter: {}, fromBlock: 0, toBlock: 'latest' diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 2c76f194..f57b738e 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -1,7 +1,7 @@ import Web3 from 'web3' import { AbiItem } from 'web3-utils/types' import Decimal from 'decimal.js' -import jsonpoolABI from '@oceanprotocol/contracts/artifacts/SPool.json' +import jsonpoolABI from '@oceanprotocol/contracts/artifacts/BPool.json' import { PoolFactory } from './PoolFactory' /** diff --git a/src/balancer/PoolFactory.ts b/src/balancer/PoolFactory.ts index c7c77c70..bf363bc1 100644 --- a/src/balancer/PoolFactory.ts +++ b/src/balancer/PoolFactory.ts @@ -1,6 +1,6 @@ import Web3 from 'web3' import { AbiItem } from 'web3-utils/types' -import jsonFactoryABI from '@oceanprotocol/contracts/artifacts/SFactory.json' +import jsonFactoryABI from '@oceanprotocol/contracts/artifacts/BFactory.json' export class PoolFactory { public GASLIMIT_DEFAULT = 5000000 @@ -43,13 +43,13 @@ export class PoolFactory { }) const transactiondata = await factory.methods - .newSPool() + .newBPool() .send({ from: account, gas: this.GASLIMIT_DEFAULT }) let pooladdress: string try { - pooladdress = transactiondata.events.SPoolRegistered.returnValues[0] + pooladdress = transactiondata.events.BPoolRegistered.returnValues[0] } catch (e) { console.error(e) } diff --git a/src/datatokens/Datatokens.ts b/src/datatokens/Datatokens.ts index 719f6cbc..b033c95e 100644 --- a/src/datatokens/Datatokens.ts +++ b/src/datatokens/Datatokens.ts @@ -35,26 +35,37 @@ export class DataTokens { /** * Create new datatoken * @param {String} metaDataStoreURI + * @param {String} name Token name + * @param {String} symbol Token symbol + * @param {String} cap Maximum cap (Number) - will be converted to wei * @param {String} address * @return {Promise} datatoken address */ - public async create(metaDataStoreURI: string, address: string): Promise { + public async create( + metaDataStoreURI: string, + name: string, + symbol: string, + cap: string, + address: string + ): Promise { // Create factory contract object const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { from: address }) const estGas = await factory.methods - .createToken(metaDataStoreURI) + .createToken(metaDataStoreURI, name, symbol, this.web3.utils.toWei(cap)) .estimateGas(function (err, estGas) { if (err) console.log('Datatokens: ' + err) return estGas }) // Invoke createToken function of the contract - const trxReceipt = await factory.methods.createToken(metaDataStoreURI).send({ - from: address, - gas: estGas + 1, - gasPrice: '3000000000' - }) + const trxReceipt = await factory.methods + .createToken(metaDataStoreURI, name, symbol, this.web3.utils.toWei(cap)) + .send({ + from: address, + gas: estGas + 1, + gasPrice: '3000000000' + }) let tokenAddress = null try { diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index dd55d687..491dd5b1 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -56,7 +56,10 @@ export class Assets extends Instantiable { metadata: Metadata, publisher: Account, services: Service[] = [], - dtAddress?: string + dtAddress?: string, + name?: string, + symbol?: string, + cap?: string ): SubscribablePromise { this.logger.log('Creating asset') return new SubscribablePromise(async (observer) => { @@ -65,11 +68,20 @@ export class Assets extends Instantiable { } if (!dtAddress) { this.logger.log('Creating datatoken') + if (!name) name = 'DataToken' + if (!symbol) symbol = 'DT' + if (!cap) cap = '1410000000000000000000000000' observer.next(CreateProgressStep.CreatingDataToken) const metadataStoreURI = this.ocean.metadatastore.getURI() const jsonBlob = { t: 1, url: metadataStoreURI } const { datatokens } = this.ocean - dtAddress = await datatokens.create(JSON.stringify(jsonBlob), publisher.getId()) + dtAddress = await datatokens.create( + JSON.stringify(jsonBlob), + name, + symbol, + cap, + publisher.getId() + ) this.logger.log('DataToken creted') observer.next(CreateProgressStep.DataTokenCreated) } diff --git a/test/TestContractHandler.ts b/test/TestContractHandler.ts index 6a4cf090..c017de8e 100644 --- a/test/TestContractHandler.ts +++ b/test/TestContractHandler.ts @@ -1,7 +1,7 @@ import Web3 from 'web3' import { Contract } from 'web3-eth-contract' import { AbiItem } from 'web3-utils/types' - +const communityCollector = '0xeE9300b7961e0a01d9f0adb863C7A227A07AaD75' export class TestContractHandler { public factory: Contract public template: Contract @@ -40,7 +40,7 @@ export class TestContractHandler { estGas = await this.template .deploy({ data: this.templateBytecode, - arguments: ['Template Contract', 'TEMPLATE', minter, cap, blob] + arguments: ['Template Contract', 'TEMPLATE', minter, cap, blob, communityCollector] }) .estimateGas(function (err, estGas) { if (err) console.log('DeployContracts: ' + err) @@ -50,7 +50,7 @@ export class TestContractHandler { this.templateAddress = await this.template .deploy({ data: this.templateBytecode, - arguments: ['Template Contract', 'TEMPLATE', minter, cap, blob] + arguments: ['Template Contract', 'TEMPLATE', minter, cap, blob, communityCollector] }) .send({ from: minter, @@ -64,7 +64,7 @@ export class TestContractHandler { estGas = await this.factory .deploy({ data: this.factoryBytecode, - arguments: [this.templateAddress] + arguments: [this.templateAddress, communityCollector] }) .estimateGas(function (err, estGas) { if (err) console.log('DeployContracts: ' + err) @@ -74,7 +74,7 @@ export class TestContractHandler { this.factoryAddress = await this.factory .deploy({ data: this.factoryBytecode, - arguments: [this.templateAddress] + arguments: [this.templateAddress, communityCollector] }) .send({ from: minter, diff --git a/test/integration/ComputeFlow.test.ts b/test/integration/ComputeFlow.test.ts index 8330a4fc..3ae5e799 100644 --- a/test/integration/ComputeFlow.test.ts +++ b/test/integration/ComputeFlow.test.ts @@ -79,7 +79,13 @@ describe('Compute flow', () => { datatokensTemplate.abi as AbiItem[], web3 ) - tokenAddress = await datatoken.create(blob, alice.getId()) + tokenAddress = await datatoken.create( + blob, + 'AliceDT', + 'DTA', + '10000000000', + alice.getId() + ) assert(tokenAddress != null) }) diff --git a/test/integration/Marketplaceflow.test.ts b/test/integration/Marketplaceflow.test.ts index a70aa8a6..480f6ece 100644 --- a/test/integration/Marketplaceflow.test.ts +++ b/test/integration/Marketplaceflow.test.ts @@ -57,7 +57,13 @@ describe('Marketplace flow', () => { datatokensTemplate.abi as AbiItem[], web3 ) - tokenAddress = await datatoken.create(blob, alice.getId()) + tokenAddress = await datatoken.create( + blob, + 'AliceDT', + 'DTA', + '10000000000', + alice.getId() + ) assert(tokenAddress != null) }) diff --git a/test/integration/Simpleflow.test.ts b/test/integration/Simpleflow.test.ts index 27086427..bddfacc5 100644 --- a/test/integration/Simpleflow.test.ts +++ b/test/integration/Simpleflow.test.ts @@ -41,7 +41,7 @@ describe('Simple flow', () => { datatokensTemplate.abi as AbiItem[], web3 ) - tokenAddress = await datatoken.create(blob, alice) + tokenAddress = await datatoken.create(blob, 'AliceDT', 'DTA', '10000000000', alice) }) it('Alice mints 100 tokens', async () => { await datatoken.mint(tokenAddress, alice, tokenAmount) diff --git a/test/unit/Datatokens.test.ts b/test/unit/Datatokens.test.ts index bdd39fb3..2e1a9dfe 100644 --- a/test/unit/Datatokens.test.ts +++ b/test/unit/Datatokens.test.ts @@ -45,7 +45,7 @@ describe('DataTokens', () => { }) it('should create datatokens smart contract', async () => { - tokenAddress = await datatoken.create(blob, minter) + tokenAddress = await datatoken.create(blob, 'AliceDT', 'DTA', '10000000000', minter) assert(tokenAddress !== null) }) diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index c8cf83d0..936f54b6 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -10,8 +10,8 @@ import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json' import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' // this will be replaced by our SFactory/SPool -import OceanPoolFactory from '@oceanprotocol/contracts/artifacts/SFactory.json' -import OceanSPool from '@oceanprotocol/contracts/artifacts/SPool.json' +import OceanPoolFactory from '@oceanprotocol/contracts/artifacts/BFactory.json' +import OceanSPool from '@oceanprotocol/contracts/artifacts/BPool.json' const web3 = new Web3('http://127.0.0.1:8545') describe('Balancer flow', () => { @@ -74,7 +74,7 @@ describe('Balancer flow', () => { }) it('should create datatokens smart contract', async () => { - tokenAddress = await datatoken.create(blob, alice) + tokenAddress = await datatoken.create(blob, 'AliceDT', 'DTA', '10000000000', alice) assert(tokenAddress !== null) }) it('Create a dummy OceanToken', async () => { @@ -85,7 +85,13 @@ describe('Balancer flow', () => { datatokensTemplate.abi as AbiItem[], web3 ) - oceanTokenAddress = await oceandatatoken.create(blob, alice) + oceanTokenAddress = await oceandatatoken.create( + blob, + 'AliceDT2', + 'DTA2', + '10000000000', + alice + ) }) it('should initialize OceanPool class', async () => { Pool = new OceanPool( diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index a7a18c08..8d59e5d4 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -79,7 +79,13 @@ describe('FixedRateExchange flow', () => { }) it('should create datatokens smart contract', async () => { - tokenAddress = await datatoken.create(blob, alice) + tokenAddress = await datatoken.create( + blob, + 'AliceDT', + 'DTA', + web3.utils.toWei('1000000000000000'), + alice + ) assert(tokenAddress !== null) if (consoleDebug) console.log("Alice's address:" + alice) if (consoleDebug) console.log('data Token address:' + tokenAddress) @@ -92,7 +98,13 @@ describe('FixedRateExchange flow', () => { datatokensTemplate.abi as AbiItem[], web3 ) - oceanTokenAddress = await oceandatatoken.create(blob, bob) + oceanTokenAddress = await oceandatatoken.create( + blob, + 'BobDT', + 'DTB', + web3.utils.toWei('1000000000000000'), + bob + ) if (consoleDebug) console.log("Bob's address:" + bob) if (consoleDebug) console.log('oceanTokenAddress:' + oceanTokenAddress) }) From 6121da3fff039b98e674dce11befa55dfb61cfcc Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Mon, 31 Aug 2020 00:33:35 -0700 Subject: [PATCH 06/11] fix lint --- test/TestContractHandler.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test/TestContractHandler.ts b/test/TestContractHandler.ts index c017de8e..59daa253 100644 --- a/test/TestContractHandler.ts +++ b/test/TestContractHandler.ts @@ -40,7 +40,14 @@ export class TestContractHandler { estGas = await this.template .deploy({ data: this.templateBytecode, - arguments: ['Template Contract', 'TEMPLATE', minter, cap, blob, communityCollector] + arguments: [ + 'Template Contract', + 'TEMPLATE', + minter, + cap, + blob, + communityCollector + ] }) .estimateGas(function (err, estGas) { if (err) console.log('DeployContracts: ' + err) @@ -50,7 +57,14 @@ export class TestContractHandler { this.templateAddress = await this.template .deploy({ data: this.templateBytecode, - arguments: ['Template Contract', 'TEMPLATE', minter, cap, blob, communityCollector] + arguments: [ + 'Template Contract', + 'TEMPLATE', + minter, + cap, + blob, + communityCollector + ] }) .send({ from: minter, From e746bbe953da6eb71dbab8b17e798a5eaac01208 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Mon, 31 Aug 2020 01:52:19 -0700 Subject: [PATCH 07/11] update contracts addresses on rinkeby --- src/utils/ConfigHelper.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index b7954579..a12efd87 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -26,12 +26,12 @@ const configs = [ chainId: 4, network: 'rinkeby', url: 'https://rinkeby.infura.io/v3', - factoryAddress: '0xcDfEe5D80041224cDCe9AE2334E85B3236385EA3', + factoryAddress: '0x2C63bf697f74C72CFB727Fb5eB8e6266cE341e13', oceanTokenAddress: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07', metadataStoreUri: 'https://aquarius.rinkeby.v3.dev-ocean.com', providerUri: 'https://provider.rinkeby.v3.dev-ocean.com', - poolFactoryAddress: '0xA4531C624A3D88323a1e178DABe1233AF178701B', - fixedRateExchangeAddress: '0x7219AfFc1C2b474830D9d9b0423ecf47073C5488' + poolFactoryAddress: '0x2C63bf697f74C72CFB727Fb5eB8e6266cE341e13', + fixedRateExchangeAddress: '0x2C63bf697f74C72CFB727Fb5eB8e6266cE341e13' }, { chainId: 1, From d65a59c5d68383182c49c813d96c0d217589f7e9 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Mon, 31 Aug 2020 06:52:20 -0700 Subject: [PATCH 08/11] update contracts to 0.4.1 --- package-lock.json | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8ed8538f..c70de235 100644 --- a/package-lock.json +++ b/package-lock.json @@ -880,9 +880,9 @@ } }, "@oceanprotocol/contracts": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-0.4.0.tgz", - "integrity": "sha512-K7enUvVKexr5USpzZC0GRG+RCNH8A9A1bwbKKi4+YR2ClXHfiLqPak2Gf+McrfsOhlPEr7fnYACITgbs0CwrBQ==" + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-0.4.1.tgz", + "integrity": "sha512-gc6bCt3pq9cpk1mYDKfsZhLlaM+8yQDFmOjtmT1KGXRmnTBcvmwCQXMrL5VohFaFi7Iqio3FZtuhYyRaEjikCw==" }, "@octokit/auth-token": { "version": "2.4.2", diff --git a/package.json b/package.json index 0911dc85..43113a69 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@ethereum-navigator/navigator": "^0.5.0", - "@oceanprotocol/contracts": "^0.4.0", + "@oceanprotocol/contracts": "^0.4.1", "decimal.js": "^10.2.0", "fs": "0.0.1-security", "node-fetch": "^2.6.0", From 7cec2e585e1db210fdc4528cd7e06a4a69453192 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Mon, 31 Aug 2020 06:58:11 -0700 Subject: [PATCH 09/11] update contracts addresses from 0.4.1 --- src/utils/ConfigHelper.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index a12efd87..bcd98722 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -26,12 +26,12 @@ const configs = [ chainId: 4, network: 'rinkeby', url: 'https://rinkeby.infura.io/v3', - factoryAddress: '0x2C63bf697f74C72CFB727Fb5eB8e6266cE341e13', + factoryAddress: '0x3ECd1429101f93149D799Ef257C07a2B1Dc30897', oceanTokenAddress: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07', metadataStoreUri: 'https://aquarius.rinkeby.v3.dev-ocean.com', providerUri: 'https://provider.rinkeby.v3.dev-ocean.com', - poolFactoryAddress: '0x2C63bf697f74C72CFB727Fb5eB8e6266cE341e13', - fixedRateExchangeAddress: '0x2C63bf697f74C72CFB727Fb5eB8e6266cE341e13' + poolFactoryAddress: '0x9B90A1358fbeEC1C4bB1DA7D4E85C708f87556Ec', + fixedRateExchangeAddress: '0x991c08bD00761A299d3126a81a985329096896D4' }, { chainId: 1, From fec3670e6a23ea4adbaf6a24bba32638a1d9fb18 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Mon, 31 Aug 2020 08:13:58 -0700 Subject: [PATCH 10/11] updates --- ...ixRateExchange.ts => FixedRateExchange.ts} | 31 +++++++------------ src/ocean/Ocean.ts | 5 ++- .../unit/exchanges/FixedPriceExchange.test.ts | 2 +- 3 files changed, 16 insertions(+), 22 deletions(-) rename src/exchange/{FixRateExchange.ts => FixedRateExchange.ts} (93%) diff --git a/src/exchange/FixRateExchange.ts b/src/exchange/FixedRateExchange.ts similarity index 93% rename from src/exchange/FixRateExchange.ts rename to src/exchange/FixedRateExchange.ts index 333a9b15..c8cb15a6 100644 --- a/src/exchange/FixRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -11,6 +11,8 @@ export interface FixedPricedExchange { supply: string } +const DEFAULT_GAS_LIMIT = 200000 + export class OceanFixedRateExchange { /** Ocean related functions */ public oceanAddress: string = null @@ -104,24 +106,14 @@ export class OceanFixedRateExchange { .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) .estimateGas(function (err, g) { if (err) { - // console.log('FixedPriceExchange: ' + err) - return 200000 + return DEFAULT_GAS_LIMIT } else { return g } }) } catch (e) { - // console.log('FixedPriceExchange: ' + e) - estGas = 200000 + estGas = DEFAULT_GAS_LIMIT } - // console.log('estGas: ' + estGas) - /* const estGas = await this.contract.methods - .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) - .estimateGas(function (err, estGas) { - if (err) console.log('FixedPriceExchange: ' + err) - return estGas - }) - */ try { const trxReceipt = await this.contract.methods .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) @@ -166,12 +158,12 @@ export class OceanFixedRateExchange { .estimateGas(function (err, estGas) { if (err) { // console.log('FixedPriceExchange: ' + err) - return 200000 + return DEFAULT_GAS_LIMIT } return estGas }) } catch (e) { - estGas = 200000 + estGas = DEFAULT_GAS_LIMIT } const trxReceipt = await this.contract.methods .setRate(exchangeId, this.web3.utils.toWei(String(newRate))) @@ -196,12 +188,12 @@ export class OceanFixedRateExchange { .estimateGas(function (err, estGas) { if (err) { // console.log('FixedPriceExchange: ' + err) - estGas = 200000 + estGas = DEFAULT_GAS_LIMIT } return estGas }) } catch (e) { - estGas = 200000 + estGas = DEFAULT_GAS_LIMIT } const trxReceipt = await this.contract.methods.activate(exchangeId).send({ from: address, @@ -224,12 +216,12 @@ export class OceanFixedRateExchange { .estimateGas(function (err, estGas) { if (err) { // console.log('FixedPriceExchange: ' + err) - estGas = 200000 + estGas = DEFAULT_GAS_LIMIT } return estGas }) } catch (e) { - estGas = 200000 + estGas = DEFAULT_GAS_LIMIT } const trxReceipt = await this.contract.methods.deactivate(exchangeId).send({ from: address, @@ -292,8 +284,7 @@ export class OceanFixedRateExchange { * @return {Promise} Exchanges list */ public async getExchanges(): Promise { - const result = await this.contract.methods.getExchanges().call() - return result + return await this.contract.methods.getExchanges().call() } /** diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index ad7c7a4a..98e8e5c4 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -13,7 +13,10 @@ import { } from '../Instantiable.abstract' import { Compute } from './Compute' import { OceanPool } from '../balancer/OceanPool' -import { OceanFixedRateExchange, FixedPricedExchange } from '../exchange/FixRateExchange' +import { + OceanFixedRateExchange, + FixedPricedExchange +} from '../exchange/FixedRateExchange' /** * Main interface for Ocean Protocol. diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index 8d59e5d4..95011b03 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -6,7 +6,7 @@ import { DataTokens } from '../../../src/datatokens/Datatokens' import { OceanFixedRateExchange, FixedPricedExchange -} from '../../../src/exchange/FixRateExchange' +} from '../../../src/exchange/FixedRateExchange' import Web3 from 'web3' import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json' From 9c7ea98fce1a84807b40787318300bb941ffc1a1 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Tue, 1 Sep 2020 00:27:33 -0700 Subject: [PATCH 11/11] use TransactionReceipt --- src/exchange/FixedRateExchange.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index c8cb15a6..fde5f405 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -1,5 +1,6 @@ import defaultFixedRateExchangeABI from '@oceanprotocol/contracts/artifacts/FixedRateExchange.json' import BigNumber from 'bignumber.js' +import { TransactionReceipt } from 'web3-core' export interface FixedPricedExchange { exchangeID?: string @@ -93,13 +94,13 @@ export class OceanFixedRateExchange { * @param {String} exchangeId ExchangeId * @param {Number} dataTokenAmount Amount of Data Tokens * @param {String} address User address - * @return {Promise} transaction receipt + * @return {Promise} transaction receipt */ public async buyDT( exchangeId: string, dataTokenAmount: string, address: string - ): Promise { + ): Promise { let estGas try { estGas = await this.contract.methods @@ -144,13 +145,13 @@ export class OceanFixedRateExchange { * @param {String} exchangeId ExchangeId * @param {Number} newRate New rate * @param {String} address User account - * @return {Promise} transaction receipt + * @return {Promise} transaction receipt */ public async setRate( exchangeId: string, newRate: number, address: string - ): Promise { + ): Promise { let estGas try { estGas = await this.contract.methods @@ -178,9 +179,12 @@ export class OceanFixedRateExchange { * Activate an exchange * @param {String} exchangeId ExchangeId * @param {String} address User address - * @return {Promise} transaction receipt + * @return {Promise} transaction receipt */ - public async activate(exchangeId: string, address: string): Promise { + public async activate( + exchangeId: string, + address: string + ): Promise { let estGas try { estGas = await this.contract.methods @@ -206,9 +210,12 @@ export class OceanFixedRateExchange { * Deactivate an exchange * @param {String} exchangeId ExchangeId * @param {String} address User address - * @return {Promise} transaction receipt + * @return {Promise} transaction receipt */ - public async deactivate(exchangeId: string, address: string): Promise { + public async deactivate( + exchangeId: string, + address: string + ): Promise { let estGas try { estGas = await this.contract.methods