Skip to content

Commit

Permalink
release (#229)
Browse files Browse the repository at this point in the history
fix: don't attempt to fetch prices from before a chain existed (#228)
  • Loading branch information
bard authored Aug 15, 2023
1 parent fa4d3a9 commit eb5d747
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type Chain = {
rpc: string;
name: string;
id: ChainId;
pricesFromTimestamp: number;
tokens: Token[];
subscriptions: Subscription[];
};
Expand All @@ -33,6 +34,7 @@ export const CHAINS: Chain[] = [
id: 1,
name: "mainnet",
rpc: `https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY ?? ""}`,
pricesFromTimestamp: Date.UTC(2022, 11, 1, 0, 0, 0),
tokens: [
{
code: "USDC",
Expand Down Expand Up @@ -83,6 +85,7 @@ export const CHAINS: Chain[] = [
id: 5,
name: "goerli",
rpc: `https://goerli.infura.io/v3/${process.env.INFURA_API_KEY ?? ""}`,
pricesFromTimestamp: Date.UTC(2022, 11, 1, 0, 0, 0),
tokens: [
{
code: "USDC",
Expand Down Expand Up @@ -188,6 +191,7 @@ export const CHAINS: Chain[] = [
rpc: `https://opt-mainnet.g.alchemy.com/v2/${
process.env.ALCHEMY_API_KEY ?? ""
}`,
pricesFromTimestamp: Date.UTC(2022, 11, 1, 0, 0, 0),
tokens: [
{
code: "USDC",
Expand Down Expand Up @@ -238,6 +242,7 @@ export const CHAINS: Chain[] = [
id: 250,
name: "fantom",
rpc: "https://rpcapi.fantom.network",
pricesFromTimestamp: Date.UTC(2022, 11, 1, 0, 0, 0),
tokens: [
{
code: "USDC",
Expand Down Expand Up @@ -291,6 +296,7 @@ export const CHAINS: Chain[] = [
id: 58008,
name: "pgn-testnet",
rpc: "https://sepolia.publicgoods.network",
pricesFromTimestamp: Date.UTC(2023, 6, 12, 0, 0, 0),
tokens: [
{
code: "ETH",
Expand Down Expand Up @@ -341,6 +347,7 @@ export const CHAINS: Chain[] = [
id: 424,
name: "pgn-mainnet",
rpc: "https://rpc.publicgoods.network",
pricesFromTimestamp: Date.UTC(2023, 6, 12, 0, 0, 0),
tokens: [
{
code: "ETH",
Expand Down
5 changes: 1 addition & 4 deletions src/prices/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import {
} from "./common.js";

const POLL_INTERVAL_MS = 60 * 1000;
const EARLIEST_PRICE_TIMESTAMP = new Date(
Date.UTC(2022, 11, 1, 0, 0, 0)
).getTime();

export interface PriceUpdaterService {
start: (opts?: { watch: boolean; toBlock: ToBlock }) => Promise<void>;
Expand Down Expand Up @@ -77,7 +74,7 @@ export function createPriceUpdater(
// get last updated price
const lastPriceAt = currentPrices.reduce(
(acc, price) => Math.max(price.timestamp + hours(1), acc),
EARLIEST_PRICE_TIMESTAMP
config.chain.pricesFromTimestamp
);

let toDate = undefined;
Expand Down
7 changes: 6 additions & 1 deletion src/utils/getBlockFromTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ async function estimateBlockNumber(

// Now, you can use `blocksPerSecond` to adjust your block number estimation.
// For instance, if you know the target timestamp is X seconds away from startTimestamp, you could estimate:
const secondsToTarget = Math.abs(targetTimestamp - startTimestamp);
const secondsToTarget = targetTimestamp - startTimestamp;
if (secondsToTarget < 0) {
throw new Error(
"Estimated block is negative, this probably means that the timestamp precedes the deployment of the chain. Check chain config and ensure that `pricesFromTimestamp` is correct."
);
}
const estimatedBlocksToTarget = blocksPerSecond * secondsToTarget;
const estimatedBlockNumber = startBlock + Math.round(estimatedBlocksToTarget);

Expand Down

0 comments on commit eb5d747

Please sign in to comment.