-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhooks.ts
More file actions
169 lines (137 loc) · 4.88 KB
/
Copy pathhooks.ts
File metadata and controls
169 lines (137 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { gql, useMutation } from '@apollo/client'
import { constants, WeiAmount } from '@rulesorg/sdk-core'
import { useWeb3React } from '@web3-react/core'
import JSBI from 'jsbi'
import { useCallback, useEffect, useMemo, useState } from 'react'
import AccountABI from 'src/abis/Account.json'
import ERC20ABI from 'src/abis/ERC20.json'
import { useMultipleContractSingleData } from 'src/lib/hooks/multicall'
import { rulesSdk } from 'src/lib/rulesWallet/rulesSdk'
import { AppState } from 'src/state'
import { useEthereumBlockNumber } from 'src/state/application/hooks'
import { useAppDispatch, useAppSelector } from 'src/state/hooks'
import { Abi, num, uint256 } from 'starknet'
import { setWalletModalMode, WalletModalMode } from './actions'
const RETRIEVE_ETHER_MUTATION = gql`
mutation ($hash: String!) {
retrieveEther(filter: { hash: $hash })
}
`
// Modal mode
export function useWalletModalMode(): AppState['wallet']['walletModalMode'] {
return useAppSelector((state: AppState) => state.wallet.walletModalMode)
}
export function useSetWalletModalMode(): (walletModalMode: WalletModalMode) => void {
const dispatch = useAppDispatch()
return useCallback(
(walletModalMode: WalletModalMode) => dispatch(setWalletModalMode({ walletModalMode })),
[dispatch]
)
}
// balance
// TODO: support multiple addresses
function useETHBalances(...addresses: (string | undefined)[]): { [address: string]: WeiAmount | undefined } {
const results = useMultipleContractSingleData(
[constants.ETH_ADDRESSES[rulesSdk.networkInfos.starknetChainId]],
ERC20ABI as Abi,
'balanceOf',
{ address: addresses[0] } // TODO use the right hook ^^
)
return useMemo(() => {
return addresses.reduce<{ [address: string]: WeiAmount | undefined }>(
(acc, address: string | undefined, index: number) => {
const balance = results[index]?.result?.balance as any // as any... We'll do better later é_è
if (!balance?.low || !balance?.high || !address) {
return acc
}
const amount = uint256.uint256ToBN(balance)
acc[address] = WeiAmount.fromRawAmount(num.toHex(amount))
return acc
},
{}
)
}, [results, addresses])
}
export function useETHBalance(address?: string): WeiAmount | undefined {
const balances = useETHBalances(address)
if (!address) return
return balances?.[address]
}
function useSTRKBalances(...addresses: (string | undefined)[]): { [address: string]: WeiAmount | undefined } {
const results = useMultipleContractSingleData(
[constants.STRK_ADDRESSES[rulesSdk.networkInfos.starknetChainId]],
ERC20ABI as Abi,
'balanceOf',
{ address: addresses[0] } // TODO use the right hook ^^
)
return useMemo(() => {
return addresses.reduce<{ [address: string]: WeiAmount | undefined }>(
(acc, address: string | undefined, index: number) => {
const balance = results[index]?.result?.balance as any // as any... We'll do better later é_è
if (!balance?.low || !balance?.high || !address) {
return acc
}
const amount = uint256.uint256ToBN(balance)
acc[address] = WeiAmount.fromRawAmount(num.toHex(amount))
return acc
},
{}
)
}, [results, addresses])
}
export function useSTRKBalance(address?: string): WeiAmount | undefined {
const balances = useSTRKBalances(address)
if (!address) return
return balances?.[address]
}
/**
* Is deployed
*/
export function useIsDeployed(address?: string): boolean | undefined {
const [deployed, setDeployed] = useState<boolean | undefined>()
useEffect(() => {
setDeployed(undefined)
if (!address) return
rulesSdk.starknet
.getNonceForAddress(address)
.then((nonce) => {
setDeployed(!!+nonce)
})
.catch(() => {
setDeployed(false)
})
}, [address])
return deployed
}
/**
* signer escape activation date
*/
export function useSignerEscapeActivationDate(address?: string): number | undefined {
const deployed = useIsDeployed(address)
const callResult = useMultipleContractSingleData(
[deployed ? address : undefined],
AccountABI as Abi,
'get_signer_escape_activation_date',
{} // TODO use the right hook ^^
)[0]
const activeDate = callResult?.result?.active_date
return activeDate ? +activeDate : undefined
}
/**
* ETH Balance
*/
export function useEthereumETHBalance(address?: string): WeiAmount | undefined {
const { provider } = useWeb3React()
const blockNumber = useEthereumBlockNumber()
const [balance, setBalance] = useState<WeiAmount | undefined>()
useEffect(() => {
if (!provider || !address || !blockNumber) return
provider.getBalance(address).then((res) => {
if (res) setBalance(WeiAmount.fromRawAmount(JSBI.BigInt(res)))
})
}, [provider, address, setBalance, blockNumber])
return balance
}
export function useRetrieveEtherMutation() {
return useMutation(RETRIEVE_ETHER_MUTATION)
}