From 0e19edb1e33ec3261150f55021912d4efc27396c Mon Sep 17 00:00:00 2001 From: Jamie Hewitt Date: Tue, 5 Jul 2022 17:44:02 +0100 Subject: [PATCH] adding a test and logs for lastPriceToken --- src/mappings/erc20Templates.ts | 6 ++- src/mappings/fixedRateExchange.ts | 9 +++- src/mappings/pool.ts | 8 ++- src/mappings/utils/tokenUtils.ts | 2 + test/integration/SimplePublishConsume.test.ts | 52 +++++++++++++++++++ 5 files changed, 74 insertions(+), 3 deletions(-) diff --git a/src/mappings/erc20Templates.ts b/src/mappings/erc20Templates.ts index 0aa7c5b..47e7979 100644 --- a/src/mappings/erc20Templates.ts +++ b/src/mappings/erc20Templates.ts @@ -1,5 +1,5 @@ import { Order, Nft, OrderReuse } from '../@types/schema' -import { BigInt } from '@graphprotocol/graph-ts' +import { BigInt, log } from '@graphprotocol/graph-ts' import { NewPaymentCollector, OrderStarted, @@ -58,7 +58,10 @@ export function handleOrderStarted(event: OrderStarted): void { order.createdTimestamp = event.block.timestamp.toI32() order.tx = event.transaction.hash.toHex() 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 + log.info('\n\n3. order.lastPriceToken: {}\n\n', [order.lastPriceToken]) order.lastPriceValue = token.lastPriceValue order.estimatedUSDValue = getUSDValue( order.lastPriceToken, @@ -78,6 +81,7 @@ export function handleOrderStarted(event: OrderStarted): void { owner.totalSales = owner.totalSales.plus(integer.ONE) owner.save() } + log.info('\n\n4. order.lastPriceToken: {}\n\n', [order.lastPriceToken]) } export function handlerOrderReused(event: OrderReused): void { diff --git a/src/mappings/fixedRateExchange.ts b/src/mappings/fixedRateExchange.ts index 0dfa05c..35309b9 100644 --- a/src/mappings/fixedRateExchange.ts +++ b/src/mappings/fixedRateExchange.ts @@ -1,4 +1,4 @@ -import { BigInt, Address } from '@graphprotocol/graph-ts' +import { BigInt, Address, log } from '@graphprotocol/graph-ts' import { ExchangeActivated, ExchangeAllowedSwapperChanged, @@ -205,9 +205,16 @@ export function handleSwap(event: Swapped): void { Address.fromString(fixedRateExchange.datatoken), true ) + log.info('\n\n5. datatoken.lastPriceToken: {}\n\n', [ + datatoken.lastPriceToken + ]) datatoken.lastPriceToken = fixedRateExchange.baseToken datatoken.lastPriceValue = fixedRateExchange.price datatoken.save() + + log.info('\n\n5. datatoken.lastPriceToken: {}\n\n', [ + datatoken.lastPriceToken + ]) } export function handlePublishMarketFeeChanged( diff --git a/src/mappings/pool.ts b/src/mappings/pool.ts index 1fb9171..4d160a7 100644 --- a/src/mappings/pool.ts +++ b/src/mappings/pool.ts @@ -1,4 +1,4 @@ -import { BigInt, Address } from '@graphprotocol/graph-ts' +import { BigInt, Address, log } from '@graphprotocol/graph-ts' import { LOG_EXIT, LOG_JOIN, @@ -188,9 +188,15 @@ export function handleSwap(event: LOG_SWAP): void { // update datatoken lastPriceToken and lastPriceValue const datatoken = getToken(Address.fromString(pool.datatoken), true) + log.info('\n\n7. datatoken.lastPriceToken: {}\n\n', [ + datatoken.lastPriceToken + ]) datatoken.lastPriceToken = pool.baseToken datatoken.lastPriceValue = spotPrice 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 diff --git a/src/mappings/utils/tokenUtils.ts b/src/mappings/utils/tokenUtils.ts index 925f81c..81cd101 100644 --- a/src/mappings/utils/tokenUtils.ts +++ b/src/mappings/utils/tokenUtils.ts @@ -23,9 +23,11 @@ export function createToken(address: Address, isDatatoken: boolean): Token { const decimals = contract.try_decimals() if (decimals.reverted) token.decimals = 18 else token.decimals = decimals.value + log.info('\n\n9. token.lastPriceToken: {}\n\n', [token.lastPriceToken]) token.lastPriceToken = ZERO_ADDRESS token.lastPriceValue = BigDecimal.zero() token.save() + log.info('\n\n10. token.lastPriceToken: {}\n\n', [token.lastPriceToken]) return token } diff --git a/test/integration/SimplePublishConsume.test.ts b/test/integration/SimplePublishConsume.test.ts index 9598650..842e5c0 100644 --- a/test/integration/SimplePublishConsume.test.ts +++ b/test/integration/SimplePublishConsume.test.ts @@ -451,4 +451,56 @@ describe('Simple Publish & consume test', async () => { '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') + }) })