@mezo-org/passport 0.4.0-dev.8 → 0.4.0-dev.81
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/README.md +18 -22
- package/dist/src/api/auth.d.ts +7 -3
- package/dist/src/api/auth.d.ts.map +1 -1
- package/dist/src/api/auth.js +3 -1
- package/dist/src/api/auth.js.map +1 -1
- package/dist/src/api/client.d.ts +4 -1
- package/dist/src/api/client.d.ts.map +1 -1
- package/dist/src/api/client.js +9 -2
- package/dist/src/api/client.js.map +1 -1
- package/dist/src/api/portal.d.ts +3 -9
- package/dist/src/api/portal.d.ts.map +1 -1
- package/dist/src/api/portal.js +8 -5
- package/dist/src/api/portal.js.map +1 -1
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts +2 -1
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -1
- package/dist/src/components/Dropdown/ConnectedTrigger.js +29 -20
- package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -1
- package/dist/src/components/Dropdown/Content.d.ts +19 -5
- package/dist/src/components/Dropdown/Content.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Content.js +19 -61
- package/dist/src/components/Dropdown/Content.js.map +1 -1
- package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts.map +1 -1
- package/dist/src/components/Dropdown/DisconnectedTrigger.js +2 -2
- package/dist/src/components/Dropdown/DisconnectedTrigger.js.map +1 -1
- package/dist/src/components/Dropdown/Dropdown.d.ts +13 -6
- package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Dropdown.js +32 -9
- package/dist/src/components/Dropdown/Dropdown.js.map +1 -1
- package/dist/src/components/Dropdown/ListingItem.d.ts +13 -0
- package/dist/src/components/Dropdown/ListingItem.d.ts.map +1 -0
- package/dist/src/components/Dropdown/ListingItem.js +34 -0
- package/dist/src/components/Dropdown/ListingItem.js.map +1 -0
- package/dist/src/components/Dropdown/NestedViewLayout.d.ts +7 -0
- package/dist/src/components/Dropdown/NestedViewLayout.d.ts.map +1 -0
- package/dist/src/components/Dropdown/NestedViewLayout.js +34 -0
- package/dist/src/components/Dropdown/NestedViewLayout.js.map +1 -0
- package/dist/src/components/Dropdown/Receive/Receive.d.ts +7 -0
- package/dist/src/components/Dropdown/Receive/Receive.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Receive/Receive.js +53 -0
- package/dist/src/components/Dropdown/Receive/Receive.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts +7 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.js +48 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts +11 -0
- package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAssetItem.js +9 -0
- package/dist/src/components/Dropdown/Root/AccountAssetItem.js.map +1 -0
- package/dist/src/components/Dropdown/{AccountBalance.d.ts → Root/AccountBalance.d.ts} +2 -1
- package/dist/src/components/Dropdown/Root/AccountBalance.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.js +15 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts +9 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.js +48 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.js.map +1 -0
- package/dist/src/components/Dropdown/Root/Root.d.ts +23 -0
- package/dist/src/components/Dropdown/Root/Root.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/Root.js +57 -0
- package/dist/src/components/Dropdown/Root/Root.js.map +1 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.d.ts +8 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.d.ts.map +1 -0
- package/dist/src/components/Dropdown/{AccountAddress.js → Root/WalletAddress.js} +21 -15
- package/dist/src/components/Dropdown/Root/WalletAddress.js.map +1 -0
- package/dist/src/components/Dropdown/{WelcomeBlock.d.ts → Root/WelcomeBlock.d.ts} +1 -1
- package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts.map +1 -0
- package/dist/src/components/Dropdown/{WelcomeBlock.js → Root/WelcomeBlock.js} +5 -5
- package/dist/src/components/Dropdown/Root/WelcomeBlock.js.map +1 -0
- package/dist/src/components/Dropdown/TestnetTopBanner.d.ts +3 -0
- package/dist/src/components/Dropdown/TestnetTopBanner.d.ts.map +1 -0
- package/dist/src/components/Dropdown/TestnetTopBanner.js +14 -0
- package/dist/src/components/Dropdown/TestnetTopBanner.js.map +1 -0
- package/dist/src/config.d.ts +29 -6
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +88 -15
- package/dist/src/config.js.map +1 -1
- package/dist/src/constants.d.ts +13 -3
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +14 -4
- package/dist/src/constants.js.map +1 -1
- package/dist/src/hooks/index.d.ts +4 -0
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +4 -0
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/useAssetsConversionRates.d.ts +23 -0
- package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -0
- package/dist/src/hooks/useAssetsConversionRates.js +90 -0
- package/dist/src/hooks/useAssetsConversionRates.js.map +1 -0
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts +13 -13
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useAuthenticateWithWallet.js +8 -7
- package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -1
- package/dist/src/hooks/useBorrowData.d.ts +82 -0
- package/dist/src/hooks/useBorrowData.d.ts.map +1 -0
- package/dist/src/hooks/useBorrowData.js +159 -0
- package/dist/src/hooks/useBorrowData.js.map +1 -0
- package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
- package/dist/src/hooks/useCreateAccount.js +22 -1
- package/dist/src/hooks/useCreateAccount.js.map +1 -1
- package/dist/src/hooks/useDropdownData.d.ts +37 -34
- package/dist/src/hooks/useDropdownData.d.ts.map +1 -1
- package/dist/src/hooks/useDropdownData.js +85 -61
- package/dist/src/hooks/useDropdownData.js.map +1 -1
- package/dist/src/hooks/useLinkAccount.d.ts +19 -13
- package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
- package/dist/src/hooks/useLinkAccount.js +20 -1
- package/dist/src/hooks/useLinkAccount.js.map +1 -1
- package/dist/src/hooks/useRefreshPassport.d.ts +19 -0
- package/dist/src/hooks/useRefreshPassport.d.ts.map +1 -0
- package/dist/src/hooks/useRefreshPassport.js +44 -0
- package/dist/src/hooks/useRefreshPassport.js.map +1 -0
- package/dist/src/hooks/useSignInWithWallet.d.ts +13 -13
- package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useSignUpWithWallet.d.ts +13 -13
- package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useTokensBalances.d.ts +74 -0
- package/dist/src/hooks/useTokensBalances.d.ts.map +1 -0
- package/dist/src/hooks/useTokensBalances.js +140 -0
- package/dist/src/hooks/useTokensBalances.js.map +1 -0
- package/dist/src/hooks/useValidateMezoId.d.ts +8 -0
- package/dist/src/hooks/useValidateMezoId.d.ts.map +1 -0
- package/dist/src/hooks/useValidateMezoId.js +24 -0
- package/dist/src/hooks/useValidateMezoId.js.map +1 -0
- package/dist/src/hooks/useWalletAccount.d.ts +2 -1
- package/dist/src/hooks/useWalletAccount.d.ts.map +1 -1
- package/dist/src/hooks/useWalletAccount.js +19 -15
- package/dist/src/hooks/useWalletAccount.js.map +1 -1
- package/dist/src/hooks/useWatchTransferEvents.d.ts +5 -0
- package/dist/src/hooks/useWatchTransferEvents.d.ts.map +1 -0
- package/dist/src/hooks/useWatchTransferEvents.js +63 -0
- package/dist/src/hooks/useWatchTransferEvents.js.map +1 -0
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/lib/contracts/index.d.ts +13 -0
- package/dist/src/lib/contracts/index.d.ts.map +1 -0
- package/dist/src/lib/contracts/index.js +62 -0
- package/dist/src/lib/contracts/index.js.map +1 -0
- package/dist/src/lib/contracts/priceOracle.d.ts +43 -0
- package/dist/src/lib/contracts/priceOracle.d.ts.map +1 -0
- package/dist/src/lib/contracts/priceOracle.js +52 -0
- package/dist/src/lib/contracts/priceOracle.js.map +1 -0
- package/dist/src/provider.js +1 -1
- package/dist/src/provider.js.map +1 -1
- package/dist/src/stores/dropdownStore.d.ts +12 -0
- package/dist/src/stores/dropdownStore.d.ts.map +1 -0
- package/dist/src/stores/dropdownStore.js +13 -0
- package/dist/src/stores/dropdownStore.js.map +1 -0
- package/dist/src/utils/address.d.ts +1 -1
- package/dist/src/utils/address.d.ts.map +1 -1
- package/dist/src/utils/address.js +2 -0
- package/dist/src/utils/address.js.map +1 -1
- package/dist/src/utils/address.test.js +8 -5
- package/dist/src/utils/address.test.js.map +1 -1
- package/dist/src/utils/cryptoAssets.d.ts +17 -1
- package/dist/src/utils/cryptoAssets.d.ts.map +1 -1
- package/dist/src/utils/cryptoAssets.js +87 -31
- package/dist/src/utils/cryptoAssets.js.map +1 -1
- package/dist/src/utils/cryptoAssets.test.js +51 -33
- package/dist/src/utils/cryptoAssets.test.js.map +1 -1
- package/dist/src/utils/currency.d.ts +8 -0
- package/dist/src/utils/currency.d.ts.map +1 -1
- package/dist/src/utils/currency.js +11 -0
- package/dist/src/utils/currency.js.map +1 -1
- package/dist/src/utils/currency.test.js +1 -0
- package/dist/src/utils/currency.test.js.map +1 -1
- package/dist/src/utils/mezoId.d.ts +7 -0
- package/dist/src/utils/mezoId.d.ts.map +1 -0
- package/dist/src/utils/mezoId.js +41 -0
- package/dist/src/utils/mezoId.js.map +1 -0
- package/dist/src/utils/numbers.d.ts +39 -17
- package/dist/src/utils/numbers.d.ts.map +1 -1
- package/dist/src/utils/numbers.js +91 -33
- package/dist/src/utils/numbers.js.map +1 -1
- package/dist/src/utils/numbers.test.js +144 -22
- package/dist/src/utils/numbers.test.js.map +1 -1
- package/dist/src/utils/siww.d.ts +1 -0
- package/dist/src/utils/siww.d.ts.map +1 -1
- package/dist/src/utils/siww.js +50 -12
- package/dist/src/utils/siww.js.map +1 -1
- package/dist/src/utils/wagmi.d.ts +3 -0
- package/dist/src/utils/wagmi.d.ts.map +1 -0
- package/dist/src/utils/wagmi.js +7 -0
- package/dist/src/utils/wagmi.js.map +1 -0
- package/dist/src/wallet/index.d.ts +1 -1
- package/dist/src/wallet/index.d.ts.map +1 -1
- package/dist/src/wallet/index.js +1 -1
- package/dist/src/wallet/index.js.map +1 -1
- package/package.json +10 -8
- package/src/api/auth.ts +8 -4
- package/src/api/client.ts +11 -2
- package/src/api/portal.ts +11 -14
- package/src/components/Dropdown/ConnectedTrigger.tsx +52 -32
- package/src/components/Dropdown/Content.tsx +67 -117
- package/src/components/Dropdown/DisconnectedTrigger.tsx +2 -1
- package/src/components/Dropdown/Dropdown.tsx +75 -15
- package/src/components/Dropdown/ListingItem.tsx +80 -0
- package/src/components/Dropdown/NestedViewLayout.tsx +75 -0
- package/src/components/Dropdown/README.md +10 -18
- package/src/components/Dropdown/Receive/Receive.tsx +119 -0
- package/src/components/Dropdown/Root/AccountAddressActions.tsx +101 -0
- package/src/components/Dropdown/Root/AccountAssetItem.tsx +26 -0
- package/src/components/Dropdown/Root/AccountBalance.tsx +30 -0
- package/src/components/Dropdown/Root/AccountOtherAssets.tsx +68 -0
- package/src/components/Dropdown/Root/Root.tsx +147 -0
- package/src/components/Dropdown/Root/WalletAddress.tsx +128 -0
- package/src/components/Dropdown/{WelcomeBlock.tsx → Root/WelcomeBlock.tsx} +5 -6
- package/src/components/Dropdown/TestnetTopBanner.tsx +32 -0
- package/src/config.ts +135 -21
- package/src/constants.ts +18 -4
- package/src/hooks/index.ts +7 -0
- package/src/hooks/useAssetsConversionRates.ts +97 -0
- package/src/hooks/useAuthenticateWithWallet.ts +33 -12
- package/src/hooks/useBorrowData.ts +187 -0
- package/src/hooks/useCreateAccount.ts +28 -2
- package/src/hooks/useDropdownData.ts +119 -97
- package/src/hooks/useLinkAccount.ts +36 -4
- package/src/hooks/useRefreshPassport.ts +56 -0
- package/src/hooks/useSignInWithWallet.ts +2 -2
- package/src/hooks/useSignUpWithWallet.ts +2 -2
- package/src/hooks/useTokensBalances.ts +187 -0
- package/src/hooks/useValidateMezoId.ts +31 -0
- package/src/hooks/useWalletAccount.ts +29 -19
- package/src/hooks/useWatchTransferEvents.ts +74 -0
- package/src/index.ts +13 -1
- package/src/lib/contracts/index.ts +92 -0
- package/src/lib/contracts/priceOracle.ts +53 -0
- package/src/provider.ts +1 -1
- package/src/stores/dropdownStore.ts +20 -0
- package/src/utils/address.test.ts +10 -6
- package/src/utils/address.ts +5 -3
- package/src/utils/cryptoAssets.test.ts +57 -37
- package/src/utils/cryptoAssets.ts +113 -35
- package/src/utils/currency.test.ts +2 -0
- package/src/utils/currency.ts +21 -0
- package/src/utils/mezoId.ts +51 -0
- package/src/utils/numbers.test.ts +181 -23
- package/src/utils/numbers.ts +121 -34
- package/src/utils/siww.ts +71 -16
- package/src/utils/wagmi.ts +12 -0
- package/src/wallet/index.ts +3 -2
- package/dist/src/components/Dropdown/AccountAddress.d.ts +0 -8
- package/dist/src/components/Dropdown/AccountAddress.d.ts.map +0 -1
- package/dist/src/components/Dropdown/AccountAddress.js.map +0 -1
- package/dist/src/components/Dropdown/AccountAssets.d.ts +0 -14
- package/dist/src/components/Dropdown/AccountAssets.d.ts.map +0 -1
- package/dist/src/components/Dropdown/AccountAssets.js +0 -44
- package/dist/src/components/Dropdown/AccountAssets.js.map +0 -1
- package/dist/src/components/Dropdown/AccountBalance.d.ts.map +0 -1
- package/dist/src/components/Dropdown/AccountBalance.js +0 -18
- package/dist/src/components/Dropdown/AccountBalance.js.map +0 -1
- package/dist/src/components/Dropdown/WelcomeBlock.d.ts.map +0 -1
- package/dist/src/components/Dropdown/WelcomeBlock.js.map +0 -1
- package/dist/src/hooks/useAssetsUSDConversion.d.ts +0 -8
- package/dist/src/hooks/useAssetsUSDConversion.d.ts.map +0 -1
- package/dist/src/hooks/useAssetsUSDConversion.js +0 -21
- package/dist/src/hooks/useAssetsUSDConversion.js.map +0 -1
- package/src/components/Dropdown/AccountAddress.tsx +0 -111
- package/src/components/Dropdown/AccountAssets.tsx +0 -110
- package/src/components/Dropdown/AccountBalance.tsx +0 -38
- package/src/hooks/useAssetsUSDConversion.ts +0 -31
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { useAccount, useReadContracts } from "wagmi"
|
|
2
|
+
import { useCallback, useMemo } from "react"
|
|
3
|
+
import { useQueryClient } from "@tanstack/react-query"
|
|
4
|
+
import { ONE_MINUTE_MS } from "../utils/time"
|
|
5
|
+
import {
|
|
6
|
+
mainnetBorrowContracts,
|
|
7
|
+
testnetBorrowContracts,
|
|
8
|
+
} from "../lib/contracts"
|
|
9
|
+
import { usePassportContext } from "./usePassportContext"
|
|
10
|
+
import { CHAIN_ID } from "../constants"
|
|
11
|
+
import { bigIntMax } from "../utils/numbers"
|
|
12
|
+
|
|
13
|
+
// Wagmi handles typesafety with ABI const assertions. TypeScript doesn't
|
|
14
|
+
// support importing JSON as const yet so types cannot be inferred from the
|
|
15
|
+
// imported contract. As a workaround there is minimal ABI definition that can
|
|
16
|
+
// be asserted types from.
|
|
17
|
+
// Ref: https://wagmi.sh/core/typescript#const-assert-abis-typed-data
|
|
18
|
+
|
|
19
|
+
const TROVE_MANAGER_ABI = [
|
|
20
|
+
{
|
|
21
|
+
inputs: [
|
|
22
|
+
{
|
|
23
|
+
internalType: "address",
|
|
24
|
+
name: "_borrower",
|
|
25
|
+
type: "address",
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
name: "getEntireDebtAndColl",
|
|
29
|
+
outputs: [
|
|
30
|
+
{
|
|
31
|
+
internalType: "uint256",
|
|
32
|
+
name: "coll",
|
|
33
|
+
type: "uint256",
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
internalType: "uint256",
|
|
37
|
+
name: "principal",
|
|
38
|
+
type: "uint256",
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
internalType: "uint256",
|
|
42
|
+
name: "interest",
|
|
43
|
+
type: "uint256",
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
internalType: "uint256",
|
|
47
|
+
name: "pendingCollateral",
|
|
48
|
+
type: "uint256",
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
internalType: "uint256",
|
|
52
|
+
name: "pendingPrincipal",
|
|
53
|
+
type: "uint256",
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
internalType: "uint256",
|
|
57
|
+
name: "pendingInterest",
|
|
58
|
+
type: "uint256",
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
stateMutability: "view",
|
|
62
|
+
type: "function",
|
|
63
|
+
},
|
|
64
|
+
] as const
|
|
65
|
+
|
|
66
|
+
const BORROWER_OPERATIONS_ABI = [
|
|
67
|
+
{
|
|
68
|
+
inputs: [],
|
|
69
|
+
name: "MUSD_GAS_COMPENSATION",
|
|
70
|
+
outputs: [
|
|
71
|
+
{
|
|
72
|
+
internalType: "uint256",
|
|
73
|
+
name: "",
|
|
74
|
+
type: "uint256",
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
stateMutability: "view",
|
|
78
|
+
type: "function",
|
|
79
|
+
},
|
|
80
|
+
] as const
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Query hook for getting borrow data. Returns collateral and trove debt for the
|
|
84
|
+
* connected account, based on it's evm address.
|
|
85
|
+
* @param queryOptions Query options passed to the underlying `useQuery` hook.
|
|
86
|
+
*/
|
|
87
|
+
export function useBorrowData(queryOptions = {}) {
|
|
88
|
+
const { environment = "mainnet" } = usePassportContext()
|
|
89
|
+
const { address } = useAccount()
|
|
90
|
+
|
|
91
|
+
const contractAddress = useMemo(() => {
|
|
92
|
+
if (environment === "mainnet") {
|
|
93
|
+
return {
|
|
94
|
+
troveManager: mainnetBorrowContracts.TroveManager.address,
|
|
95
|
+
borrowerOperations: mainnetBorrowContracts.BorrowerOperations.address,
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
troveManager: testnetBorrowContracts.TroveManager.address,
|
|
100
|
+
borrowerOperations: testnetBorrowContracts.BorrowerOperations.address,
|
|
101
|
+
}
|
|
102
|
+
}, [environment])
|
|
103
|
+
|
|
104
|
+
const chainId = CHAIN_ID[environment]
|
|
105
|
+
|
|
106
|
+
return useReadContracts({
|
|
107
|
+
contracts: [
|
|
108
|
+
{
|
|
109
|
+
abi: TROVE_MANAGER_ABI,
|
|
110
|
+
address: contractAddress.troveManager,
|
|
111
|
+
functionName: "getEntireDebtAndColl",
|
|
112
|
+
args: [address || "0x"],
|
|
113
|
+
chainId,
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
abi: BORROWER_OPERATIONS_ABI,
|
|
117
|
+
address: contractAddress.borrowerOperations,
|
|
118
|
+
functionName: "MUSD_GAS_COMPENSATION",
|
|
119
|
+
args: [],
|
|
120
|
+
chainId,
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
query: {
|
|
124
|
+
enabled: !!address,
|
|
125
|
+
staleTime: 5 * ONE_MINUTE_MS,
|
|
126
|
+
retry: 1,
|
|
127
|
+
select: (data) => {
|
|
128
|
+
if (!data || data.some((item) => !item.result)) return undefined
|
|
129
|
+
|
|
130
|
+
const [collateral, principal, interest] = data[0].result!
|
|
131
|
+
const gasCompensation = data[1].result!
|
|
132
|
+
|
|
133
|
+
const troveDebt = bigIntMax(
|
|
134
|
+
principal + interest - gasCompensation,
|
|
135
|
+
0n, // Ensure that debt is not negative
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
collateral,
|
|
140
|
+
// Principal includes gas compensation value which is not
|
|
141
|
+
troveDebt,
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
...queryOptions,
|
|
145
|
+
},
|
|
146
|
+
})
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Hook for for invalidating current user's borrow data. Can be used to
|
|
151
|
+
* invalidate borrow data manually, which forces the data to be re-fetched.
|
|
152
|
+
* @returns Function `invalidateBorrowData` for invalidating the borrow data
|
|
153
|
+
*/
|
|
154
|
+
export function useInvalidateBorrowData() {
|
|
155
|
+
const queryClient = useQueryClient()
|
|
156
|
+
|
|
157
|
+
const { queryKey } = useBorrowData()
|
|
158
|
+
|
|
159
|
+
const invalidateBorrowDataHandler = useCallback(
|
|
160
|
+
() => queryClient.invalidateQueries({ queryKey }),
|
|
161
|
+
[queryClient, queryKey],
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
invalidateBorrowData: invalidateBorrowDataHandler,
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Hook for for resetting current user's borrow data. Can be used to reset
|
|
171
|
+
* borrow data manually, which forces the data to be re-fetched.
|
|
172
|
+
* @returns Function `resetBorrowData` for resetting the borrow data
|
|
173
|
+
*/
|
|
174
|
+
export function useResetBorrowData() {
|
|
175
|
+
const queryClient = useQueryClient()
|
|
176
|
+
|
|
177
|
+
const { queryKey } = useBorrowData()
|
|
178
|
+
|
|
179
|
+
const resetBorrowDataHandler = useCallback(
|
|
180
|
+
() => queryClient.resetQueries({ queryKey }),
|
|
181
|
+
[queryClient, queryKey],
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
resetBorrowData: resetBorrowDataHandler,
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
import { useAuthApiClient } from "./useAuthApiClient"
|
|
8
8
|
import { QUERY_KEYS } from "./constants"
|
|
9
9
|
import type { CreateAccountRequest, CreateAccountResponse } from "../api"
|
|
10
|
+
import useWalletAccount from "./useWalletAccount"
|
|
11
|
+
import { getBitcoinPublicKeyFromConnector } from "../utils/wagmi"
|
|
10
12
|
|
|
11
13
|
export function useCreateAccount(
|
|
12
14
|
mutationOptions: Omit<
|
|
@@ -16,12 +18,36 @@ export function useCreateAccount(
|
|
|
16
18
|
) {
|
|
17
19
|
const queryClient = useQueryClient()
|
|
18
20
|
const authApiClient = useAuthApiClient()
|
|
21
|
+
const { connector } = useWalletAccount()
|
|
19
22
|
|
|
20
23
|
const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
|
|
21
24
|
|
|
22
25
|
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
23
|
-
mutationFn: (
|
|
24
|
-
|
|
26
|
+
mutationFn: async (
|
|
27
|
+
createAccountMutationFnParameters: CreateAccountRequest,
|
|
28
|
+
) => {
|
|
29
|
+
if (createAccountMutationFnParameters.type === "wallet") {
|
|
30
|
+
if (!connector) {
|
|
31
|
+
throw new Error("Connector not defined.")
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const bitcoinPublicKey =
|
|
35
|
+
await getBitcoinPublicKeyFromConnector(connector)
|
|
36
|
+
|
|
37
|
+
// Creating bitcoin mezo account
|
|
38
|
+
if (bitcoinPublicKey) {
|
|
39
|
+
return authApiClient.createAccount({
|
|
40
|
+
...createAccountMutationFnParameters,
|
|
41
|
+
bitcoinPublicKey,
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Creating evm mezo account
|
|
47
|
+
return authApiClient.createAccount({
|
|
48
|
+
...createAccountMutationFnParameters,
|
|
49
|
+
})
|
|
50
|
+
},
|
|
25
51
|
onSuccess: (data, variables, context) => {
|
|
26
52
|
queryClient.resetQueries({
|
|
27
53
|
queryKey: [QUERY_KEYS.ACCOUNT],
|
|
@@ -1,130 +1,152 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useBalance } from "wagmi"
|
|
2
2
|
import { useGetCurrentAccount } from "."
|
|
3
3
|
import {
|
|
4
4
|
CryptoAssetKey,
|
|
5
5
|
getCryptoAsset,
|
|
6
6
|
isBitcoinLikeCryptoAsset,
|
|
7
|
-
|
|
7
|
+
mapCryptoAssetToDetails,
|
|
8
8
|
} from "../utils/cryptoAssets"
|
|
9
9
|
import { fromFixedPoint } from "../utils/numbers"
|
|
10
|
-
import {
|
|
10
|
+
import { useAssetsConversionRates } from "./useAssetsConversionRates"
|
|
11
11
|
import useWalletAccount from "./useWalletAccount"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
* @returns The balance details
|
|
19
|
-
*/
|
|
20
|
-
export function formatCryptoAsset(
|
|
21
|
-
type: CryptoAssetKey,
|
|
22
|
-
balance: bigint,
|
|
23
|
-
btcUsdConversionRate: number,
|
|
24
|
-
) {
|
|
25
|
-
const { decimals } = getCryptoAsset(type)
|
|
26
|
-
|
|
27
|
-
const nativeBalance = fromFixedPoint(balance, decimals, 4)
|
|
28
|
-
let balanceInUsd = 0
|
|
29
|
-
if (isBitcoinLikeCryptoAsset(type)) {
|
|
30
|
-
balanceInUsd = nativeBalance * btcUsdConversionRate
|
|
31
|
-
}
|
|
32
|
-
if (isUsdLikeCryptoAsset(type)) {
|
|
33
|
-
balanceInUsd = nativeBalance
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
type,
|
|
38
|
-
balance: nativeBalance,
|
|
39
|
-
balanceInUsd,
|
|
40
|
-
decimals,
|
|
41
|
-
}
|
|
42
|
-
}
|
|
12
|
+
import { useTokensBalances } from "./useTokensBalances"
|
|
13
|
+
import { useBorrowData } from "./useBorrowData"
|
|
14
|
+
import { CHAIN_ID } from "../constants"
|
|
15
|
+
import { usePassportContext } from "./usePassportContext"
|
|
16
|
+
import { useWatchTransferEventsForAllTokens } from "./useWatchTransferEvents"
|
|
17
|
+
import { convertToUsd } from "../utils/currency"
|
|
43
18
|
|
|
44
19
|
export type DropdownCryptoAsset<T = CryptoAssetKey> = {
|
|
45
20
|
type: T
|
|
46
21
|
balance: bigint
|
|
47
22
|
}
|
|
48
23
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
walletType: "bitcoin" | "evm"
|
|
55
|
-
totalBalanceInUsd: number
|
|
56
|
-
formattedNativeAssets: DropdownDetailedCryptoAsset[]
|
|
57
|
-
matsnet: {
|
|
58
|
-
mats: number
|
|
59
|
-
formattedAssets: DropdownDetailedCryptoAsset[]
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
type UseDropdownDataOptions = {
|
|
64
|
-
// This is workaround. It should be read from contracts or fetched from API.
|
|
65
|
-
evmNativeAssets?: DropdownCryptoAsset[]
|
|
66
|
-
matsnetAssets?: DropdownCryptoAsset[]
|
|
24
|
+
const DEFAULT_ASSET_DATA = {
|
|
25
|
+
rawBalance: 0n,
|
|
26
|
+
formattedBalance: "0.0000",
|
|
27
|
+
usdBalance: 0,
|
|
28
|
+
formattedUsdBalance: "$0.00",
|
|
67
29
|
}
|
|
68
30
|
|
|
69
31
|
/**
|
|
70
32
|
* Aggregates dropdown data for Dropdown component.
|
|
71
|
-
* @param
|
|
72
|
-
*
|
|
73
|
-
*
|
|
33
|
+
* @param {accountDataRefetchInterval: number, nativeBalanceRefetchInterval: number }
|
|
34
|
+
* Object containing refetch intervals time (in milliseconds) for account data
|
|
35
|
+
* (like mats or mezo id) and for native balance. After that time the specific
|
|
36
|
+
* value should be marked as stale and re-fetched. Default value for both is
|
|
37
|
+
* 90000 (90 secs).
|
|
74
38
|
* @returns Dropdown data.
|
|
39
|
+
* @dev This hook is for internal use only.
|
|
75
40
|
*/
|
|
76
|
-
export default function useDropdownData(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
41
|
+
export default function useDropdownData({
|
|
42
|
+
accountDataRefetchInterval = 90000,
|
|
43
|
+
nativeBalanceRefetchInterval = 90000,
|
|
44
|
+
}) {
|
|
45
|
+
const {
|
|
46
|
+
walletAddress,
|
|
47
|
+
accountAddress,
|
|
48
|
+
networkFamily: walletType,
|
|
49
|
+
} = useWalletAccount()
|
|
50
|
+
const { environment = "mainnet" } = usePassportContext()
|
|
51
|
+
const { data: btcBalance } = useBalance({
|
|
52
|
+
address: accountAddress,
|
|
53
|
+
chainId: CHAIN_ID[environment],
|
|
54
|
+
query: {
|
|
55
|
+
staleTime: nativeBalanceRefetchInterval,
|
|
56
|
+
refetchInterval: nativeBalanceRefetchInterval,
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
const { data: passportAccount } = useGetCurrentAccount({
|
|
60
|
+
staleTime: accountDataRefetchInterval,
|
|
61
|
+
refetchInterval: accountDataRefetchInterval,
|
|
62
|
+
enabled: !!accountAddress,
|
|
63
|
+
})
|
|
64
|
+
const { data: assetsConversionRates } = useAssetsConversionRates()
|
|
65
|
+
const { data: tokensBalances } = useTokensBalances()
|
|
66
|
+
const { data: debt } = useBorrowData()
|
|
67
|
+
|
|
68
|
+
useWatchTransferEventsForAllTokens()
|
|
69
|
+
|
|
70
|
+
const mezoId = passportAccount?.mezoId
|
|
71
|
+
|
|
72
|
+
const detailedAssets = [
|
|
73
|
+
["BTC", btcBalance?.value ?? 0n],
|
|
74
|
+
...Object.entries(tokensBalances ?? {}),
|
|
75
|
+
].map((asset) => {
|
|
76
|
+
const [type, balance] = asset as [CryptoAssetKey, bigint]
|
|
77
|
+
|
|
78
|
+
if (isBitcoinLikeCryptoAsset(type)) {
|
|
79
|
+
return mapCryptoAssetToDetails(
|
|
80
|
+
type,
|
|
81
|
+
balance,
|
|
82
|
+
assetsConversionRates.btc.price,
|
|
83
|
+
assetsConversionRates.btc.decimals,
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
if (type === "mT") {
|
|
87
|
+
return mapCryptoAssetToDetails(
|
|
88
|
+
type,
|
|
89
|
+
balance,
|
|
90
|
+
assetsConversionRates.t.price,
|
|
91
|
+
assetsConversionRates.t.decimals,
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return mapCryptoAssetToDetails(type, balance, 1n, 0)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
const btcData = detailedAssets.find(({ type }) => type === "BTC")!
|
|
99
|
+
const musdData = detailedAssets.find(({ type }) => type === "MUSD")!
|
|
100
|
+
const otherAssetsData = detailedAssets.filter(
|
|
101
|
+
({ type }) => !["BTC", "MUSD"].includes(type),
|
|
102
|
+
)
|
|
101
103
|
|
|
102
|
-
const
|
|
104
|
+
const otherAssetsCount = otherAssetsData.filter(
|
|
105
|
+
(asset) => asset.rawBalance !== 0n,
|
|
106
|
+
).length
|
|
103
107
|
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
108
|
+
const assets = {
|
|
109
|
+
btc: btcData || {
|
|
110
|
+
type: "BTC",
|
|
111
|
+
...DEFAULT_ASSET_DATA,
|
|
112
|
+
},
|
|
113
|
+
musd: musdData || {
|
|
114
|
+
type: "mUSD",
|
|
115
|
+
...DEFAULT_ASSET_DATA,
|
|
116
|
+
},
|
|
117
|
+
}
|
|
107
118
|
|
|
108
|
-
const
|
|
109
|
-
(sum,
|
|
119
|
+
const otherAssetsUsdTotal = otherAssetsData.reduce(
|
|
120
|
+
(sum, { usdBalance }) => sum + usdBalance,
|
|
110
121
|
0,
|
|
111
122
|
)
|
|
112
123
|
|
|
113
|
-
const matsBalance = passportAccount
|
|
124
|
+
const matsBalance = passportAccount?.mats.totalMats || 0
|
|
114
125
|
|
|
115
|
-
const
|
|
116
|
-
|
|
126
|
+
const usdCollateral = convertToUsd(
|
|
127
|
+
debt?.collateral ?? 0n,
|
|
128
|
+
getCryptoAsset("MUSD").decimals,
|
|
129
|
+
assetsConversionRates.btc.price,
|
|
130
|
+
assetsConversionRates.btc.decimals,
|
|
117
131
|
)
|
|
118
132
|
|
|
133
|
+
const usdTroveDebt = fromFixedPoint(debt?.troveDebt ?? 0n, 18)
|
|
134
|
+
|
|
135
|
+
const usdTotalBalance =
|
|
136
|
+
detailedAssets.reduce((sum, { usdBalance }) => sum + usdBalance, 0) +
|
|
137
|
+
usdCollateral
|
|
138
|
+
|
|
119
139
|
return {
|
|
120
140
|
mezoId,
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
141
|
+
matsBalance,
|
|
142
|
+
accountAddress,
|
|
143
|
+
walletAddress,
|
|
144
|
+
walletType,
|
|
145
|
+
usdTotalBalance,
|
|
146
|
+
usdTroveDebt,
|
|
147
|
+
usdCollateral,
|
|
148
|
+
assets,
|
|
149
|
+
otherAssetsCount,
|
|
150
|
+
otherAssetsUsdTotal,
|
|
129
151
|
}
|
|
130
152
|
}
|
|
@@ -5,23 +5,55 @@ import {
|
|
|
5
5
|
DefaultError,
|
|
6
6
|
} from "@tanstack/react-query"
|
|
7
7
|
import { useAuthApiClient } from "./useAuthApiClient"
|
|
8
|
-
import type {
|
|
8
|
+
import type { LinkAccountResponse, SignatureData } from "../api"
|
|
9
9
|
import { QUERY_KEYS } from "./constants"
|
|
10
|
+
import useWalletAccount from "./useWalletAccount"
|
|
11
|
+
import { getBitcoinPublicKeyFromConnector } from "../utils/wagmi"
|
|
12
|
+
|
|
13
|
+
type LinkAccountMutationFnParameters =
|
|
14
|
+
| ({ type: "wallet" } & SignatureData)
|
|
15
|
+
| { type: "discord" }
|
|
10
16
|
|
|
11
17
|
export function useLinkAccount(
|
|
12
18
|
mutationOptions: Omit<
|
|
13
|
-
MutationOptions<
|
|
19
|
+
MutationOptions<
|
|
20
|
+
LinkAccountResponse,
|
|
21
|
+
DefaultError,
|
|
22
|
+
LinkAccountMutationFnParameters
|
|
23
|
+
>,
|
|
14
24
|
"mutationFn" | "mutationKey"
|
|
15
25
|
> = {},
|
|
16
26
|
) {
|
|
17
27
|
const queryClient = useQueryClient()
|
|
18
28
|
const authApiClient = useAuthApiClient()
|
|
29
|
+
const { connector } = useWalletAccount()
|
|
19
30
|
|
|
20
31
|
const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
|
|
21
32
|
|
|
22
33
|
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
23
|
-
mutationFn: (
|
|
24
|
-
|
|
34
|
+
mutationFn: async (
|
|
35
|
+
linkAccountMutationFnParameters: LinkAccountMutationFnParameters,
|
|
36
|
+
) => {
|
|
37
|
+
if (linkAccountMutationFnParameters.type === "wallet") {
|
|
38
|
+
if (!connector) {
|
|
39
|
+
throw new Error("Connector not defined.")
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const bitcoinPublicKey =
|
|
43
|
+
await getBitcoinPublicKeyFromConnector(connector)
|
|
44
|
+
|
|
45
|
+
// Linking bitcoin account
|
|
46
|
+
if (bitcoinPublicKey) {
|
|
47
|
+
return authApiClient.linkAccount({
|
|
48
|
+
...linkAccountMutationFnParameters,
|
|
49
|
+
bitcoinPublicKey,
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Linking any other account like wallet evm address account or discord
|
|
55
|
+
return authApiClient.linkAccount(linkAccountMutationFnParameters)
|
|
56
|
+
},
|
|
25
57
|
onSuccess: (data, variables, context) => {
|
|
26
58
|
queryClient.resetQueries({ queryKey: [QUERY_KEYS.ACCOUNT] })
|
|
27
59
|
if (customOnSuccess) customOnSuccess(data, variables, context)
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { useQueryClient } from "@tanstack/react-query"
|
|
2
|
+
import { useCallback } from "react"
|
|
3
|
+
import { useAccount, useBalance } from "wagmi"
|
|
4
|
+
import { QUERY_KEYS } from "./constants"
|
|
5
|
+
import { useInvalidateBorrowData } from "./useBorrowData"
|
|
6
|
+
import { useInvalidateTokensBalances } from "./useTokensBalances"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Hook to refresh the passport using query invalidation mechanism.
|
|
10
|
+
* By calling the refresh function, the latest data will be refetched as soon as possible.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const { refreshCurrentAccount, refreshUsdConversion } = useRefreshPassport()
|
|
14
|
+
*
|
|
15
|
+
* // Call this function to refresh the current account
|
|
16
|
+
* const handlerefreshCurrentAccount = async () => {
|
|
17
|
+
* await refreshCurrentAccount()
|
|
18
|
+
* }
|
|
19
|
+
*/
|
|
20
|
+
export function useRefreshPassport() {
|
|
21
|
+
const { address } = useAccount()
|
|
22
|
+
const queryClient = useQueryClient()
|
|
23
|
+
const { invalidateBorrowData } = useInvalidateBorrowData()
|
|
24
|
+
const { invalidateTokensBalances } = useInvalidateTokensBalances()
|
|
25
|
+
const { queryKey: nativeTokenQueryKey } = useBalance({ address })
|
|
26
|
+
|
|
27
|
+
const refreshCurrentAccount = useCallback(
|
|
28
|
+
() =>
|
|
29
|
+
queryClient.invalidateQueries({
|
|
30
|
+
queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
|
|
31
|
+
}),
|
|
32
|
+
[queryClient],
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
const refreshUsdConversion = useCallback(
|
|
36
|
+
() =>
|
|
37
|
+
queryClient.invalidateQueries({
|
|
38
|
+
queryKey: [QUERY_KEYS.ASSETS_USD_CONVERSION],
|
|
39
|
+
}),
|
|
40
|
+
[queryClient],
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
const refreshTokensBalances = useCallback(async () => {
|
|
44
|
+
await queryClient.invalidateQueries({
|
|
45
|
+
queryKey: nativeTokenQueryKey,
|
|
46
|
+
})
|
|
47
|
+
await invalidateTokensBalances()
|
|
48
|
+
}, [queryClient, nativeTokenQueryKey, invalidateTokensBalances])
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
refreshCurrentAccount,
|
|
52
|
+
refreshUsdConversion,
|
|
53
|
+
refreshBorrowData: invalidateBorrowData,
|
|
54
|
+
refreshTokensBalances,
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { MutationOptions } from "@tanstack/react-query"
|
|
1
|
+
import { DefaultError, MutationOptions } from "@tanstack/react-query"
|
|
2
2
|
import { useAuthenticateWithWallet } from "./useAuthenticateWithWallet"
|
|
3
3
|
import type { Session } from "../api"
|
|
4
4
|
|
|
5
5
|
function useSignInWithWallet(
|
|
6
6
|
mutationOptions: Omit<
|
|
7
|
-
MutationOptions<Session>,
|
|
7
|
+
MutationOptions<Session, DefaultError, number | void>,
|
|
8
8
|
"mutationFn" | "mutationKey"
|
|
9
9
|
> = {},
|
|
10
10
|
) {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { MutationOptions } from "@tanstack/react-query"
|
|
1
|
+
import { DefaultError, MutationOptions } from "@tanstack/react-query"
|
|
2
2
|
import { useAuthenticateWithWallet } from "./useAuthenticateWithWallet"
|
|
3
3
|
import type { Session } from "../api"
|
|
4
4
|
|
|
5
5
|
function useSignUpWithWallet(
|
|
6
6
|
mutationOptions: Omit<
|
|
7
|
-
MutationOptions<Session>,
|
|
7
|
+
MutationOptions<Session, DefaultError, number | void>,
|
|
8
8
|
"mutationFn" | "mutationKey"
|
|
9
9
|
> = {},
|
|
10
10
|
) {
|