From 91a44319385a729f048c23924ad59886eeb5c79e Mon Sep 17 00:00:00 2001 From: Alex Coseru Date: Mon, 14 Feb 2022 18:36:15 +0200 Subject: [PATCH] update to contracts alpha.19 (#1286) --- package-lock.json | 14 ++++---- package.json | 2 +- src/@types/Erc20.ts | 6 ++++ src/@types/Pool.ts | 8 +++++ src/factories/NFTFactory.ts | 8 +++-- src/pools/balancer/Pool.ts | 47 ++++++++++++++++++++++++--- src/tokens/Datatoken.ts | 28 +++++++++++++--- src/utils/ConfigHelper.ts | 14 ++++---- test/integration/ComputeFlow.test.ts | 7 ++-- test/unit/NftFactory.test.ts | 11 +++++-- test/unit/pools/balancer/Pool.test.ts | 13 +++++--- test/unit/tokens/Datatoken.test.ts | 16 +++++++-- 12 files changed, 136 insertions(+), 38 deletions(-) diff --git a/package-lock.json b/package-lock.json index 32629e87d..3880fec7c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0-next.17", "license": "Apache-2.0", "dependencies": { - "@oceanprotocol/contracts": "1.0.0-alpha.18", + "@oceanprotocol/contracts": "1.0.0-alpha.19", "bignumber.js": "^9.0.2", "cross-fetch": "^3.1.5", "crypto-js": "^4.1.1", @@ -3033,9 +3033,9 @@ } }, "node_modules/@oceanprotocol/contracts": { - "version": "1.0.0-alpha.18", - "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.18.tgz", - "integrity": "sha512-0MFX5n71dke095drRKXoCvF25FrAuraAcNyuUqps0WsVyM7N+jIdiJsF4u26/TrfYzNV/Y/0zRmi6bR4mTA7KA==", + "version": "1.0.0-alpha.19", + "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.19.tgz", + "integrity": "sha512-3E4eMo2lMyWMjiGk5dvSZvs/QNq7GSAPPzfMbxVLCdjfyP7kvVVVgn0pxabQtexkK4NK7tpNdA+IbsJaM0BBdw==", "dependencies": { "@openzeppelin/contracts": "^4.3.3", "@openzeppelin/test-helpers": "^0.5.15", @@ -27682,9 +27682,9 @@ } }, "@oceanprotocol/contracts": { - "version": "1.0.0-alpha.18", - "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.18.tgz", - "integrity": "sha512-0MFX5n71dke095drRKXoCvF25FrAuraAcNyuUqps0WsVyM7N+jIdiJsF4u26/TrfYzNV/Y/0zRmi6bR4mTA7KA==", + "version": "1.0.0-alpha.19", + "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.19.tgz", + "integrity": "sha512-3E4eMo2lMyWMjiGk5dvSZvs/QNq7GSAPPzfMbxVLCdjfyP7kvVVVgn0pxabQtexkK4NK7tpNdA+IbsJaM0BBdw==", "requires": { "@openzeppelin/contracts": "^4.3.3", "@openzeppelin/test-helpers": "^0.5.15", diff --git a/package.json b/package.json index 4809ef284..f3948907b 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "web3": "^1.7.0" }, "dependencies": { - "@oceanprotocol/contracts": "1.0.0-alpha.18", + "@oceanprotocol/contracts": "1.0.0-alpha.19", "bignumber.js": "^9.0.2", "cross-fetch": "^3.1.5", "crypto-js": "^4.1.1", diff --git a/src/@types/Erc20.ts b/src/@types/Erc20.ts index a7a0892db..5df3e7c7c 100644 --- a/src/@types/Erc20.ts +++ b/src/@types/Erc20.ts @@ -9,3 +9,9 @@ export interface Erc20CreateParams { name?: string symbol?: string } + +export interface ConsumeMarketFee { + consumeMarketFeeAddress: string + consumeMarketFeeToken: string // address of the token marketplace wants to add fee on top + consumeMarketFeeAmount: string +} diff --git a/src/@types/Pool.ts b/src/@types/Pool.ts index a01269d6c..d93e4a693 100644 --- a/src/@types/Pool.ts +++ b/src/@types/Pool.ts @@ -38,3 +38,11 @@ export interface AmountsOutMaxFee { swapMarketFee: string maxPrice?: string } + +export interface PoolPriceAndFees { + tokenAmount: string + liquidityProviderSwapFeeAmount: string + oceanFeeAmount: string + publishMarketSwapFeeAmount: string + consumeMarketSwapFeeAmount: string +} diff --git a/src/factories/NFTFactory.ts b/src/factories/NFTFactory.ts index 5e743d9ad..1458b1d77 100644 --- a/src/factories/NFTFactory.ts +++ b/src/factories/NFTFactory.ts @@ -19,7 +19,8 @@ import { FreCreationParams, Erc20CreateParams, PoolCreationParams, - DispenserCreationParams + DispenserCreationParams, + ConsumeMarketFee } from '../@types/index.js' interface Template { @@ -31,7 +32,8 @@ export interface TokenOrder { tokenAddress: string consumer: string serviceIndex: number - _providerFees: ProviderFees + _providerFee: ProviderFees + _consumeMarketFee: ConsumeMarketFee } export interface NftCreateData { @@ -91,6 +93,7 @@ export class NftFactory { nftData.symbol, nftData.templateIndex, addressZERO, + addressZERO, nftData.tokenURI ) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) @@ -133,6 +136,7 @@ export class NftFactory { nftData.symbol, nftData.templateIndex, addressZERO, + addressZERO, nftData.tokenURI ) .send({ diff --git a/src/pools/balancer/Pool.ts b/src/pools/balancer/Pool.ts index 531dc9cef..fb429beb5 100644 --- a/src/pools/balancer/Pool.ts +++ b/src/pools/balancer/Pool.ts @@ -17,7 +17,8 @@ import { CurrentFees, TokenInOutMarket, AmountsInMaxFee, - AmountsOutMaxFee + AmountsOutMaxFee, + PoolPriceAndFees } from '../../@types' import { Config } from '../../models' const MaxUint256 = @@ -1415,7 +1416,7 @@ export class Pool { tokenOut: string, tokenAmountOut: string, swapMarketFee: string - ): Promise { + ): Promise { const pool = setContractDefaults( new this.web3.eth.Contract(this.poolAbi, poolAddress), this.config @@ -1434,7 +1435,25 @@ export class Pool { this.web3.utils.toWei(swapMarketFee) ) .call() - amount = await unitsToAmount(this.web3, tokenIn, result) + amount = { + tokenAmount: await unitsToAmount(this.web3, tokenOut, result.tokenAmountIn), + liquidityProviderSwapFeeAmount: await unitsToAmount( + this.web3, + tokenIn, + result.lpFeeAmount + ), + oceanFeeAmount: await unitsToAmount(this.web3, tokenIn, result.oceanFeeAmount), + publishMarketSwapFeeAmount: await unitsToAmount( + this.web3, + tokenIn, + result.publishMarketSwapFeeAmount + ), + consumeMarketSwapFeeAmount: await unitsToAmount( + this.web3, + tokenIn, + result.consumeMarketSwapFeeAmount + ) + } } catch (e) { LoggerInstance.error(`ERROR: Failed to calcInGivenOut ${e.message}`) } @@ -1455,7 +1474,7 @@ export class Pool { tokenOut: string, tokenAmountIn: string, swapMarketFee: string - ): Promise { + ): Promise { const pool = setContractDefaults( new this.web3.eth.Contract(this.poolAbi, poolAddress), this.config @@ -1475,7 +1494,25 @@ export class Pool { ) .call() - amount = await unitsToAmount(this.web3, tokenOut, result) + amount = { + tokenAmount: await unitsToAmount(this.web3, tokenOut, result.tokenAmountOut), + liquidityProviderSwapFeeAmount: await unitsToAmount( + this.web3, + tokenIn, + result.lpFeeAmount + ), + oceanFeeAmount: await unitsToAmount(this.web3, tokenIn, result.oceanFeeAmount), + publishMarketSwapFeeAmount: await unitsToAmount( + this.web3, + tokenIn, + result.publishMarketSwapFeeAmount + ), + consumeMarketSwapFeeAmount: await unitsToAmount( + this.web3, + tokenIn, + result.consumeMarketSwapFeeAmount + ) + } } catch (e) { LoggerInstance.error(`ERROR: Failed to calcOutGivenIn ${e.message}`) } diff --git a/src/tokens/Datatoken.ts b/src/tokens/Datatoken.ts index d213cf926..b63e911cc 100644 --- a/src/tokens/Datatoken.ts +++ b/src/tokens/Datatoken.ts @@ -12,7 +12,12 @@ import { configHelperNetworks, getFreOrderParams } from '../utils' -import { FreOrderParams, FreCreationParams, ProviderFees } from '../@types' +import { + ConsumeMarketFee, + FreOrderParams, + FreCreationParams, + ProviderFees +} from '../@types' import { Nft } from './NFT' import { Config } from '../models/index.js' @@ -27,7 +32,8 @@ interface Roles { export interface OrderParams { consumer: string serviceIndex: number - _providerFees: ProviderFees + _providerFee: ProviderFees + _consumeMarketFee: ConsumeMarketFee } export interface DispenserParams { @@ -913,6 +919,7 @@ export class Datatoken { * @param {String} consumer Consumer Address * @param {Number} serviceIndex Service index in the metadata * @param {providerFees} providerFees provider fees + * @param {consumeMarketFee} ConsumeMarketFee consume market fees * @param {Contract} contractInstance optional contract instance * @return {Promise} */ @@ -922,6 +929,7 @@ export class Datatoken { consumer: string, serviceIndex: number, providerFees: ProviderFees, + consumeMarketFee?: ConsumeMarketFee, contractInstance?: Contract ): Promise { const dtContract = @@ -936,7 +944,7 @@ export class Datatoken { let estGas try { estGas = await dtContract.methods - .startOrder(consumer, serviceIndex, providerFees) + .startOrder(consumer, serviceIndex, providerFees, consumeMarketFee) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { estGas = gasLimitDefault @@ -950,6 +958,7 @@ export class Datatoken { * @param {String} consumer Consumer Address * @param {Number} serviceIndex Service index in the metadata * @param {providerFees} providerFees provider fees + * @param {consumeMarketFee} ConsumeMarketFee consume market fees * @return {Promise} string */ public async startOrder( @@ -957,12 +966,20 @@ export class Datatoken { address: string, consumer: string, serviceIndex: number, - providerFees: ProviderFees + providerFees: ProviderFees, + consumeMarketFee?: ConsumeMarketFee ): Promise { const dtContract = setContractDefaults( new this.web3.eth.Contract(this.datatokensAbi, dtAddress), this.config ) + if (!consumeMarketFee) { + consumeMarketFee = { + consumeMarketFeeAddress: '0x0000000000000000000000000000000000000000', + consumeMarketFeeToken: '0x0000000000000000000000000000000000000000', + consumeMarketFeeAmount: '0' + } + } try { const estGas = await this.estGasStartOrder( dtAddress, @@ -970,11 +987,12 @@ export class Datatoken { consumer, serviceIndex, providerFees, + consumeMarketFee, dtContract ) const trxReceipt = await dtContract.methods - .startOrder(consumer, serviceIndex, providerFees) + .startOrder(consumer, serviceIndex, providerFees, consumeMarketFee) .send({ from: address, gas: estGas + 1, diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index fa70cbc89..45493eb52 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -39,8 +39,8 @@ export const configHelperNetworks: Config[] = [ chainId: 3, network: 'ropsten', nodeUri: 'https://ropsten.infura.io/v3', - providerUri: 'https://provider.ropsten.oceanprotocol.com', - subgraphUri: 'https://subgraph.ropsten.oceanprotocol.com', + providerUri: 'https://providerv4.ropsten.oceanprotocol.com', + subgraphUri: 'https://subgraphv4.ropsten.oceanprotocol.com', explorerUri: 'https://ropsten.etherscan.io', startBlock: 9227563 }, @@ -82,10 +82,10 @@ export const configHelperNetworks: Config[] = [ { ...configHelperNetworksBase, chainId: 1287, - network: 'moonbeamalpha', + network: 'moonbase', nodeUri: 'https://rpc.testnet.moonbeam.network', - providerUri: 'https://provider.moonbeamalpha.oceanprotocol.com', - subgraphUri: 'https://subgraph.moonbeamalpha.oceanprotocol.com', + providerUri: 'https://providerv4.moonbase.oceanprotocol.com', + subgraphUri: 'https://subgraphv4.moonbase.oceanprotocol.com', explorerUri: 'https://moonbase-blockscout.testnet.moonbeam.network/', startBlock: 90707 }, @@ -113,8 +113,8 @@ export const configHelperNetworks: Config[] = [ chainId: 80001, network: 'mumbai', nodeUri: 'https://polygon-mumbai.infura.io/v3', - providerUri: 'https://provider.mumbai.oceanprotocol.com', - subgraphUri: 'https://subgraph.mumbai.oceanprotocol.com', + providerUri: 'https://providerv4.mumbai.oceanprotocol.com', + subgraphUri: 'https://subgraphv4.mumbai.oceanprotocol.com', explorerUri: 'https://mumbai.polygonscan.com' }, { diff --git a/test/integration/ComputeFlow.test.ts b/test/integration/ComputeFlow.test.ts index 9ad1fb4f7..2936ac864 100644 --- a/test/integration/ComputeFlow.test.ts +++ b/test/integration/ComputeFlow.test.ts @@ -11,6 +11,7 @@ import { SHA256 } from 'crypto-js' import { homedir } from 'os' import fs from 'fs' import { ProviderFees, Erc20CreateParams } from '../../src/@types' +import console from 'console' const data = JSON.parse( fs.readFileSync( @@ -240,11 +241,12 @@ describe('Simple compute tests', async () => { const computeEnvs = await ProviderInstance.getComputeEnvironments(providerUrl) assert(computeEnvs, 'No Compute environments found') // we choose the first env + console.log(computeEnvs) const computeEnv = computeEnvs[0].id const computeConsumerAddress = computeEnvs[0].consumerAddress - // let's have 60 seconds of compute access + // let's have 10 minutesof compute access const mytime = new Date() - mytime.setMinutes(mytime.getMinutes() + 1) + mytime.setMinutes(mytime.getMinutes() + 19) const computeValidUntil = Math.floor(mytime.getTime() / 1000) // initialize provider orders for algo const initializeDataAlgo = await ProviderInstance.initialize( @@ -295,6 +297,7 @@ describe('Simple compute tests', async () => { computeEnv, computeValidUntil ) + console.log(initializeData) const providerDatasetFees: ProviderFees = { providerFeeAddress: initializeData.providerFee.providerFeeAddress, providerFeeToken: initializeData.providerFee.providerFeeToken, diff --git a/test/unit/NftFactory.test.ts b/test/unit/NftFactory.test.ts index 4282b97d9..a34793af5 100644 --- a/test/unit/NftFactory.test.ts +++ b/test/unit/NftFactory.test.ts @@ -398,18 +398,25 @@ describe('Nft Factory test', () => { providerData: web3.utils.toHex(web3.utils.asciiToHex(providerData)), validUntil: providerValidUntil } + const consumeMarketFee = { + consumeMarketFeeAddress: '0x0000000000000000000000000000000000000000', + consumeMarketFeeToken: '0x0000000000000000000000000000000000000000', + consumeMarketFeeAmount: '0' + } const orders: TokenOrder[] = [ { tokenAddress: dtAddress, consumer: consumer, serviceIndex: serviceIndex, - _providerFees: providerFees + _providerFee: providerFees, + _consumeMarketFee: consumeMarketFee }, { tokenAddress: dtAddress2, consumer: consumer, serviceIndex: serviceIndex, - _providerFees: providerFees + _providerFee: providerFees, + _consumeMarketFee: consumeMarketFee } ] await nftFactory.startMultipleTokenOrder(user2, orders) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 52a0e754e..5f0953a25 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -417,13 +417,14 @@ describe('Pool unit test', () => { const maxBTPIn = '0.5' const exactDAIOut = '1' - const amountIn = await pool.getAmountInExactOut( + const result = await pool.getAmountInExactOut( poolAddress, erc20Token, contracts.daiAddress, exactDAIOut, '0.1' ) + const amountIn = result.tokenAmount assert(amountIn != null) @@ -443,13 +444,14 @@ describe('Pool unit test', () => { it('#getAmountOutExactIn- should get the amount out for exact In', async () => { const exactDTIn = '1' - const amountOut = await pool.getAmountOutExactIn( + const result = await pool.getAmountOutExactIn( poolAddress, erc20Token, contracts.daiAddress, exactDTIn, '0.1' ) + const amountOut = result.tokenAmount assert(amountOut != null) @@ -919,14 +921,14 @@ describe('Pool unit test', () => { const maxBTPIn = '0.5' const exactUSDCOut = '1' - const amountIn = await pool.getAmountInExactOut( + const result = await pool.getAmountInExactOut( poolAddress, erc20Token, contracts.usdcAddress, exactUSDCOut, '0.1' ) - + const amountIn = result.tokenAmount assert(amountIn != null) const spotPrice = await pool.getSpotPrice( @@ -942,13 +944,14 @@ describe('Pool unit test', () => { it('#getAmountOutExactIn- should get the amount out for exact In', async () => { const exactDTIn = '1' - const amountOut = await pool.getAmountOutExactIn( + const result = await pool.getAmountOutExactIn( poolAddress, erc20Token, contracts.usdcAddress, exactDTIn, '0.1' ) + const amountOut = result.tokenAmount assert(amountOut != null) diff --git a/test/unit/tokens/Datatoken.test.ts b/test/unit/tokens/Datatoken.test.ts index b96144c92..455160233 100644 --- a/test/unit/tokens/Datatoken.test.ts +++ b/test/unit/tokens/Datatoken.test.ts @@ -408,10 +408,16 @@ describe('Datatoken', () => { providerData: web3.utils.toHex(web3.utils.asciiToHex(providerData)), validUntil: providerValidUntil } + const consumeMarketFee = { + consumeMarketFeeAddress: '0x0000000000000000000000000000000000000000', + consumeMarketFeeToken: '0x0000000000000000000000000000000000000000', + consumeMarketFeeAmount: '0' + } const order: OrderParams = { consumer: user1, serviceIndex: 1, - _providerFees: providerFees + _providerFee: providerFees, + _consumeMarketFee: consumeMarketFee } const buyFromDispenseTx = await datatoken.buyFromDispenserAndOrder( datatokenAddress, @@ -444,10 +450,16 @@ describe('Datatoken', () => { providerData: web3.utils.toHex(web3.utils.asciiToHex(providerData)), validUntil: providerValidUntil } + const consumeMarketFee = { + consumeMarketFeeAddress: '0x0000000000000000000000000000000000000000', + consumeMarketFeeToken: '0x0000000000000000000000000000000000000000', + consumeMarketFeeAmount: '0' + } const order: OrderParams = { consumer: user1, serviceIndex: 1, - _providerFees: providerFees + _providerFee: providerFees, + _consumeMarketFee: consumeMarketFee } const fre: FreOrderParams = {