Skip to content

Commit

Permalink
update to contracts alpha.19 (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcos20 authored Feb 14, 2022
1 parent bad1fd5 commit 91a4431
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 38 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/@types/Erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions src/@types/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ export interface AmountsOutMaxFee {
swapMarketFee: string
maxPrice?: string
}

export interface PoolPriceAndFees {
tokenAmount: string
liquidityProviderSwapFeeAmount: string
oceanFeeAmount: string
publishMarketSwapFeeAmount: string
consumeMarketSwapFeeAmount: string
}
8 changes: 6 additions & 2 deletions src/factories/NFTFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
FreCreationParams,
Erc20CreateParams,
PoolCreationParams,
DispenserCreationParams
DispenserCreationParams,
ConsumeMarketFee
} from '../@types/index.js'

interface Template {
Expand All @@ -31,7 +32,8 @@ export interface TokenOrder {
tokenAddress: string
consumer: string
serviceIndex: number
_providerFees: ProviderFees
_providerFee: ProviderFees
_consumeMarketFee: ConsumeMarketFee
}

export interface NftCreateData {
Expand Down Expand Up @@ -91,6 +93,7 @@ export class NftFactory {
nftData.symbol,
nftData.templateIndex,
addressZERO,
addressZERO,
nftData.tokenURI
)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
Expand Down Expand Up @@ -133,6 +136,7 @@ export class NftFactory {
nftData.symbol,
nftData.templateIndex,
addressZERO,
addressZERO,
nftData.tokenURI
)
.send({
Expand Down
47 changes: 42 additions & 5 deletions src/pools/balancer/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
CurrentFees,
TokenInOutMarket,
AmountsInMaxFee,
AmountsOutMaxFee
AmountsOutMaxFee,
PoolPriceAndFees
} from '../../@types'
import { Config } from '../../models'
const MaxUint256 =
Expand Down Expand Up @@ -1415,7 +1416,7 @@ export class Pool {
tokenOut: string,
tokenAmountOut: string,
swapMarketFee: string
): Promise<string> {
): Promise<PoolPriceAndFees> {
const pool = setContractDefaults(
new this.web3.eth.Contract(this.poolAbi, poolAddress),
this.config
Expand All @@ -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}`)
}
Expand All @@ -1455,7 +1474,7 @@ export class Pool {
tokenOut: string,
tokenAmountIn: string,
swapMarketFee: string
): Promise<string> {
): Promise<PoolPriceAndFees> {
const pool = setContractDefaults(
new this.web3.eth.Contract(this.poolAbi, poolAddress),
this.config
Expand All @@ -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}`)
}
Expand Down
28 changes: 23 additions & 5 deletions src/tokens/Datatoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -27,7 +32,8 @@ interface Roles {
export interface OrderParams {
consumer: string
serviceIndex: number
_providerFees: ProviderFees
_providerFee: ProviderFees
_consumeMarketFee: ConsumeMarketFee
}

export interface DispenserParams {
Expand Down Expand Up @@ -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<any>}
*/
Expand All @@ -922,6 +929,7 @@ export class Datatoken {
consumer: string,
serviceIndex: number,
providerFees: ProviderFees,
consumeMarketFee?: ConsumeMarketFee,
contractInstance?: Contract
): Promise<any> {
const dtContract =
Expand All @@ -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
Expand All @@ -950,31 +958,41 @@ 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<TransactionReceipt>} string
*/
public async startOrder(
dtAddress: string,
address: string,
consumer: string,
serviceIndex: number,
providerFees: ProviderFees
providerFees: ProviderFees,
consumeMarketFee?: ConsumeMarketFee
): Promise<TransactionReceipt> {
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,
address,
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,
Expand Down
14 changes: 7 additions & 7 deletions src/utils/ConfigHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -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'
},
{
Expand Down
7 changes: 5 additions & 2 deletions test/integration/ComputeFlow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions test/unit/NftFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 91a4431

Please sign in to comment.