@gardenfi/utils 2.5.0 → 2.5.2
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/dist/index4.cjs +1 -1
- package/dist/index4.js +38 -38
- package/package.json +1 -1
package/dist/index4.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("./index3.cjs"),e=require("./index5.cjs"),g=require("./index19.cjs"),l=require("./index20.cjs"),A=require("./index21.cjs"),h=require("./index22.cjs"),m=async(a,o,i,t)=>{if(!t.account)return e.Err("No account found");const n=g.getContract({address:u.with0x(o),abi:h.erc20Abi,client:t});try{const c=u.with0x(t.account.address),r=u.with0x(i),f=await n.read.allowance([c,r]);if(BigInt(f)<BigInt(a)){const p=await n.write.approve([r,l.maxUint256],{account:t.account,chain:t.chain}),s=await w(t,p);if(!s.ok)return e.Err(s.error);if(!s.val||s.val.status!=="success")return e.Err("Failed to approve");const d=await n.read.allowance([c,r]);return d<a?e.Err(`Insufficient allowance after approval. Needed ${a.toString()}, got ${d.toString()}.`):e.Ok(p)}return e.Ok("Already approved")}catch(c){return e.Err("Failed to approve: "+c)}},w=async(a,o,i=2e3,t=12e4)=>{const n=Math.ceil(t/i);for(let c=0;c<n;c++){try{const r=await A.getTransactionReceipt(a,{hash:o});if(r)return e.Ok(r)}catch(r){if(!r.message.includes(`Transaction receipt with hash "${o}" could not be found.`))return e.Err(r.message)}c<n-1&&await new Promise(r=>setTimeout(r,i))}return e.Err(`Timed out waiting for receipt of ${o}`)};exports.checkAllowanceAndApprove=m;exports.waitForTransactionReceipt=w;
|
package/dist/index4.js
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
import { with0x as
|
|
2
|
-
import { Err as
|
|
3
|
-
import { getContract as
|
|
4
|
-
import { maxUint256 as
|
|
5
|
-
import { getTransactionReceipt as
|
|
6
|
-
import { erc20Abi as
|
|
7
|
-
const
|
|
8
|
-
if (!t.account) return
|
|
9
|
-
const
|
|
10
|
-
address:
|
|
11
|
-
abi:
|
|
1
|
+
import { with0x as p } from "./index3.js";
|
|
2
|
+
import { Err as o, Ok as f } from "./index5.js";
|
|
3
|
+
import { getContract as w } from "./index19.js";
|
|
4
|
+
import { maxUint256 as g } from "./index20.js";
|
|
5
|
+
import { getTransactionReceipt as l } from "./index21.js";
|
|
6
|
+
import { erc20Abi as v } from "./index22.js";
|
|
7
|
+
const k = async (c, n, i, t) => {
|
|
8
|
+
if (!t.account) return o("No account found");
|
|
9
|
+
const a = w({
|
|
10
|
+
address: p(n),
|
|
11
|
+
abi: v,
|
|
12
12
|
client: t
|
|
13
13
|
});
|
|
14
14
|
try {
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
15
|
+
const e = p(t.account.address), r = p(i), m = await a.read.allowance([e, r]);
|
|
16
|
+
if (BigInt(m) < BigInt(c)) {
|
|
17
|
+
const u = await a.write.approve([r, g], {
|
|
18
|
+
account: t.account,
|
|
19
|
+
chain: t.chain
|
|
20
|
+
}), s = await A(t, u);
|
|
21
|
+
if (!s.ok) return o(s.error);
|
|
22
|
+
if (!s.val || s.val.status !== "success")
|
|
23
|
+
return o("Failed to approve");
|
|
24
|
+
const d = await a.read.allowance([e, r]);
|
|
25
|
+
return d < c ? o(
|
|
26
|
+
`Insufficient allowance after approval. Needed ${c.toString()}, got ${d.toString()}.`
|
|
27
|
+
) : f(u);
|
|
28
28
|
}
|
|
29
|
-
return
|
|
30
|
-
} catch (
|
|
31
|
-
return
|
|
29
|
+
return f("Already approved");
|
|
30
|
+
} catch (e) {
|
|
31
|
+
return o("Failed to approve: " + e);
|
|
32
32
|
}
|
|
33
|
-
},
|
|
34
|
-
const
|
|
35
|
-
for (let
|
|
33
|
+
}, A = async (c, n, i = 2e3, t = 12e4) => {
|
|
34
|
+
const a = Math.ceil(t / i);
|
|
35
|
+
for (let e = 0; e < a; e++) {
|
|
36
36
|
try {
|
|
37
|
-
const r = await
|
|
37
|
+
const r = await l(c, { hash: n });
|
|
38
38
|
if (r)
|
|
39
|
-
return
|
|
39
|
+
return f(r);
|
|
40
40
|
} catch (r) {
|
|
41
41
|
if (!r.message.includes(
|
|
42
|
-
`Transaction receipt with hash "${
|
|
42
|
+
`Transaction receipt with hash "${n}" could not be found.`
|
|
43
43
|
))
|
|
44
|
-
return
|
|
44
|
+
return o(r.message);
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
e < a - 1 && await new Promise((r) => setTimeout(r, i));
|
|
47
47
|
}
|
|
48
|
-
return
|
|
48
|
+
return o(`Timed out waiting for receipt of ${n}`);
|
|
49
49
|
};
|
|
50
50
|
export {
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
k as checkAllowanceAndApprove,
|
|
52
|
+
A as waitForTransactionReceipt
|
|
53
53
|
};
|