Issue 471 provider fees (#474)

* Adding providerFee to Order in the schema

* Adding ProviderFee to subgraph.template.yaml

* Creating order utils

* Saving providerFee as a string

* Saving providerFee as an array of all previous provider fees

* Creating test for providerFee

* Removing arrary from providerFee

* Updating or Creating OrderResue provider fee

* lint:fix

* Creating additional tests for testing providerFees on Order and reuseOrder

* lint:fix

* Updating JSON string notation

* Fixing first test

* fixing second test: testing provider fees after calling reuseOrder on a using a previous txId

* added providerFeeValidUntil

Co-authored-by: mihaisc <mihai@oceanprotocol.com>
This commit is contained in:
Jamie Hewitt 2022-07-12 09:11:43 +01:00 committed by GitHub
parent db8cf22d16
commit 3ebc29a74d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 328 additions and 95 deletions

7
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
"recommendations": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"wix.vscode-import-cost"
]
}

17
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],
"search.exclude": {
"**/.next": true,
"**/out": true
}
}

View File

@ -249,6 +249,8 @@ type OrderReuse @entity {
createdTimestamp: BigInt! createdTimestamp: BigInt!
tx: String! tx: String!
block: BigInt! block: BigInt!
providerFee: String
providerFeeValidUntil: BigInt
} }
type Order @entity { type Order @entity {
"transaction hash - token address - from address" "transaction hash - token address - from address"
@ -265,6 +267,8 @@ type Order @entity {
publishingMarket: User publishingMarket: User
publishingMarketToken: Token # publishingMarketToken: Token #
publishingMarketAmmount: BigDecimal #call contract to get fee amount publishingMarketAmmount: BigDecimal #call contract to get fee amount
providerFee: String
providerFeeValidUntil: BigInt
consumerMarket: User consumerMarket: User
consumerMarketToken: Token # consumerMarketToken: Token #

View File

@ -10,7 +10,8 @@ import {
RemovedMinter, RemovedMinter,
RemovedPaymentManager, RemovedPaymentManager,
CleanedPermissions, CleanedPermissions,
OrderReused OrderReused,
ProviderFee
} from '../@types/templates/ERC20Template/ERC20Template' } from '../@types/templates/ERC20Template/ERC20Template'
import { integer } from './utils/constants' import { integer } from './utils/constants'
@ -18,14 +19,7 @@ import { weiToDecimal } from './utils/generic'
import { addOrder } from './utils/globalUtils' import { addOrder } from './utils/globalUtils'
import { getToken, getUSDValue } from './utils/tokenUtils' import { getToken, getUSDValue } from './utils/tokenUtils'
import { getUser } from './utils/userUtils' import { getUser } from './utils/userUtils'
import { getOrderId } from './utils/orderUtils'
function getOrderId(
tx: string,
tokenAddress: string,
fromAddress: string
): string {
return `${tx}-${tokenAddress}-${fromAddress}`
}
export function handleOrderStarted(event: OrderStarted): void { export function handleOrderStarted(event: OrderStarted): void {
const order = new Order( const order = new Order(
@ -204,26 +198,38 @@ export function handleNewPaymentCollector(event: NewPaymentCollector): void {
token.save() token.save()
} }
// export function handlePublishMarketFees(event: PublishMarketFees): void { export function handleProviderFee(event: ProviderFee): void {
// const order = Order.load( const providerFee: string = `{"providerFeeAddress": "${event.params.providerFeeAddress.toHex()}", "providerFeeToken": "${event.params.providerFeeToken.toHex()}", "providerFeeAmount": "${
// getOrderId( event.params.providerFeeAmount
// event.transaction.hash.toHex(), }", "providerData": "${event.params.providerData.toHexString()}", "v": "${
// event.address.toHex(), event.params.v
// event.transaction.from.toHex() }", "r": "${event.params.r.toHexString()}", "s": "${event.params.s.toHexString()}", "validUntil": "${
// ) event.params.validUntil
// ) }"}`
// order.save() const orderId = getOrderId(
// } event.transaction.hash.toHex(),
event.address.toHex(),
event.transaction.from.toHex()
)
const order = Order.load(orderId)
// export function handleConsumeMarketFees(event: ConsumeMarketFees): void { if (order) {
// const order = Order.load( order.providerFee = providerFee
// getOrderId( order.providerFeeValidUntil = event.params.validUntil
// event.transaction.hash.toHex(), order.save()
// event.address.toHex(), return
// event.transaction.from.toHex() }
// )
// )
// order.save() let orderReuse = OrderReuse.load(event.transaction.hash.toHex())
// } if (orderReuse) {
orderReuse.providerFee = providerFee
orderReuse.providerFeeValidUntil = event.params.validUntil
orderReuse.save()
} else {
orderReuse = new OrderReuse(event.transaction.hash.toHex())
orderReuse.providerFee = providerFee
orderReuse.providerFeeValidUntil = event.params.validUntil
orderReuse.save()
}
}

View File

@ -0,0 +1,27 @@
import { Order } from '../../@types/schema'
export function getOrderId(
tx: string,
tokenAddress: string,
fromAddress: string
): string {
return `${tx}-${tokenAddress}-${fromAddress}`
}
export function createOrder(orderId: string): Order {
const order = new Order(orderId)
return order
}
export function getOrder(
transactionHash: string,
address: string,
transactionFrom: string
): Order {
const orderId = getOrderId(transactionHash, address, transactionFrom)
let newOrder = Order.load(orderId)
if (newOrder === null) {
newOrder = createOrder(orderId)
}
return newOrder
}

View File

@ -113,6 +113,8 @@ templates:
handler: handleRemovedPaymentManager handler: handleRemovedPaymentManager
- event: CleanedPermissions(indexed address,uint256,uint256) - event: CleanedPermissions(indexed address,uint256,uint256)
handler: handleCleanedPermissions handler: handleCleanedPermissions
- event: ProviderFee(indexed address,indexed address,uint256,bytes,uint8,bytes32,bytes32,uint256)
handler: handleProviderFee
- name: BPool - name: BPool
kind: ethereum/contract kind: ethereum/contract

View File

@ -1,14 +1,15 @@
import { import {
// Aquarius, Datatoken,
// Datatoken,
Erc20CreateParams, Erc20CreateParams,
ProviderInstance, ProviderInstance,
// ProviderFees, ProviderFees,
Nft, Nft,
NftFactory, NftFactory,
NftCreateData, NftCreateData,
getHash, getHash,
sleep sleep,
ZERO_ADDRESS,
signHash
} from '@oceanprotocol/lib' } from '@oceanprotocol/lib'
import { assert } from 'chai' import { assert } from 'chai'
import Web3 from 'web3' import Web3 from 'web3'
@ -71,12 +72,31 @@ const ddo = {
} }
describe('Simple Publish & consume test', async () => { describe('Simple Publish & consume test', async () => {
let datatokenAddress: string
let datatoken: Datatoken
let nft: Nft
let Factory: NftFactory
let accounts: string[]
let publisherAccount: string
let newOwnerAccount: string
let user1: string
let user2: string
let user3: string
let user4: string
before(async () => {
nft = new Nft(web3)
Factory = new NftFactory(addresses.ERC721Factory, web3)
accounts = await web3.eth.getAccounts()
publisherAccount = accounts[0]
newOwnerAccount = accounts[1].toLowerCase()
user1 = accounts[2]
user2 = accounts[3]
user3 = accounts[4]
user4 = accounts[4]
})
it('should publish a dataset (create NFT + ERC20)', async () => { it('should publish a dataset (create NFT + ERC20)', async () => {
const nft = new Nft(web3)
// const datatoken = new Datatoken(web3)
const Factory = new NftFactory(addresses.ERC721Factory, web3)
const accounts = await web3.eth.getAccounts()
const publisherAccount = accounts[0]
// const consumerAccount = accounts[1] // const consumerAccount = accounts[1]
const nftParams: NftCreateData = { const nftParams: NftCreateData = {
name: 'testNFT', name: 'testNFT',
@ -101,7 +121,7 @@ describe('Simple Publish & consume test', async () => {
erc20Params erc20Params
) )
const erc721Address = result.events.NFTCreated.returnValues[0] const erc721Address = result.events.NFTCreated.returnValues[0]
const datatokenAddress = result.events.TokenCreated.returnValues[0] datatokenAddress = result.events.TokenCreated.returnValues[0]
// create the files encrypted string // create the files encrypted string
let providerResponse = await ProviderInstance.encrypt(assetUrl, providerUrl) let providerResponse = await ProviderInstance.encrypt(assetUrl, providerUrl)
@ -127,8 +147,6 @@ describe('Simple Publish & consume test', async () => {
encryptedResponse, encryptedResponse,
'0x' + metadataHash '0x' + metadataHash
) )
// const resolvedDDO = await aquarius.waitForAqua(ddo.id)
// assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
// graph tests here // graph tests here
await sleep(2000) await sleep(2000)
@ -143,62 +161,8 @@ describe('Simple Publish & consume test', async () => {
}) })
const queryResult = await response.json() const queryResult = await response.json()
assert(queryResult.data.nft.id === graphNftToken) assert(queryResult.data.nft.id === graphNftToken)
/*
// mint 1 ERC20 and send it to the consumer
await datatoken.mint(datatokenAddress, publisherAccount, '1', consumerAccount)
// initialize provider
const initializeData = await ProviderInstance.initialize(
resolvedDDO.id,
resolvedDDO.services[0].id,
0,
consumerAccount,
providerUrl
)
const providerFees: ProviderFees = {
providerFeeAddress: initializeData.providerFee.providerFeeAddress,
providerFeeToken: initializeData.providerFee.providerFeeToken,
providerFeeAmount: initializeData.providerFee.providerFeeAmount,
v: initializeData.providerFee.v,
r: initializeData.providerFee.r,
s: initializeData.providerFee.s,
providerData: initializeData.providerFee.providerData,
validUntil: initializeData.providerFee.validUntil
}
// make the payment
const txid = await datatoken.startOrder(
datatokenAddress,
consumerAccount,
consumerAccount,
0,
providerFees
)
// get the url
const downloadURL = await ProviderInstance.getDownloadUrl(
ddo.id,
consumerAccount,
ddo.services[0].id,
0,
txid.transactionHash,
providerUrl,
web3
)
assert(downloadURL, 'Provider getDownloadUrl failed')
try {
const fileData = await downloadFile(downloadURL)
} catch (e) {
assert.fail('Download failed')
}
*/
}) })
it('should publish and transfer an NFT', async () => { it('should publish and transfer an NFT', async () => {
const nft = new Nft(web3)
const Factory = new NftFactory(addresses.ERC721Factory, web3)
const accounts = await web3.eth.getAccounts()
const publisherAccount = accounts[0]
const newOwnerAccount = accounts[1].toLowerCase()
const nftParams: NftCreateData = { const nftParams: NftCreateData = {
name: 'testNFT', name: 'testNFT',
symbol: 'TST', symbol: 'TST',
@ -281,4 +245,210 @@ describe('Simple Publish & consume test', async () => {
const queryResult = await response.json() const queryResult = await response.json()
assert(queryResult.data.nft.owner === newOwnerAccount) assert(queryResult.data.nft.owner === newOwnerAccount)
}) })
it('should save provider fees after startOrder is called', async () => {
datatoken = new Datatoken(web3, 8996)
await datatoken.mint(datatokenAddress, publisherAccount, '100', user1)
const user1Balance = await datatoken.balance(datatokenAddress, user1)
assert(user1Balance === '100', 'user1 has no datatokens')
const providerData = JSON.stringify({ timeout: 0 })
const providerFeeToken = ZERO_ADDRESS
const providerFeeAmount = '10000'
const providerValidUntil = '0'
const message = web3.utils.soliditySha3(
{ t: 'bytes', v: web3.utils.toHex(web3.utils.asciiToHex(providerData)) },
{ t: 'address', v: user3 },
{ t: 'address', v: providerFeeToken },
{ t: 'uint256', v: providerFeeAmount },
{ t: 'uint256', v: providerValidUntil }
)
const { v, r, s } = await signHash(web3, message, user3)
const setProviderFee: ProviderFees = {
providerFeeAddress: user3,
providerFeeToken,
providerFeeAmount,
v,
r,
s,
providerData: web3.utils.toHex(web3.utils.asciiToHex(providerData)),
validUntil: providerValidUntil
}
const orderTx = await datatoken.startOrder(
datatokenAddress,
user1,
user2,
1,
setProviderFee
)
const orderId = `${orderTx.transactionHash.toLocaleLowerCase()}-${datatokenAddress.toLocaleLowerCase()}-${user1.toLocaleLowerCase()}`
const query = { query: `query {order(id:"${orderId}"){id, providerFee}}` }
await sleep(2000)
const response = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(query)
})
const queryResult = await response.json()
const providerFeeJSON = JSON.parse(queryResult.data.order.providerFee)
assert(
providerFeeJSON.providerFeeAddress.toLowerCase() ===
setProviderFee.providerFeeAddress.toLowerCase(),
'Wrong providerFeeAddress set'
)
assert(
providerFeeJSON.providerFeeAmount.toLowerCase() ===
setProviderFee.providerFeeAmount.toLowerCase(),
'Wrong providerFeeAmount set'
)
assert(
providerFeeJSON.providerFeeToken.toLowerCase() ===
setProviderFee.providerFeeToken.toLowerCase(),
'Wrong providerFeeToken set'
)
})
it('should save provider fees after calling reuseOrder on a using a previous txId', async () => {
await datatoken.mint(datatokenAddress, publisherAccount, '100', user4)
const user4Balance = await datatoken.balance(datatokenAddress, user4)
assert(user4Balance === '100', 'publisherAccount has no datatokens')
const providerData = JSON.stringify({ timeout: 0 })
const providerFeeToken = ZERO_ADDRESS
let providerFeeAmount = '90'
let providerValidUntil = '0'
let message = web3.utils.soliditySha3(
{ t: 'bytes', v: web3.utils.toHex(web3.utils.asciiToHex(providerData)) },
{ t: 'address', v: user3 },
{ t: 'address', v: providerFeeToken },
{ t: 'uint256', v: providerFeeAmount },
{ t: 'uint256', v: providerValidUntil }
)
let { v, r, s } = await signHash(web3, message, user3)
const setInitialProviderFee: ProviderFees = {
providerFeeAddress: user3,
providerFeeToken,
providerFeeAmount,
v,
r,
s,
providerData: web3.utils.toHex(web3.utils.asciiToHex(providerData)),
validUntil: providerValidUntil
}
const orderTx = await datatoken.startOrder(
datatokenAddress,
user4,
user2,
1,
setInitialProviderFee
)
assert(orderTx.transactionHash, 'Failed to start order')
// Check initial provider fee has been set correctly
const orderId = `${orderTx.transactionHash.toLowerCase()}-${datatokenAddress.toLowerCase()}-${user4.toLowerCase()}`
const initialQuery = {
query: `query {order(id:"${orderId}"){id, providerFee}}`
}
await sleep(2000)
const initialResponse = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(initialQuery)
})
const initialQueryResult = await initialResponse.json()
const initialProviderFeeJSON = JSON.parse(
initialQueryResult.data.order.providerFee
)
assert(
initialProviderFeeJSON.providerFeeAddress.toLowerCase() ===
setInitialProviderFee.providerFeeAddress.toLowerCase(),
'Wrong initial providerFeeAddress set'
)
assert(
initialProviderFeeJSON.providerFeeAmount.toLowerCase() ===
setInitialProviderFee.providerFeeAmount.toLowerCase(),
'Wrong initial providerFeeAmount set'
)
assert(
initialProviderFeeJSON.providerFeeToken.toLowerCase() ===
setInitialProviderFee.providerFeeToken.toLowerCase(),
'Wrong initial providerFeeToken set'
)
providerFeeAmount = '990000'
providerValidUntil = '10000'
message = web3.utils.soliditySha3(
{ t: 'bytes', v: web3.utils.toHex(web3.utils.asciiToHex(providerData)) },
{ t: 'address', v: user3 },
{ t: 'address', v: providerFeeToken },
{ t: 'uint256', v: providerFeeAmount },
{ t: 'uint256', v: providerValidUntil }
)
const msgResult = await signHash(web3, message, user3)
v = msgResult.v
r = msgResult.r
s = msgResult.s
const setNewProviderFee: ProviderFees = {
providerFeeAddress: user3,
providerFeeToken,
providerFeeAmount,
v,
r,
s,
providerData: web3.utils.toHex(web3.utils.asciiToHex(providerData)),
validUntil: providerValidUntil
}
const reusedOrder = await datatoken.reuseOrder(
datatokenAddress,
user2,
orderTx.transactionHash,
setNewProviderFee
)
assert(reusedOrder.events.OrderReused.event === 'OrderReused')
assert(reusedOrder.events.ProviderFee.event === 'ProviderFee')
sleep(4000)
// Check the new provider fee has been set in OrderReuse
const reuseQuery = {
query: `query {orderReuse(id:"${reusedOrder.transactionHash}"){id, providerFee}}`
}
await sleep(2000)
const response = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(reuseQuery)
})
const reuseQueryResult = await response.json()
const reuseProviderFeeJSON = JSON.parse(
reuseQueryResult.data.orderReuse.providerFee
)
assert(
reuseProviderFeeJSON.providerFeeAddress.toLowerCase() ===
setNewProviderFee.providerFeeAddress.toLowerCase(),
'New providerFeeAddress set in reuse order is wrong'
)
assert(
reuseProviderFeeJSON.providerFeeAmount.toLowerCase() ===
setNewProviderFee.providerFeeAmount.toLowerCase(),
'New providerFeeAmount set in reuse order is wrong'
)
assert(
reuseProviderFeeJSON.providerFeeToken.toLowerCase() ===
setNewProviderFee.providerFeeToken.toLowerCase(),
'New providerFeeToken set in reuse order is wrong'
)
})
}) })