@keplr-wallet/hooks-evm 0.13.15-rc.0
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/.eslintrc.json +14 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +18 -0
- package/build/index.js.map +1 -0
- package/build/tx/amount.d.ts +27 -0
- package/build/tx/amount.js +238 -0
- package/build/tx/amount.js.map +1 -0
- package/build/tx/chain.d.ts +10 -0
- package/build/tx/chain.js +37 -0
- package/build/tx/chain.js.map +1 -0
- package/build/tx/errors.d.ts +30 -0
- package/build/tx/errors.js +84 -0
- package/build/tx/errors.js.map +1 -0
- package/build/tx/evm-fee-utils.d.ts +28 -0
- package/build/tx/evm-fee-utils.js +133 -0
- package/build/tx/evm-fee-utils.js.map +1 -0
- package/build/tx/fee.d.ts +61 -0
- package/build/tx/fee.js +523 -0
- package/build/tx/fee.js.map +1 -0
- package/build/tx/gas-simulator.d.ts +89 -0
- package/build/tx/gas-simulator.js +465 -0
- package/build/tx/gas-simulator.js.map +1 -0
- package/build/tx/gas.d.ts +12 -0
- package/build/tx/gas.js +84 -0
- package/build/tx/gas.js.map +1 -0
- package/build/tx/index.d.ts +13 -0
- package/build/tx/index.js +30 -0
- package/build/tx/index.js.map +1 -0
- package/build/tx/internal.d.ts +3 -0
- package/build/tx/internal.js +3 -0
- package/build/tx/internal.js.map +1 -0
- package/build/tx/name-service-ens.d.ts +40 -0
- package/build/tx/name-service-ens.js +189 -0
- package/build/tx/name-service-ens.js.map +1 -0
- package/build/tx/name-service.d.ts +20 -0
- package/build/tx/name-service.js +20 -0
- package/build/tx/name-service.js.map +1 -0
- package/build/tx/recipient.d.ts +35 -0
- package/build/tx/recipient.js +131 -0
- package/build/tx/recipient.js.map +1 -0
- package/build/tx/send-tx.d.ts +13 -0
- package/build/tx/send-tx.js +24 -0
- package/build/tx/send-tx.js.map +1 -0
- package/build/tx/sender.d.ts +12 -0
- package/build/tx/sender.js +73 -0
- package/build/tx/sender.js.map +1 -0
- package/build/tx/types.d.ts +102 -0
- package/build/tx/types.js +3 -0
- package/build/tx/types.js.map +1 -0
- package/build/tx/validate.d.ts +11 -0
- package/build/tx/validate.js +37 -0
- package/build/tx/validate.js.map +1 -0
- package/package.json +40 -0
- package/src/index.ts +1 -0
- package/src/tx/amount.ts +273 -0
- package/src/tx/chain.ts +31 -0
- package/src/tx/errors.ts +79 -0
- package/src/tx/evm-fee-utils.ts +217 -0
- package/src/tx/fee.ts +622 -0
- package/src/tx/gas-simulator.ts +567 -0
- package/src/tx/gas.ts +93 -0
- package/src/tx/index.ts +13 -0
- package/src/tx/internal.ts +4 -0
- package/src/tx/name-service-ens.ts +207 -0
- package/src/tx/name-service.ts +39 -0
- package/src/tx/recipient.ts +166 -0
- package/src/tx/send-tx.ts +55 -0
- package/src/tx/sender.ts +82 -0
- package/src/tx/types.ts +153 -0
- package/src/tx/validate.ts +55 -0
- package/tsconfig.check.json +90 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IFeeConfig, IGasConfig, IGasSimulator, UIProperties } from "./types";
|
|
3
|
+
import { IReactionDisposer } from "mobx";
|
|
4
|
+
import { KVStore } from "@keplr-wallet/common";
|
|
5
|
+
import { TxChainSetter } from "./chain";
|
|
6
|
+
import { ChainGetter } from "@keplr-wallet/stores";
|
|
7
|
+
import { EvmGasSimulationOutcome } from "@keplr-wallet/types";
|
|
8
|
+
import { CoinPretty } from "@keplr-wallet/unit";
|
|
9
|
+
export type SimulateGasFn = () => {
|
|
10
|
+
simulate: () => Promise<{
|
|
11
|
+
gasUsed: number;
|
|
12
|
+
evmSimulationOutcome?: EvmGasSimulationOutcome;
|
|
13
|
+
}>;
|
|
14
|
+
};
|
|
15
|
+
declare class GasSimulatorState {
|
|
16
|
+
protected _isInitialized: boolean;
|
|
17
|
+
protected _initialGasEstimated: number | undefined;
|
|
18
|
+
protected _recentGasEstimated: number | undefined;
|
|
19
|
+
protected _recentEvmSimulationOutcome: EvmGasSimulationOutcome | undefined;
|
|
20
|
+
protected _simulateFn: (() => Promise<{
|
|
21
|
+
gasUsed: number;
|
|
22
|
+
evmSimulationOutcome?: EvmGasSimulationOutcome;
|
|
23
|
+
}>) | undefined;
|
|
24
|
+
protected _isZeroFee: boolean;
|
|
25
|
+
protected _error: Error | undefined;
|
|
26
|
+
constructor();
|
|
27
|
+
setIsInitialized(value: boolean): void;
|
|
28
|
+
get isInitialized(): boolean;
|
|
29
|
+
setInitialGasEstimated(gas: number): void;
|
|
30
|
+
get initialGasEstimated(): number | undefined;
|
|
31
|
+
setRecentGasEstimated(gas: number): void;
|
|
32
|
+
get recentGasEstimated(): number | undefined;
|
|
33
|
+
setRecentEvmSimulationOutcome(outcome: EvmGasSimulationOutcome | undefined): void;
|
|
34
|
+
get recentEvmSimulationOutcome(): EvmGasSimulationOutcome | undefined;
|
|
35
|
+
refreshSimulateFn(fn: () => Promise<{
|
|
36
|
+
gasUsed: number;
|
|
37
|
+
evmSimulationOutcome?: EvmGasSimulationOutcome;
|
|
38
|
+
}>): void;
|
|
39
|
+
get simulateFn(): (() => Promise<{
|
|
40
|
+
gasUsed: number;
|
|
41
|
+
evmSimulationOutcome?: EvmGasSimulationOutcome;
|
|
42
|
+
}>) | undefined;
|
|
43
|
+
setError(error: Error | undefined): void;
|
|
44
|
+
get error(): Error | undefined;
|
|
45
|
+
get isZeroFee(): boolean;
|
|
46
|
+
setIsZeroFee(value: boolean): void;
|
|
47
|
+
static isZeroFee(fee: CoinPretty | undefined): boolean;
|
|
48
|
+
}
|
|
49
|
+
export declare class GasSimulator extends TxChainSetter implements IGasSimulator {
|
|
50
|
+
protected kvStore: KVStore;
|
|
51
|
+
protected readonly gasConfig: IGasConfig;
|
|
52
|
+
protected readonly feeConfig: IFeeConfig;
|
|
53
|
+
protected readonly initialKey: string;
|
|
54
|
+
protected simulateGasFn: SimulateGasFn;
|
|
55
|
+
protected _key: string;
|
|
56
|
+
protected _gasAdjustmentValue: string;
|
|
57
|
+
protected _enabled: boolean;
|
|
58
|
+
protected _forceDisabled: boolean;
|
|
59
|
+
protected _forceDisableReason: Error | undefined;
|
|
60
|
+
protected _isSimulating: boolean;
|
|
61
|
+
protected _stateMap: Map<string, GasSimulatorState>;
|
|
62
|
+
protected _debounceTimeoutId: NodeJS.Timeout | null;
|
|
63
|
+
protected readonly _debounceMs: number;
|
|
64
|
+
protected _disposers: IReactionDisposer[];
|
|
65
|
+
constructor(kvStore: KVStore, chainGetter: ChainGetter, initialChainId: string, gasConfig: IGasConfig, feeConfig: IFeeConfig, initialKey: string, simulateGasFn: SimulateGasFn);
|
|
66
|
+
setKVStore(kvStore: KVStore): void;
|
|
67
|
+
get key(): string;
|
|
68
|
+
setKey(value: string): void;
|
|
69
|
+
get isSimulating(): boolean;
|
|
70
|
+
setSimulateGasFn(simulateGasFn: SimulateGasFn): void;
|
|
71
|
+
get enabled(): boolean;
|
|
72
|
+
setEnabled(value: boolean): void;
|
|
73
|
+
get forceDisabled(): boolean;
|
|
74
|
+
get forceDisableReason(): Error | undefined;
|
|
75
|
+
forceDisable(valueOrReason: boolean | Error): void;
|
|
76
|
+
get error(): Error | undefined;
|
|
77
|
+
get gasEstimated(): number | undefined;
|
|
78
|
+
get gasAdjustment(): number;
|
|
79
|
+
get gasAdjustmentValue(): string;
|
|
80
|
+
setGasAdjustmentValue(gasAdjustment: string | number): void;
|
|
81
|
+
get evmSimulationOutcome(): EvmGasSimulationOutcome | undefined;
|
|
82
|
+
protected init(): void;
|
|
83
|
+
dispose(): void;
|
|
84
|
+
get uiProperties(): UIProperties;
|
|
85
|
+
protected getState(key: string): GasSimulatorState;
|
|
86
|
+
protected get storeKey(): string;
|
|
87
|
+
}
|
|
88
|
+
export declare const useGasSimulator: (kvStore: KVStore, chainGetter: ChainGetter, chainId: string, gasConfig: IGasConfig, feeConfig: IFeeConfig, key: string, simulateGasFn: SimulateGasFn, initialDisabled?: boolean) => GasSimulator;
|
|
89
|
+
export {};
|
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.useGasSimulator = exports.GasSimulator = void 0;
|
|
10
|
+
const mobx_1 = require("mobx");
|
|
11
|
+
const react_1 = require("react");
|
|
12
|
+
const cosmos_1 = require("@keplr-wallet/cosmos");
|
|
13
|
+
const chain_1 = require("./chain");
|
|
14
|
+
const simple_fetch_1 = require("@keplr-wallet/simple-fetch");
|
|
15
|
+
class GasSimulatorState {
|
|
16
|
+
constructor() {
|
|
17
|
+
this._isInitialized = false;
|
|
18
|
+
this._initialGasEstimated = undefined;
|
|
19
|
+
this._recentGasEstimated = undefined;
|
|
20
|
+
this._recentEvmSimulationOutcome = undefined;
|
|
21
|
+
this._simulateFn = undefined;
|
|
22
|
+
this._isZeroFee = true;
|
|
23
|
+
this._error = undefined;
|
|
24
|
+
(0, mobx_1.makeObservable)(this);
|
|
25
|
+
}
|
|
26
|
+
setIsInitialized(value) {
|
|
27
|
+
this._isInitialized = value;
|
|
28
|
+
}
|
|
29
|
+
get isInitialized() {
|
|
30
|
+
return this._isInitialized;
|
|
31
|
+
}
|
|
32
|
+
setInitialGasEstimated(gas) {
|
|
33
|
+
this._initialGasEstimated = gas;
|
|
34
|
+
}
|
|
35
|
+
get initialGasEstimated() {
|
|
36
|
+
return this._initialGasEstimated;
|
|
37
|
+
}
|
|
38
|
+
setRecentGasEstimated(gas) {
|
|
39
|
+
this._recentGasEstimated = gas;
|
|
40
|
+
}
|
|
41
|
+
get recentGasEstimated() {
|
|
42
|
+
return this._recentGasEstimated;
|
|
43
|
+
}
|
|
44
|
+
setRecentEvmSimulationOutcome(outcome) {
|
|
45
|
+
this._recentEvmSimulationOutcome = outcome;
|
|
46
|
+
}
|
|
47
|
+
get recentEvmSimulationOutcome() {
|
|
48
|
+
return this._recentEvmSimulationOutcome;
|
|
49
|
+
}
|
|
50
|
+
refreshSimulateFn(fn) {
|
|
51
|
+
this._simulateFn = fn;
|
|
52
|
+
}
|
|
53
|
+
get simulateFn() {
|
|
54
|
+
return this._simulateFn;
|
|
55
|
+
}
|
|
56
|
+
setError(error) {
|
|
57
|
+
this._error = error;
|
|
58
|
+
}
|
|
59
|
+
get error() {
|
|
60
|
+
return this._error;
|
|
61
|
+
}
|
|
62
|
+
get isZeroFee() {
|
|
63
|
+
return this._isZeroFee;
|
|
64
|
+
}
|
|
65
|
+
setIsZeroFee(value) {
|
|
66
|
+
this._isZeroFee = value;
|
|
67
|
+
}
|
|
68
|
+
static isZeroFee(fee) {
|
|
69
|
+
if (!fee) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
return fee.toCoin().amount === "0";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
__decorate([
|
|
76
|
+
mobx_1.observable
|
|
77
|
+
], GasSimulatorState.prototype, "_isInitialized", void 0);
|
|
78
|
+
__decorate([
|
|
79
|
+
mobx_1.observable
|
|
80
|
+
], GasSimulatorState.prototype, "_initialGasEstimated", void 0);
|
|
81
|
+
__decorate([
|
|
82
|
+
mobx_1.observable
|
|
83
|
+
], GasSimulatorState.prototype, "_recentGasEstimated", void 0);
|
|
84
|
+
__decorate([
|
|
85
|
+
mobx_1.observable.ref
|
|
86
|
+
], GasSimulatorState.prototype, "_recentEvmSimulationOutcome", void 0);
|
|
87
|
+
__decorate([
|
|
88
|
+
mobx_1.observable.ref
|
|
89
|
+
], GasSimulatorState.prototype, "_simulateFn", void 0);
|
|
90
|
+
__decorate([
|
|
91
|
+
mobx_1.observable
|
|
92
|
+
], GasSimulatorState.prototype, "_isZeroFee", void 0);
|
|
93
|
+
__decorate([
|
|
94
|
+
mobx_1.observable.ref
|
|
95
|
+
], GasSimulatorState.prototype, "_error", void 0);
|
|
96
|
+
__decorate([
|
|
97
|
+
mobx_1.action
|
|
98
|
+
], GasSimulatorState.prototype, "setIsInitialized", null);
|
|
99
|
+
__decorate([
|
|
100
|
+
mobx_1.action
|
|
101
|
+
], GasSimulatorState.prototype, "setInitialGasEstimated", null);
|
|
102
|
+
__decorate([
|
|
103
|
+
mobx_1.action
|
|
104
|
+
], GasSimulatorState.prototype, "setRecentGasEstimated", null);
|
|
105
|
+
__decorate([
|
|
106
|
+
mobx_1.action
|
|
107
|
+
], GasSimulatorState.prototype, "setRecentEvmSimulationOutcome", null);
|
|
108
|
+
__decorate([
|
|
109
|
+
mobx_1.action
|
|
110
|
+
], GasSimulatorState.prototype, "refreshSimulateFn", null);
|
|
111
|
+
__decorate([
|
|
112
|
+
mobx_1.action
|
|
113
|
+
], GasSimulatorState.prototype, "setError", null);
|
|
114
|
+
__decorate([
|
|
115
|
+
mobx_1.action
|
|
116
|
+
], GasSimulatorState.prototype, "setIsZeroFee", null);
|
|
117
|
+
class GasSimulator extends chain_1.TxChainSetter {
|
|
118
|
+
constructor(kvStore, chainGetter, initialChainId, gasConfig, feeConfig, initialKey, simulateGasFn) {
|
|
119
|
+
super(chainGetter, initialChainId);
|
|
120
|
+
this.kvStore = kvStore;
|
|
121
|
+
this.gasConfig = gasConfig;
|
|
122
|
+
this.feeConfig = feeConfig;
|
|
123
|
+
this.initialKey = initialKey;
|
|
124
|
+
this.simulateGasFn = simulateGasFn;
|
|
125
|
+
this._gasAdjustmentValue = "1.3";
|
|
126
|
+
this._enabled = false;
|
|
127
|
+
this._forceDisabled = false;
|
|
128
|
+
this._forceDisableReason = undefined;
|
|
129
|
+
this._isSimulating = false;
|
|
130
|
+
this._stateMap = new Map();
|
|
131
|
+
this._debounceTimeoutId = null;
|
|
132
|
+
this._debounceMs = 300;
|
|
133
|
+
this._disposers = [];
|
|
134
|
+
this._key = initialKey;
|
|
135
|
+
(0, mobx_1.makeObservable)(this);
|
|
136
|
+
this.init();
|
|
137
|
+
}
|
|
138
|
+
setKVStore(kvStore) {
|
|
139
|
+
this.kvStore = kvStore;
|
|
140
|
+
}
|
|
141
|
+
get key() {
|
|
142
|
+
return this._key;
|
|
143
|
+
}
|
|
144
|
+
setKey(value) {
|
|
145
|
+
this._key = value;
|
|
146
|
+
}
|
|
147
|
+
get isSimulating() {
|
|
148
|
+
return this._isSimulating;
|
|
149
|
+
}
|
|
150
|
+
setSimulateGasFn(simulateGasFn) {
|
|
151
|
+
this.simulateGasFn = simulateGasFn;
|
|
152
|
+
}
|
|
153
|
+
get enabled() {
|
|
154
|
+
if (this._forceDisabled) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
return this._enabled;
|
|
158
|
+
}
|
|
159
|
+
setEnabled(value) {
|
|
160
|
+
if (this._forceDisabled && value) {
|
|
161
|
+
console.log("Gas simulator is disabled by force. You can not enable the gas simulator");
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
this._enabled = value;
|
|
165
|
+
}
|
|
166
|
+
get forceDisabled() {
|
|
167
|
+
return this._forceDisabled;
|
|
168
|
+
}
|
|
169
|
+
get forceDisableReason() {
|
|
170
|
+
return this._forceDisableReason;
|
|
171
|
+
}
|
|
172
|
+
forceDisable(valueOrReason) {
|
|
173
|
+
if (!valueOrReason) {
|
|
174
|
+
this._forceDisabled = false;
|
|
175
|
+
this._forceDisableReason = undefined;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
if (this.enabled) {
|
|
179
|
+
this.setEnabled(false);
|
|
180
|
+
}
|
|
181
|
+
this._forceDisabled = true;
|
|
182
|
+
if (typeof valueOrReason !== "boolean") {
|
|
183
|
+
this._forceDisableReason = valueOrReason;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
get error() {
|
|
188
|
+
const key = this.storeKey;
|
|
189
|
+
const state = this.getState(key);
|
|
190
|
+
return state.error;
|
|
191
|
+
}
|
|
192
|
+
get gasEstimated() {
|
|
193
|
+
const key = this.storeKey;
|
|
194
|
+
const state = this.getState(key);
|
|
195
|
+
if (state.recentGasEstimated != null) {
|
|
196
|
+
return state.recentGasEstimated;
|
|
197
|
+
}
|
|
198
|
+
return state.initialGasEstimated;
|
|
199
|
+
}
|
|
200
|
+
get gasAdjustment() {
|
|
201
|
+
if (this._gasAdjustmentValue === "") {
|
|
202
|
+
return 0;
|
|
203
|
+
}
|
|
204
|
+
const num = parseFloat(this._gasAdjustmentValue);
|
|
205
|
+
if (Number.isNaN(num) || num < 0) {
|
|
206
|
+
return 0;
|
|
207
|
+
}
|
|
208
|
+
return num;
|
|
209
|
+
}
|
|
210
|
+
get gasAdjustmentValue() {
|
|
211
|
+
return this._gasAdjustmentValue;
|
|
212
|
+
}
|
|
213
|
+
setGasAdjustmentValue(gasAdjustment) {
|
|
214
|
+
if (typeof gasAdjustment === "number") {
|
|
215
|
+
if (gasAdjustment < 0 || gasAdjustment > 3) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
this._gasAdjustmentValue = gasAdjustment.toString();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if (gasAdjustment === "") {
|
|
222
|
+
this._gasAdjustmentValue = "";
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (gasAdjustment.startsWith(".")) {
|
|
226
|
+
this._gasAdjustmentValue = "0" + gasAdjustment;
|
|
227
|
+
}
|
|
228
|
+
const num = parseFloat(gasAdjustment);
|
|
229
|
+
if (Number.isNaN(num) || num < 0 || num > 3) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
this._gasAdjustmentValue = gasAdjustment;
|
|
233
|
+
}
|
|
234
|
+
get evmSimulationOutcome() {
|
|
235
|
+
const key = this.storeKey;
|
|
236
|
+
const state = this.getState(key);
|
|
237
|
+
return state.recentEvmSimulationOutcome;
|
|
238
|
+
}
|
|
239
|
+
init() {
|
|
240
|
+
this._disposers.push((0, mobx_1.autorun)(() => {
|
|
241
|
+
if (!this.enabled) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const key = this.storeKey;
|
|
245
|
+
const state = this.getState(key);
|
|
246
|
+
this.kvStore.get(key).then((saved) => {
|
|
247
|
+
if (saved) {
|
|
248
|
+
try {
|
|
249
|
+
const gas = JSON.parse(saved);
|
|
250
|
+
if (typeof gas === "number") {
|
|
251
|
+
state.setInitialGasEstimated(gas);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
catch (e) {
|
|
255
|
+
console.warn(e);
|
|
256
|
+
this.kvStore.set(key, "");
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
state.setIsInitialized(true);
|
|
260
|
+
});
|
|
261
|
+
}));
|
|
262
|
+
// Every time the observable used in simulateGasFn is updated, the simulation is refreshed.
|
|
263
|
+
// The simulation is also refreshed when changing from zero fee to paying fee or vice versa.
|
|
264
|
+
this._disposers.push((0, mobx_1.autorun)(() => {
|
|
265
|
+
if (!this.enabled) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
try {
|
|
269
|
+
const key = this.storeKey;
|
|
270
|
+
const state = this.getState(key);
|
|
271
|
+
if (!state.isInitialized) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
const { simulate } = this.simulateGasFn();
|
|
275
|
+
const isZeroFee = GasSimulatorState.isZeroFee(this.feeConfig.fee);
|
|
276
|
+
(0, mobx_1.runInAction)(() => {
|
|
277
|
+
if (state.recentGasEstimated == null ||
|
|
278
|
+
state.error != null ||
|
|
279
|
+
state.isZeroFee !== isZeroFee) {
|
|
280
|
+
state.refreshSimulateFn(simulate);
|
|
281
|
+
state.setIsZeroFee(isZeroFee);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
catch (e) {
|
|
286
|
+
console.log(e);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
}));
|
|
290
|
+
this._disposers.push((0, mobx_1.autorun)(() => {
|
|
291
|
+
const key = this.storeKey;
|
|
292
|
+
const state = this.getState(key);
|
|
293
|
+
if (!state.simulateFn) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (this._debounceTimeoutId) {
|
|
297
|
+
clearTimeout(this._debounceTimeoutId);
|
|
298
|
+
}
|
|
299
|
+
const promise = state.simulateFn();
|
|
300
|
+
this._debounceTimeoutId = setTimeout(() => {
|
|
301
|
+
(0, mobx_1.runInAction)(() => {
|
|
302
|
+
this._isSimulating = true;
|
|
303
|
+
});
|
|
304
|
+
promise
|
|
305
|
+
.then((res) => {
|
|
306
|
+
const { gasUsed, evmSimulationOutcome } = res;
|
|
307
|
+
const shouldUpdate = !state.recentGasEstimated ||
|
|
308
|
+
Math.abs(state.recentGasEstimated - gasUsed) /
|
|
309
|
+
state.recentGasEstimated >
|
|
310
|
+
0.02 ||
|
|
311
|
+
evmSimulationOutcome !== state.recentEvmSimulationOutcome;
|
|
312
|
+
if (shouldUpdate) {
|
|
313
|
+
state.setRecentGasEstimated(gasUsed);
|
|
314
|
+
state.setRecentEvmSimulationOutcome(evmSimulationOutcome);
|
|
315
|
+
}
|
|
316
|
+
state.setError(undefined);
|
|
317
|
+
this.kvStore.set(key, JSON.stringify(gasUsed)).catch((e) => {
|
|
318
|
+
console.log(e);
|
|
319
|
+
});
|
|
320
|
+
})
|
|
321
|
+
.catch((e) => {
|
|
322
|
+
var _a, _b;
|
|
323
|
+
console.log("evm gas simulate error", e);
|
|
324
|
+
if ((0, simple_fetch_1.isSimpleFetchError)(e) && e.response) {
|
|
325
|
+
let message = "";
|
|
326
|
+
const contentType = e.response.headers
|
|
327
|
+
? e.response.headers.get("content-type") || ""
|
|
328
|
+
: "";
|
|
329
|
+
if (contentType.startsWith("text/plain") &&
|
|
330
|
+
typeof e.response.data === "string") {
|
|
331
|
+
message = e.response.data;
|
|
332
|
+
}
|
|
333
|
+
if (contentType.startsWith("application/json") &&
|
|
334
|
+
((_a = e.response.data) === null || _a === void 0 ? void 0 : _a.message) &&
|
|
335
|
+
typeof ((_b = e.response.data) === null || _b === void 0 ? void 0 : _b.message) === "string") {
|
|
336
|
+
message = e.response.data.message;
|
|
337
|
+
}
|
|
338
|
+
if (message !== "") {
|
|
339
|
+
state.setError(new Error(message));
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
state.setError(e);
|
|
344
|
+
})
|
|
345
|
+
.finally(() => {
|
|
346
|
+
(0, mobx_1.runInAction)(() => {
|
|
347
|
+
this._isSimulating = false;
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
}, this._debounceMs);
|
|
351
|
+
}));
|
|
352
|
+
this._disposers.push((0, mobx_1.autorun)(() => {
|
|
353
|
+
if (this.enabled &&
|
|
354
|
+
this.gasEstimated != null &&
|
|
355
|
+
!Number.isNaN(this.gasEstimated)) {
|
|
356
|
+
this.gasConfig.setValue(this.gasEstimated * this.gasAdjustment);
|
|
357
|
+
}
|
|
358
|
+
}));
|
|
359
|
+
}
|
|
360
|
+
dispose() {
|
|
361
|
+
for (const disposer of this._disposers) {
|
|
362
|
+
disposer();
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
get uiProperties() {
|
|
366
|
+
const key = this.storeKey;
|
|
367
|
+
const state = this.getState(key);
|
|
368
|
+
return {
|
|
369
|
+
warning: (() => {
|
|
370
|
+
if (this.forceDisableReason) {
|
|
371
|
+
return this.forceDisableReason;
|
|
372
|
+
}
|
|
373
|
+
if (this.error) {
|
|
374
|
+
return this.error;
|
|
375
|
+
}
|
|
376
|
+
})(),
|
|
377
|
+
loadingState: (() => {
|
|
378
|
+
if (!this.enabled) {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (this.isSimulating) {
|
|
382
|
+
return state.initialGasEstimated == null
|
|
383
|
+
? "loading-block"
|
|
384
|
+
: "loading";
|
|
385
|
+
}
|
|
386
|
+
})(),
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
getState(key) {
|
|
390
|
+
if (!this._stateMap.has(key)) {
|
|
391
|
+
(0, mobx_1.runInAction)(() => {
|
|
392
|
+
this._stateMap.set(key, new GasSimulatorState());
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
return this._stateMap.get(key);
|
|
396
|
+
}
|
|
397
|
+
get storeKey() {
|
|
398
|
+
var _a, _b;
|
|
399
|
+
const chainIdentifier = cosmos_1.ChainIdHelper.parse(this.chainId);
|
|
400
|
+
const feeDenom = (_b = (_a = this.feeConfig.fee) === null || _a === void 0 ? void 0 : _a.currency.coinMinimalDenom) !== null && _b !== void 0 ? _b : "";
|
|
401
|
+
return `${chainIdentifier.identifier}/${feeDenom}/${this.key}`;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
__decorate([
|
|
405
|
+
mobx_1.observable
|
|
406
|
+
], GasSimulator.prototype, "_key", void 0);
|
|
407
|
+
__decorate([
|
|
408
|
+
mobx_1.observable
|
|
409
|
+
], GasSimulator.prototype, "_gasAdjustmentValue", void 0);
|
|
410
|
+
__decorate([
|
|
411
|
+
mobx_1.observable
|
|
412
|
+
], GasSimulator.prototype, "_enabled", void 0);
|
|
413
|
+
__decorate([
|
|
414
|
+
mobx_1.observable
|
|
415
|
+
], GasSimulator.prototype, "_forceDisabled", void 0);
|
|
416
|
+
__decorate([
|
|
417
|
+
mobx_1.observable
|
|
418
|
+
], GasSimulator.prototype, "_forceDisableReason", void 0);
|
|
419
|
+
__decorate([
|
|
420
|
+
mobx_1.observable
|
|
421
|
+
], GasSimulator.prototype, "_isSimulating", void 0);
|
|
422
|
+
__decorate([
|
|
423
|
+
mobx_1.observable.shallow
|
|
424
|
+
], GasSimulator.prototype, "_stateMap", void 0);
|
|
425
|
+
__decorate([
|
|
426
|
+
mobx_1.action
|
|
427
|
+
], GasSimulator.prototype, "setKey", null);
|
|
428
|
+
__decorate([
|
|
429
|
+
mobx_1.action
|
|
430
|
+
], GasSimulator.prototype, "setEnabled", null);
|
|
431
|
+
__decorate([
|
|
432
|
+
mobx_1.action
|
|
433
|
+
], GasSimulator.prototype, "forceDisable", null);
|
|
434
|
+
__decorate([
|
|
435
|
+
mobx_1.action
|
|
436
|
+
], GasSimulator.prototype, "setGasAdjustmentValue", null);
|
|
437
|
+
__decorate([
|
|
438
|
+
mobx_1.computed
|
|
439
|
+
], GasSimulator.prototype, "storeKey", null);
|
|
440
|
+
exports.GasSimulator = GasSimulator;
|
|
441
|
+
// CONTRACT: Use with `observer`
|
|
442
|
+
const useGasSimulator = (kvStore, chainGetter, chainId, gasConfig, feeConfig, key, simulateGasFn, initialDisabled) => {
|
|
443
|
+
const [gasSimulator] = (0, react_1.useState)(() => {
|
|
444
|
+
const gasSimulator = new GasSimulator(kvStore, chainGetter, chainId, gasConfig, feeConfig, key, simulateGasFn);
|
|
445
|
+
if (initialDisabled) {
|
|
446
|
+
gasSimulator.setEnabled(false);
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
gasSimulator.setEnabled(true);
|
|
450
|
+
}
|
|
451
|
+
return gasSimulator;
|
|
452
|
+
});
|
|
453
|
+
gasSimulator.setKVStore(kvStore);
|
|
454
|
+
gasSimulator.setChain(chainId);
|
|
455
|
+
gasSimulator.setKey(key);
|
|
456
|
+
gasSimulator.setSimulateGasFn(simulateGasFn);
|
|
457
|
+
(0, react_1.useEffect)(() => {
|
|
458
|
+
return () => {
|
|
459
|
+
gasSimulator.dispose();
|
|
460
|
+
};
|
|
461
|
+
}, [gasSimulator]);
|
|
462
|
+
return gasSimulator;
|
|
463
|
+
};
|
|
464
|
+
exports.useGasSimulator = useGasSimulator;
|
|
465
|
+
//# sourceMappingUR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AKR;AA/XH,oCAgYC;AAED,gCAAgC;AACzB,MAAM,eAAe,GAAG,CAC7B,OAAgB,EAChB,WAAwB,EACxB,OAAe,EACf,SAAqB,EACrB,SAAqB,EACrB,GAAW,EACX,aAA4B,EAC5B,eAAyB,EACzB,EAAE;IACF,MAAM,CAAC,YAAY,CAAC,GAAG,IAAA,gBAAQ,EAAC,GAAG,EAAE;QACnC,MAAM,YAAY,GAAG,IAAI,YAAY,CACnC,OAAO,EACP,WAAW,EACX,OAAO,EACP,SAAS,EACT,SAAS,EACT,GAAG,EACH,aAAa,CACd,CAAC;QACF,IAAI,eAAe,EAAE;YACnB,YAAY,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;SAChC;aAAM;YACL,YAAY,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;SAC/B;QAED,OAAO,YAAY,CAAC;IACtB,CAAC,CAAC,CAAC;IACH,YAAY,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IACjC,YAAY,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IAC/B,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACzB,YAAY,CAAC,gBAAgB,CAAC,aAAa,CAAC,CAAC;IAE7C,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,OAAO,GAAG,EAAE;YACV,YAAY,CAAC,OAAO,EAAE,CAAC;QACzB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC;IAEnB,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAxCW,QAAA,eAAe,mBAwC1B"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IGasConfig, UIProperties } from "./types";
|
|
2
|
+
import { TxChainSetter } from "./chain";
|
|
3
|
+
import { ChainGetter } from "@keplr-wallet/stores";
|
|
4
|
+
export declare class GasConfig extends TxChainSetter implements IGasConfig {
|
|
5
|
+
protected _value: string;
|
|
6
|
+
constructor(chainGetter: ChainGetter, initialChainId: string, initialGas?: number);
|
|
7
|
+
get value(): string;
|
|
8
|
+
setValue(value: string | number): void;
|
|
9
|
+
get gas(): number;
|
|
10
|
+
get uiProperties(): UIProperties;
|
|
11
|
+
}
|
|
12
|
+
export declare const useGasConfig: (chainGetter: ChainGetter, chainId: string, initialGas?: number) => GasConfig;
|
package/build/tx/gas.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.useGasConfig = exports.GasConfig = void 0;
|
|
10
|
+
const chain_1 = require("./chain");
|
|
11
|
+
const mobx_1 = require("mobx");
|
|
12
|
+
const react_1 = require("react");
|
|
13
|
+
class GasConfig extends chain_1.TxChainSetter {
|
|
14
|
+
constructor(chainGetter, initialChainId, initialGas) {
|
|
15
|
+
super(chainGetter, initialChainId);
|
|
16
|
+
this._value = "";
|
|
17
|
+
if (initialGas) {
|
|
18
|
+
this._value = initialGas.toString();
|
|
19
|
+
}
|
|
20
|
+
(0, mobx_1.makeObservable)(this);
|
|
21
|
+
}
|
|
22
|
+
get value() {
|
|
23
|
+
return this._value;
|
|
24
|
+
}
|
|
25
|
+
setValue(value) {
|
|
26
|
+
if (typeof value === "number") {
|
|
27
|
+
this._value = Math.ceil(value).toString();
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this._value = value;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
get gas() {
|
|
34
|
+
if (this.value.trim() === "") {
|
|
35
|
+
return 0;
|
|
36
|
+
}
|
|
37
|
+
const num = Number.parseInt(this.value);
|
|
38
|
+
if (Number.isNaN(num)) {
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
return num;
|
|
42
|
+
}
|
|
43
|
+
get uiProperties() {
|
|
44
|
+
if (this.value.trim() === "") {
|
|
45
|
+
return {
|
|
46
|
+
error: new Error("Please enter a gas amount"),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const parsed = Number.parseFloat(this.value);
|
|
50
|
+
if (Number.isNaN(parsed)) {
|
|
51
|
+
return {
|
|
52
|
+
error: new Error("Enter a valid number for gas"),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (this.value.includes(".") || !Number.isInteger(parsed)) {
|
|
56
|
+
return {
|
|
57
|
+
error: new Error("Gas must be a whole number"),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (this.gas <= 0) {
|
|
61
|
+
return {
|
|
62
|
+
error: new Error("Gas must be greater than 0"),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
__decorate([
|
|
69
|
+
mobx_1.observable
|
|
70
|
+
], GasConfig.prototype, "_value", void 0);
|
|
71
|
+
__decorate([
|
|
72
|
+
mobx_1.action
|
|
73
|
+
], GasConfig.prototype, "setValue", null);
|
|
74
|
+
__decorate([
|
|
75
|
+
mobx_1.computed
|
|
76
|
+
], GasConfig.prototype, "uiProperties", null);
|
|
77
|
+
exports.GasConfig = GasConfig;
|
|
78
|
+
const useGasConfig = (chainGetter, chainId, initialGas) => {
|
|
79
|
+
const [txConfig] = (0, react_1.useState)(() => new GasConfig(chainGetter, chainId, initialGas));
|
|
80
|
+
txConfig.setChain(chainId);
|
|
81
|
+
return txConfig;
|
|
82
|
+
};
|
|
83
|
+
exports.useGasCon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