-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuseStarknetTx.ts
More file actions
190 lines (149 loc) Β· 5.51 KB
/
Copy pathuseStarknetTx.ts
File metadata and controls
190 lines (149 loc) Β· 5.51 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { Unit, WeiAmount } from '@rulesorg/sdk-core'
import { useCallback, useMemo, useState } from 'react'
import { ExecutedOrPendingTx } from 'src/types'
import { ParsedNetworkFee, StxAction } from 'src/types/starknetTx'
import { useBoundStore } from 'src/zustand'
import { DeployContractResponse, EstimateFee, InvokeFunctionResponse } from 'starknet'
import { shallow } from 'zustand/shallow'
import useRulesAccount from './useRulesAccount'
export function useEstimateFees() {
const [parsedNetworkFee, setParsedNetworkFee] = useState<ParsedNetworkFee | null>(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const { calls, accountDeploymentPayload, txValue, migration } = useBoundStore(
(state) => ({
calls: state.stxCalls,
accountDeploymentPayload: state.stxAccountDeploymentPayload,
txValue: state.stxValue,
migration: state.stxMigration,
}),
shallow
)
const { account } = useRulesAccount()
const estimatedFees = useCallback(
async (privateKey?: string) => {
const stxAccount = migration ? account?.old : account
if (!stxAccount) return
if (privateKey) {
stxAccount.updateSigner(privateKey)
}
setLoading(true)
setError(null)
try {
let estimatedFees: EstimateFee | undefined
if (accountDeploymentPayload) {
estimatedFees = await stxAccount.estimateAccountDeployFee(accountDeploymentPayload)
} else {
estimatedFees = await stxAccount.estimateFee(calls)
}
const maxFee = estimatedFees.suggestedMaxFee.toString() ?? '0'
const fee = estimatedFees.overall_fee.toString() ?? '0'
const gasPrice = +(estimatedFees.gas_price?.toString() ?? '0')
if (!+maxFee || !+fee || !gasPrice) {
throw new Error('Failed to estimate fees')
}
setParsedNetworkFee({
maxFee: WeiAmount.fromRawAmount(maxFee),
fee: WeiAmount.fromRawAmount(fee),
gasPrice,
})
} catch (error) {
setError(error?.message ?? 'Unkown error')
}
setLoading(false)
},
[account, calls, accountDeploymentPayload, migration]
)
const parsedTotalCost = useMemo(() => {
if (!parsedNetworkFee) return null
return {
cost: txValue['ETH'].add(parsedNetworkFee.fee),
maxCost: txValue['ETH'].add(parsedNetworkFee.maxFee),
}
}, [txValue, parsedNetworkFee?.maxFee])
const data = {
parsedNetworkFee,
parsedTotalCost,
gasPrice: parsedNetworkFee?.gasPrice,
}
return [estimatedFees, { data, loading, error }] as const
}
export function useExecuteTx() {
const [loading, setLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const { calls, accountDeploymentPayload, txHash, setTxHash, migration, beforeExecutionCallback } = useBoundStore(
(state) => ({
calls: state.stxCalls,
txHash: state.stxHash,
setTxHash: state.stxSetHash,
accountDeploymentPayload: state.stxAccountDeploymentPayload,
migration: state.stxMigration,
beforeExecutionCallback: state.stxBeforeExecutionCallback,
}),
shallow
)
const { account } = useRulesAccount()
const executeTx = useCallback(
async (parsedMaxFee: WeiAmount, action: StxAction, privateKey?: string) => {
const stxAccount = migration ? account?.old : account
if (!stxAccount) return
if (privateKey) {
stxAccount.updateSigner(privateKey)
}
setLoading(true)
setError(null)
const maxFee = parsedMaxFee.toUnitFixed(Unit.WEI)
const stxCalls = beforeExecutionCallback
? await beforeExecutionCallback(JSON.parse(JSON.stringify(calls)), maxFee)
: calls
try {
let tx: InvokeFunctionResponse | DeployContractResponse | undefined
if (accountDeploymentPayload) {
tx = await stxAccount.deployAccount(accountDeploymentPayload)
} else {
tx = await stxAccount.execute(stxCalls, undefined, { maxFee })
}
if (!tx.transaction_hash) throw 'Failed to push transaction on starknet'
setTxHash(tx.transaction_hash, action)
} catch (error) {
setError(error?.message ?? 'Unkown error')
}
setLoading(false)
},
[account, calls, accountDeploymentPayload, migration, beforeExecutionCallback]
)
return [executeTx, { data: { txHash }, loading, error }] as const
}
export default function useStarknetTx() {
return useBoundStore(
(state) => ({
setCalls: state.stxSetCalls,
pushCalls: state.stxPushCalls,
setAccountDeploymentPayload: state.stxSetAccountDeploymentPayload,
resetStarknetTx: state.stxResetStarknetTx,
txValue: state.stxValue,
increaseTxValue: state.stxIncreaseValue,
setSigning: state.stxSetSigning,
signing: state.stxSigning,
txHash: state.stxHash,
migration: state.stxMigration,
setMigration: state.stxSetMigration,
setBeforeExecutionCallback: state.stxSetBeforeExecutionCallback,
}),
shallow
)
}
export function useStxHistory() {
const { txAction, txHash, executedTxs } = useBoundStore(
(state) => ({
txAction: state.stxAction,
txHash: state.stxHash,
executedTxs: state.executedStxs,
}),
shallow
)
return useMemo(() => {
const receivedTxs = txHash ? [{ hash: txHash, action: txAction, loading: true }] : []
return [...receivedTxs, ...executedTxs.filter((tx) => !!tx.action)] as ExecutedOrPendingTx[]
}, [executedTxs.length, txHash, txAction])
}