Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/tvl-issues #18

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down
7 changes: 4 additions & 3 deletions schema.graphql
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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!
Expand Down
28 changes: 25 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -310,10 +324,18 @@ export function createPoolTransaction(

pool.consumePrice = poolTx.consumePrice
pool.spotPrice = poolTx.spotPrice
const lockedValue = pool.lockedValue
pool.lockedValue = pool.oceanReserve + (pool.datatokenReserve * pool.spotPrice)
const valueLocked = pool.valueLocked
const spotPrice = pool.spotPrice >= ZERO_BD ? pool.spotPrice : ZERO_BD
pool.valueLocked = poolTx.oceanReserve + (poolTx.datatokenReserve * spotPrice)
let factory = PoolFactory.load('1')
factory.totalLockedValue = factory.totalLockedValue - lockedValue + pool.lockedValue
if (valueLocked < ZERO_BD || pool.valueLocked < ZERO_BD) {
log.warning(
'EEEEEEEEEEEEEEEEE valueLocked < Zero: pool={}, oldVL={}, newVL={}, OCEAN={}, DT={}, spotPrice={}',
[pool.id, valueLocked.toString(), pool.valueLocked.toString(),
poolTx.oceanReserve.toString(), poolTx.datatokenReserve.toString(),
pool.spotPrice.toString()])
}
factory.totalValueLocked = factory.totalValueLocked - valueLocked + pool.valueLocked

pool.transactionCount = pool.transactionCount.plus(BigInt.fromI32(1))

Expand Down
6 changes: 3 additions & 3 deletions src/mappings/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 15 additions & 3 deletions src/mappings/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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())
Expand All @@ -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 (
Expand Down