Skip to content

Commit cd4eaff

Browse files
authored
feat: get oracle price util (#214)
1 parent 02dbb86 commit cd4eaff

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

.changeset/early-items-perform.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@folks-finance/xchain-sdk": patch
3+
---
4+
5+
Added get oracle price util

.changeset/sharp-ladybugs-study.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@folks-finance/xchain-sdk": patch
3+
---
4+
5+
Refactored get oracle prices to accept an array

src/chains/evm/hub/modules/folks-hub-oracle.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@ import { getOracleManagerContract } from "../utils/contract.js";
55

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

12+
export async function getOraclePrice(provider: Client, network: NetworkType, poolId: number): Promise<OraclePrice> {
13+
const hubChain = getHubChain(network);
14+
15+
const oracleManager = getOracleManagerContract(provider, hubChain.oracleManagerAddress);
16+
17+
const { price, decimals } = await oracleManager.read.processPriceFeed([poolId]);
18+
return { price: [price, 18], decimals };
19+
}
20+
1221
export async function getOraclePrices(
1322
provider: Client,
1423
network: NetworkType,

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export { getSupportedMessageAdapters } from "./common/utils/adapter.js";
2424
export { convertFromGenericAddress, convertToGenericAddress } from "./common/utils/address.js";
2525
export { buildAccountId, buildLoanId } from "./common/utils/lending.js";
2626
export { getAdapterAddress } from "./common/utils/chain.js";
27-
export { toFAmount, toUnderlyingAmount } from "./common/utils/formulae.js";
27+
export { toFAmount, toUnderlyingAmount, calcAssetDollarValue } from "./common/utils/formulae.js";
2828
export { getCcipData, getWormholeData } from "./common/utils/gmp.js";
2929
export { getOperationIdsByTransaction, waitOperationIds } from "./common/utils/messages.js";
3030
export { waitTransaction } from "./common/utils/transaction.js";

src/xchain/modules/folks-oracle.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import { FolksHubOracle } from "../../chains/evm/hub/modules/index.js";
2-
import { getHubTokensData } from "../../chains/evm/hub/utils/chain.js";
2+
import { getHubTokenData, getHubTokensData } from "../../chains/evm/hub/utils/chain.js";
33
import { FolksCore } from "../core/folks-core.js";
44

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

78
export const read = {
8-
async oraclePrices(): Promise<OraclePrices> {
9+
async oraclePrice(folksTokenId: FolksTokenId): Promise<OraclePrice> {
910
const network = FolksCore.getSelectedNetwork();
1011

11-
const tokensData = Object.values(getHubTokensData(network));
12+
const { poolId } = getHubTokenData(folksTokenId, network);
13+
14+
return await FolksHubOracle.getOraclePrice(FolksCore.getHubProvider(), network, poolId);
15+
},
16+
17+
async oraclePrices(folksTokenIds?: Array<FolksTokenId>): Promise<OraclePrices> {
18+
const network = FolksCore.getSelectedNetwork();
19+
20+
const tokensData = folksTokenIds
21+
? folksTokenIds.map((folksTokenId) => getHubTokenData(folksTokenId, network))
22+
: Object.values(getHubTokensData(network));
1223

1324
return FolksHubOracle.getOraclePrices(FolksCore.getHubProvider(), network, tokensData);
1425
},

0 commit comments

Comments
 (0)