add lastPriceToken (#567)

* add lastPriceToken

* test

* Updating tests to include lastPriceToken

Co-authored-by: Jamie Hewitt <jamie.hewitt15@gmail.com>
This commit is contained in:
mihaisc 2022-11-21 16:31:13 +02:00 committed by GitHub
parent 8eb80507c9
commit 6c4fc1c346
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 40 additions and 37 deletions

View File

@ -1,7 +1,7 @@
version: '3' version: '3'
services: services:
graph-node: graph-node:
image: graphprotocol/graph-node:v0.26.0 image: graphprotocol/graph-node:v0.27.0
ports: ports:
- '9000:8000' - '9000:8000'
- '8001:8001' - '8001:8001'
@ -17,7 +17,7 @@ services:
postgres_pass: let-me-in postgres_pass: let-me-in
postgres_db: graph-node postgres_db: graph-node
ipfs: 'ipfs:5001' ipfs: 'ipfs:5001'
ethereum: 'rinkeby:https://rinkeby.infura.io/v3/${INFURA_PROJECT_ID}' ethereum: 'goerli:https://goerli.infura.io/v3/${INFURA_PROJECT_ID}'
RUST_LOG: info RUST_LOG: info
ipfs: ipfs:
image: ipfs/go-ipfs:v0.4.23 image: ipfs/go-ipfs:v0.4.23

View File

@ -26,10 +26,12 @@
"test": "npm run codegen && npm run lint && npm run type-check", "test": "npm run codegen && npm run lint && npm run type-check",
"test-integration": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/**/*.test.ts'", "test-integration": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/**/*.test.ts'",
"test-dispenser": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/Dispenser.test.ts'", "test-dispenser": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/Dispenser.test.ts'",
"test-simple": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/SimplePublishConsume.test.ts'",
"test-fixed": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/FixedRateExchange.test.ts'", "test-fixed": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/FixedRateExchange.test.ts'",
"test-users": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/users.test.ts'", "test-users": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/users.test.ts'",
"test-ve": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/VeOcean.test.ts'", "test-ve": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/VeOcean.test.ts'",
"test-df": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/DFRewards.test.ts'", "test-df": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/DFRewards.test.ts'",
"test-dt": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/Datatoken.test.ts'",
"test-zend": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/ZEnding.test.ts'", "test-zend": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/ZEnding.test.ts'",
"lint": "eslint --ignore-path .gitignore --ext .js --ext .ts --ext .tsx .", "lint": "eslint --ignore-path .gitignore --ext .js --ext .ts --ext .tsx .",
"lint:fix": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx . --fix", "lint:fix": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx . --fix",

View File

@ -56,7 +56,7 @@ type Token @entity {
"block number when it was created" "block number when it was created"
block: Int! block: Int!
lastPriceToken: String! lastPriceToken: Token
lastPriceValue: BigDecimal! lastPriceValue: BigDecimal!
} }
@ -167,7 +167,7 @@ type Order @entity {
tx: String! tx: String!
block: Int! block: Int!
lastPriceToken: String! lastPriceToken: Token
lastPriceValue: BigDecimal! lastPriceValue: BigDecimal!
estimatedUSDValue: BigDecimal! estimatedUSDValue: BigDecimal!
gasUsed: BigDecimal gasUsed: BigDecimal

View File

@ -1,5 +1,5 @@
import { Order, Nft, OrderReuse } from '../@types/schema' import { Order, Nft, OrderReuse } from '../@types/schema'
import { BigInt, BigDecimal } from '@graphprotocol/graph-ts' import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts'
import { import {
NewPaymentCollector, NewPaymentCollector,
@ -59,13 +59,18 @@ export function handleOrderStarted(event: OrderStarted): void {
order.createdTimestamp = event.block.timestamp.toI32() order.createdTimestamp = event.block.timestamp.toI32()
order.tx = event.transaction.hash.toHex() order.tx = event.transaction.hash.toHex()
order.block = event.block.number.toI32() order.block = event.block.number.toI32()
order.lastPriceToken = token.lastPriceToken const tokenId = token.lastPriceToken
order.lastPriceValue = token.lastPriceValue if (tokenId) {
order.estimatedUSDValue = getUSDValue( const priceToken = getToken(Address.fromString(tokenId), false)
order.lastPriceToken, order.lastPriceToken = priceToken.id
order.lastPriceValue, order.lastPriceValue = token.lastPriceValue
order.createdTimestamp order.estimatedUSDValue = getUSDValue(
) priceToken.id,
order.lastPriceValue,
order.createdTimestamp
)
}
if (event.receipt !== null && event.receipt!.gasUsed) { if (event.receipt !== null && event.receipt!.gasUsed) {
order.gasUsed = event.receipt!.gasUsed.toBigDecimal() order.gasUsed = event.receipt!.gasUsed.toBigDecimal()
} else { } else {

View File

@ -206,7 +206,11 @@ export function handleSwap(event: Swapped): void {
Address.fromString(fixedRateExchange.datatoken), Address.fromString(fixedRateExchange.datatoken),
true true
) )
datatoken.lastPriceToken = fixedRateExchange.baseToken const priceToken = getToken(
Address.fromString(fixedRateExchange.baseToken),
false
)
datatoken.lastPriceToken = priceToken.id
datatoken.lastPriceValue = fixedRateExchange.price datatoken.lastPriceValue = fixedRateExchange.price
datatoken.save() datatoken.save()
} }

View File

@ -162,7 +162,6 @@ describe('Datatoken tests', async () => {
createdTimestamp, createdTimestamp,
tx, tx,
block, block,
lastPriceToken,
lastPriceValue lastPriceValue
}}` }}`
} }
@ -214,10 +213,6 @@ describe('Datatoken tests', async () => {
assert(tx.blockNumber >= blockNumber, 'incorrect value for: tx') assert(tx.blockNumber >= blockNumber, 'incorrect value for: tx')
assert(dt.block >= blockNumber, 'incorrect value for: block') assert(dt.block >= blockNumber, 'incorrect value for: block')
assert(dt.block < blockNumber + 50, 'incorrect value for: block') assert(dt.block < blockNumber + 50, 'incorrect value for: block')
assert(
dt.lastPriceToken === '0x0000000000000000000000000000000000000000',
'incorrect value for: lastPriceToken'
)
assert(dt.lastPriceValue === '0', 'incorrect value for: lastPriceValue') assert(dt.lastPriceValue === '0', 'incorrect value for: lastPriceValue')
}) })
@ -275,7 +270,6 @@ describe('Datatoken tests', async () => {
createdTimestamp, createdTimestamp,
tx, tx,
block, block,
lastPriceToken,
lastPriceValue lastPriceValue
}}` }}`
} }
@ -326,10 +320,6 @@ describe('Datatoken tests', async () => {
assert(tx.blockNumber >= blockNumber, 'incorrect value for: tx') assert(tx.blockNumber >= blockNumber, 'incorrect value for: tx')
assert(dt.block >= blockNumber, 'incorrect value for: block') assert(dt.block >= blockNumber, 'incorrect value for: block')
assert(dt.block < blockNumber + 50, 'incorrect value for: block') assert(dt.block < blockNumber + 50, 'incorrect value for: block')
assert(
dt.lastPriceToken === '0x0000000000000000000000000000000000000000',
'incorrect value for: lastPriceToken'
)
assert(dt.lastPriceValue === '0', 'incorrect value for: lastPriceValue') assert(dt.lastPriceValue === '0', 'incorrect value for: lastPriceValue')
}) })
@ -368,7 +358,7 @@ describe('Datatoken tests', async () => {
assert(Number(user2balance) === 0, 'Invalid user2 balance') assert(Number(user2balance) === 0, 'Invalid user2 balance')
const query = { const query = {
query: `query {token(id: "${newDtAddress.toLowerCase()}"){id,orderCount,orders {id}}}` query: `query {token(id: "${newDtAddress.toLowerCase()}"){id,orderCount,orders {id, lastPriceToken{id}}}}`
} }
await sleep(2000) await sleep(2000)
@ -428,5 +418,6 @@ describe('Datatoken tests', async () => {
assert(token, 'Invalid token') assert(token, 'Invalid token')
assert(token.orderCount === '1', 'Invalid orderCount after order') assert(token.orderCount === '1', 'Invalid orderCount after order')
assert(token.orders[0].id === orderId) assert(token.orders[0].id === orderId)
assert(token.orders[0].lastPriceToken.id === ZERO_ADDRESS)
}) })
}) })

View File

@ -202,7 +202,6 @@ describe('Dispenser tests', async () => {
createdTimestamp, createdTimestamp,
tx, tx,
block, block,
lastPriceToken,
lastPriceValue lastPriceValue
}}` }}`
} }
@ -254,10 +253,6 @@ describe('Dispenser tests', async () => {
assert(dtTx.blockNumber >= blockNumber, 'incorrect value for: tx') assert(dtTx.blockNumber >= blockNumber, 'incorrect value for: tx')
assert(dt.block >= blockNumber, 'incorrect value for: block') assert(dt.block >= blockNumber, 'incorrect value for: block')
assert(dt.block < blockNumber + 50, 'incorrect value for: block') assert(dt.block < blockNumber + 50, 'incorrect value for: block')
assert(
dt.lastPriceToken === '0x0000000000000000000000000000000000000000',
'incorrect value for: lastPriceToken'
)
assert(dt.lastPriceValue === '0', 'incorrect value for: lastPriceValue') assert(dt.lastPriceValue === '0', 'incorrect value for: lastPriceValue')
}) })

View File

@ -273,10 +273,6 @@ describe('Fixed Rate Exchange tests', async () => {
assert(dtTx.blockNumber >= blockNumber, 'incorrect value for: tx') assert(dtTx.blockNumber >= blockNumber, 'incorrect value for: tx')
assert(dt.block >= blockNumber, 'incorrect value for: block') assert(dt.block >= blockNumber, 'incorrect value for: block')
assert(dt.block < blockNumber + 50, 'incorrect value for: block') assert(dt.block < blockNumber + 50, 'incorrect value for: block')
assert(
dt.lastPriceToken === '0x0000000000000000000000000000000000000000',
'incorrect value for: lastPriceToken'
)
assert(dt.lastPriceValue === '0', 'incorrect value for: lastPriceValue') assert(dt.lastPriceValue === '0', 'incorrect value for: lastPriceValue')
}) })

View File

@ -282,7 +282,9 @@ describe('Simple Publish & consume test', async () => {
) )
const orderId = `${orderTx.transactionHash.toLowerCase()}-${datatokenAddress.toLowerCase()}-${user1.toLowerCase()}` const orderId = `${orderTx.transactionHash.toLowerCase()}-${datatokenAddress.toLowerCase()}-${user1.toLowerCase()}`
const query = { query: `query {order(id:"${orderId}"){id, providerFee}}` } const query = {
query: `query {order(id:"${orderId}"){id, providerFee, lastPriceToken{id}}}`
}
await sleep(2000) await sleep(2000)
const response = await fetch(subgraphUrl, { const response = await fetch(subgraphUrl, {
@ -293,6 +295,9 @@ describe('Simple Publish & consume test', async () => {
const queryResult = await response.json() const queryResult = await response.json()
const providerFeeJSON = JSON.parse(queryResult.data.order.providerFee) const providerFeeJSON = JSON.parse(queryResult.data.order.providerFee)
const lastPriceToken = queryResult.data.order.lastPriceToken.id
assert(lastPriceToken === ZERO_ADDRESS, 'Wrong lastPriceToken')
assert( assert(
providerFeeJSON.providerFeeAddress.toLowerCase() === providerFeeJSON.providerFeeAddress.toLowerCase() ===
@ -352,7 +357,7 @@ describe('Simple Publish & consume test', async () => {
const orderId = `${orderTx.transactionHash.toLowerCase()}-${datatokenAddress.toLowerCase()}-${user4.toLowerCase()}` const orderId = `${orderTx.transactionHash.toLowerCase()}-${datatokenAddress.toLowerCase()}-${user4.toLowerCase()}`
const initialQuery = { const initialQuery = {
query: `query {order(id:"${orderId}"){id, providerFee}}` query: `query {order(id:"${orderId}"){id, providerFee, lastPriceToken{id}}}`
} }
await sleep(2000) await sleep(2000)
const initialResponse = await fetch(subgraphUrl, { const initialResponse = await fetch(subgraphUrl, {
@ -363,7 +368,9 @@ describe('Simple Publish & consume test', async () => {
const initialProviderFeeJSON = JSON.parse( const initialProviderFeeJSON = JSON.parse(
initialQueryResult.data.order.providerFee initialQueryResult.data.order.providerFee
) )
const lastPriceToken = initialQueryResult.data.order.lastPriceToken.id
assert(lastPriceToken === ZERO_ADDRESS, 'Wrong initial lastPriceToken set')
assert( assert(
initialProviderFeeJSON.providerFeeAddress.toLowerCase() === initialProviderFeeJSON.providerFeeAddress.toLowerCase() ===
setInitialProviderFee.providerFeeAddress.toLowerCase(), setInitialProviderFee.providerFeeAddress.toLowerCase(),

View File

@ -33,7 +33,7 @@ async function userQuery(user: string) {
user(id:"${user}"){ user(id:"${user}"){
id id
tokenBalancesOwned {id} tokenBalancesOwned {id}
orders {id} orders {id, lastPriceToken{id}}
freSwaps {id} freSwaps {id}
totalOrders totalOrders
totalSales totalSales
@ -284,7 +284,6 @@ describe('User tests', async () => {
await sleep(2000) await sleep(2000)
const user = await userQuery(user3) const user = await userQuery(user3)
assert(user.id === user3, 'incorrect value for: id') assert(user.id === user3, 'incorrect value for: id')
assert(user.tokenBalancesOwned.length === 0, 'incorrect tokenBalancesOwned') assert(user.tokenBalancesOwned.length === 0, 'incorrect tokenBalancesOwned')
assert(user.orders.length === 1, 'incorrect value for: orders') assert(user.orders.length === 1, 'incorrect value for: orders')
@ -292,5 +291,9 @@ describe('User tests', async () => {
assert(user.totalOrders === '1', 'incorrect value for: totalOrders') assert(user.totalOrders === '1', 'incorrect value for: totalOrders')
assert(user.totalSales === '0', 'incorrect value for: totalSales') assert(user.totalSales === '0', 'incorrect value for: totalSales')
assert(user.__typename === 'User', 'incorrect value for: __typename') assert(user.__typename === 'User', 'incorrect value for: __typename')
assert(
user.orders[0].lastPriceToken.id === ZERO_ADDRESS,
'incorrect value for: lastPriceToken'
)
}) })
}) })