@@ -8,7 +8,12 @@ import {
8
8
SwapFeeChanged
9
9
} from '../@types/templates/BPool/BPool'
10
10
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'
12
17
import { weiToDecimal } from './utils/generic'
13
18
import {
14
19
addLiquidity ,
@@ -115,14 +120,14 @@ export function handleSwap(event: LOG_SWAP): void {
115
120
pool . joinCount = pool . joinCount . plus ( integer . ONE )
116
121
117
122
const poolSnapshot = getPoolSnapshot ( pool . id , event . block . timestamp . toI32 ( ) )
118
- // get token out and update pool transaction, value is negative
119
123
const tokenOut = getToken ( event . params . tokenOut , false )
120
124
const tokenIn = getToken ( event . params . tokenIn , false )
125
+ let spotPrice = decimal . ZERO
126
+
121
127
const ammountOut = weiToDecimal (
122
128
event . params . tokenAmountOut . toBigDecimal ( ) ,
123
129
tokenOut . decimals
124
130
)
125
- let baseTokenDecimals = 18
126
131
const tokenOutNewBalance = weiToDecimal (
127
132
event . params . outBalance . toBigDecimal ( ) ,
128
133
tokenOut . decimals
@@ -140,7 +145,12 @@ export function handleSwap(event: LOG_SWAP): void {
140
145
} else {
141
146
poolTx . baseToken = tokenOut . id
142
147
poolTx . baseTokenValue = ammountOut . neg ( )
143
- baseTokenDecimals = tokenOut . decimals
148
+
149
+ spotPrice = weiToDecimal (
150
+ event . params . newSpotPrice . toBigDecimal ( ) ,
151
+ tokenOut . decimals
152
+ )
153
+
144
154
pool . baseTokenLiquidity = tokenOutNewBalance
145
155
poolSnapshot . swapVolume = poolSnapshot . swapVolume . plus ( ammountOut )
146
156
@@ -161,18 +171,17 @@ export function handleSwap(event: LOG_SWAP): void {
161
171
} else {
162
172
poolTx . baseToken = tokenIn . id
163
173
poolTx . baseTokenValue = ammountIn
164
- baseTokenDecimals = tokenIn . decimals
174
+
175
+ spotPrice = decimal . ONE . div (
176
+ weiToDecimal ( event . params . newSpotPrice . toBigDecimal ( ) , tokenOut . decimals )
177
+ )
165
178
pool . baseTokenLiquidity = tokenInNewBalance
166
179
poolSnapshot . swapVolume = poolSnapshot . swapVolume . plus ( ammountIn )
167
180
addLiquidity ( tokenIn . id , ammountIn )
168
181
addPoolSwap ( tokenIn . id , ammountIn )
169
182
}
170
183
171
184
// update spot price
172
- const spotPrice = weiToDecimal (
173
- event . params . newSpotPrice . toBigDecimal ( ) ,
174
- baseTokenDecimals
175
- )
176
185
pool . spotPrice = spotPrice
177
186
poolSnapshot . spotPrice = spotPrice
178
187
poolSnapshot . baseTokenLiquidity = pool . baseTokenLiquidity
0 commit comments