Skip to content

Commit

Permalink
Set liquidity using swap event param. Bump contract addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Pote committed May 4, 2021
1 parent 4c34d90 commit e8ba1e2
Show file tree
Hide file tree
Showing 6 changed files with 3,246 additions and 3,599 deletions.
50 changes: 6 additions & 44 deletions abis/factory.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
[
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -130,7 +125,7 @@
"inputs": [
{
"internalType": "uint24",
"name": "",
"name": "fee",
"type": "uint24"
}
],
Expand All @@ -149,25 +144,25 @@
"inputs": [
{
"internalType": "address",
"name": "",
"name": "tokenA",
"type": "address"
},
{
"internalType": "address",
"name": "",
"name": "tokenB",
"type": "address"
},
{
"internalType": "uint24",
"name": "",
"name": "fee",
"type": "uint24"
}
],
"name": "getPool",
"outputs": [
{
"internalType": "address",
"name": "",
"name": "pool",
"type": "address"
}
],
Expand All @@ -187,39 +182,6 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "parameters",
"outputs": [
{
"internalType": "address",
"name": "factory",
"type": "address"
},
{
"internalType": "address",
"name": "token0",
"type": "address"
},
{
"internalType": "address",
"name": "token1",
"type": "address"
},
{
"internalType": "uint24",
"name": "fee",
"type": "uint24"
},
{
"internalType": "int24",
"name": "tickSpacing",
"type": "int24"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand All @@ -233,4 +195,4 @@
"stateMutability": "nonpayable",
"type": "function"
}
]
]
119 changes: 68 additions & 51 deletions abis/pool.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@
"name": "sqrtPriceX96",
"type": "uint160"
},
{
"indexed": false,
"internalType": "uint128",
"name": "liquidity",
"type": "uint128"
},
{
"indexed": false,
"internalType": "int24",
Expand Down Expand Up @@ -617,7 +623,7 @@
"inputs": [
{
"internalType": "uint256",
"name": "",
"name": "index",
"type": "uint256"
}
],
Expand All @@ -635,7 +641,7 @@
},
{
"internalType": "uint160",
"name": "liquidityCumulative",
"name": "secondsPerLiquidityCumulativeX128",
"type": "uint160"
},
{
Expand Down Expand Up @@ -664,7 +670,7 @@
},
{
"internalType": "uint160[]",
"name": "liquidityCumulatives",
"name": "secondsPerLiquidityCumulativeX128s",
"type": "uint160[]"
}
],
Expand All @@ -675,15 +681,15 @@
"inputs": [
{
"internalType": "bytes32",
"name": "",
"name": "key",
"type": "bytes32"
}
],
"name": "positions",
"outputs": [
{
"internalType": "uint128",
"name": "liquidity",
"name": "_liquidity",
"type": "uint128"
},
{
Expand Down Expand Up @@ -728,49 +734,6 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "int24",
"name": "tickLower",
"type": "int24"
},
{
"internalType": "int24",
"name": "tickUpper",
"type": "int24"
}
],
"name": "secondsInside",
"outputs": [
{
"internalType": "uint32",
"name": "",
"type": "uint32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "int24",
"name": "",
"type": "int24"
}
],
"name": "secondsOutside",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -832,6 +795,40 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "int24",
"name": "tickLower",
"type": "int24"
},
{
"internalType": "int24",
"name": "tickUpper",
"type": "int24"
}
],
"name": "snapshotCumulativesInside",
"outputs": [
{
"internalType": "int56",
"name": "tickCumulativeInside",
"type": "int56"
},
{
"internalType": "uint160",
"name": "secondsPerLiquidityInsideX128",
"type": "uint160"
},
{
"internalType": "uint32",
"name": "secondsInside",
"type": "uint32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -880,7 +877,7 @@
"inputs": [
{
"internalType": "int16",
"name": "",
"name": "wordPosition",
"type": "int16"
}
],
Expand Down Expand Up @@ -912,7 +909,7 @@
"inputs": [
{
"internalType": "int24",
"name": "",
"name": "tick",
"type": "int24"
}
],
Expand All @@ -937,6 +934,26 @@
"internalType": "uint256",
"name": "feeGrowthOutside1X128",
"type": "uint256"
},
{
"internalType": "int56",
"name": "tickCumulativeOutside",
"type": "int56"
},
{
"internalType": "uint160",
"name": "secondsPerLiquidityOutsideX128",
"type": "uint160"
},
{
"internalType": "uint32",
"name": "secondsOutside",
"type": "uint32"
},
{
"internalType": "bool",
"name": "initialized",
"type": "bool"
}
],
"stateMutability": "view",
Expand Down Expand Up @@ -968,4 +985,4 @@
"stateMutability": "view",
"type": "function"
}
]
]
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@
"eslint-config-prettier": "^6.1.0",
"prettier": "^1.18.2",
"typescript": "^3.5.2"
},
"dependencies": {}
}
}
38 changes: 2 additions & 36 deletions src/mappings/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,47 +294,13 @@ export function handleSwap(event: SwapEvent): void {
pool.volumeUSD = pool.volumeUSD.plus(amountTotalUSD)
pool.txCount = pool.txCount.plus(ONE_BI)

// Update the pools active liquidity.
// A swap can cause the currently active tick to change, which can cause a change in the active liquidity.
let previousTick = pool.tick
let newTick = BigInt.fromI32(event.params.tick)
let tickSpacing = feeTierToTickSpacing(pool.feeTier)
// Snap to the previous initializable tick (i.e. a multiple of fee spacing)
let previousActiveTick: BigInt = previousTick.div(tickSpacing).times(tickSpacing)
let newActiveTick: BigInt = newTick.div(tickSpacing).times(tickSpacing)

// If our new tick is bigger, we check all initializable ticks up to and including the new tick, and apply their net liquidities.
if (previousTick.le(newTick)) {
for (let activeTick = previousActiveTick.plus(tickSpacing); activeTick.le(newActiveTick); activeTick = activeTick.plus(tickSpacing)) {
let activeTickId = pool.id + "#" + activeTick.toString()
let activeTickLoaded = Tick.load(activeTickId)

if (activeTickLoaded) {
pool.liquidity = pool.liquidity.plus(activeTickLoaded.liquidityNet)
}
}
} else {
// Our new tick is smaller. We don't want to apply net liquidity until we have passed the tick with the net.
// e.g. if our new tick = 200, and 200 has net liquidity, we *don't* want to apply 200's net liquidity yet (since 200s liquidity)
// is still active.
let previousActiveTickIter = ZERO_BI.plus(previousActiveTick)
for (let activeTick = previousActiveTickIter.minus(tickSpacing); activeTick.ge(newActiveTick); activeTick = activeTick.minus(tickSpacing)) {
let previousActiveTickId = pool.id + "#" + previousActiveTickIter.toString()
let activeTickLoaded = Tick.load(previousActiveTickId)
if (activeTickLoaded) {
pool.liquidity = pool.liquidity.minus(activeTickLoaded.liquidityNet)
}
previousActiveTickIter = activeTick
}
}

// Update the pools tick
// Update the pool with the new active liquidity, price, and tick.
pool.liquidity = event.params.liquidity;
pool.tick = BigInt.fromI32(event.params.tick as i32)
pool.sqrtPrice = event.params.sqrtPriceX96
pool.totalValueLockedToken0 = pool.totalValueLockedToken0.plus(amount0)
pool.totalValueLockedToken1 = pool.totalValueLockedToken1.plus(amount1)


// update token0 data
token0.volume = token0.volume.plus(amount0Abs)
token0.totalValueLocked = token0.totalValueLocked.plus(amount0)
Expand Down
6 changes: 3 additions & 3 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dataSources:
name: Factory
network: rinkeby
source:
address: '0x815BCC87613315327E04e4A3b7c96a79Ae80760c'
address: '0x3820c1adA8d5Eb2b200F4dFcaB7C2B10062B1CfE'
abi: Factory
startBlock: 8459128
mapping:
Expand All @@ -35,7 +35,7 @@ dataSources:
name: NonfungiblePositionManager
network: rinkeby
source:
address: '0x3255160392215494bee8B5aBf8C4C40965d0986C'
address: '0xC62B9b27207ac749f72d65A98FF177cF5eF54099'
abi: NonfungiblePositionManager
startBlock: 8372189
mapping:
Expand Down Expand Up @@ -82,7 +82,7 @@ templates:
eventHandlers:
- event: Initialize(uint160,int24)
handler: handleInitialize
- event: Swap(indexed address,indexed address,int256,int256,uint160,int24)
- event: Swap(indexed address,indexed address,int256,int256,uint160,uint128,int24)
handler: handleSwap
- event: Mint(address,indexed address,indexed int24,indexed int24,uint128,uint256,uint256)
handler: handleMint
Expand Down
Loading

0 comments on commit e8ba1e2

Please sign in to comment.