Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get oracle price util #214

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export { getSupportedMessageAdapters } from "./common/utils/adapter.js";
export { convertFromGenericAddress, convertToGenericAddress } from "./common/utils/address.js";
export { buildAccountId, buildLoanId } from "./common/utils/lending.js";
export { getAdapterAddress } from "./common/utils/chain.js";
export { toFAmount, toUnderlyingAmount } from "./common/utils/formulae.js";
export { toFAmount, toUnderlyingAmount, calcAssetDollarValue } from "./common/utils/formulae.js";
export { getCcipData, getWormholeData } from "./common/utils/gmp.js";
export { getOperationIdsByTransaction, waitOperationIds } from "./common/utils/messages.js";
export { waitTransaction } from "./common/utils/transaction.js";
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
Loading