Skip to content

Commit

Permalink
feat: get oracle price util
Browse files Browse the repository at this point in the history
  • Loading branch information
palace22 committed Jan 22, 2025
1 parent 02dbb86 commit 81eaee1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-items-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@folks-finance/xchain-sdk": patch
---

Added get oracle price util
5 changes: 5 additions & 0 deletions .changeset/sharp-ladybugs-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@folks-finance/xchain-sdk": patch
---

Refactored get oracle prices to accept an array
11 changes: 10 additions & 1 deletion src/chains/evm/hub/modules/folks-hub-oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import { getOracleManagerContract } from "../utils/contract.js";

import type { NetworkType } from "../../../../common/types/chain.js";
import type { OracleManagerAbi } from "../constants/abi/oracle-manager-abi.js";
import type { OraclePrices } from "../types/oracle.js";
import type { OraclePrice, OraclePrices } from "../types/oracle.js";
import type { HubTokenData } from "../types/token.js";
import type { Client, ContractFunctionParameters, ReadContractReturnType } from "viem";

export async function getOraclePrice(provider: Client, network: NetworkType, poolId: number): Promise<OraclePrice> {
const hubChain = getHubChain(network);

const oracleManager = getOracleManagerContract(provider, hubChain.oracleManagerAddress);

const { price, decimals } = await oracleManager.read.processPriceFeed([poolId]);
return { price: [price, 18], decimals };
}

export async function getOraclePrices(
provider: Client,
network: NetworkType,
Expand Down
19 changes: 15 additions & 4 deletions src/xchain/modules/folks-oracle.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { FolksHubOracle } from "../../chains/evm/hub/modules/index.js";
import { getHubTokensData } from "../../chains/evm/hub/utils/chain.js";
import { getHubTokenData, getHubTokensData } from "../../chains/evm/hub/utils/chain.js";
import { FolksCore } from "../core/folks-core.js";

import type { OraclePrices } from "../../chains/evm/hub/types/oracle.js";
import type { OraclePrice, OraclePrices } from "../../chains/evm/hub/types/oracle.js";
import type { FolksTokenId } from "../../common/types/token.js";

export const read = {
async oraclePrices(): Promise<OraclePrices> {
async oraclePrice(folksTokenId: FolksTokenId): Promise<OraclePrice> {
const network = FolksCore.getSelectedNetwork();

const tokensData = Object.values(getHubTokensData(network));
const { poolId } = getHubTokenData(folksTokenId, network);

return await FolksHubOracle.getOraclePrice(FolksCore.getHubProvider(), network, poolId);
},

async oraclePrices(folksTokenIds?: Array<FolksTokenId>): Promise<OraclePrices> {
const network = FolksCore.getSelectedNetwork();

const tokensData = folksTokenIds
? folksTokenIds.map((folksTokenId) => getHubTokenData(folksTokenId, network))
: Object.values(getHubTokensData(network));

return FolksHubOracle.getOraclePrices(FolksCore.getHubProvider(), network, tokensData);
},
Expand Down

0 comments on commit 81eaee1

Please sign in to comment.