adding a test and logs for lastPriceToken

This commit is contained in:
Jamie Hewitt 2022-07-05 17:44:02 +01:00
parent f848d419b0
commit 0e19edb1e3
5 changed files with 74 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import { Order, Nft, OrderReuse } from '../@types/schema' import { Order, Nft, OrderReuse } from '../@types/schema'
import { BigInt } from '@graphprotocol/graph-ts' import { BigInt, log } from '@graphprotocol/graph-ts'
import { import {
NewPaymentCollector, NewPaymentCollector,
OrderStarted, OrderStarted,
@ -58,7 +58,10 @@ 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()
log.info('\n\n1. order.lastPriceToken: {}\n\n', [order.lastPriceToken])
log.info('\n\n2. token.lastPriceToken: {}\n\n', [token.lastPriceToken])
order.lastPriceToken = token.lastPriceToken order.lastPriceToken = token.lastPriceToken
log.info('\n\n3. order.lastPriceToken: {}\n\n', [order.lastPriceToken])
order.lastPriceValue = token.lastPriceValue order.lastPriceValue = token.lastPriceValue
order.estimatedUSDValue = getUSDValue( order.estimatedUSDValue = getUSDValue(
order.lastPriceToken, order.lastPriceToken,
@ -78,6 +81,7 @@ export function handleOrderStarted(event: OrderStarted): void {
owner.totalSales = owner.totalSales.plus(integer.ONE) owner.totalSales = owner.totalSales.plus(integer.ONE)
owner.save() owner.save()
} }
log.info('\n\n4. order.lastPriceToken: {}\n\n', [order.lastPriceToken])
} }
export function handlerOrderReused(event: OrderReused): void { export function handlerOrderReused(event: OrderReused): void {

View File

@ -1,4 +1,4 @@
import { BigInt, Address } from '@graphprotocol/graph-ts' import { BigInt, Address, log } from '@graphprotocol/graph-ts'
import { import {
ExchangeActivated, ExchangeActivated,
ExchangeAllowedSwapperChanged, ExchangeAllowedSwapperChanged,
@ -205,9 +205,16 @@ export function handleSwap(event: Swapped): void {
Address.fromString(fixedRateExchange.datatoken), Address.fromString(fixedRateExchange.datatoken),
true true
) )
log.info('\n\n5. datatoken.lastPriceToken: {}\n\n', [
datatoken.lastPriceToken
])
datatoken.lastPriceToken = fixedRateExchange.baseToken datatoken.lastPriceToken = fixedRateExchange.baseToken
datatoken.lastPriceValue = fixedRateExchange.price datatoken.lastPriceValue = fixedRateExchange.price
datatoken.save() datatoken.save()
log.info('\n\n5. datatoken.lastPriceToken: {}\n\n', [
datatoken.lastPriceToken
])
} }
export function handlePublishMarketFeeChanged( export function handlePublishMarketFeeChanged(

View File

@ -1,4 +1,4 @@
import { BigInt, Address } from '@graphprotocol/graph-ts' import { BigInt, Address, log } from '@graphprotocol/graph-ts'
import { import {
LOG_EXIT, LOG_EXIT,
LOG_JOIN, LOG_JOIN,
@ -188,9 +188,15 @@ export function handleSwap(event: LOG_SWAP): void {
// update datatoken lastPriceToken and lastPriceValue // update datatoken lastPriceToken and lastPriceValue
const datatoken = getToken(Address.fromString(pool.datatoken), true) const datatoken = getToken(Address.fromString(pool.datatoken), true)
log.info('\n\n7. datatoken.lastPriceToken: {}\n\n', [
datatoken.lastPriceToken
])
datatoken.lastPriceToken = pool.baseToken datatoken.lastPriceToken = pool.baseToken
datatoken.lastPriceValue = spotPrice datatoken.lastPriceValue = spotPrice
datatoken.save() datatoken.save()
log.info('\n\n8. datatoken.lastPriceToken: {}\n\n', [
datatoken.lastPriceToken
])
} }
// setup is just to set token weight(it will mostly be 50:50) and spotPrice // setup is just to set token weight(it will mostly be 50:50) and spotPrice

View File

@ -23,9 +23,11 @@ export function createToken(address: Address, isDatatoken: boolean): Token {
const decimals = contract.try_decimals() const decimals = contract.try_decimals()
if (decimals.reverted) token.decimals = 18 if (decimals.reverted) token.decimals = 18
else token.decimals = decimals.value else token.decimals = decimals.value
log.info('\n\n9. token.lastPriceToken: {}\n\n', [token.lastPriceToken])
token.lastPriceToken = ZERO_ADDRESS token.lastPriceToken = ZERO_ADDRESS
token.lastPriceValue = BigDecimal.zero() token.lastPriceValue = BigDecimal.zero()
token.save() token.save()
log.info('\n\n10. token.lastPriceToken: {}\n\n', [token.lastPriceToken])
return token return token
} }

View File

@ -451,4 +451,56 @@ describe('Simple Publish & consume test', async () => {
'New providerFeeToken set in reuse order is wrong' 'New providerFeeToken set in reuse order is wrong'
) )
}) })
it('lastPriceToken is stored correctly', async () => {
const providerData = JSON.stringify({ timeout: 0 })
const providerFeeToken = ZERO_ADDRESS
const providerFeeAmount = '90'
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 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, lastPriceToken {id}}}`
}
await sleep(2000)
const initialResponse = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(initialQuery)
})
const initialQueryResult = await initialResponse.json()
console.log('initialQueryResult:', initialQueryResult)
const lastPriceToken = initialQueryResult.data.order.lastPriceToken
console.log('lastPriceToken:', lastPriceToken)
assert(lastPriceToken === '0x0000000000000000000000000000000000000000')
})
}) })