1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

added provider fees

This commit is contained in:
Bogdan Fazakas 2021-12-07 10:43:44 +02:00
parent ee6e935c0c
commit 075fbe2d9b
6 changed files with 97 additions and 28 deletions

28
package-lock.json generated
View File

@ -3019,7 +3019,7 @@
"node_modules/@oceanprotocol/contracts": {
"name": "hardhat-project",
"version": "v1.0.0-alpha.1",
"resolved": "git+ssh://git@github.com/oceanprotocol/contracts.git#9d734766e9a44ce668321bc79e2d863f91fba116",
"resolved": "git+ssh://git@github.com/oceanprotocol/contracts.git#0129022423d8b0b6a7506b96bb45b9bf4f125ca5",
"dependencies": {
"@openzeppelin/contracts": "^4.3.3",
"@openzeppelin/test-helpers": "^0.5.15",
@ -3618,9 +3618,9 @@
}
},
"node_modules/@truffle/contract": {
"version": "4.3.42",
"resolved": "https://registry.npmjs.org/@truffle/contract/-/contract-4.3.42.tgz",
"integrity": "sha512-CWbKz3L6ldAoh0JX14nNzOyXxWsLiGX5PYpswrwOy0Uk4JYpbVtpSzoQxJbnDTfLUciowfCdG/4QMZ+zo2WVqA==",
"version": "4.3.43",
"resolved": "https://registry.npmjs.org/@truffle/contract/-/contract-4.3.43.tgz",
"integrity": "sha512-F06lBURJ3Mow4fov/9O2rI2giVUhlCskpfESjLSuYnE5ZEDwTiNF99P2iv66EhUqNg6JkUFd9RlDZE5bDnZzJA==",
"dependencies": {
"@ensdomains/ensjs": "^2.0.1",
"@truffle/blockchain-utils": "^0.0.31",
@ -7432,9 +7432,9 @@
}
},
"node_modules/core-js-pure": {
"version": "3.19.2",
"resolved": "https://registry.npmjs.org/core-js-pure/-/core-js-pure-3.19.2.tgz",
"integrity": "sha512-5LkcgQEy8pFeVnd/zomkUBSwnmIxuF1C8E9KrMAbOc8f34IBT9RGvTYeNDdp1PnvMJrrVhvk1hg/yVV5h/znlg==",
"version": "3.19.3",
"resolved": "https://registry.npmjs.org/core-js-pure/-/core-js-pure-3.19.3.tgz",
"integrity": "sha512-N3JruInmCyt7EJj5mAq3csCgGYgiSqu7p7TQp2KOztr180/OAIxyIvL1FCjzgmQk/t3Yniua50Fsak7FShI9lA==",
"hasInstallScript": true,
"peer": true,
"funding": {
@ -27712,7 +27712,7 @@
}
},
"@oceanprotocol/contracts": {
"version": "git+ssh://git@github.com/oceanprotocol/contracts.git#9d734766e9a44ce668321bc79e2d863f91fba116",
"version": "git+ssh://git@github.com/oceanprotocol/contracts.git#0129022423d8b0b6a7506b96bb45b9bf4f125ca5",
"from": "@oceanprotocol/contracts@github:oceanprotocol/contracts#v4main_postaudit",
"requires": {
"@openzeppelin/contracts": "^4.3.3",
@ -28241,9 +28241,9 @@
}
},
"@truffle/contract": {
"version": "4.3.42",
"resolved": "https://registry.npmjs.org/@truffle/contract/-/contract-4.3.42.tgz",
"integrity": "sha512-CWbKz3L6ldAoh0JX14nNzOyXxWsLiGX5PYpswrwOy0Uk4JYpbVtpSzoQxJbnDTfLUciowfCdG/4QMZ+zo2WVqA==",
"version": "4.3.43",
"resolved": "https://registry.npmjs.org/@truffle/contract/-/contract-4.3.43.tgz",
"integrity": "sha512-F06lBURJ3Mow4fov/9O2rI2giVUhlCskpfESjLSuYnE5ZEDwTiNF99P2iv66EhUqNg6JkUFd9RlDZE5bDnZzJA==",
"requires": {
"@ensdomains/ensjs": "^2.0.1",
"@truffle/blockchain-utils": "^0.0.31",
@ -31354,9 +31354,9 @@
}
},
"core-js-pure": {
"version": "3.19.2",
"resolved": "https://registry.npmjs.org/core-js-pure/-/core-js-pure-3.19.2.tgz",
"integrity": "sha512-5LkcgQEy8pFeVnd/zomkUBSwnmIxuF1C8E9KrMAbOc8f34IBT9RGvTYeNDdp1PnvMJrrVhvk1hg/yVV5h/znlg==",
"version": "3.19.3",
"resolved": "https://registry.npmjs.org/core-js-pure/-/core-js-pure-3.19.3.tgz",
"integrity": "sha512-N3JruInmCyt7EJj5mAq3csCgGYgiSqu7p7TQp2KOztr180/OAIxyIvL1FCjzgmQk/t3Yniua50Fsak7FShI9lA==",
"peer": true
},
"core-util-is": {

View File

@ -20,6 +20,9 @@ export interface OrderParams {
consumer: string
amount: string
serviceIndex: number
providerFeeAddress: string
providerFeeToken: string
providerFeeAmount: string
}
export interface DispenserParams {
@ -816,6 +819,9 @@ export class Datatoken {
* @param {String} consumer Consumer Address
* @param {String} amount Amount of tokens that is going to be transfered
* @param {Number} serviceIndex Service index in the metadata
* @param {String} providerFeeAddress Consume marketplace fee address
* @param {String} providerFeeToken address of the token marketplace wants to add fee on top
* @param {String} providerFeeAmount amount of feeToken to be transferred to mpFeeAddress on top, will be converted to WEI
* @param {Contract} contractInstance optional contract instance
* @return {Promise<any>}
*/
@ -825,6 +831,9 @@ export class Datatoken {
consumer: string,
amount: string,
serviceIndex: number,
providerFeeAddress: string,
providerFeeToken: string,
providerFeeAmount: string,
contractInstance?: Contract
): Promise<any> {
const dtContract =
@ -835,7 +844,14 @@ export class Datatoken {
let estGas
try {
estGas = await dtContract.methods
.startOrder(consumer, this.web3.utils.toWei(amount), serviceIndex)
.startOrder(
consumer,
this.web3.utils.toWei(amount),
serviceIndex,
providerFeeAddress,
providerFeeToken,
this.web3.utils.toWei(providerFeeAmount)
)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
@ -849,6 +865,10 @@ export class Datatoken {
* @param {String} consumer Consumer Address
* @param {String} amount Amount of tokens that is going to be transfered
* @param {Number} serviceIndex Service index in the metadata
* @param {String} providerFeeAddress Consume marketplace fee address
* @param {String} providerFeeToken address of the token marketplace wants to add fee on top
* @param {String} providerFeeAmount amount of feeToken to be transferred to mpFeeAddress on top, will be converted to WEI
* @return {Promise<TransactionReceipt>} string
*/
public async startOrder(
@ -856,9 +876,15 @@ export class Datatoken {
address: string,
consumer: string,
amount: string,
serviceIndex: number
serviceIndex: number,
providerFeeAddress: string,
providerFeeToken: string,
providerFeeAmount: string
): Promise<TransactionReceipt> {
const dtContract = new this.web3.eth.Contract(this.datatokensABI, dtAddress)
if (!providerFeeAddress)
providerFeeAddress = '0x0000000000000000000000000000000000000000'
try {
const estGas = await this.estGasStartOrder(
dtAddress,
@ -866,11 +892,21 @@ export class Datatoken {
consumer,
amount,
serviceIndex,
providerFeeAddress,
providerFeeToken,
providerFeeAmount,
dtContract
)
const trxReceipt = await dtContract.methods
.startOrder(consumer, this.web3.utils.toWei(amount), serviceIndex)
.startOrder(
consumer,
this.web3.utils.toWei(amount),
serviceIndex,
providerFeeAddress,
providerFeeToken,
this.web3.utils.toWei(providerFeeAmount)
)
.send({
from: address,
gas: estGas + 1,

View File

@ -23,6 +23,9 @@ export interface TokenOrder {
consumer: string
amount: string | number
serviceIndex: number
providerFeeAddress: string
providerFeeToken: string
providerFeeAmount: string
}
export interface NFTCreateData {

View File

@ -253,7 +253,16 @@ describe('Datatoken', () => {
'User2 does not hold 0 datatokens'
)
const order = await datatoken.startOrder(datatokenAddress, user1, user2, '1', 1)
const order = await datatoken.startOrder(
datatokenAddress,
user1,
user2,
'1',
1,
user3,
'0x0000000000000000000000000000000000000000',
'0'
)
assert(order !== null)
assert(
@ -273,7 +282,10 @@ describe('Datatoken', () => {
const order: OrderParams = {
consumer: user1,
amount: '1',
serviceIndex: 1
serviceIndex: 1,
providerFeeAddress: user1,
providerFeeToken: '0x0000000000000000000000000000000000000000',
providerFeeAmount: '0'
}
const buyFromDispenseTx = await datatoken.buyFromDispenserAndOrder(
@ -289,14 +301,17 @@ describe('Datatoken', () => {
const order: OrderParams = {
consumer: user1,
amount: '1',
serviceIndex: 1
serviceIndex: 1,
providerFeeAddress: user1,
providerFeeToken: '0x0000000000000000000000000000000000000000',
providerFeeAmount: '0'
}
const fre: FreOrderParams = {
exchangeContract: fixedRateAddress,
exchangeId: exchangeId,
maxBaseTokenAmount: '1',
swapMarketFee: this.web3.utils.toWei('0.1'),
swapMarketFee: web3.utils.toWei('0.1'),
marketFeeAddress: '0x0000000000000000000000000000000000000000'
}

View File

@ -313,7 +313,7 @@ describe('NFT Factory test', () => {
const dtAmount = web3.utils.toWei('1')
const serviceIndex = 1 // dummy index
const consumeFeeAddress = user3 // marketplace fee Collector
const consumeFeeAmount = 0 // fee to be collected on top, requires approval
const consumeFeeAmount = '0' // fee to be collected on top, requires approval
const consumeFeeToken = contracts.daiAddress // token address for the feeAmount, in this case DAI
// we reuse a DT created in a previous test
@ -348,13 +348,19 @@ describe('NFT Factory test', () => {
tokenAddress: dtAddress,
consumer: consumer,
amount: dtAmount,
serviceIndex: serviceIndex
serviceIndex: serviceIndex,
providerFeeAddress: consumeFeeAddress,
providerFeeToken: consumeFeeToken,
providerFeeAmount: consumeFeeAmount
},
{
tokenAddress: dtAddress2,
consumer: consumer,
amount: dtAmount,
serviceIndex: serviceIndex
serviceIndex: serviceIndex,
providerFeeAddress: consumeFeeAddress,
providerFeeToken: consumeFeeToken,
providerFeeAmount: consumeFeeAmount
}
]

View File

@ -590,13 +590,22 @@ describe('SideStaking unit test', () => {
(await pool.amountToUnits(contracts.usdcAddress, '990')).toString()
)
await pool.approve(user2, contracts.usdcAddress, poolAddress, '100')
const tokenInOutMarket: TokenInOutMarket = {
tokenIn: contracts.usdcAddress,
tokenOut: erc20Token,
marketFeeAddress: '0x0000000000000000000000000000000000000000'
}
const amountsInOutMaxFee: AmountsOutMaxFee = {
maxAmountIn: '100',
tokenAmountOut: '50',
maxPrice: MaxUint256,
swapMarketFee: '0.1'
}
const tx = await pool.swapExactAmountOut(
user2,
poolAddress,
contracts.usdcAddress,
'100',
erc20Token,
'50'
tokenInOutMarket,
amountsInOutMaxFee
)
assert(tx != null)
// console.log(tx.events)