Skip to content

Commit c531773

Browse files
authored
fix (#405)
1 parent d8751fc commit c531773

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/mappings/pool.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import {
88
SwapFeeChanged
99
} from '../@types/templates/BPool/BPool'
1010
import { Transfer } from '../@types/templates/BPool/BToken'
11-
import { integer, PoolTransactionType, ZERO_ADDRESS } from './utils/constants'
11+
import {
12+
decimal,
13+
integer,
14+
PoolTransactionType,
15+
ZERO_ADDRESS
16+
} from './utils/constants'
1217
import { weiToDecimal } from './utils/generic'
1318
import {
1419
addLiquidity,
@@ -115,14 +120,14 @@ export function handleSwap(event: LOG_SWAP): void {
115120
pool.joinCount = pool.joinCount.plus(integer.ONE)
116121

117122
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
118-
// get token out and update pool transaction, value is negative
119123
const tokenOut = getToken(event.params.tokenOut, false)
120124
const tokenIn = getToken(event.params.tokenIn, false)
125+
let spotPrice = decimal.ZERO
126+
121127
const ammountOut = weiToDecimal(
122128
event.params.tokenAmountOut.toBigDecimal(),
123129
tokenOut.decimals
124130
)
125-
let baseTokenDecimals = 18
126131
const tokenOutNewBalance = weiToDecimal(
127132
event.params.outBalance.toBigDecimal(),
128133
tokenOut.decimals
@@ -140,7 +145,12 @@ export function handleSwap(event: LOG_SWAP): void {
140145
} else {
141146
poolTx.baseToken = tokenOut.id
142147
poolTx.baseTokenValue = ammountOut.neg()
143-
baseTokenDecimals = tokenOut.decimals
148+
149+
spotPrice = weiToDecimal(
150+
event.params.newSpotPrice.toBigDecimal(),
151+
tokenOut.decimals
152+
)
153+
144154
pool.baseTokenLiquidity = tokenOutNewBalance
145155
poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountOut)
146156

@@ -161,18 +171,17 @@ export function handleSwap(event: LOG_SWAP): void {
161171
} else {
162172
poolTx.baseToken = tokenIn.id
163173
poolTx.baseTokenValue = ammountIn
164-
baseTokenDecimals = tokenIn.decimals
174+
175+
spotPrice = decimal.ONE.div(
176+
weiToDecimal(event.params.newSpotPrice.toBigDecimal(), tokenOut.decimals)
177+
)
165178
pool.baseTokenLiquidity = tokenInNewBalance
166179
poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountIn)
167180
addLiquidity(tokenIn.id, ammountIn)
168181
addPoolSwap(tokenIn.id, ammountIn)
169182
}
170183

171184
// update spot price
172-
const spotPrice = weiToDecimal(
173-
event.params.newSpotPrice.toBigDecimal(),
174-
baseTokenDecimals
175-
)
176185
pool.spotPrice = spotPrice
177186
poolSnapshot.spotPrice = spotPrice
178187
poolSnapshot.baseTokenLiquidity = pool.baseTokenLiquidity

0 commit comments

Comments
 (0)