-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additionally fetch exchange rates from ICPSwap (#7139)
- Loading branch information
Showing
13 changed files
with
175 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
10 changes: 10 additions & 0 deletions
10
frontend/openchat-agent/src/services/icpSwap/candid/can.did
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,10 @@ | ||
type PublicTokenOverview = | ||
record { | ||
address: text; | ||
priceUSD: float64; | ||
symbol: text; | ||
volumeUSD7d: float64; | ||
}; | ||
service : { | ||
getAllTokens: () -> (vec PublicTokenOverview) query; | ||
} |
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,7 @@ | ||
import type { IDL } from "@dfinity/candid"; | ||
import { _SERVICE } from "./types"; | ||
export { | ||
_SERVICE as ICPSwapService, | ||
}; | ||
|
||
export const idlFactory: IDL.InterfaceFactory; |
12 changes: 12 additions & 0 deletions
12
frontend/openchat-agent/src/services/icpSwap/candid/idl.js
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,12 @@ | ||
export const idlFactory = ({ IDL }) => { | ||
const PublicTokenOverview = IDL.Record({ | ||
'volumeUSD7d' : IDL.Float64, | ||
'address' : IDL.Text, | ||
'priceUSD' : IDL.Float64, | ||
'symbol' : IDL.Text, | ||
}); | ||
return IDL.Service({ | ||
'getAllTokens' : IDL.Func([], [IDL.Vec(PublicTokenOverview)], ['query']), | ||
}); | ||
}; | ||
export const init = ({ IDL }) => { return []; }; |
15 changes: 15 additions & 0 deletions
15
frontend/openchat-agent/src/services/icpSwap/candid/types.d.ts
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,15 @@ | ||
import type { Principal } from '@dfinity/principal'; | ||
import type { ActorMethod } from '@dfinity/agent'; | ||
import type { IDL } from '@dfinity/candid'; | ||
|
||
export interface PublicTokenOverview { | ||
'volumeUSD7d' : number, | ||
'address' : string, | ||
'priceUSD' : number, | ||
'symbol' : string, | ||
} | ||
export interface _SERVICE { | ||
'getAllTokens' : ActorMethod<[], Array<PublicTokenOverview>>, | ||
} | ||
export declare const idlFactory: IDL.InterfaceFactory; | ||
export declare const init: (args: { IDL: typeof IDL }) => IDL.Type[]; |
27 changes: 27 additions & 0 deletions
27
frontend/openchat-agent/src/services/icpSwap/icpSwapClient.ts
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,27 @@ | ||
import type { HttpAgent, Identity } from "@dfinity/agent"; | ||
import type { CryptocurrencyDetails, TokenExchangeRates } from "openchat-shared"; | ||
import { idlFactory, type ICPSwapService } from "./candid/idl"; | ||
import { CandidService } from "../candidService"; | ||
import { getAllTokensResponse } from "./mappers"; | ||
import type { ExchangeRateClient } from "../openchatAgent"; | ||
|
||
const ICPSWAP_CANISTER_ID = "ggzvv-5qaaa-aaaag-qck7a-cai"; | ||
|
||
export class IcpSwapClient extends CandidService implements ExchangeRateClient { | ||
private service: ICPSwapService; | ||
|
||
constructor(identity: Identity, agent: HttpAgent) { | ||
super(identity, agent, ICPSWAP_CANISTER_ID); | ||
|
||
this.service = this.createServiceClient<ICPSwapService>(idlFactory); | ||
} | ||
|
||
exchangeRates( | ||
supportedTokens: CryptocurrencyDetails[], | ||
): Promise<Record<string, TokenExchangeRates>> { | ||
return this.handleQueryResponse( | ||
() => this.service.getAllTokens(), | ||
(resp) => getAllTokensResponse(resp, supportedTokens), | ||
); | ||
} | ||
} |
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,20 @@ | ||
import { type CryptocurrencyDetails, type TokenExchangeRates } from "openchat-shared"; | ||
import type { PublicTokenOverview } from "./candid/types"; | ||
|
||
export function getAllTokensResponse( | ||
candid: Array<PublicTokenOverview>, | ||
supportedTokens: CryptocurrencyDetails[], | ||
): Record<string, TokenExchangeRates> { | ||
const exchangeRates: Record<string, TokenExchangeRates> = {}; | ||
const supportedLedgers = new Set<string>(supportedTokens.map((t) => t.ledger)); | ||
|
||
for (const token of candid) { | ||
if (supportedLedgers.has(token.address)) { | ||
exchangeRates[token.symbol.trim().toLowerCase()] = { | ||
toUSD: token.priceUSD, | ||
}; | ||
} | ||
} | ||
|
||
return exchangeRates; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function mean(arr: (number | undefined)[]): number { | ||
let total = 0; | ||
let count = 0; | ||
|
||
for (const value of arr) { | ||
if (value !== undefined) { | ||
total += value; | ||
count++; | ||
} | ||
} | ||
|
||
return count > 0 ? total / count : 0; | ||
} |
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