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

merged/dependabot-tvl-fix #21

Merged
merged 6 commits into from
Feb 9, 2021
Merged
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
51 changes: 31 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

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
31 changes: 26 additions & 5 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,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))

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