Skip to content

Commit

Permalink
Fix TokenAmoun id clash (#145)
Browse files Browse the repository at this point in the history
* Add logIndex in AssetAmount ids
* Fix LPToken out zero for the first add liquidity
  • Loading branch information
jeanchambras authored May 2, 2024
1 parent 87836b3 commit 545aacc
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/entities/AssetAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export function getAssetAmount(
assetAddress: Address,
amount: BigInt,
type: string,
logIndex: string,
timestamp: BigInt
): AssetAmount {
let id = generateAssetAmountId(
transactionAddress.toHex(),
assetAddress.toHex(),
logIndex,
type
)

Expand Down
9 changes: 4 additions & 5 deletions src/entities/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class PoolDetails {
ptAddress: Address
factoryAddress: Address
timestamp: BigInt
logIndex: BigInt
transactionHash: Bytes
}

Expand All @@ -33,6 +34,7 @@ export function createPool(params: PoolDetails): Pool {
params.ibtAddress,
ZERO_BI,
AssetType.IBT,
params.logIndex.toString(),
params.timestamp
)

Expand All @@ -44,6 +46,7 @@ export function createPool(params: PoolDetails): Pool {
params.ptAddress,
ZERO_BI,
AssetType.PT,
params.logIndex.toString(),
params.timestamp
)

Expand Down Expand Up @@ -79,11 +82,7 @@ export function createPool(params: PoolDetails): Pool {

pool.liquidityToken = lpToken.id

const lpTotalSupply = getERC20TotalSupply(
Address.fromBytes(lpToken.address)
)

pool.lpTotalSupply = lpTotalSupply
pool.lpTotalSupply = ZERO_BI

let factory = Factory.load(params.factoryAddress.toHex())
if (factory) {
Expand Down
13 changes: 11 additions & 2 deletions src/mappings/amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function handleAddLiquidity(event: AddLiquidity): void {
Address.fromString(ibtAddress),
event.params.token_amounts[0],
AssetType.IBT,
event.logIndex.toString(),
eventTimestamp
)

Expand All @@ -62,6 +63,7 @@ export function handleAddLiquidity(event: AddLiquidity): void {
Address.fromString(ptAddress),
event.params.token_amounts[1],
AssetType.PT,
event.logIndex.toString(),
eventTimestamp
)

Expand All @@ -75,15 +77,15 @@ export function handleAddLiquidity(event: AddLiquidity): void {
let lpTokenAddress = getPoolLPToken(event.address)

const lpTotalSupply = getERC20TotalSupply(lpTokenAddress)
pool.lpTotalSupply = lpTotalSupply

let lpTokenDiff = lpTotalSupply.minus(pool.lpTotalSupply)
pool.lpTotalSupply = lpTotalSupply

let lpAmountOut = getAssetAmount(
event.transaction.hash,
lpTokenAddress,
lpTokenDiff,
AssetType.LP,
event.logIndex.toString(),
eventTimestamp
)

Expand Down Expand Up @@ -185,6 +187,7 @@ export function handleRemoveLiquidity(event: RemoveLiquidity): void {
lpTokenAddress,
lpTokenDiff,
AssetType.LP,
event.logIndex.toString(),
eventTimestamp
)

Expand Down Expand Up @@ -214,6 +217,7 @@ export function handleRemoveLiquidity(event: RemoveLiquidity): void {
Address.fromString(ibtAddress),
event.params.token_amounts[0],
AssetType.IBT,
event.logIndex.toString(),
eventTimestamp
)

Expand All @@ -229,6 +233,7 @@ export function handleRemoveLiquidity(event: RemoveLiquidity): void {
Address.fromString(ptAddress),
event.params.token_amounts[1],
AssetType.PT,
event.logIndex.toString(),
eventTimestamp
)

Expand Down Expand Up @@ -317,6 +322,7 @@ export function handleTokenExchange(event: TokenExchange): void {
Address.fromString(poolAssetInAmount.asset),
event.params.tokens_sold,
event.params.sold_id.equals(ZERO_BI) ? AssetType.IBT : AssetType.PT,
event.logIndex.toString(),
eventTimestamp
)

Expand All @@ -334,6 +340,7 @@ export function handleTokenExchange(event: TokenExchange): void {
event.params.bought_id.equals(ZERO_BI)
? AssetType.IBT
: AssetType.PT,
event.logIndex.toString(),
eventTimestamp
)

Expand Down Expand Up @@ -466,6 +473,7 @@ export function handleRemoveLiquidityOne(event: RemoveLiquidityOne): void {
lpTokenAddress,
event.params.token_amount,
AssetType.LP,
event.logIndex.toString(),
eventTimestamp
)

Expand Down Expand Up @@ -496,6 +504,7 @@ export function handleRemoveLiquidityOne(event: RemoveLiquidityOne): void {
Address.fromString(withdrawnTokenAddress),
event.params.coin_amount,
withdrawnAssetType,
event.logIndex.toString(),
eventTimestamp
)

Expand Down
5 changes: 5 additions & 0 deletions src/mappings/futures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export function handleMint(event: Mint): void {
ptAddress,
event.params.amount,
AssetType.PT,
event.logIndex.toString(),
event.block.timestamp
)

Expand All @@ -234,6 +235,7 @@ export function handleMint(event: Mint): void {
ytAddress,
event.params.amount,
AssetType.YT,
event.logIndex.toString(),
event.block.timestamp
)

Expand Down Expand Up @@ -301,6 +303,7 @@ export function handleRedeem(event: Redeem): void {
ptAddress,
event.params.amount,
AssetType.PT,
event.logIndex.toString(),
event.block.timestamp
)

Expand All @@ -316,6 +319,7 @@ export function handleRedeem(event: Redeem): void {
ytAddress,
event.params.amount,
AssetType.YT,
event.logIndex.toString(),
event.block.timestamp
)

Expand Down Expand Up @@ -403,6 +407,7 @@ export function handleCurvePoolDeployed(event: CurvePoolDeployed): void {
factoryAddress: event.address,
ptAddress: event.params.pt,
timestamp: event.block.timestamp,
logIndex: event.logIndex,
transactionHash: event.transaction.hash,
})
}
Expand Down
1 change: 1 addition & 0 deletions src/mappings/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function handleTransfer(event: TransferEvent): void {
event.address,
event.params.value,
asset.type,
event.logIndex.toString(),
eventTimestamp
)

Expand Down
Loading

0 comments on commit 545aacc

Please sign in to comment.