@mezo-org/passport 0.5.2-dev.4 → 0.5.2-dev.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -1
- package/dist/src/hooks/useAssetsConversionRates.js +60 -8
- package/dist/src/hooks/useAssetsConversionRates.js.map +1 -1
- package/dist/src/hooks/useBorrowData.d.ts +15 -0
- package/dist/src/hooks/useBorrowData.d.ts.map +1 -1
- package/dist/src/hooks/useBorrowData.js +25 -3
- package/dist/src/hooks/useBorrowData.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useAssetsConversionRates.ts +73 -9
- package/src/hooks/useBorrowData.ts +27 -5
- package/dist/src/hooks/useCollateralPrice.d.ts +0 -17
- package/dist/src/hooks/useCollateralPrice.d.ts.map +0 -1
- package/dist/src/hooks/useCollateralPrice.js +0 -56
- package/dist/src/hooks/useCollateralPrice.js.map +0 -1
- package/src/hooks/useCollateralPrice.ts +0 -64
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAssetsConversionRates.d.ts","sourceRoot":"","sources":["../../../src/hooks/useAssetsConversionRates.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useAssetsConversionRates.d.ts","sourceRoot":"","sources":["../../../src/hooks/useAssetsConversionRates.ts"],"names":[],"mappings":"AA8BA,eAAO,MAAM,wBAAwB,KAAK,CAAA;AAK1C,eAAO,MAAM,SAAS,mCAAmC,CAAA;AAEzD;;;;;;;GAOG;AACH,wBAAgB,wBAAwB;;;;;;UAoEvC"}
|
|
@@ -1,8 +1,28 @@
|
|
|
1
1
|
import { useQuery } from "@tanstack/react-query";
|
|
2
|
+
import { useConfig } from "wagmi";
|
|
3
|
+
import { readContract } from "wagmi/actions";
|
|
2
4
|
import { parseUnits } from "viem";
|
|
3
5
|
import { ONE_SECOND_MS } from "../utils/time";
|
|
4
6
|
import { usePortalApiClient } from "./usePortalApiClient";
|
|
5
|
-
import {
|
|
7
|
+
import { mainnetBorrowContracts, testnetBorrowContracts, } from "../lib/contracts";
|
|
8
|
+
import { CHAIN_ID } from "../constants";
|
|
9
|
+
import { usePassportContext } from "./usePassportContext";
|
|
10
|
+
import { normalizePrecision } from "../utils/numbers";
|
|
11
|
+
const PRICE_FEED_ABI = [
|
|
12
|
+
{
|
|
13
|
+
inputs: [],
|
|
14
|
+
name: "fetchPrice",
|
|
15
|
+
outputs: [
|
|
16
|
+
{
|
|
17
|
+
internalType: "uint256",
|
|
18
|
+
name: "",
|
|
19
|
+
type: "uint256",
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
stateMutability: "view",
|
|
23
|
+
type: "function",
|
|
24
|
+
},
|
|
25
|
+
];
|
|
6
26
|
export const CONVERSION_RATE_DECIMALS = 18;
|
|
7
27
|
// If the oracle has not been updated in at least 60 seconds, it is stale.
|
|
8
28
|
const MAX_PRICE_DELAY = 60 * ONE_SECOND_MS;
|
|
@@ -17,24 +37,56 @@ export const QUERY_KEY = "passport.assetsConversionRates";
|
|
|
17
37
|
*/
|
|
18
38
|
export function useAssetsConversionRates() {
|
|
19
39
|
const portalApiClient = usePortalApiClient();
|
|
20
|
-
const {
|
|
21
|
-
const
|
|
40
|
+
const { environment = "mainnet" } = usePassportContext();
|
|
41
|
+
const config = useConfig();
|
|
22
42
|
return useQuery({
|
|
23
|
-
queryKey: [QUERY_KEY
|
|
24
|
-
queryFn: async () =>
|
|
25
|
-
|
|
26
|
-
|
|
43
|
+
queryKey: [QUERY_KEY],
|
|
44
|
+
queryFn: async () => {
|
|
45
|
+
const { chainId } = config.state;
|
|
46
|
+
const isMainnet = chainId === CHAIN_ID.mainnet;
|
|
47
|
+
const { address: priceFeedAddress } = isMainnet
|
|
48
|
+
? mainnetBorrowContracts.PriceFeed
|
|
49
|
+
: testnetBorrowContracts.PriceFeed;
|
|
50
|
+
return Promise.all([
|
|
51
|
+
portalApiClient.getPortalStatistics(),
|
|
52
|
+
readContract(config, {
|
|
53
|
+
address: priceFeedAddress,
|
|
54
|
+
abi: PRICE_FEED_ABI,
|
|
55
|
+
functionName: "fetchPrice",
|
|
56
|
+
chainId: CHAIN_ID[environment],
|
|
57
|
+
}),
|
|
58
|
+
]);
|
|
59
|
+
},
|
|
60
|
+
select: ([portalStatistics, fetchedPriceFeed]) => {
|
|
27
61
|
const tTokenConversionRate = portalStatistics.tTokenPrice;
|
|
62
|
+
const btcConversionRate = fetchedPriceFeed;
|
|
28
63
|
const tPrice = parseUnits(tTokenConversionRate.toString(), CONVERSION_RATE_DECIMALS);
|
|
64
|
+
const btcPrice = normalizePrecision(btcConversionRate, CONVERSION_RATE_DECIMALS);
|
|
29
65
|
return {
|
|
30
66
|
rates: {
|
|
31
67
|
mT: tPrice,
|
|
32
|
-
BTC:
|
|
68
|
+
BTC: btcPrice,
|
|
33
69
|
},
|
|
34
70
|
decimals: CONVERSION_RATE_DECIMALS,
|
|
35
71
|
};
|
|
36
72
|
},
|
|
73
|
+
// TODO: Investigate optimised strategy for calculating refetch interval as
|
|
74
|
+
// done with price oracle contract. Left previous implementation for
|
|
75
|
+
// reference.
|
|
37
76
|
refetchInterval: MAX_PRICE_DELAY,
|
|
77
|
+
// refetchInterval: ({ state }) => {
|
|
78
|
+
// if (!state.data) return MAX_PRICE_DELAY
|
|
79
|
+
// const [, btcPriceData] = state.data
|
|
80
|
+
// const [, , , updatedAt] = btcPriceData
|
|
81
|
+
// if (!updatedAt) return MAX_PRICE_DELAY
|
|
82
|
+
// const age = Date.now() - Number(updatedAt) * 1000
|
|
83
|
+
// return age > MAX_PRICE_DELAY
|
|
84
|
+
// ? // If the data is stale, we want to refetch as soon as possible, but
|
|
85
|
+
// // not faster than once per second, to avoid excessive requests.
|
|
86
|
+
// // Math.max ensures the interval is at least 1 second.
|
|
87
|
+
// Math.max(ONE_SECOND_MS, MAX_PRICE_DELAY - age)
|
|
88
|
+
// : MAX_PRICE_DELAY - age
|
|
89
|
+
// },
|
|
38
90
|
});
|
|
39
91
|
}
|
|
40
92
|
//# sourceMappingURL=useAssetsConversionRates.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAssetsConversionRates.js","sourceRoot":"","sources":["../../../src/hooks/useAssetsConversionRates.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAA;AACjC,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"useAssetsConversionRates.js","sourceRoot":"","sources":["../../../src/hooks/useAssetsConversionRates.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAA;AACjC,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EACL,sBAAsB,EACtB,sBAAsB,GACvB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AAErD,MAAM,cAAc,GAAG;IACrB;QACE,MAAM,EAAE,EAAE;QACV,IAAI,EAAE,YAAY;QAClB,OAAO,EAAE;YACP;gBACE,YAAY,EAAE,SAAS;gBACvB,IAAI,EAAE,EAAE;gBACR,IAAI,EAAE,SAAS;aAChB;S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AA;YAE1C,MAAM,MAAM,GAAG,UAAU,CACvB,oBAAoB,CAAC,QAAQ,EAAE,EAC/B,wBAAwB,CACzB,CAAA;YAED,MAAM,QAAQ,GAAG,kBAAkB,CACjC,iBAAiB,EACjB,wBAAwB,CACzB,CAAA;YAED,OAAO;gBACL,KAAK,EAAE;oBACL,EAAE,EAAE,MAAM;oBACV,GAAG,EAAE,QAAQ;iBACd;gBACD,QAAQ,EAAE,wBAAwB;aACnC,CAAA;QACH,CAAC;QACD,2EAA2E;QAC3E,oEAAoE;QACpE,aAAa;QACb,eAAe,EAAE,eAAe;QAChC,oCAAoC;QACpC,4CAA4C;QAE5C,wCAAwC;QACxC,2CAA2C;QAE3C,2CAA2C;QAE3C,sDAAsD;QAEtD,iCAAiC;QACjC,6EAA6E;QAC7E,yEAAyE;QACzE,+DAA+D;QAC/D,uDAAuD;QACvD,8BAA8B;QAC9B,KAAK;KACN,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -58,6 +58,21 @@ export declare function useBorrowData(queryOptions?: {}): import("wagmi").UseRea
|
|
|
58
58
|
readonly address: `0x${string}`;
|
|
59
59
|
readonly functionName: "MUSD_GAS_COMPENSATION";
|
|
60
60
|
readonly chainId: number;
|
|
61
|
+
}, {
|
|
62
|
+
readonly abi: readonly [{
|
|
63
|
+
readonly inputs: readonly [];
|
|
64
|
+
readonly name: "fetchPrice";
|
|
65
|
+
readonly outputs: readonly [{
|
|
66
|
+
readonly internalType: "uint256";
|
|
67
|
+
readonly name: "";
|
|
68
|
+
readonly type: "uint256";
|
|
69
|
+
}];
|
|
70
|
+
readonly stateMutability: "view";
|
|
71
|
+
readonly type: "function";
|
|
72
|
+
}];
|
|
73
|
+
readonly address: `0x${string}`;
|
|
74
|
+
readonly functionName: "fetchPrice";
|
|
75
|
+
readonly chainId: number;
|
|
61
76
|
}], true, {
|
|
62
77
|
collateral: {
|
|
63
78
|
value: bigint;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useBorrowData.d.ts","sourceRoot":"","sources":["../../../src/hooks/useBorrowData.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useBorrowData.d.ts","sourceRoot":"","sources":["../../../src/hooks/useBorrowData.ts"],"names":[],"mappings":"AAuGA;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,YAAY,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiG9C;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB;;EAatC;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB;;EAajC"}
|
|
@@ -10,7 +10,6 @@ import { bigIntMax, normalizePrecision } from "../utils/numbers";
|
|
|
10
10
|
import useWalletAccount from "./useWalletAccount";
|
|
11
11
|
import { getAsset } from "../utils/assets";
|
|
12
12
|
import { convertToUsd } from "../utils/currency";
|
|
13
|
-
import { useCollateralPrice } from "./useCollateralPrice";
|
|
14
13
|
const DEBT_AND_COLL_PRECISION = 18;
|
|
15
14
|
// Wagmi handles typesafety with ABI const assertions. TypeScript doesn't
|
|
16
15
|
// support importing JSON as const yet so types cannot be inferred from the
|
|
@@ -78,6 +77,21 @@ const BORROWER_OPERATIONS_ABI = [
|
|
|
78
77
|
type: "function",
|
|
79
78
|
},
|
|
80
79
|
];
|
|
80
|
+
const PRICE_FEED_ABI = [
|
|
81
|
+
{
|
|
82
|
+
inputs: [],
|
|
83
|
+
name: "fetchPrice",
|
|
84
|
+
outputs: [
|
|
85
|
+
{
|
|
86
|
+
internalType: "uint256",
|
|
87
|
+
name: "",
|
|
88
|
+
type: "uint256",
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
stateMutability: "view",
|
|
92
|
+
type: "function",
|
|
93
|
+
},
|
|
94
|
+
];
|
|
81
95
|
/**
|
|
82
96
|
* Query hook for getting borrow data. Returns collateral and trove debt for the
|
|
83
97
|
* connected account, based on it's evm address.
|
|
@@ -86,17 +100,18 @@ const BORROWER_OPERATIONS_ABI = [
|
|
|
86
100
|
export function useBorrowData(queryOptions = {}) {
|
|
87
101
|
const { environment = "mainnet" } = usePassportContext();
|
|
88
102
|
const walletAccount = useWalletAccount();
|
|
89
|
-
const { data: collateralPrice } = useCollateralPrice();
|
|
90
103
|
const contractAddress = useMemo(() => {
|
|
91
104
|
if (environment === "mainnet") {
|
|
92
105
|
return {
|
|
93
106
|
troveManager: mainnetBorrowContracts.TroveManager.address,
|
|
94
107
|
borrowerOperations: mainnetBorrowContracts.BorrowerOperations.address,
|
|
108
|
+
priceFeed: mainnetBorrowContracts.PriceFeed.address,
|
|
95
109
|
};
|
|
96
110
|
}
|
|
97
111
|
return {
|
|
98
112
|
troveManager: testnetBorrowContracts.TroveManager.address,
|
|
99
113
|
borrowerOperations: testnetBorrowContracts.BorrowerOperations.address,
|
|
114
|
+
priceFeed: testnetBorrowContracts.PriceFeed.address,
|
|
100
115
|
};
|
|
101
116
|
}, [environment]);
|
|
102
117
|
const chainId = CHAIN_ID[environment];
|
|
@@ -117,9 +132,15 @@ export function useBorrowData(queryOptions = {}) {
|
|
|
117
132
|
functionName: "MUSD_GAS_COMPENSATION",
|
|
118
133
|
chainId,
|
|
119
134
|
},
|
|
135
|
+
{
|
|
136
|
+
abi: PRICE_FEED_ABI,
|
|
137
|
+
address: contractAddress.priceFeed,
|
|
138
|
+
functionName: "fetchPrice",
|
|
139
|
+
chainId,
|
|
140
|
+
},
|
|
120
141
|
],
|
|
121
142
|
query: {
|
|
122
|
-
enabled: !!walletAccount.accountAddress
|
|
143
|
+
enabled: !!walletAccount.accountAddress,
|
|
123
144
|
staleTime: 5 * ONE_MINUTE_MS,
|
|
124
145
|
retry: 1,
|
|
125
146
|
select: (data) => {
|
|
@@ -127,6 +148,7 @@ export function useBorrowData(queryOptions = {}) {
|
|
|
127
148
|
throw new Error("No borrow data available");
|
|
128
149
|
const [rawCollateral, principal, interest] = data[0].result;
|
|
129
150
|
const gasCompensation = data[1].result;
|
|
151
|
+
const collateralPrice = data[2].result;
|
|
130
152
|
const btcDetails = getAsset("BTC");
|
|
131
153
|
const collateral = {
|
|
132
154
|
value: rawCollateral,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useBorrowData.js","sourceRoot":"","sources":["../../../src/hooks/useBorrowData.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAA;AACxC,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,MAAM,CAAA;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAC7C,OAAO,EACL,sBAAsB,EACtB,sBAAsB,GACvB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AACvC,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AAChE,OAAO,gBAAgB,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"useBorrowData.js","sourceRoot":"","sources":["../../../src/hooks/useBorrowData.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAA;AACxC,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,MAAM,CAAA;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAC7C,OAAO,EACL,sBAA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C,CAAC,WAAW,EAAE,QAAQ,CAAC,CACxB,CAAA;IAED,OAAO;QACL,eAAe,EAAE,sBAAsB;KACxC,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,32 @@
|
|
|
1
1
|
import { useQuery } from "@tanstack/react-query"
|
|
2
|
+
import { useConfig } from "wagmi"
|
|
3
|
+
import { readContract } from "wagmi/actions"
|
|
2
4
|
import { parseUnits } from "viem"
|
|
3
5
|
import { ONE_SECOND_MS } from "../utils/time"
|
|
4
6
|
import { usePortalApiClient } from "./usePortalApiClient"
|
|
5
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
mainnetBorrowContracts,
|
|
9
|
+
testnetBorrowContracts,
|
|
10
|
+
} from "../lib/contracts"
|
|
11
|
+
import { CHAIN_ID } from "../constants"
|
|
12
|
+
import { usePassportContext } from "./usePassportContext"
|
|
13
|
+
import { normalizePrecision } from "../utils/numbers"
|
|
14
|
+
|
|
15
|
+
const PRICE_FEED_ABI = [
|
|
16
|
+
{
|
|
17
|
+
inputs: [],
|
|
18
|
+
name: "fetchPrice",
|
|
19
|
+
outputs: [
|
|
20
|
+
{
|
|
21
|
+
internalType: "uint256",
|
|
22
|
+
name: "",
|
|
23
|
+
type: "uint256",
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
stateMutability: "view",
|
|
27
|
+
type: "function",
|
|
28
|
+
},
|
|
29
|
+
] as const
|
|
6
30
|
|
|
7
31
|
export const CONVERSION_RATE_DECIMALS = 18
|
|
8
32
|
|
|
@@ -21,30 +45,70 @@ export const QUERY_KEY = "passport.assetsConversionRates"
|
|
|
21
45
|
*/
|
|
22
46
|
export function useAssetsConversionRates() {
|
|
23
47
|
const portalApiClient = usePortalApiClient()
|
|
24
|
-
const {
|
|
25
|
-
|
|
26
|
-
const collateralPrice = collateralPriceData?.toString()
|
|
48
|
+
const { environment = "mainnet" } = usePassportContext()
|
|
49
|
+
const config = useConfig()
|
|
27
50
|
|
|
28
51
|
return useQuery({
|
|
29
|
-
queryKey: [QUERY_KEY
|
|
30
|
-
queryFn: async () =>
|
|
31
|
-
|
|
32
|
-
|
|
52
|
+
queryKey: [QUERY_KEY],
|
|
53
|
+
queryFn: async () => {
|
|
54
|
+
const { chainId } = config.state
|
|
55
|
+
const isMainnet = chainId === CHAIN_ID.mainnet
|
|
56
|
+
const { address: priceFeedAddress } = isMainnet
|
|
57
|
+
? mainnetBorrowContracts.PriceFeed
|
|
58
|
+
: testnetBorrowContracts.PriceFeed
|
|
59
|
+
|
|
60
|
+
return Promise.all([
|
|
61
|
+
portalApiClient.getPortalStatistics(),
|
|
62
|
+
readContract(config, {
|
|
63
|
+
address: priceFeedAddress,
|
|
64
|
+
abi: PRICE_FEED_ABI,
|
|
65
|
+
functionName: "fetchPrice",
|
|
66
|
+
chainId: CHAIN_ID[environment],
|
|
67
|
+
}),
|
|
68
|
+
])
|
|
69
|
+
},
|
|
70
|
+
select: ([portalStatistics, fetchedPriceFeed]) => {
|
|
33
71
|
const tTokenConversionRate = portalStatistics.tTokenPrice
|
|
72
|
+
const btcConversionRate = fetchedPriceFeed
|
|
34
73
|
|
|
35
74
|
const tPrice = parseUnits(
|
|
36
75
|
tTokenConversionRate.toString(),
|
|
37
76
|
CONVERSION_RATE_DECIMALS,
|
|
38
77
|
)
|
|
39
78
|
|
|
79
|
+
const btcPrice = normalizePrecision(
|
|
80
|
+
btcConversionRate,
|
|
81
|
+
CONVERSION_RATE_DECIMALS,
|
|
82
|
+
)
|
|
83
|
+
|
|
40
84
|
return {
|
|
41
85
|
rates: {
|
|
42
86
|
mT: tPrice,
|
|
43
|
-
BTC:
|
|
87
|
+
BTC: btcPrice,
|
|
44
88
|
},
|
|
45
89
|
decimals: CONVERSION_RATE_DECIMALS,
|
|
46
90
|
}
|
|
47
91
|
},
|
|
92
|
+
// TODO: Investigate optimised strategy for calculating refetch interval as
|
|
93
|
+
// done with price oracle contract. Left previous implementation for
|
|
94
|
+
// reference.
|
|
48
95
|
refetchInterval: MAX_PRICE_DELAY,
|
|
96
|
+
// refetchInterval: ({ state }) => {
|
|
97
|
+
// if (!state.data) return MAX_PRICE_DELAY
|
|
98
|
+
|
|
99
|
+
// const [, btcPriceData] = state.data
|
|
100
|
+
// const [, , , updatedAt] = btcPriceData
|
|
101
|
+
|
|
102
|
+
// if (!updatedAt) return MAX_PRICE_DELAY
|
|
103
|
+
|
|
104
|
+
// const age = Date.now() - Number(updatedAt) * 1000
|
|
105
|
+
|
|
106
|
+
// return age > MAX_PRICE_DELAY
|
|
107
|
+
// ? // If the data is stale, we want to refetch as soon as possible, but
|
|
108
|
+
// // not faster than once per second, to avoid excessive requests.
|
|
109
|
+
// // Math.max ensures the interval is at least 1 second.
|
|
110
|
+
// Math.max(ONE_SECOND_MS, MAX_PRICE_DELAY - age)
|
|
111
|
+
// : MAX_PRICE_DELAY - age
|
|
112
|
+
// },
|
|
49
113
|
})
|
|
50
114
|
}
|
|
@@ -13,7 +13,6 @@ import { bigIntMax, normalizePrecision } from "../utils/numbers"
|
|
|
13
13
|
import useWalletAccount from "./useWalletAccount"
|
|
14
14
|
import { getAsset } from "../utils/assets"
|
|
15
15
|
import { convertToUsd } from "../utils/currency"
|
|
16
|
-
import { useCollateralPrice } from "./useCollateralPrice"
|
|
17
16
|
|
|
18
17
|
const DEBT_AND_COLL_PRECISION = 18
|
|
19
18
|
|
|
@@ -86,6 +85,22 @@ const BORROWER_OPERATIONS_ABI = [
|
|
|
86
85
|
},
|
|
87
86
|
] as const
|
|
88
87
|
|
|
88
|
+
const PRICE_FEED_ABI = [
|
|
89
|
+
{
|
|
90
|
+
inputs: [],
|
|
91
|
+
name: "fetchPrice",
|
|
92
|
+
outputs: [
|
|
93
|
+
{
|
|
94
|
+
internalType: "uint256",
|
|
95
|
+
name: "",
|
|
96
|
+
type: "uint256",
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
stateMutability: "view",
|
|
100
|
+
type: "function",
|
|
101
|
+
},
|
|
102
|
+
] as const
|
|
103
|
+
|
|
89
104
|
/**
|
|
90
105
|
* Query hook for getting borrow data. Returns collateral and trove debt for the
|
|
91
106
|
* connected account, based on it's evm address.
|
|
@@ -95,18 +110,18 @@ export function useBorrowData(queryOptions = {}) {
|
|
|
95
110
|
const { environment = "mainnet" } = usePassportContext()
|
|
96
111
|
const walletAccount = useWalletAccount()
|
|
97
112
|
|
|
98
|
-
const { data: collateralPrice } = useCollateralPrice()
|
|
99
|
-
|
|
100
113
|
const contractAddress = useMemo(() => {
|
|
101
114
|
if (environment === "mainnet") {
|
|
102
115
|
return {
|
|
103
116
|
troveManager: mainnetBorrowContracts.TroveManager.address,
|
|
104
117
|
borrowerOperations: mainnetBorrowContracts.BorrowerOperations.address,
|
|
118
|
+
priceFeed: mainnetBorrowContracts.PriceFeed.address,
|
|
105
119
|
}
|
|
106
120
|
}
|
|
107
121
|
return {
|
|
108
122
|
troveManager: testnetBorrowContracts.TroveManager.address,
|
|
109
123
|
borrowerOperations: testnetBorrowContracts.BorrowerOperations.address,
|
|
124
|
+
priceFeed: testnetBorrowContracts.PriceFeed.address,
|
|
110
125
|
}
|
|
111
126
|
}, [environment])
|
|
112
127
|
|
|
@@ -129,9 +144,15 @@ export function useBorrowData(queryOptions = {}) {
|
|
|
129
144
|
functionName: "MUSD_GAS_COMPENSATION",
|
|
130
145
|
chainId,
|
|
131
146
|
},
|
|
147
|
+
{
|
|
148
|
+
abi: PRICE_FEED_ABI,
|
|
149
|
+
address: contractAddress.priceFeed,
|
|
150
|
+
functionName: "fetchPrice",
|
|
151
|
+
chainId,
|
|
152
|
+
},
|
|
132
153
|
],
|
|
133
154
|
query: {
|
|
134
|
-
enabled: !!walletAccount.accountAddress
|
|
155
|
+
enabled: !!walletAccount.accountAddress,
|
|
135
156
|
staleTime: 5 * ONE_MINUTE_MS,
|
|
136
157
|
retry: 1,
|
|
137
158
|
select: (data) => {
|
|
@@ -140,6 +161,7 @@ export function useBorrowData(queryOptions = {}) {
|
|
|
140
161
|
|
|
141
162
|
const [rawCollateral, principal, interest] = data[0].result!
|
|
142
163
|
const gasCompensation = data[1].result!
|
|
164
|
+
const collateralPrice = data[2].result!
|
|
143
165
|
|
|
144
166
|
const btcDetails = getAsset("BTC")
|
|
145
167
|
|
|
@@ -151,7 +173,7 @@ export function useBorrowData(queryOptions = {}) {
|
|
|
151
173
|
usd: convertToUsd(
|
|
152
174
|
rawCollateral,
|
|
153
175
|
DEBT_AND_COLL_PRECISION,
|
|
154
|
-
collateralPrice
|
|
176
|
+
collateralPrice,
|
|
155
177
|
DEBT_AND_COLL_PRECISION,
|
|
156
178
|
),
|
|
157
179
|
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Query hook for getting borrow data. Returns collateral and trove debt for the
|
|
3
|
-
* connected account, based on it's evm address.
|
|
4
|
-
* @param queryOptions Query options passed to the underlying `useQuery` hook.
|
|
5
|
-
*/
|
|
6
|
-
export declare function useCollateralPrice(queryOptions?: {}): import("wagmi").UseReadContractReturnType<readonly [{
|
|
7
|
-
readonly inputs: readonly [];
|
|
8
|
-
readonly name: "fetchPrice";
|
|
9
|
-
readonly outputs: readonly [{
|
|
10
|
-
readonly internalType: "uint256";
|
|
11
|
-
readonly name: "";
|
|
12
|
-
readonly type: "uint256";
|
|
13
|
-
}];
|
|
14
|
-
readonly stateMutability: "view";
|
|
15
|
-
readonly type: "function";
|
|
16
|
-
}], "fetchPrice", readonly [], bigint>;
|
|
17
|
-
//# sourceMappingURL=useCollateralPrice.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useCollateralPrice.d.ts","sourceRoot":"","sources":["../../../src/hooks/useCollateralPrice.ts"],"names":[],"mappings":"AAiCA;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,YAAY,KAAK;;;;;;;;;;uCAyBnD"}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { useReadContract } from "wagmi";
|
|
2
|
-
import { useMemo } from "react";
|
|
3
|
-
import { ONE_MINUTE_MS } from "../utils/time";
|
|
4
|
-
import { mainnetBorrowContracts, testnetBorrowContracts, } from "../lib/contracts";
|
|
5
|
-
import { usePassportContext } from "./usePassportContext";
|
|
6
|
-
import { CHAIN_ID } from "../constants";
|
|
7
|
-
import useWalletAccount from "./useWalletAccount";
|
|
8
|
-
// Wagmi handles typesafety with ABI const assertions. TypeScript doesn't
|
|
9
|
-
// support importing JSON as const yet so types cannot be inferred from the
|
|
10
|
-
// imported contract. As a workaround there is minimal ABI definition that can
|
|
11
|
-
// be asserted types from.
|
|
12
|
-
// Ref: https://wagmi.sh/core/typescript#const-assert-abis-typed-data
|
|
13
|
-
const PRICE_FEED_ABI = [
|
|
14
|
-
{
|
|
15
|
-
inputs: [],
|
|
16
|
-
name: "fetchPrice",
|
|
17
|
-
outputs: [
|
|
18
|
-
{
|
|
19
|
-
internalType: "uint256",
|
|
20
|
-
name: "",
|
|
21
|
-
type: "uint256",
|
|
22
|
-
},
|
|
23
|
-
],
|
|
24
|
-
stateMutability: "view",
|
|
25
|
-
type: "function",
|
|
26
|
-
},
|
|
27
|
-
];
|
|
28
|
-
/**
|
|
29
|
-
* Query hook for getting borrow data. Returns collateral and trove debt for the
|
|
30
|
-
* connected account, based on it's evm address.
|
|
31
|
-
* @param queryOptions Query options passed to the underlying `useQuery` hook.
|
|
32
|
-
*/
|
|
33
|
-
export function useCollateralPrice(queryOptions = {}) {
|
|
34
|
-
const { environment = "mainnet" } = usePassportContext();
|
|
35
|
-
const walletAccount = useWalletAccount();
|
|
36
|
-
const priceFeedContractAddress = useMemo(() => {
|
|
37
|
-
if (environment === "mainnet") {
|
|
38
|
-
return mainnetBorrowContracts.PriceFeed.address;
|
|
39
|
-
}
|
|
40
|
-
return testnetBorrowContracts.PriceFeed.address;
|
|
41
|
-
}, [environment]);
|
|
42
|
-
const chainId = CHAIN_ID[environment];
|
|
43
|
-
return useReadContract({
|
|
44
|
-
abi: PRICE_FEED_ABI,
|
|
45
|
-
address: priceFeedContractAddress,
|
|
46
|
-
functionName: "fetchPrice",
|
|
47
|
-
chainId,
|
|
48
|
-
query: {
|
|
49
|
-
enabled: !!walletAccount.accountAddress,
|
|
50
|
-
staleTime: 5 * ONE_MINUTE_MS,
|
|
51
|
-
retry: 1,
|
|
52
|
-
...queryOptions,
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
//# sourceMappingURL=useCollateralPrice.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useCollateralPrice.js","sourceRoot":"","sources":["../../../src/hooks/useCollateralPrice.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,OAAO,CAAA;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAC/B,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAC7C,OAAO,EACL,sBAAsB,EACtB,sBAAsB,GACvB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AACvC,OAAO,gBAAgB,MAAM,oBAAoB,CAAA;AAEjD,yEAAyE;AACzE,2EAA2E;AAC3E,8EAA8E;AAC9E,0BAA0B;AAC1B,qEAAqE;AAErE,MAAM,cAAc,GAAG;IACrB;QACE,MAAM,EAAE,EAAE;QACV,IAAI,EAAE,YAAY;QAClB,OAAO,EAAE;YACP;gBACE,YAAY,EAAE,SAAS;gBACvB,IAAI,EAAE,EAAE;gBACR,IAAI,EAAE,SAAS;aAChB;SACF;QACD,eAAe,EAAE,MAAM;QACvB,IAAI,EAAE,UAAU;KACjB;CACO,CAAA;AAEV;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,YAAY,GAAG,EAAE;IAClD,MAAM,EAAE,WAAW,GAAG,SAAS,EAAE,GAAG,kBAAkB,EAAE,CAAA;IACxD,MAAM,aAAa,GAAG,gBAAgB,EAAE,CAAA;IAExC,MAAM,wBAAwB,GAAG,OAAO,CAAC,GAAG,EAAE;QAC5C,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;YAC9B,OAAO,sBAAsB,CAAC,SAAS,CAAC,OAAO,CAAA;QACjD,CAAC;QACD,OAAO,sBAAsB,CAAC,SAAS,CAAC,OAAO,CAAA;IACjD,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC,CAAA;IAEjB,MAAM,OAAO,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAA;IAErC,OAAO,eAAe,CAAC;QACrB,GAAG,EAAE,cAAc;QACnB,OAAO,EAAE,wBAAwB;QACjC,YAAY,EAAE,YAAY;QAC1B,OAAO;QACP,KAAK,EAAE;YACL,OAAO,EAAE,CAAC,CAAC,aAAa,CAAC,cAAc;YACvC,SAAS,EAAE,CAAC,GAAG,aAAa;YAC5B,KAAK,EAAE,CAAC;YACR,GAAG,YAAY;SAChB;KACF,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { useReadContract } from "wagmi"
|
|
2
|
-
import { useMemo } from "react"
|
|
3
|
-
import { ONE_MINUTE_MS } from "../utils/time"
|
|
4
|
-
import {
|
|
5
|
-
mainnetBorrowContracts,
|
|
6
|
-
testnetBorrowContracts,
|
|
7
|
-
} from "../lib/contracts"
|
|
8
|
-
import { usePassportContext } from "./usePassportContext"
|
|
9
|
-
import { CHAIN_ID } from "../constants"
|
|
10
|
-
import useWalletAccount from "./useWalletAccount"
|
|
11
|
-
|
|
12
|
-
// Wagmi handles typesafety with ABI const assertions. TypeScript doesn't
|
|
13
|
-
// support importing JSON as const yet so types cannot be inferred from the
|
|
14
|
-
// imported contract. As a workaround there is minimal ABI definition that can
|
|
15
|
-
// be asserted types from.
|
|
16
|
-
// Ref: https://wagmi.sh/core/typescript#const-assert-abis-typed-data
|
|
17
|
-
|
|
18
|
-
const PRICE_FEED_ABI = [
|
|
19
|
-
{
|
|
20
|
-
inputs: [],
|
|
21
|
-
name: "fetchPrice",
|
|
22
|
-
outputs: [
|
|
23
|
-
{
|
|
24
|
-
internalType: "uint256",
|
|
25
|
-
name: "",
|
|
26
|
-
type: "uint256",
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
stateMutability: "view",
|
|
30
|
-
type: "function",
|
|
31
|
-
},
|
|
32
|
-
] as const
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Query hook for getting borrow data. Returns collateral and trove debt for the
|
|
36
|
-
* connected account, based on it's evm address.
|
|
37
|
-
* @param queryOptions Query options passed to the underlying `useQuery` hook.
|
|
38
|
-
*/
|
|
39
|
-
export function useCollateralPrice(queryOptions = {}) {
|
|
40
|
-
const { environment = "mainnet" } = usePassportContext()
|
|
41
|
-
const walletAccount = useWalletAccount()
|
|
42
|
-
|
|
43
|
-
const priceFeedContractAddress = useMemo(() => {
|
|
44
|
-
if (environment === "mainnet") {
|
|
45
|
-
return mainnetBorrowContracts.PriceFeed.address
|
|
46
|
-
}
|
|
47
|
-
return testnetBorrowContracts.PriceFeed.address
|
|
48
|
-
}, [environment])
|
|
49
|
-
|
|
50
|
-
const chainId = CHAIN_ID[environment]
|
|
51
|
-
|
|
52
|
-
return useReadContract({
|
|
53
|
-
abi: PRICE_FEED_ABI,
|
|
54
|
-
address: priceFeedContractAddress,
|
|
55
|
-
functionName: "fetchPrice",
|
|
56
|
-
chainId,
|
|
57
|
-
query: {
|
|
58
|
-
enabled: !!walletAccount.accountAddress,
|
|
59
|
-
staleTime: 5 * ONE_MINUTE_MS,
|
|
60
|
-
retry: 1,
|
|
61
|
-
...queryOptions,
|
|
62
|
-
},
|
|
63
|
-
})
|
|
64
|
-
}
|