Skip to content

Commit f603f1a

Browse files
committed
add estimateWriteContractGas
1 parent 6915b50 commit f603f1a

File tree

7 files changed

+133
-81
lines changed

7 files changed

+133
-81
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ let data = await writeContract({
137137
})
138138
```
139139

140+
## EstimateWriteContractGas
141+
142+
```js
143+
import {erc20ABI, estimateWriteContractGas} from '@kolirt/vue-web3-auth'
144+
145+
const gas = await estimateWriteContractGas({
146+
abi: erc20ABI,
147+
address: '0x55d398326f99059fF775485246999027B3197955',
148+
functionName: 'approve',
149+
args: ['0x685B1ded8013785d6623CC18D214320b6Bb64759', 100]
150+
}).catch(e => {})
151+
```
152+
140153
# Demo
141154
[Demo here](https://kolirt.github.io/vue-web3-auth/).
142155

dist/vue-web3-auth.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export declare const erc20ABI: ({
7070
anonymous?: undefined;
7171
})[];
7272

73+
export declare function estimateWriteContractGas(data: WriteContract): Promise<bigint>;
74+
7375
export declare enum Events {
7476
Connected = "connected",
7577
Disconnected = "disconnect",

dist/vue-web3-auth.js

Lines changed: 90 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as J from "@wagmi/core/chains";
2-
import { reactive as m, computed as k, ref as M, watch as g, toRaw as h } from "vue";
3-
import { disconnect as v, configureChains as $, createConfig as I, watchNetwork as E, watchAccount as x, readContract as N, writeContract as S, waitForTransaction as U, multicall as D } from "@wagmi/core";
4-
import { w3mProvider as F, w3mConnectors as _, EthereumClient as j } from "@web3modal/ethereum";
5-
import { Web3Modal as P } from "@web3modal/html";
1+
import * as K from "@wagmi/core/chains";
2+
import { reactive as y, computed as k, ref as M, watch as g, toRaw as f } from "vue";
3+
import { disconnect as v, configureChains as I, createConfig as $, watchNetwork as x, watchAccount as P, readContract as E, writeContract as F, waitForTransaction as N, getPublicClient as S, multicall as U } from "@wagmi/core";
4+
import { w3mProvider as D, w3mConnectors as G, EthereumClient as _ } from "@web3modal/ethereum";
5+
import { Web3Modal as j } from "@web3modal/html";
66
import A from "js-event-bus";
77
var H = {
88
id: 1,
@@ -49,7 +49,7 @@ var H = {
4949
}
5050
}
5151
};
52-
const V = H, t = m({
52+
const V = H, t = y({
5353
autoInit: !0,
5454
projectId: "",
5555
chains: [V],
@@ -62,21 +62,21 @@ const V = H, t = m({
6262
themeVariables: {}
6363
}
6464
});
65-
function G(e) {
65+
function W(e) {
6666
"autoInit" in e && (t.autoInit = e.autoInit), "projectId" in e && (t.projectId = e.projectId), "chains" in e && (t.chains = e.chains), "autoConnect" in e && (t.autoConnect = e.autoConnect), "disconnectUnknownChain" in e && (t.disconnectUnknownChain = e.disconnectUnknownChain), "reconnectToChain" in e && (t.reconnectToChain = e.reconnectToChain), "logEnabled" in e && (t.logEnabled = e.logEnabled), "web3modalOptions" in e && ("themeMode" in e.web3modalOptions && (t.web3modalOptions.themeMode = e.web3modalOptions.themeMode), "themeVariables" in e.web3modalOptions && (t.web3modalOptions.themeVariables = e.web3modalOptions.themeVariables));
6767
}
68-
const c = m({
68+
const c = y({
6969
bufferChain: null,
7070
currentChain: null
71-
}), f = k(() => c.currentChain ? c.currentChain : t.chains[0]);
72-
function O() {
71+
}), h = k(() => c.currentChain ? c.currentChain : t.chains[0]);
72+
function B() {
7373
return t.chains;
7474
}
75-
async function B(e) {
75+
async function ee(e) {
7676
var n;
7777
o.value || b(), await ((n = w.client) == null ? void 0 : n.switchNetwork({ chainId: e.id }));
7878
}
79-
async function ee() {
79+
async function ne() {
8080
var e;
8181
o.value || b(), await ((e = o.value) == null ? void 0 : e.openModal({
8282
route: "SelectNetwork"
@@ -87,136 +87,151 @@ function r(...e) {
8787
}
8888
var s = /* @__PURE__ */ ((e) => (e.Connected = "connected", e.Disconnected = "disconnect", e.ChainSwitched = "chain_switched", e.UnknownChain = "unknown_chain", e.ModalStateChanged = "modal_state_changed", e))(s || {});
8989
const T = new A();
90-
function l(e, ...n) {
90+
function u(e, ...n) {
9191
e === s.Connected || e === s.Disconnected ? (c.currentChain = c.bufferChain, d.currentAccount = d.bufferAccount) : e === s.ChainSwitched && (c.currentChain = c.bufferChain), T.emit(e, null, ...n);
9292
}
93-
function ne(e, n) {
93+
function te(e, n) {
9494
T.on(e, n), t.logEnabled && r(`Subscribe for ${e} event.`);
9595
}
96-
function te(e, n) {
96+
function ae(e, n) {
9797
T.detach(e, n), t.logEnabled && r(`Unsubscribe for ${e} event.`);
9898
}
9999
const o = M(null);
100-
function W(e) {
100+
function L(e) {
101101
var n;
102-
o.value = new P(
102+
o.value = new j(
103103
{
104104
projectId: t.projectId,
105105
...(t == null ? void 0 : t.web3modalOptions) || []
106106
},
107107
e
108108
), (n = o.value) == null || n.subscribeModal(({ open: a }) => {
109-
l(s.ModalStateChanged, a);
109+
u(s.ModalStateChanged, a);
110110
});
111111
}
112-
const d = m({
112+
const d = y({
113113
bufferAccount: null,
114114
currentAccount: null
115-
}), u = m({
115+
}), l = y({
116116
connected: !1,
117117
address: void 0,
118118
shortAddress: void 0
119119
});
120-
async function L() {
120+
async function R() {
121121
await v();
122122
}
123-
async function ae(e) {
123+
async function ie(e) {
124124
var n, a;
125125
o.value || b(), e instanceof Event && (e = t.chains[0]), (n = o.value) == null || n.setDefaultChain(e || t.chains[0]), await ((a = o.value) == null ? void 0 : a.openModal({
126126
route: "ConnectWallet"
127127
}));
128128
}
129-
async function ie() {
129+
async function se() {
130130
var e;
131131
o.value || b(), await ((e = o.value) == null ? void 0 : e.openModal({
132132
route: "Account"
133133
}));
134134
}
135-
function R(e = "") {
135+
function z(e = "") {
136136
return `${e.slice(0, 5)}...${e.slice(-4)}`;
137137
}
138138
g(() => d.currentAccount, (e) => {
139-
e ? (u.connected = !0, u.address = e.address, u.shortAddress = R(e.address)) : (u.connected = !1, u.address = void 0, u.shortAddress = void 0);
139+
e ? (l.connected = !0, l.address = e.address, l.shortAddress = z(e.address)) : (l.connected = !1, l.address = void 0, l.shortAddress = void 0);
140140
});
141141
let C;
142-
const w = m({
142+
const w = y({
143143
client: null
144144
});
145-
function y(e) {
145+
function m(e) {
146146
return String(e).toLowerCase();
147147
}
148-
async function z([e, n], [a, i]) {
148+
async function q([e, n], [a, i]) {
149149
if (t.disconnectUnknownChain && (!a && t.disconnectUnknownChain || a) && n && !t.chains.some((p) => p.id === n.id)) {
150-
await L(), i && (l(s.Disconnected), r(`account ${e.address} disconnected from ${y(i.name)} chain.`)), l(s.UnknownChain, { chain: n }), r("switched to unsupported chain.");
150+
await R(), i && (u(s.Disconnected), r(`account ${e.address} disconnected from ${m(i.name)} chain.`)), u(s.UnknownChain, { chain: n }), r("switched to unsupported chain.");
151151
return;
152152
}
153-
(a == null ? void 0 : a.address) !== (e == null ? void 0 : e.address) && !(i != null && i.unsupported) && (a && (l(s.Disconnected), r(`account ${a.address} disconnected from ${y(i.name)} chain.`)), e && (l(s.Connected, { chain: c.bufferChain, account: d.bufferAccount }), r(`account ${e.address} connected to ${y(n.name)} chain.`))), i && n && i.id !== n.id && (t.reconnectToChain ? (l(s.Disconnected), r(`account ${a.address} disconnected from ${y(i.name)} chain.`), l(s.Connected, { chain: c.bufferChain, account: d.bufferAccount }), r(`account ${e.address} connected to ${y(n.name)} chain.`)) : (l(s.ChainSwitched, { chain: n }), r(`account ${e.address} switched to ${y(n.name)} chain.`)));
153+
(a == null ? void 0 : a.address) !== (e == null ? void 0 : e.address) && !(i != null && i.unsupported) && (a && (u(s.Disconnected), r(`account ${a.address} disconnected from ${m(i.name)} chain.`)), e && (u(s.Connected, { chain: c.bufferChain, account: d.bufferAccount }), r(`account ${e.address} connected to ${m(n.name)} chain.`))), i && n && i.id !== n.id && (t.reconnectToChain ? (u(s.Disconnected), r(`account ${a.address} disconnected from ${m(i.name)} chain.`), u(s.Connected, { chain: c.bufferChain, account: d.bufferAccount }), r(`account ${e.address} connected to ${m(n.name)} chain.`)) : (u(s.ChainSwitched, { chain: n }), r(`account ${e.address} switched to ${m(n.name)} chain.`)));
154154
}
155-
function q([e, n], [a, i]) {
156-
clearTimeout(C), C = setTimeout(z, 200, [e, n], [a, i]);
155+
function J([e, n], [a, i]) {
156+
clearTimeout(C), C = setTimeout(q, 200, [e, n], [a, i]);
157157
}
158158
function b() {
159159
if (o.value)
160160
return;
161-
const { publicClient: e } = $(h(t.chains), [F({ projectId: t.projectId })]), n = I({
161+
const { publicClient: e } = I(f(t.chains), [D({ projectId: t.projectId })]), n = $({
162162
autoConnect: t.autoConnect,
163-
connectors: _({
163+
connectors: G({
164164
projectId: t.projectId,
165165
version: 2,
166-
chains: h(t.chains)
166+
chains: f(t.chains)
167167
}),
168168
publicClient: e
169169
});
170-
E((i) => {
170+
x((i) => {
171171
var p;
172172
(p = i.chain) != null && p.unsupported && (i.chain.name = "Unsupported"), c.bufferChain = i.chain || null;
173-
}), x((i) => {
173+
}), P((i) => {
174174
d.bufferAccount = i.address ? i : null;
175-
}), g([() => d.bufferAccount, () => c.bufferChain], q);
176-
const a = new j(n, h(t.chains));
177-
w.client = a, W(a);
175+
}), g([() => d.bufferAccount, () => c.bufferChain], J);
176+
const a = new _(n, f(t.chains));
177+
w.client = a, L(a);
178178
}
179-
function se(e) {
179+
function oe(e) {
180180
return {
181181
install() {
182-
G(e), b();
182+
W(e), b();
183183
}
184184
};
185185
}
186-
async function oe(e) {
187-
return N({
188-
chainId: e.chainId || f.value.id,
186+
async function ce(e) {
187+
return E({
188+
chainId: e.chainId || h.value.id,
189189
address: e.address,
190190
abi: e.abi,
191191
functionName: e.functionName,
192192
args: e.args || [],
193-
account: e.account || u.address,
193+
account: e.account || l.address,
194194
blockNumber: e.blockNumber,
195195
blockTag: e.blockTag
196196
});
197197
}
198-
async function ce(e) {
199-
const { hash: n } = await S({
200-
chainId: e.chainId || f.value.id,
198+
async function re(e) {
199+
const { hash: n } = await F({
200+
chainId: e.chainId || h.value.id,
201201
address: e.address,
202202
abi: e.abi,
203203
functionName: e.functionName,
204204
args: e.args || [],
205-
account: e.account || u.address,
205+
account: e.account || l.address,
206206
gas: e.gas,
207207
gasPrice: e.gasPrice,
208208
maxFeePerGas: e.maxFeePerGas,
209209
maxPriorityFeePerGas: e.maxPriorityFeePerGas,
210210
nonce: e.nonce,
211211
value: e.value
212212
});
213-
return U({
214-
chainId: e.chainId || f.value.id,
213+
return N({
214+
chainId: e.chainId || h.value.id,
215215
hash: n,
216216
confirmations: e.confirmations || 1
217217
});
218218
}
219-
async function re(e) {
219+
async function le(e) {
220+
return await S({ chainId: e.chainId || h.value.id }).estimateContractGas({
221+
address: e.address,
222+
abi: e.abi,
223+
functionName: e.functionName,
224+
args: e.args || [],
225+
// @ts-ignore
226+
account: e.account || l.address,
227+
gasPrice: e.gasPrice,
228+
maxFeePerGas: e.maxFeePerGas,
229+
maxPriorityFeePerGas: e.maxPriorityFeePerGas,
230+
nonce: e.nonce,
231+
value: e.value
232+
});
233+
}
234+
async function ue(e) {
220235
const n = [];
221236
return e.calls.forEach((a) => {
222237
a.calls.forEach(([i, p]) => {
@@ -227,7 +242,7 @@ async function re(e) {
227242
args: p
228243
});
229244
});
230-
}), await D({
245+
}), await U({
231246
chainId: e.chainId,
232247
contracts: n,
233248
multicallAddress: e.multicallAddress,
@@ -237,7 +252,7 @@ async function re(e) {
237252
allowFailure: e.allowFailure
238253
});
239254
}
240-
const le = [
255+
const de = [
241256
{
242257
inputs: [
243258
{ internalType: "string", name: "name_", type: "string" },
@@ -431,7 +446,7 @@ const le = [
431446
stateMutability: "nonpayable",
432447
type: "function"
433448
}
434-
], ue = [
449+
], pe = [
435450
{
436451
inputs: [
437452
{
@@ -573,23 +588,24 @@ const le = [
573588
}
574589
];
575590
export {
576-
te as $off,
577-
ne as $on,
578-
J as Chains,
591+
ae as $off,
592+
te as $on,
593+
K as Chains,
579594
s as Events,
580-
u as account,
581-
ie as accountDetails,
582-
f as chain,
583-
ae as connect,
584-
se as createWeb3Auth,
585-
L as disconnect,
586-
le as erc20ABI,
587-
O as getAvailableChains,
588-
re as multicall,
589-
ue as multicallABI,
590-
oe as readContract,
591-
ee as selectChain,
592-
R as shortAddressFilter,
593-
B as switchChain,
594-
ce as writeContract
595+
l as account,
596+
se as accountDetails,
597+
h as chain,
598+
ie as connect,
599+
oe as createWeb3Auth,
600+
R as disconnect,
601+
de as erc20ABI,
602+
le as estimateWriteContractGas,
603+
B as getAvailableChains,
604+
ue as multicall,
605+
pe as multicallABI,
606+
ce as readContract,
607+
ne as selectChain,
608+
z as shortAddressFilter,
609+
ee as switchChain,
610+
re as writeContract
595611
};

0 commit comments

Comments
 (0)