Skip to content

Commit ccded4c

Browse files
authored
Feature/lastprices (#369)
* consume volume step 1
1 parent 1cfe520 commit ccded4c

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

schema.graphql

+8-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ type Token @entity {
5757
tx: String!
5858

5959
"block number when it was created"
60-
block: Int!
60+
block: Int!
61+
62+
lastPriceToken: String!
63+
lastPriceValue: BigDecimal!
6164
}
6265

6366
"utility type"
@@ -261,6 +264,10 @@ type Order @entity {
261264
createdTimestamp: Int!
262265
tx: String!
263266
block: Int!
267+
268+
lastPriceToken: String!
269+
lastPriceValue: BigDecimal!
270+
estimatedUSDValue: BigDecimal!
264271
}
265272

266273
# to be removed, mabye for pool shares only

src/mappings/erc20Templates.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
import { integer } from './utils/constants'
1616
import { weiToDecimal } from './utils/generic'
1717
import { addOrder } from './utils/globalUtils'
18-
import { getToken } from './utils/tokenUtils'
18+
import { getToken, getUSDValue } from './utils/tokenUtils'
1919
import { getUser } from './utils/userUtils'
2020

2121
function getOrderId(
@@ -63,7 +63,13 @@ export function handleOrderStarted(event: OrderStarted): void {
6363
order.createdTimestamp = event.block.timestamp.toI32()
6464
order.tx = event.transaction.hash.toHex()
6565
order.block = event.block.number.toI32()
66-
66+
order.lastPriceToken = token.lastPriceToken
67+
order.lastPriceValue = token.lastPriceValue
68+
order.estimatedUSDValue = getUSDValue(
69+
order.lastPriceToken,
70+
order.lastPriceValue,
71+
order.createdTimestamp
72+
)
6773
order.save()
6874
token.save()
6975
addOrder()

src/mappings/fixedRateExchange.ts

+9
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ export function handleSwap(event: Swapped): void {
199199
event.params.tokenOutAddress.toHexString(),
200200
swap.baseTokenAmount
201201
)
202+
203+
// update datatoken lastPriceToken and lastPriceValue
204+
const datatoken = getToken(
205+
Address.fromString(fixedRateExchange.datatoken),
206+
true
207+
)
208+
datatoken.lastPriceToken = fixedRateExchange.baseToken
209+
datatoken.lastPriceValue = fixedRateExchange.price
210+
datatoken.save()
202211
}
203212

204213
export function handlePublishMarketFeeChanged(

src/mappings/pool.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BigInt } from '@graphprotocol/graph-ts'
1+
import { BigInt, Address } from '@graphprotocol/graph-ts'
22
import {
33
LOG_EXIT,
44
LOG_JOIN,
@@ -177,6 +177,12 @@ export function handleSwap(event: LOG_SWAP): void {
177177
poolSnapshot.save()
178178
poolTx.save()
179179
pool.save()
180+
181+
// update datatoken lastPriceToken and lastPriceValue
182+
const datatoken = getToken(Address.fromString(pool.datatoken), true)
183+
datatoken.lastPriceToken = pool.baseToken
184+
datatoken.lastPriceValue = spotPrice
185+
datatoken.save()
180186
}
181187

182188
// setup is just to set token weight(it will mostly be 50:50) and spotPrice

src/mappings/utils/tokenUtils.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Address, log } from '@graphprotocol/graph-ts'
1+
import { Address, log, BigDecimal } from '@graphprotocol/graph-ts'
22
import { Nft, Token } from '../../@types/schema'
33
import { ERC20 } from '../../@types/templates/ERC20Template/ERC20'
44
import { ERC20Template, ERC721Template } from '../../@types/templates'
55
import { addNft } from './globalUtils'
6+
import { ZERO_ADDRESS } from './constants'
67

78
export function createToken(address: Address, isDatatoken: boolean): Token {
89
log.debug('started creating token with address: {}', [address.toHexString()])
@@ -22,6 +23,8 @@ export function createToken(address: Address, isDatatoken: boolean): Token {
2223
const decimals = contract.try_decimals()
2324
if (decimals.reverted) token.decimals = 18
2425
else token.decimals = decimals.value
26+
token.lastPriceToken = ZERO_ADDRESS
27+
token.lastPriceValue = BigDecimal.zero()
2528
token.save()
2629
return token
2730
}
@@ -56,3 +59,11 @@ export function getNftToken(address: Address): Nft {
5659
}
5760
return newToken
5861
}
62+
63+
export function getUSDValue(
64+
address: string,
65+
value: BigDecimal,
66+
timestamp: number
67+
): BigDecimal {
68+
return BigDecimal.zero()
69+
}

0 commit comments

Comments
 (0)