Skip to content

Commit

Permalink
Handle multicall
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Mar 28, 2024
1 parent f8af929 commit dd4f664
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/utils/apr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,28 @@ export class AprState {
public addTransaction(collectedAmountUSD: BigDecimal, collectTimestamp: BigInt, totalValueLocked: BigDecimal): void {
const entry = new AprStateEntry(collectedAmountUSD, collectTimestamp, totalValueLocked)

if (this.collects.length === 0) {
this.collects.push(entry)
return
}

// check if the entry is in the right strict order
const lastTimestamp =
this.collects.length === 0 ? BigInt.fromI32(-1) : this.collects[this.collects.length - 1].collectTimestamp
if (!entry.collectTimestamp.gt(lastTimestamp)) {
const lastEntry = this.collects[this.collects.length - 1]

// merge entries with the same timestamp
if (entry.collectTimestamp.equals(lastEntry.collectTimestamp)) {
lastEntry.collectedAmount = lastEntry.collectedAmount.plus(entry.collectedAmount)
lastEntry.totalValueLocked = entry.totalValueLocked
} else if (!entry.collectTimestamp.gt(lastEntry.collectTimestamp)) {
log.error("AprCalc: collectTimestamp is not in order, trying to insert {}, when last ts is {}", [
entry.collectTimestamp.toString(),
lastTimestamp.toString(),
lastEntry.collectTimestamp.toString(),
])
throw new Error("AprCalc: collectTimestamp is not in order")
} else {
// latest entry is the last one
this.collects.push(entry)
}

// latest entry is the last one
this.collects.push(entry)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/.latest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "0.6.0",
"timestamp": 1711280326167
"timestamp": 1711654541275
}
14 changes: 14 additions & 0 deletions tests/utils/apr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,20 @@ describe("AprCalc", () => {
assertIsCloseTo(res, BigDecimal.fromString("96.8550368"), BigDecimal.fromString("0.0001"))
})

test("should allow multiple changes in the same timestamp/block (multicall)", () => {
let aprState = AprState.deserialize(new Array<BigDecimal>())
const now = DAY

aprState.addTransaction(BigDecimal.fromString("100"), BigInt.fromI32(0), BigDecimal.fromString("1000"))
aprState.addTransaction(BigDecimal.fromString("100"), BigInt.fromI32(10000), BigDecimal.fromString("2000"))
aprState.addTransaction(BigDecimal.fromString("100"), BigInt.fromI32(10000), BigDecimal.fromString("2000"))
aprState.addTransaction(BigDecimal.fromString("300"), DAY, BigDecimal.fromString("3000"))
const res = AprCalc.calculateLastApr(DAY, aprState, now)
log.debug("res: {}", [res.toString()])

assertIsCloseTo(res, BigDecimal.fromString("96.8550368"), BigDecimal.fromString("0.0001"))
})

test("should compute apr when the day is not over yet", () => {
let aprState = AprState.deserialize(new Array<BigDecimal>())
// using 6 decimals
Expand Down

0 comments on commit dd4f664

Please sign in to comment.