From 5d829e6fcd511d49eee0d9d7b2ba4eeefe87bdbe Mon Sep 17 00:00:00 2001 From: ssallam Date: Fri, 29 Jan 2021 11:36:07 +0100 Subject: [PATCH 1/4] Update the readme file. --- README.md | 57 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 759b600..b0786ba 100644 --- a/README.md +++ b/README.md @@ -92,11 +92,13 @@ cd graph-node/docker docker-compose up ``` -Note: making contract calls using Infura fails with `missing trie node` errors. The fix requires editing `ethereum_adapter.rs` line 434 to use the latest block instead of a specific block number. Replace: `web3.eth().call(req, Some(block_id)).then(|result| {` with `web3.eth().call(req, Some(BlockNumber::Latest.into())).then(|result| {` +Note: making contract calls using Infura fails with `missing trie node` errors. The fix requires +editing `ethereum_adapter.rs` line 434 to use the latest block instead of a specific block number. +Replace: `web3.eth().call(req, Some(block_id)).then(|result| {` with `web3.eth().call(req, Some(BlockNumber::Latest.into())).then(|result| {` To run the graph-node with this fix it must be run from source. -First, delete the `graph-node` container from the `docker-compose.yml` file +First, remove the `graph-node` container from the `docker-compose.yml` file then run `docker-compose up` to get the postgresql and ipfs services running. Now you can build and run the graph-node from source @@ -108,25 +110,10 @@ cargo run -p graph-node --release > graphnode.log -- --ipfs 127.0.0.1:5001 ``` -- Once the graph node is ready, do the following to deploy the ocean-subgraph to the local graph-node +To deploy the ocean-subgraph to graph-node, see the `Deployment` section below. -```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 -``` +You can make changes to the event handlers and/or features and re-deploy, again see the `Deployment` section below. -- 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 `graph-node/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 ## ✨ Code Style @@ -142,7 +129,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 @@ -162,6 +151,34 @@ 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 to mainnet. To create/deploy 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 graph-node run (Ctrl+C) + - Stop the docker-compose run (`docker-compose down` or Ctrl+C) + - Delete the `ipfs` and `postgres` folders in `graph-node/docker/data` (`rm -rf ./docker/data/*`) + - Run `docker-compose up` to restart ipfs and postgres db + - Run the graph-node as above (using the cargo command) + - 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 the above `local` create/deploy will work as is. + ## 🏛 License From b5dd448cec8663168f101d6245e801f2686a1ab4 Mon Sep 17 00:00:00 2001 From: ssallam Date: Fri, 29 Jan 2021 11:57:05 +0100 Subject: [PATCH 2/4] Add totalOceanLiquidity and fix TVL calculation --- schema.graphql | 3 ++- src/helpers.ts | 24 +++++++++++++++++++++++- src/mappings/factory.ts | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/schema.graphql b/schema.graphql index 397a7cb..0825442 100644 --- a/schema.graphql +++ b/schema.graphql @@ -2,7 +2,8 @@ type PoolFactory @entity { id: ID! totalLockedValue: BigDecimal # total value from all pools expressed in OCEAN - totalLiquidity: BigDecimal! # All the pools liquidity value 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 diff --git a/src/helpers.ts b/src/helpers.ts index 7039b91..bfd60f8 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 @@ -311,8 +325,16 @@ export function createPoolTransaction( pool.consumePrice = poolTx.consumePrice pool.spotPrice = poolTx.spotPrice const lockedValue = pool.lockedValue - pool.lockedValue = pool.oceanReserve + (pool.datatokenReserve * pool.spotPrice) + const spotPrice = pool.spotPrice >= ZERO_BD ? pool.spotPrice : ZERO_BD + pool.lockedValue = poolTx.oceanReserve + (poolTx.datatokenReserve * spotPrice) let factory = PoolFactory.load('1') + if (lockedValue < ZERO_BD || pool.lockedValue < ZERO_BD) { + log.warning( + 'EEEEEEEEEEEEEEEEE valueLocked < Zero: pool={}, oldVL={}, newVL={}, OCEAN={}, DT={}, spotPrice={}', + [pool.id, lockedValue.toString(), pool.lockedValue.toString(), + poolTx.oceanReserve.toString(), poolTx.datatokenReserve.toString(), + pool.spotPrice.toString()]) + } factory.totalLockedValue = factory.totalLockedValue - lockedValue + pool.lockedValue pool.transactionCount = pool.transactionCount.plus(BigInt.fromI32(1)) diff --git a/src/mappings/factory.ts b/src/mappings/factory.ts index 8963be8..e7e0d1e 100644 --- a/src/mappings/factory.ts +++ b/src/mappings/factory.ts @@ -9,7 +9,7 @@ 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 From 83bcc6828a2006498829a6038ccbebb2299e653b Mon Sep 17 00:00:00 2001 From: ssallam Date: Fri, 29 Jan 2021 12:27:28 +0100 Subject: [PATCH 3/4] add logs to track pool shares. --- src/mappings/pool.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/mappings/pool.ts b/src/mappings/pool.ts index 7a74a5a..dc39eab 100644 --- a/src/mappings/pool.ts +++ b/src/mappings/pool.ts @@ -420,11 +420,14 @@ export function handleTransfer(event: Transfer): void { const poolShareToId = poolId.concat('-').concat(event.params.to.toHex()) let poolShareTo = PoolShare.load(poolShareToId) - const poolShareToBalance = poolShareTo == null ? ZERO_BD : poolShareTo.balance + const poolShareToBalance = + poolShareTo == null ? ZERO_BD : poolShareTo.balance 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,7 +441,8 @@ 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) { @@ -452,7 +456,8 @@ 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()) @@ -467,6 +472,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 ( From 89a87d8d77c9f76ba19b388d02135de0167d6d7e Mon Sep 17 00:00:00 2001 From: ssallam Date: Tue, 9 Feb 2021 09:29:52 +0100 Subject: [PATCH 4/4] update variable names --- schema.graphql | 4 ++-- src/helpers.ts | 10 +++++----- src/mappings/factory.ts | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/schema.graphql b/schema.graphql index 0825442..bbe0f99 100644 --- a/schema.graphql +++ b/schema.graphql @@ -1,7 +1,7 @@ type PoolFactory @entity { id: ID! - totalLockedValue: BigDecimal # total value from all pools expressed 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 @@ -30,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 bfd60f8..1f2bac7 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -324,18 +324,18 @@ export function createPoolTransaction( pool.consumePrice = poolTx.consumePrice pool.spotPrice = poolTx.spotPrice - const lockedValue = pool.lockedValue + const valueLocked = pool.valueLocked const spotPrice = pool.spotPrice >= ZERO_BD ? pool.spotPrice : ZERO_BD - pool.lockedValue = poolTx.oceanReserve + (poolTx.datatokenReserve * spotPrice) + pool.valueLocked = poolTx.oceanReserve + (poolTx.datatokenReserve * spotPrice) let factory = PoolFactory.load('1') - if (lockedValue < ZERO_BD || pool.lockedValue < ZERO_BD) { + if (valueLocked < ZERO_BD || pool.valueLocked < ZERO_BD) { log.warning( 'EEEEEEEEEEEEEEEEE valueLocked < Zero: pool={}, oldVL={}, newVL={}, OCEAN={}, DT={}, spotPrice={}', - [pool.id, lockedValue.toString(), pool.lockedValue.toString(), + [pool.id, valueLocked.toString(), pool.valueLocked.toString(), poolTx.oceanReserve.toString(), poolTx.datatokenReserve.toString(), pool.spotPrice.toString()]) } - factory.totalLockedValue = factory.totalLockedValue - lockedValue + pool.lockedValue + factory.totalValueLocked = factory.totalValueLocked - valueLocked + pool.valueLocked pool.transactionCount = pool.transactionCount.plus(BigInt.fromI32(1)) diff --git a/src/mappings/factory.ts b/src/mappings/factory.ts index e7e0d1e..0c27aa6 100644 --- a/src/mappings/factory.ts +++ b/src/mappings/factory.ts @@ -12,7 +12,7 @@ export function handleNewPool(event: BPoolRegistered): void { 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