Skip to content

Commit 9837a71

Browse files
authored
fix liquidity (#383)
* fix liquidity * fix lint
1 parent ac9e483 commit 9837a71

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

package-lock.json

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/mappings/pool.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
getPoolShare,
2424
getPoolSnapshot,
2525
getPoolLpSwapFee,
26-
getPoolPublisherMarketFee
26+
getPoolPublisherMarketFee,
27+
getBalance
2728
} from './utils/poolUtils'
2829
import { getToken } from './utils/tokenUtils'
2930
import { getUser } from './utils/userUtils'
@@ -122,16 +123,23 @@ export function handleSwap(event: LOG_SWAP): void {
122123
event.params.tokenAmountOut.toBigDecimal(),
123124
tokenOut.decimals
124125
)
126+
127+
const tokenOutNewBalance = getBalance(
128+
event.address,
129+
event.params.tokenOut,
130+
tokenOut.decimals
131+
)
132+
125133
if (tokenOut.isDatatoken) {
126134
poolTx.datatoken = tokenOut.id
127135
poolTx.datatokenValue = ammountOut.neg()
128136

129-
pool.datatokenLiquidity = pool.datatokenLiquidity.minus(ammountOut)
137+
pool.datatokenLiquidity = tokenOutNewBalance
130138
} else {
131139
poolTx.baseToken = tokenOut.id
132140
poolTx.baseTokenValue = ammountOut.neg()
133141

134-
pool.baseTokenLiquidity = pool.baseTokenLiquidity.minus(ammountOut)
142+
pool.baseTokenLiquidity = tokenOutNewBalance
135143
poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountOut)
136144

137145
addPoolSwap(tokenOut.id, ammountOut)
@@ -144,18 +152,21 @@ export function handleSwap(event: LOG_SWAP): void {
144152
event.params.tokenAmountIn.toBigDecimal(),
145153
tokenIn.decimals
146154
)
155+
const tokenInNewBalance = getBalance(
156+
event.address,
157+
event.params.tokenIn,
158+
tokenIn.decimals
159+
)
147160
if (tokenIn.isDatatoken) {
148161
poolTx.datatoken = tokenIn.id
149162
poolTx.datatokenValue = ammountIn
150163

151-
pool.datatokenLiquidity = pool.datatokenLiquidity.plus(ammountIn)
164+
pool.datatokenLiquidity = tokenInNewBalance
152165
} else {
153166
poolTx.baseToken = tokenIn.id
154167
poolTx.baseTokenValue = ammountIn
155-
156-
pool.baseTokenLiquidity = pool.baseTokenLiquidity.plus(ammountIn)
168+
pool.baseTokenLiquidity = tokenInNewBalance
157169
poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountIn)
158-
159170
addLiquidity(tokenIn.id, ammountIn)
160171
addPoolSwap(tokenIn.id, ammountIn)
161172
}

src/mappings/utils/poolUtils.ts

+11
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,14 @@ export function getPoolPublisherMarketFee(poolAddress: Address): BigDecimal {
141141
const marketFee = weiToDecimal(marketFeeWei.toBigDecimal(), 18)
142142
return marketFee
143143
}
144+
145+
export function getBalance(
146+
poolAddress: Address,
147+
tokenAddress: Address,
148+
tokenDecimals: i32
149+
): BigDecimal {
150+
const contract = BPool.bind(poolAddress)
151+
const balanceWei = contract.getBalance(tokenAddress)
152+
const balance = weiToDecimal(balanceWei.toBigDecimal(), tokenDecimals)
153+
return balance
154+
}

subgraph.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ dataSources:
88
name: ERC721Factory
99
network: rinkeby
1010
source:
11-
address: '0x1b1FE18bE79249AE8A30b3cEef03d70ca299a75b'
11+
address: '0x548fd8639eC4Add192f12FC5c3EF6Dd80e1634ce'
1212
abi: ERC721Factory
13-
startBlock: 10182337
13+
startBlock: 10364536
1414
mapping:
1515
kind: ethereum/events
1616
apiVersion: 0.0.6
@@ -33,9 +33,9 @@ dataSources:
3333
name: FactoryRouter
3434
network: rinkeby
3535
source:
36-
address: '0x6CE85a8B9dF0aEbd6deE3593d4E9cde7398ff842'
36+
address: '0xfe1a9F07718b40071F4c8d0f4bA22121dD5C0559'
3737
abi: FactoryRouter
38-
startBlock: 10182337
38+
startBlock: 10364536
3939
mapping:
4040
kind: ethereum/events
4141
apiVersion: 0.0.6

0 commit comments

Comments
 (0)