fix liquidity (#383)

* fix liquidity

* fix lint
This commit is contained in:
mihaisc 2022-03-23 03:55:30 -07:00 committed by GitHub
parent ac9e4832a7
commit 9837a71a27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 12 deletions

1
package-lock.json generated
View File

@ -5,7 +5,6 @@
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "ocean-subgraph",
"version": "1.2.0", "version": "1.2.0",
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {

View File

@ -23,7 +23,8 @@ import {
getPoolShare, getPoolShare,
getPoolSnapshot, getPoolSnapshot,
getPoolLpSwapFee, getPoolLpSwapFee,
getPoolPublisherMarketFee getPoolPublisherMarketFee,
getBalance
} from './utils/poolUtils' } from './utils/poolUtils'
import { getToken } from './utils/tokenUtils' import { getToken } from './utils/tokenUtils'
import { getUser } from './utils/userUtils' import { getUser } from './utils/userUtils'
@ -122,16 +123,23 @@ export function handleSwap(event: LOG_SWAP): void {
event.params.tokenAmountOut.toBigDecimal(), event.params.tokenAmountOut.toBigDecimal(),
tokenOut.decimals tokenOut.decimals
) )
const tokenOutNewBalance = getBalance(
event.address,
event.params.tokenOut,
tokenOut.decimals
)
if (tokenOut.isDatatoken) { if (tokenOut.isDatatoken) {
poolTx.datatoken = tokenOut.id poolTx.datatoken = tokenOut.id
poolTx.datatokenValue = ammountOut.neg() poolTx.datatokenValue = ammountOut.neg()
pool.datatokenLiquidity = pool.datatokenLiquidity.minus(ammountOut) pool.datatokenLiquidity = tokenOutNewBalance
} else { } else {
poolTx.baseToken = tokenOut.id poolTx.baseToken = tokenOut.id
poolTx.baseTokenValue = ammountOut.neg() poolTx.baseTokenValue = ammountOut.neg()
pool.baseTokenLiquidity = pool.baseTokenLiquidity.minus(ammountOut) pool.baseTokenLiquidity = tokenOutNewBalance
poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountOut) poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountOut)
addPoolSwap(tokenOut.id, ammountOut) addPoolSwap(tokenOut.id, ammountOut)
@ -144,18 +152,21 @@ export function handleSwap(event: LOG_SWAP): void {
event.params.tokenAmountIn.toBigDecimal(), event.params.tokenAmountIn.toBigDecimal(),
tokenIn.decimals tokenIn.decimals
) )
const tokenInNewBalance = getBalance(
event.address,
event.params.tokenIn,
tokenIn.decimals
)
if (tokenIn.isDatatoken) { if (tokenIn.isDatatoken) {
poolTx.datatoken = tokenIn.id poolTx.datatoken = tokenIn.id
poolTx.datatokenValue = ammountIn poolTx.datatokenValue = ammountIn
pool.datatokenLiquidity = pool.datatokenLiquidity.plus(ammountIn) pool.datatokenLiquidity = tokenInNewBalance
} else { } else {
poolTx.baseToken = tokenIn.id poolTx.baseToken = tokenIn.id
poolTx.baseTokenValue = ammountIn poolTx.baseTokenValue = ammountIn
pool.baseTokenLiquidity = tokenInNewBalance
pool.baseTokenLiquidity = pool.baseTokenLiquidity.plus(ammountIn)
poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountIn) poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountIn)
addLiquidity(tokenIn.id, ammountIn) addLiquidity(tokenIn.id, ammountIn)
addPoolSwap(tokenIn.id, ammountIn) addPoolSwap(tokenIn.id, ammountIn)
} }

View File

@ -141,3 +141,14 @@ export function getPoolPublisherMarketFee(poolAddress: Address): BigDecimal {
const marketFee = weiToDecimal(marketFeeWei.toBigDecimal(), 18) const marketFee = weiToDecimal(marketFeeWei.toBigDecimal(), 18)
return marketFee return marketFee
} }
export function getBalance(
poolAddress: Address,
tokenAddress: Address,
tokenDecimals: i32
): BigDecimal {
const contract = BPool.bind(poolAddress)
const balanceWei = contract.getBalance(tokenAddress)
const balance = weiToDecimal(balanceWei.toBigDecimal(), tokenDecimals)
return balance
}

View File

@ -8,9 +8,9 @@ dataSources:
name: ERC721Factory name: ERC721Factory
network: rinkeby network: rinkeby
source: source:
address: '0x1b1FE18bE79249AE8A30b3cEef03d70ca299a75b' address: '0x548fd8639eC4Add192f12FC5c3EF6Dd80e1634ce'
abi: ERC721Factory abi: ERC721Factory
startBlock: 10182337 startBlock: 10364536
mapping: mapping:
kind: ethereum/events kind: ethereum/events
apiVersion: 0.0.6 apiVersion: 0.0.6
@ -33,9 +33,9 @@ dataSources:
name: FactoryRouter name: FactoryRouter
network: rinkeby network: rinkeby
source: source:
address: '0x6CE85a8B9dF0aEbd6deE3593d4E9cde7398ff842' address: '0xfe1a9F07718b40071F4c8d0f4bA22121dD5C0559'
abi: FactoryRouter abi: FactoryRouter
startBlock: 10182337 startBlock: 10364536
mapping: mapping:
kind: ethereum/events kind: ethereum/events
apiVersion: 0.0.6 apiVersion: 0.0.6