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

View File

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

View File

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

View File

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

View File

@ -313,7 +313,7 @@ describe('NFT Factory test', () => {
const dtAmount = web3.utils.toWei('1') const dtAmount = web3.utils.toWei('1')
const serviceIndex = 1 // dummy index const serviceIndex = 1 // dummy index
const consumeFeeAddress = user3 // marketplace fee Collector 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 const consumeFeeToken = contracts.daiAddress // token address for the feeAmount, in this case DAI
// we reuse a DT created in a previous test // we reuse a DT created in a previous test
@ -348,13 +348,19 @@ describe('NFT Factory test', () => {
tokenAddress: dtAddress, tokenAddress: dtAddress,
consumer: consumer, consumer: consumer,
amount: dtAmount, amount: dtAmount,
serviceIndex: serviceIndex serviceIndex: serviceIndex,
providerFeeAddress: consumeFeeAddress,
providerFeeToken: consumeFeeToken,
providerFeeAmount: consumeFeeAmount
}, },
{ {
tokenAddress: dtAddress2, tokenAddress: dtAddress2,
consumer: consumer, consumer: consumer,
amount: dtAmount, 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.amountToUnits(contracts.usdcAddress, '990')).toString()
) )
await pool.approve(user2, contracts.usdcAddress, poolAddress, '100') 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( const tx = await pool.swapExactAmountOut(
user2, user2,
poolAddress, poolAddress,
contracts.usdcAddress, tokenInOutMarket,
'100', amountsInOutMaxFee
erc20Token,
'50'
) )
assert(tx != null) assert(tx != null)
// console.log(tx.events) // console.log(tx.events)