|
1 | 1 | import { useIntl } from "react-intl"; |
2 | 2 | import { useStore } from "../../stores"; |
3 | | -import { CoinPretty, Dec, Int } from "@keplr-wallet/unit"; |
| 3 | +import { CoinPretty, Dec } from "@keplr-wallet/unit"; |
4 | 4 | import { Call, CallData, num } from "starknet"; |
5 | 5 | import { ERC20Currency } from "@keplr-wallet/types"; |
6 | 6 | import { InExtensionMessageRequester } from "@keplr-wallet/router-extension"; |
@@ -91,45 +91,39 @@ export const useStarknetClaimRewards = () => { |
91 | 91 | ); |
92 | 92 | } |
93 | 93 |
|
94 | | - const { |
95 | | - l1_gas_consumed, |
96 | | - l1_gas_price, |
97 | | - l2_gas_consumed, |
98 | | - l2_gas_price, |
99 | | - l1_data_gas_consumed, |
100 | | - l1_data_gas_price, |
101 | | - } = await starknetAccount.estimateInvokeFee( |
| 94 | + const { resourceBounds } = await starknetAccount.estimateInvokeFee( |
102 | 95 | account.starknetHexAddress, |
103 | 96 | calls |
104 | 97 | ); |
105 | 98 |
|
106 | | - const extraL2GasForOnchainVerification = account.isNanoLedger |
107 | | - ? new Dec(90240) |
108 | | - : new Dec(22039040); |
109 | | - |
110 | | - const adjustedL2GasConsumed = new Dec(l2_gas_consumed ?? 0).add( |
111 | | - extraL2GasForOnchainVerification |
112 | | - ); |
| 99 | + const { l1_gas, l2_gas, l1_data_gas } = resourceBounds; |
113 | 100 |
|
114 | | - const l1Fee = new Dec(l1_gas_consumed).mul(new Dec(l1_gas_price)); |
115 | | - const l2Fee = adjustedL2GasConsumed.mul(new Dec(l2_gas_price ?? 0)); |
116 | | - const l1DataFee = new Dec(l1_data_gas_consumed).mul( |
117 | | - new Dec(l1_data_gas_price) |
118 | | - ); |
| 101 | + const extraL2GasForOnchainVerification = account.isNanoLedger |
| 102 | + ? BigInt(90240) |
| 103 | + : BigInt(22039040); |
119 | 104 |
|
120 | | - const calculatedOverallFee = l1Fee.add(l2Fee).add(l1DataFee); |
| 105 | + const adjustedL2GasConsumed = |
| 106 | + l2_gas.max_amount + extraL2GasForOnchainVerification; |
121 | 107 |
|
122 | | - const margin = new Dec(1.5); |
| 108 | + const l1Fee = l1_gas.max_amount * l1_gas.max_price_per_unit; |
| 109 | + const l2Fee = adjustedL2GasConsumed * l2_gas.max_price_per_unit; |
| 110 | + const l1DataFee = |
| 111 | + l1_data_gas.max_amount * l1_data_gas.max_price_per_unit; |
123 | 112 |
|
124 | | - const totalGasConsumed = new Dec(l1_gas_consumed) |
125 | | - .add(new Dec(l2_gas_consumed ?? 0)) |
126 | | - .add(new Dec(l1_data_gas_consumed)); |
| 113 | + const calculatedOverallFee = l1Fee + l2Fee + l1DataFee; |
127 | 114 |
|
128 | | - const adjustedGasPrice = calculatedOverallFee.quo(totalGasConsumed); |
| 115 | + const totalGasConsumed = |
| 116 | + l1_gas.max_amount + adjustedL2GasConsumed + l1_data_gas.max_amount; |
129 | 117 |
|
130 | | - const gasPrice = new CoinPretty(feeCurrency, adjustedGasPrice); |
131 | | - const maxGasPrice = gasPrice.mul(margin); |
132 | | - const maxGas = totalGasConsumed.mul(margin); |
| 118 | + // margin 1.5x = 3/2 |
| 119 | + const adjustedGasPrice = calculatedOverallFee / totalGasConsumed; |
| 120 | + const maxGasPrice = new CoinPretty( |
| 121 | + feeCurrency, |
| 122 | + new Dec(((adjustedGasPrice * BigInt(3)) / BigInt(2)).toString()) |
| 123 | + ); |
| 124 | + const maxGas = new Dec( |
| 125 | + ((totalGasConsumed * BigInt(3)) / BigInt(2)).toString() |
| 126 | + ); |
133 | 127 |
|
134 | 128 | // compare the account balance and fee |
135 | 129 | const feeCurrencyBalance = |
@@ -177,24 +171,27 @@ export const useStarknetClaimRewards = () => { |
177 | 171 | ); |
178 | 172 | } |
179 | 173 |
|
180 | | - const maxL1DataGas = new Dec(l1_data_gas_consumed).mul(margin); |
181 | | - const maxL1Gas = new Dec(l1_gas_consumed).mul(margin); |
182 | | - const maxL2Gas = adjustedL2GasConsumed.mul(margin); |
183 | | - |
184 | | - const maxL1DataGasPrice = new Dec(l1_data_gas_price).mul(margin); |
185 | | - const maxL1GasPrice = new Dec(l1_gas_price).mul(margin); |
186 | | - const maxL2GasPrice = new Dec(l2_gas_price ?? 0).mul(margin); |
| 174 | + // margin 1.5x = 3/2 |
| 175 | + const maxL1Gas = (l1_gas.max_amount * BigInt(3)) / BigInt(2); |
| 176 | + const maxL1GasPrice = |
| 177 | + (l1_gas.max_price_per_unit * BigInt(3)) / BigInt(2); |
| 178 | + const maxL2Gas = (adjustedL2GasConsumed * BigInt(3)) / BigInt(2); |
| 179 | + const maxL2GasPrice = |
| 180 | + (l2_gas.max_price_per_unit * BigInt(3)) / BigInt(2); |
| 181 | + const maxL1DataGas = (l1_data_gas.max_amount * BigInt(3)) / BigInt(2); |
| 182 | + const maxL1DataGasPrice = |
| 183 | + (l1_data_gas.max_price_per_unit * BigInt(3)) / BigInt(2); |
187 | 184 |
|
188 | 185 | const { transaction_hash: txHash } = await starknetAccount.execute( |
189 | 186 | account.starknetHexAddress, |
190 | 187 | calls, |
191 | 188 | { |
192 | | - l1MaxGas: maxL1Gas.truncate().toString(), |
193 | | - l1MaxGasPrice: maxL1GasPrice.truncate().toString(), |
194 | | - l1MaxDataGas: maxL1DataGas.truncate().toString(), |
195 | | - l1MaxDataGasPrice: maxL1DataGasPrice.truncate().toString(), |
196 | | - l2MaxGas: maxL2Gas.truncate().toString(), |
197 | | - l2MaxGasPrice: maxL2GasPrice.truncate().toString(), |
| 189 | + l1MaxGas: num.toHex(maxL1Gas), |
| 190 | + l1MaxGasPrice: num.toHex(maxL1GasPrice), |
| 191 | + l1MaxDataGas: num.toHex(maxL1DataGas), |
| 192 | + l1MaxDataGasPrice: num.toHex(maxL1DataGasPrice), |
| 193 | + l2MaxGas: num.toHex(maxL2Gas), |
| 194 | + l2MaxGasPrice: num.toHex(maxL2GasPrice), |
198 | 195 | }, |
199 | 196 | async (chainId, calls, details) => { |
200 | 197 | return await new InExtensionMessageRequester().sendMessage( |
@@ -291,54 +288,39 @@ export const useStarknetClaimRewards = () => { |
291 | 288 | try { |
292 | 289 | state.setIsSimulating(true); |
293 | 290 |
|
294 | | - const { |
295 | | - l1_gas_consumed, |
296 | | - l1_gas_price, |
297 | | - l2_gas_consumed, |
298 | | - l2_gas_price, |
299 | | - l1_data_gas_consumed, |
300 | | - l1_data_gas_price, |
301 | | - } = await starknetAccount.estimateInvokeFee( |
| 291 | + const { resourceBounds } = await starknetAccount.estimateInvokeFee( |
302 | 292 | account.starknetHexAddress, |
303 | 293 | calls |
304 | 294 | ); |
305 | 295 |
|
306 | | - const extraL2GasForOnchainVerification = account.isNanoLedger |
307 | | - ? new Dec(90240) |
308 | | - : new Dec(22039040); |
309 | | - |
310 | | - const adjustedL2GasConsumed = new Dec(l2_gas_consumed ?? 0).add( |
311 | | - extraL2GasForOnchainVerification |
312 | | - ); |
| 296 | + const { l1_gas, l2_gas, l1_data_gas } = resourceBounds; |
313 | 297 |
|
314 | | - const margin = new Dec(1.5); |
315 | | - |
316 | | - const maxL1DataGas = new Dec(l1_data_gas_consumed).mul(margin); |
317 | | - const maxL1Gas = new Dec(l1_gas_consumed).mul(margin); |
318 | | - const maxL2Gas = adjustedL2GasConsumed.mul(margin); |
| 298 | + const extraL2GasForOnchainVerification = account.isNanoLedger |
| 299 | + ? BigInt(90240) |
| 300 | + : BigInt(22039040); |
319 | 301 |
|
320 | | - const maxL1DataGasPrice = new Dec(l1_data_gas_price).mul(margin); |
321 | | - const maxL1GasPrice = new Dec(l1_gas_price).mul(margin); |
322 | | - const maxL2GasPrice = new Dec(l2_gas_price ?? 0).mul(margin); |
| 302 | + const adjustedL2GasConsumed = |
| 303 | + l2_gas.max_amount + extraL2GasForOnchainVerification; |
323 | 304 |
|
324 | | - const safeToHex = (value: string | Int | null | undefined): string => { |
325 | | - if (value === null || value === undefined) { |
326 | | - return "0"; |
327 | | - } |
328 | | - const val = value.toString(); |
329 | | - return num.toHex(val === "0x" ? "0" : val); |
330 | | - }; |
| 305 | + // margin 1.5x = 3/2 |
| 306 | + const maxL1Gas = (l1_gas.max_amount * BigInt(3)) / BigInt(2); |
| 307 | + const maxL1GasPrice = (l1_gas.max_price_per_unit * BigInt(3)) / BigInt(2); |
| 308 | + const maxL2Gas = (adjustedL2GasConsumed * BigInt(3)) / BigInt(2); |
| 309 | + const maxL2GasPrice = (l2_gas.max_price_per_unit * BigInt(3)) / BigInt(2); |
| 310 | + const maxL1DataGas = (l1_data_gas.max_amount * BigInt(3)) / BigInt(2); |
| 311 | + const maxL1DataGasPrice = |
| 312 | + (l1_data_gas.max_price_per_unit * BigInt(3)) / BigInt(2); |
331 | 313 |
|
332 | 314 | const { transaction_hash: txHash } = await starknetAccount.execute( |
333 | 315 | account.starknetHexAddress, |
334 | 316 | calls, |
335 | 317 | { |
336 | | - l1MaxGas: safeToHex(maxL1Gas.truncate()), |
337 | | - l1MaxGasPrice: safeToHex(maxL1GasPrice.truncate()), |
338 | | - l1MaxDataGas: safeToHex(maxL1DataGas.truncate()), |
339 | | - l1MaxDataGasPrice: safeToHex(maxL1DataGasPrice.truncate()), |
340 | | - l2MaxGas: safeToHex(maxL2Gas.truncate()), |
341 | | - l2MaxGasPrice: safeToHex(maxL2GasPrice.truncate()), |
| 318 | + l1MaxGas: num.toHex(maxL1Gas), |
| 319 | + l1MaxGasPrice: num.toHex(maxL1GasPrice), |
| 320 | + l1MaxDataGas: num.toHex(maxL1DataGas), |
| 321 | + l1MaxDataGasPrice: num.toHex(maxL1DataGasPrice), |
| 322 | + l2MaxGas: num.toHex(maxL2Gas), |
| 323 | + l2MaxGasPrice: num.toHex(maxL2GasPrice), |
342 | 324 | } |
343 | 325 | ); |
344 | 326 | if (!txHash) { |
|
0 commit comments