-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Motivation Replace web2 calls to display TVL in $ with [TVL canister](https://github.com/dfinity/ic/tree/master/rs/rosetta-api/tvl). # Changes - remove Binance and Governance `/metrics` rest API - implement TVL canister - add API and services to query TVL - integrate TVL in worker and post message - uses new TVL object in component - update periodicity to one hour to not refresh the value dynamically for now # Screenshot No visual UI changes.
- Loading branch information
1 parent
e2748a6
commit 3c5dc7c
Showing
26 changed files
with
378 additions
and
385 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,49 @@ | ||
import { TVLCanister } from "$lib/canisters/tvl/tvl.canister"; | ||
import type { TvlResult } from "$lib/canisters/tvl/tvl.types"; | ||
import { TVL_CANISTER_ID } from "$lib/constants/canister-ids.constants"; | ||
import { HOST_IC0_APP } from "$lib/constants/environment.constants"; | ||
import { logWithTimestamp } from "$lib/utils/dev.utils"; | ||
import type { Identity } from "@dfinity/agent"; | ||
import type { Principal } from "@dfinity/principal"; | ||
/** | ||
* HTTP-Agent explicit CJS import for compatibility with web worker - avoid Error [RollupError]: Unexpected token (Note that you need plugins to import files that are not JavaScript) | ||
*/ | ||
import { HttpAgent } from "@dfinity/agent/lib/cjs/index"; | ||
|
||
export const queryTVL = async ({ | ||
identity, | ||
certified, | ||
}: { | ||
identity: Identity; | ||
certified: boolean; | ||
}): Promise<TvlResult> => { | ||
logWithTimestamp(`Getting canister ${TVL_CANISTER_ID.toText()} TVL call...`); | ||
|
||
const { getTVL } = await canister({ identity, canisterId: TVL_CANISTER_ID }); | ||
|
||
const result = getTVL({ certified }); | ||
|
||
logWithTimestamp( | ||
`Getting canister ${TVL_CANISTER_ID.toText()} TVL complete.` | ||
); | ||
|
||
return result; | ||
}; | ||
|
||
const canister = async ({ | ||
identity, | ||
canisterId, | ||
}: { | ||
identity: Identity; | ||
canisterId: Principal; | ||
}): Promise<TVLCanister> => { | ||
const agent = new HttpAgent({ | ||
identity, | ||
host: HOST_IC0_APP, | ||
}); | ||
|
||
return TVLCanister.create({ | ||
agent, | ||
canisterId, | ||
}); | ||
}; |
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 type { TVLCanisterOptions } from "$lib/canisters/tvl/tvl.canister.types"; | ||
import { Actor } from "@dfinity/agent"; | ||
import { idlFactory as certifiedIdlFactory } from "./tvl.certified.idl"; | ||
import { idlFactory } from "./tvl.idl"; | ||
import type { TvlResult, _SERVICE as TVLService } from "./tvl.types"; | ||
|
||
export class TVLCanister { | ||
private constructor( | ||
private readonly service: TVLService, | ||
private readonly certifiedService: TVLService | ||
) { | ||
this.service = service; | ||
this.certifiedService = certifiedService; | ||
} | ||
|
||
public static create(options: TVLCanisterOptions) { | ||
const agent = options.agent; | ||
const canisterId = options.canisterId; | ||
|
||
const service = Actor.createActor<TVLService>(idlFactory, { | ||
agent, | ||
canisterId, | ||
}); | ||
|
||
const certifiedService = Actor.createActor<TVLService>( | ||
certifiedIdlFactory, | ||
{ | ||
agent, | ||
canisterId, | ||
} | ||
); | ||
|
||
return new TVLCanister(service, certifiedService); | ||
} | ||
|
||
private caller = ({ certified = true }: { certified: boolean }): TVLService => | ||
certified ? this.certifiedService : this.service; | ||
|
||
public getTVL = async (params: { | ||
certified: boolean; | ||
}): Promise<TvlResult> => { | ||
const response = await this.caller(params).get_tvl(); | ||
|
||
if ("Err" in response) { | ||
throw new Error(response.Err.message); | ||
} | ||
|
||
return response.Ok; | ||
}; | ||
} |
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,9 @@ | ||
import type { Agent } from "@dfinity/agent"; | ||
import type { Principal } from "@dfinity/principal"; | ||
|
||
export interface TVLCanisterOptions { | ||
// The agent to use when communicating with the governance canister. | ||
agent: Agent; | ||
// The TVL canister's ID. | ||
canisterId: Principal; | ||
} |
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,2 @@ | ||
import type { IDL } from "@dfinity/candid"; | ||
export const idlFactory: IDL.InterfaceFactory; |
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,33 @@ | ||
/* Do not edit. Compiled with ./scripts/compile-idl-js from packages/tvl/candid/tvl.did */ | ||
export const idlFactory = ({ IDL }) => { | ||
const InitArgs = IDL.Record({ | ||
governance_id: IDL.Principal, | ||
update_period: IDL.Nat64, | ||
xrc_id: IDL.Principal, | ||
}); | ||
const TimeseriesEntry = IDL.Record({ | ||
value: IDL.Nat, | ||
time_sec: IDL.Nat, | ||
}); | ||
const TimeseriesResult = IDL.Record({ | ||
timeseries: IDL.Vec(TimeseriesEntry), | ||
}); | ||
const TvlResult = IDL.Record({ tvl: IDL.Nat, time_sec: IDL.Nat }); | ||
const TvlResultError = IDL.Record({ message: IDL.Text }); | ||
const Result_tvl = IDL.Variant({ Ok: TvlResult, Err: TvlResultError }); | ||
const TvlTimeseriesResult = IDL.Record({ timeseries: IDL.Vec(TvlResult) }); | ||
return IDL.Service({ | ||
get_locked_e8s_timeseries: IDL.Func([], [TimeseriesResult], []), | ||
get_tvl: IDL.Func([], [Result_tvl], []), | ||
get_tvl_timeseries: IDL.Func([], [TvlTimeseriesResult], []), | ||
get_xr_timeseries: IDL.Func([], [TimeseriesResult], []), | ||
}); | ||
}; | ||
export const init = ({ IDL }) => { | ||
const InitArgs = IDL.Record({ | ||
governance_id: IDL.Principal, | ||
update_period: IDL.Nat64, | ||
xrc_id: IDL.Principal, | ||
}); | ||
return [InitArgs]; | ||
}; |
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,36 @@ | ||
type InitArgs = record { | ||
governance_id : principal; | ||
xrc_id : principal; | ||
update_period: nat64; | ||
}; | ||
|
||
type TimeseriesEntry = record { | ||
time_sec: nat; | ||
value: nat; | ||
}; | ||
|
||
type TimeseriesResult = record { | ||
timeseries: vec TimeseriesEntry; | ||
}; | ||
|
||
type TvlResult = record { | ||
time_sec: nat; | ||
tvl: nat; | ||
}; | ||
|
||
type TvlTimeseriesResult = record { | ||
timeseries: vec TvlResult; | ||
}; | ||
|
||
type TvlResultError = record { | ||
message: text; | ||
}; | ||
|
||
type Result_tvl = variant { Ok : TvlResult; Err : TvlResultError }; | ||
|
||
service : (InitArgs) -> { | ||
get_tvl : () -> (Result_tvl) query; | ||
get_tvl_timeseries : () -> (TvlTimeseriesResult); | ||
get_xr_timeseries : () -> (TimeseriesResult); | ||
get_locked_e8s_timeseries : () -> (TimeseriesResult); | ||
} |
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,2 @@ | ||
import type { IDL } from "@dfinity/candid"; | ||
export const idlFactory: IDL.InterfaceFactory; |
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,33 @@ | ||
/* Do not edit. Compiled with ./scripts/compile-idl-js from packages/tvl/candid/tvl.did */ | ||
export const idlFactory = ({ IDL }) => { | ||
const InitArgs = IDL.Record({ | ||
governance_id: IDL.Principal, | ||
update_period: IDL.Nat64, | ||
xrc_id: IDL.Principal, | ||
}); | ||
const TimeseriesEntry = IDL.Record({ | ||
value: IDL.Nat, | ||
time_sec: IDL.Nat, | ||
}); | ||
const TimeseriesResult = IDL.Record({ | ||
timeseries: IDL.Vec(TimeseriesEntry), | ||
}); | ||
const TvlResult = IDL.Record({ tvl: IDL.Nat, time_sec: IDL.Nat }); | ||
const TvlResultError = IDL.Record({ message: IDL.Text }); | ||
const Result_tvl = IDL.Variant({ Ok: TvlResult, Err: TvlResultError }); | ||
const TvlTimeseriesResult = IDL.Record({ timeseries: IDL.Vec(TvlResult) }); | ||
return IDL.Service({ | ||
get_locked_e8s_timeseries: IDL.Func([], [TimeseriesResult], []), | ||
get_tvl: IDL.Func([], [Result_tvl], ["query"]), | ||
get_tvl_timeseries: IDL.Func([], [TvlTimeseriesResult], []), | ||
get_xr_timeseries: IDL.Func([], [TimeseriesResult], []), | ||
}); | ||
}; | ||
export const init = ({ IDL }) => { | ||
const InitArgs = IDL.Record({ | ||
governance_id: IDL.Principal, | ||
update_period: IDL.Nat64, | ||
xrc_id: IDL.Principal, | ||
}); | ||
return [InitArgs]; | ||
}; |
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,32 @@ | ||
import type { ActorMethod } from "@dfinity/agent"; | ||
import type { Principal } from "@dfinity/principal"; | ||
|
||
export interface InitArgs { | ||
governance_id: Principal; | ||
update_period: bigint; | ||
xrc_id: Principal; | ||
} | ||
export type Result_tvl = { Ok: TvlResult } | { Err: TvlResultError }; | ||
export interface TimeseriesEntry { | ||
value: bigint; | ||
time_sec: bigint; | ||
} | ||
export interface TimeseriesResult { | ||
timeseries: Array<TimeseriesEntry>; | ||
} | ||
export interface TvlResult { | ||
tvl: bigint; | ||
time_sec: bigint; | ||
} | ||
export interface TvlResultError { | ||
message: string; | ||
} | ||
export interface TvlTimeseriesResult { | ||
timeseries: Array<TvlResult>; | ||
} | ||
export interface _SERVICE { | ||
get_locked_e8s_timeseries: ActorMethod<[], TimeseriesResult>; | ||
get_tvl: ActorMethod<[], Result_tvl>; | ||
get_tvl_timeseries: ActorMethod<[], TvlTimeseriesResult>; | ||
get_xr_timeseries: ActorMethod<[], TimeseriesResult>; | ||
} |
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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { SECONDS_IN_MINUTE } from "$lib/constants/constants"; | ||
|
||
// Workers | ||
export const SYNC_METRICS_TIMER_INTERVAL = SECONDS_IN_MINUTE * 1000; // 1 minute | ||
// Workers periodicity | ||
// 60 minutes - i.e. currently longer than a session therefore not refreshed | ||
// We might revert this to a more dynamic data in the future e.g. every minute, that's why we keep the feature | ||
export const SYNC_METRICS_TIMER_INTERVAL = SECONDS_IN_MINUTE * 60000; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.