From 533a1e156a655a4fe2767e2a2072097312c629d7 Mon Sep 17 00:00:00 2001 From: Samer <36411297+ssallam@users.noreply.github.com> Date: Tue, 9 Feb 2021 11:06:23 +0100 Subject: [PATCH] merged/dependabot-tvl-fix (#21) * Update the readme file. * Add totalOceanLiquidity and fix TVL calculation * add logs to track pool shares. * update variable names * fix error after merge Co-authored-by: ssallam --- README.md | 51 +++++++++++++++++++++++++---------------- schema.graphql | 7 +++--- src/helpers.ts | 31 +++++++++++++++++++++---- src/mappings/factory.ts | 6 ++--- src/mappings/pool.ts | 41 +++++++++++++++++++++++++-------- 5 files changed, 95 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index c2dd46f..b8da669 100644 --- a/README.md +++ b/README.md @@ -93,26 +93,9 @@ docker-compose --env-file .env up Switch to a new terminal: +To deploy the ocean-subgraph to graph-node, see the `Deployment` section below. -- Once the graph node is ready, do the following to deploy the ocean-subgraph to the local graph-node - -```bash -git clone https://github.com/oceanprotocol/ocean-subgraph/ -cd ocean-subgraph -npm i -npm run codegen -npm run create:local-rinkeby -npm run deploy:local-rinkeby -``` - -- You can edit the event handler code and then run `npm run deploy:local` - - Running deploy will fail if the code has no changes - - Sometimes deploy will fail no matter what, in this case: - - Stop the docker-compose run (`docker-compose down`) - - Delete the `ipfs` and `postgres` folders in `docker/data` - - Restart docker-compose - - Run `npm run create:local` to create the ocean-subgraph - - Run `npm run deploy:local` to deploy the ocean-subgraph +You can make changes to the event handlers and/or features and re-deploy, again see the `Deployment` section below. ## ✨ Code Style @@ -128,7 +111,9 @@ npm run format ## ⬆️ Releases -Releases are managed semi-automatically. They are always manually triggered from a developer's machine with release scripts. From a clean `main` branch you can run the release task bumping the version accordingly based on semantic versioning: +Releases are managed semi-automatically. They are always manually triggered from a developer's +machine with release scripts. From a clean `main` branch you can run the release task bumping +the version accordingly based on semantic versioning: ```bash npm run release @@ -148,6 +133,32 @@ For the GitHub releases steps a GitHub personal access token, exported as `GITHU ## 🛳 Production ## ⬆️ Deployment +- Do the following to deploy the ocean-subgraph to a graph-node running locally: + +```bash +git clone https://github.com/oceanprotocol/ocean-subgraph/ +cd ocean-subgraph +npm i +npm run codegen +npm run create:local +npm run deploy:local +``` + +The above will deploy ocean-subgraph connecting to mainnet. To create/deploy subgraph connecting to Rinkeby or Ropsten test net, +use :local-rinkeby or :local-ropsten with either create or deploy command. + +- You can edit the event handler code and then run `npm run deploy:local` + - Running deploy will fail if the code has no changes + - Sometimes deploy will fail no matter what, in this case: + - Stop the docker-compose run (`docker-compose down` or Ctrl+C) + This should stop the graph-node, ipfs and postgres containers + - Delete the `ipfs` and `postgres` folders in `/docker/data` (`rm -rf ./docker/data/*`) + - Run `docker-compose up` to restart graph-node, ipfs and postgres + - Run `npm run create:local` to create the ocean-subgraph + - Run `npm run deploy:local` to deploy the ocean-subgraph + +Note: to deploy to one of the remote nodes run by Ocean, you can do port-forwarding then using the +above `local` create/deploy commands will work as is. ## 🏛 License diff --git a/schema.graphql b/schema.graphql index 397a7cb..bbe0f99 100644 --- a/schema.graphql +++ b/schema.graphql @@ -1,8 +1,9 @@ type PoolFactory @entity { id: ID! - totalLockedValue: BigDecimal # total value from all pools expressed in OCEAN - totalLiquidity: BigDecimal! # All the pools liquidity value in Ocean + totalValueLocked: BigDecimal # total value from all pools expressed in OCEAN + + totalOceanLiquidity: BigDecimal! # Total of OCEAN liquidity from all pools totalSwapVolume: BigDecimal! # All the swap volume in Ocean totalSwapFee: BigDecimal! # All the swap fee in Ocean @@ -29,7 +30,7 @@ type Pool @entity { totalSwapVolume: BigDecimal! # Total swap volume in OCEAN totalSwapFee: BigDecimal! # Total swap fee in OCEAN - lockedValue: BigDecimal! # locked value expressed in OCEAN (captures both Ocean and Datatoken) + valueLocked: BigDecimal! # value locked in pool expressed in OCEAN (captures both Ocean and Datatoken) datatokenReserve: BigDecimal! # Total pool reserve of Datatoken oceanReserve: BigDecimal! # Total pool reserve of OCEAN spotPrice: BigDecimal! diff --git a/src/helpers.ts b/src/helpers.ts index a63db65..c92ce2d 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -109,6 +109,12 @@ export function updatePoolTokenBalance( null, [source, poolToken.balance.toString(), balance.toString(), poolToken.poolId] ) + if (balance < ZERO_BD || poolToken.balance < ZERO_BD) { + log.warning( + 'EEEEEEEEEEEEEEEEE poolToken.balance < Zero: pool={}, poolToken={}, oldBalance={}, newBalance={}', + [poolToken.poolId, poolToken.tokenAddress.toString(), poolToken.balance.toString(), balance.toString()]) + } + poolToken.balance = balance } @@ -183,8 +189,16 @@ export function updatePoolTransactionToken( ptxTokenValues.save() if (ptxTokenValues.tokenAddress == OCEAN) { + let factory = PoolFactory.load('1') + factory.totalOceanLiquidity = factory.totalOceanLiquidity + ptxTokenValues.tokenReserve - pool.oceanReserve + if (factory.totalOceanLiquidity < ZERO_BD || pool.oceanReserve < ZERO_BD) { + log.warning( + 'EEEEEEEEEEEEEEEEE totalOceanLiquidity or oceanReserve < Zero: pool={}, totOcnLiq={}, ocnRes={}', + [pool.id, factory.totalOceanLiquidity.toString(), pool.oceanReserve.toString()]) + } ptx.oceanReserve = ptxTokenValues.tokenReserve pool.oceanReserve = ptxTokenValues.tokenReserve + factory.save() } else { ptx.datatokenReserve = ptxTokenValues.tokenReserve pool.datatokenReserve = ptxTokenValues.tokenReserve @@ -310,11 +324,18 @@ export function createPoolTransaction( pool.consumePrice = poolTx.consumePrice pool.spotPrice = poolTx.spotPrice - const oldLockedValue = pool.lockedValue - pool.lockedValue = pool.oceanReserve + pool.datatokenReserve * pool.spotPrice - const factory = PoolFactory.load('1') - factory.totalLockedValue = - factory.totalLockedValue - oldLockedValue + pool.lockedValue + const oldValueLocked = pool.valueLocked + const spotPrice = pool.spotPrice >= ZERO_BD ? pool.spotPrice : ZERO_BD + pool.valueLocked = poolTx.oceanReserve + (poolTx.datatokenReserve * spotPrice) + let factory = PoolFactory.load('1') + if (oldValueLocked < ZERO_BD || pool.valueLocked < ZERO_BD) { + log.warning( + 'EEEEEEEEEEEEEEEEE valueLocked < Zero: pool={}, oldVL={}, newVL={}, OCEAN={}, DT={}, spotPrice={}', + [pool.id, oldValueLocked.toString(), pool.valueLocked.toString(), + poolTx.oceanReserve.toString(), poolTx.datatokenReserve.toString(), + pool.spotPrice.toString()]) + } + factory.totalValueLocked = factory.totalValueLocked - oldValueLocked + pool.valueLocked pool.transactionCount = pool.transactionCount.plus(BigInt.fromI32(1)) diff --git a/src/mappings/factory.ts b/src/mappings/factory.ts index 093cea8..9eb59f7 100644 --- a/src/mappings/factory.ts +++ b/src/mappings/factory.ts @@ -9,10 +9,10 @@ export function handleNewPool(event: BPoolRegistered): void { if (factory == null) { factory = new PoolFactory('1') - factory.totalLiquidity = ZERO_BD + factory.totalOceanLiquidity = ZERO_BD factory.totalSwapVolume = ZERO_BD factory.totalSwapFee = ZERO_BD - factory.totalLockedValue = ZERO_BD + factory.totalValueLocked = ZERO_BD factory.poolCount = 0 factory.finalizedPoolCount = 0 @@ -37,7 +37,7 @@ export function handleNewPool(event: BPoolRegistered): void { pool.totalShares = ZERO_BD pool.totalSwapVolume = ZERO_BD pool.totalSwapFee = ZERO_BD - pool.lockedValue = ZERO_BD + pool.valueLocked = ZERO_BD pool.datatokenReserve = ZERO_BD pool.oceanReserve = ZERO_BD diff --git a/src/mappings/pool.ts b/src/mappings/pool.ts index 4440eaa..7309521 100644 --- a/src/mappings/pool.ts +++ b/src/mappings/pool.ts @@ -425,6 +425,8 @@ export function handleTransfer(event: Transfer): void { const pool = Pool.load(poolId) const poolTx = PoolTransaction.load(event.transaction.hash.toHexString()) const value = tokenToDecimal(event.params.value.toBigDecimal(), 18) + debuglog('poolShare Transfer event: (from, to, value)', event, + [event.params.from.toHex(), event.params.to.toHex(), value.toString()]) if (isMint) { if (poolShareTo == null) { @@ -438,11 +440,17 @@ export function handleTransfer(event: Transfer): void { poolTx.sharesTransferAmount = value poolTx.sharesBalance = poolShareTo.balance } - debuglog('pool shares mint: (id, value, totalShares)', event, [ - poolId, - value.toString(), - pool.totalShares.toString() - ]) + debuglog( + 'pool shares mint: (id, value, totalShares, shareToBalance, toAddress)', + event, + [ + poolId, + value.toString(), + pool.totalShares.toString(), + poolShareTo.balance.toString(), + poolShareTo.userAddress + ] + ) } else if (isBurn) { if (poolShareFrom == null) { createPoolShareEntity(poolShareFromId, poolId, event.params.from.toHex()) @@ -455,11 +463,17 @@ export function handleTransfer(event: Transfer): void { poolTx.sharesTransferAmount = -value poolTx.sharesBalance = poolShareFrom.balance } - debuglog('pool shares burn: (id, value, totalShares)', event, [ - poolId, - value.toString(), - pool.totalShares.toString() - ]) + debuglog( + 'pool shares burn: (id, value, totalShares, shareFromBalance, fromAddress)', + event, + [ + poolId, + value.toString(), + pool.totalShares.toString(), + poolShareFrom.balance.toString(), + poolShareFrom.userAddress + ] + ) } else { if (poolShareTo == null) { createPoolShareEntity(poolShareToId, poolId, event.params.to.toHex()) @@ -474,6 +488,13 @@ export function handleTransfer(event: Transfer): void { } poolShareFrom.balance -= value poolShareFrom.save() + debuglog( + 'pool shares transfer: ' + + '(id, value, totalShares, shareToBalance, shareFromBalance, toAddress, fromAddress)', event, + [poolId, value.toString(), pool.totalShares.toString(), + poolShareTo.balance.toString(), poolShareFrom.balance.toString(), + poolShareTo.userAddress, poolShareFrom.userAddress + ]) } if (