-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added pickle jar zap in simulation support [WEB-529] (#60)
* feat: added pickle jar zap in simulation support * fix: renamed pickle jar * fix: only include yvboost-eth pickle jar * fix: implemented pricing logic for pickle jars * fix: linted * chore: refactor structure Co-authored-by: nymmrx <[email protected]>
- Loading branch information
Showing
5 changed files
with
163 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { getAddress } from "@ethersproject/address"; | ||
import { BigNumber } from "bignumber.js"; | ||
|
||
import { Service } from "../../common"; | ||
import { Address } from "../../types/common"; | ||
|
||
const HourInMilliseconds = 1000 * 60 * 60; | ||
const PickleApiUrl = "https://stkpowy01i.execute-api.us-west-1.amazonaws.com/prod/protocol/pools"; | ||
|
||
export const PickleJars = [ | ||
"0xCeD67a187b923F0E5ebcc77C7f2F7da20099e378" // yvboost-eth | ||
]; | ||
|
||
export class PickleService extends Service { | ||
private pickleJarUSDPrices: Map<Address, BigNumber> = new Map(); | ||
private lastFetchedDate: Date = new Date(0); | ||
|
||
/** | ||
* Fetches the USD price of a pickle jar token | ||
* @param jar the address of the jar to fetch | ||
* @returns the price of the jar token in USD | ||
*/ | ||
async getPriceUsd(jar: Address): Promise<BigNumber> { | ||
const oneHourAgo = new Date(Date.now() - HourInMilliseconds); | ||
if (this.lastFetchedDate < oneHourAgo) { | ||
await this.fetchPickleJarPrices(); | ||
} | ||
return this.pickleJarUSDPrices.get(jar) || new BigNumber(0); | ||
} | ||
|
||
private async fetchPickleJarPrices() { | ||
interface JarDatum { | ||
liquidity_locked: number; | ||
jarAddress: Address; | ||
tokens: number; | ||
} | ||
|
||
const jarData: JarDatum[] = await fetch(PickleApiUrl).then(res => res.json()); | ||
|
||
this.pickleJarUSDPrices.clear(); | ||
this.lastFetchedDate = new Date(); | ||
|
||
const relevantJars = jarData.filter(jar => PickleJars.includes(getAddress(jar.jarAddress))); | ||
|
||
for (const jarDatum of relevantJars) { | ||
const usdPrice = new BigNumber(jarDatum.liquidity_locked / jarDatum.tokens); | ||
this.pickleJarUSDPrices.set(getAddress(jarDatum.jarAddress), usdPrice); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters