Skip to content

Commit e7cb995

Browse files
authored
add event Index to pool transactions (#497)
1 parent 13d7fb7 commit e7cb995

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

schema.graphql

+2-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ type PoolShare @entity {
207207
}
208208

209209
type PoolTransaction @entity {
210-
"tx address + caller address"
210+
"tx address + eventIndex"
211211
id: ID!
212212
"pool related to this tx"
213213
pool: Pool!
@@ -222,6 +222,7 @@ type PoolTransaction @entity {
222222
timestamp: Int!
223223
"pool creation transaction id"
224224
tx: String!
225+
eventIndex: BigInt
225226
"block number when it was created"
226227
block: Int
227228

src/mappings/pool.ts

+26-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ import { getUser } from './utils/userUtils'
3232
export function handleJoin(event: LOG_JOIN): void {
3333
const pool = getPool(event.address.toHex())
3434
const user = getUser(event.params.caller.toHex())
35-
const poolTx = getPoolTransaction(event, user.id, PoolTransactionType.JOIN)
35+
const poolTx = getPoolTransaction(
36+
event,
37+
user.id,
38+
PoolTransactionType.JOIN,
39+
event.logIndex
40+
)
3641

3742
pool.transactionCount = pool.transactionCount.plus(integer.ONE)
3843
pool.joinCount = pool.joinCount.plus(integer.ONE)
@@ -72,7 +77,12 @@ export function handleJoin(event: LOG_JOIN): void {
7277
export function handleExit(event: LOG_EXIT): void {
7378
const pool = getPool(event.address.toHex())
7479
const user = getUser(event.params.caller.toHex())
75-
const poolTx = getPoolTransaction(event, user.id, PoolTransactionType.EXIT)
80+
const poolTx = getPoolTransaction(
81+
event,
82+
user.id,
83+
PoolTransactionType.EXIT,
84+
event.logIndex
85+
)
7686

7787
pool.transactionCount = pool.transactionCount.plus(integer.ONE)
7888
pool.joinCount = pool.joinCount.plus(integer.ONE)
@@ -109,7 +119,12 @@ export function handleExit(event: LOG_EXIT): void {
109119
export function handleSwap(event: LOG_SWAP): void {
110120
const pool = getPool(event.address.toHex())
111121
const user = getUser(event.params.caller.toHex())
112-
const poolTx = getPoolTransaction(event, user.id, PoolTransactionType.SWAP)
122+
const poolTx = getPoolTransaction(
123+
event,
124+
user.id,
125+
PoolTransactionType.SWAP,
126+
event.logIndex
127+
)
113128

114129
pool.transactionCount = pool.transactionCount.plus(integer.ONE)
115130
pool.joinCount = pool.joinCount.plus(integer.ONE)
@@ -227,7 +242,8 @@ export function handleSetup(event: LOG_SETUP): void {
227242
const poolTx = getPoolTransaction(
228243
event,
229244
fromUser.id,
230-
PoolTransactionType.SETUP
245+
PoolTransactionType.SETUP,
246+
event.logIndex
231247
)
232248
poolTx.type = PoolTransactionType.SETUP
233249
poolTx.baseToken = token.id
@@ -259,7 +275,12 @@ export function handlerBptTransfer(event: Transfer): void {
259275
const toAddress = event.params.dst.toHexString()
260276
const poolAddress = event.address.toHex()
261277
const caller = getUser(event.transaction.from.toHex())
262-
const poolTx = getPoolTransaction(event, caller.id, PoolTransactionType.SWAP)
278+
const poolTx = getPoolTransaction(
279+
event,
280+
caller.id,
281+
PoolTransactionType.SWAP,
282+
event.logIndex
283+
)
263284

264285
// btoken has 18 decimals
265286
const ammount = weiToDecimal(event.params.amt.toBigDecimal(), 18)

src/mappings/utils/poolUtils.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Address, BigDecimal, ethereum } from '@graphprotocol/graph-ts'
1+
import { Address, BigDecimal, BigInt, ethereum } from '@graphprotocol/graph-ts'
22
import {
33
Pool,
44
PoolShare,
@@ -19,19 +19,20 @@ export function getPoolShareId(
1919

2020
export function getPoolTransactionId(
2121
txHash: string,
22-
userAddress: string
22+
eventIndex: BigInt
2323
): string {
24-
return `${txHash}-${userAddress}`
24+
return `${txHash}-` + eventIndex.toString()
2525
}
2626

2727
export function getPoolTransaction(
2828
event: ethereum.Event,
2929
userAddress: string,
30-
type: string
30+
type: string,
31+
eventIndex: BigInt
3132
): PoolTransaction {
3233
const txId = getPoolTransactionId(
3334
event.transaction.hash.toHexString(),
34-
userAddress
35+
eventIndex
3536
)
3637
let poolTx = PoolTransaction.load(txId)
3738

@@ -45,6 +46,7 @@ export function getPoolTransaction(
4546

4647
poolTx.timestamp = event.block.timestamp.toI32()
4748
poolTx.tx = event.transaction.hash.toHex()
49+
poolTx.eventIndex = eventIndex
4850
poolTx.block = event.block.number.toI32()
4951

5052
poolTx.gasPrice = gweiToEth(event.transaction.gasPrice.toBigDecimal())

0 commit comments

Comments
 (0)