@defuse-protocol/nearintents-mpp-sdk 0.4.1 → 0.5.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/dist/client/Charge.d.ts +20 -1
- package/dist/client/Charge.d.ts.map +1 -1
- package/dist/client/Charge.js +75 -13
- package/dist/client/Charge.js.map +1 -1
- package/package.json +4 -4
- package/src/client/Charge.ts +90 -15
package/dist/client/Charge.d.ts
CHANGED
|
@@ -17,6 +17,16 @@ import * as Types from '../Types.js';
|
|
|
17
17
|
* `eip155:*` origins (native transfers and ERC-20 `transfer`); any viem
|
|
18
18
|
* WalletClient extended with public actions satisfies the interface.
|
|
19
19
|
*
|
|
20
|
+
* A method instance pays each deposit address (and memo) at most once per
|
|
21
|
+
* payer: a later challenge for the same address re-presents the first
|
|
22
|
+
* deposit's hash. After a non-success terminal, the server's retry challenge
|
|
23
|
+
* can still carry the spent quote, which mppx's fetch would otherwise pay
|
|
24
|
+
* again. The payer is the wallet's account for the built-in broadcast and the
|
|
25
|
+
* instance itself for `sendDeposit`; a wallet without an account address is
|
|
26
|
+
* not tracked. This holds only within one instance, so keep one for the life
|
|
27
|
+
* of the payer's session. A deposit is forgotten 24 hours after the latest
|
|
28
|
+
* `expires` of a challenge for it, long after any quote for it has expired.
|
|
29
|
+
*
|
|
20
30
|
* @example
|
|
21
31
|
* ```ts
|
|
22
32
|
* import { Mppx } from 'mppx/client'
|
|
@@ -124,8 +134,14 @@ export declare namespace charge {
|
|
|
124
134
|
hash: `0x${string}`;
|
|
125
135
|
}) => Promise<{
|
|
126
136
|
status?: string | undefined;
|
|
137
|
+
transactionHash?: string | undefined;
|
|
127
138
|
}>) | undefined;
|
|
128
139
|
};
|
|
140
|
+
/**
|
|
141
|
+
* Pays the deposit and resolves with its confirmed tx hash. Reject only when
|
|
142
|
+
* nothing was broadcast: a rejection lets the next challenge for this
|
|
143
|
+
* deposit address pay it again.
|
|
144
|
+
*/
|
|
129
145
|
type SendDeposit = (parameters: {
|
|
130
146
|
challenge: {
|
|
131
147
|
id: string;
|
|
@@ -136,7 +152,10 @@ export declare namespace charge {
|
|
|
136
152
|
type Parameters = {
|
|
137
153
|
/** Payment policy asserted before every deposit. */
|
|
138
154
|
policy?: Policy | undefined;
|
|
139
|
-
/**
|
|
155
|
+
/**
|
|
156
|
+
* Pays the origin-chain leg and resolves with the confirmed tx hash. Reject
|
|
157
|
+
* only when nothing was broadcast, or the next challenge pays again.
|
|
158
|
+
*/
|
|
140
159
|
sendDeposit?: SendDeposit | undefined;
|
|
141
160
|
/** EVM wallet for the built-in `eip155:*` broadcast. */
|
|
142
161
|
walletClient?: EvmWalletClient | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Charge.d.ts","sourceRoot":"","sources":["../../src/client/Charge.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+B,MAAM,EAAE,CAAC,EAAE,MAAM,MAAM,CAAA;AAG7D,OAAO,KAAK,KAAK,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"Charge.d.ts","sourceRoot":"","sources":["../../src/client/Charge.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+B,MAAM,EAAE,CAAC,EAAE,MAAM,MAAM,CAAA;AAG7D,OAAO,KAAK,KAAK,MAAM,aAAa,CAAA;AAIpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,wBAAgB,MAAM,CAAC,UAAU,GAAE,MAAM,CAAC,UAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBA8FxD;AAED,MAAM,CAAC,OAAO,WAAW,MAAM,CAAC;IAC9B;;;;OAIG;IACH,KAAK,MAAM,GAAG;QACZ,8DAA8D;QAC9D,qBAAqB,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,CAAA;QACrD,+DAA+D;QAC/D,iBAAiB,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,CAAA;QAC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vD,OAAO,EAAE,KAAK,CAAC,aAAa,CAAA;KAC7B,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;IAErB,KAAK,UAAU,GAAG;QAChB,oDAAoD;QACpD,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QAC3B;;;WAGG;QACH,WAAW,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;QACrC,wDAAwD;QACxD,YAAY,CAAC,EAAE,eAAe,GAAG,SAAS,CAAA;QAC1C,mFAAmF;QACnF,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAC5B,CAAA;CACF"}
|
package/dist/client/Charge.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Credential, Errors, Expires, Method, z } from 'mppx';
|
|
2
2
|
import * as Methods from '../Methods.js';
|
|
3
3
|
import * as Types from '../Types.js';
|
|
4
|
+
const forgetAfterMs = 24 * 60 * 60 * 1000;
|
|
4
5
|
/**
|
|
5
6
|
* Creates a `nearintents` charge method for usage on the client.
|
|
6
7
|
*
|
|
@@ -18,6 +19,16 @@ import * as Types from '../Types.js';
|
|
|
18
19
|
* `eip155:*` origins (native transfers and ERC-20 `transfer`); any viem
|
|
19
20
|
* WalletClient extended with public actions satisfies the interface.
|
|
20
21
|
*
|
|
22
|
+
* A method instance pays each deposit address (and memo) at most once per
|
|
23
|
+
* payer: a later challenge for the same address re-presents the first
|
|
24
|
+
* deposit's hash. After a non-success terminal, the server's retry challenge
|
|
25
|
+
* can still carry the spent quote, which mppx's fetch would otherwise pay
|
|
26
|
+
* again. The payer is the wallet's account for the built-in broadcast and the
|
|
27
|
+
* instance itself for `sendDeposit`; a wallet without an account address is
|
|
28
|
+
* not tracked. This holds only within one instance, so keep one for the life
|
|
29
|
+
* of the payer's session. A deposit is forgotten 24 hours after the latest
|
|
30
|
+
* `expires` of a challenge for it, long after any quote for it has expired.
|
|
31
|
+
*
|
|
21
32
|
* @example
|
|
22
33
|
* ```ts
|
|
23
34
|
* import { Mppx } from 'mppx/client'
|
|
@@ -38,6 +49,9 @@ import * as Types from '../Types.js';
|
|
|
38
49
|
* ```
|
|
39
50
|
*/
|
|
40
51
|
export function charge(parameters = {}) {
|
|
52
|
+
// ponytail: in-memory and per instance; persist it if a client must survive restarts.
|
|
53
|
+
const deposits = new Map();
|
|
54
|
+
let nextSweep = 0;
|
|
41
55
|
return Method.toClient(Methods.charge, {
|
|
42
56
|
context: z.object({
|
|
43
57
|
/** Tx hash of an already-broadcast deposit (push mode, bring-your-own-broadcast). */
|
|
@@ -67,14 +81,48 @@ export function charge(parameters = {}) {
|
|
|
67
81
|
Expires.assert(challenge.expires, challenge.id);
|
|
68
82
|
assertPolicy(parameters.policy, request);
|
|
69
83
|
const walletClient = context?.walletClient ?? parameters.walletClient;
|
|
70
|
-
const hash = await (
|
|
84
|
+
const hash = await (() => {
|
|
71
85
|
if (context?.hash)
|
|
72
86
|
return context.hash;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
87
|
+
const pay = async () => {
|
|
88
|
+
if (parameters.sendDeposit)
|
|
89
|
+
return parameters.sendDeposit({ challenge, request });
|
|
90
|
+
if (walletClient)
|
|
91
|
+
return broadcastEvmDeposit(walletClient, request);
|
|
92
|
+
throw new Error('nearintents-mpp-sdk: no way to pay — provide context.hash, a sendDeposit callback, or a walletClient.');
|
|
93
|
+
};
|
|
94
|
+
const payer = parameters.sendDeposit ? '' : walletClient?.account?.address.toLowerCase();
|
|
95
|
+
if (payer === undefined)
|
|
96
|
+
return pay();
|
|
97
|
+
const now = Date.now();
|
|
98
|
+
if (now >= nextSweep) {
|
|
99
|
+
for (const [entryKey, entry] of deposits)
|
|
100
|
+
if (entry.until <= now)
|
|
101
|
+
deposits.delete(entryKey);
|
|
102
|
+
nextSweep = now + 60_000;
|
|
103
|
+
}
|
|
104
|
+
const key = JSON.stringify([
|
|
105
|
+
payer,
|
|
106
|
+
request.methodDetails.originNetwork,
|
|
107
|
+
request.recipient,
|
|
108
|
+
request.methodDetails.depositMemo ?? null,
|
|
109
|
+
]);
|
|
110
|
+
const until = Math.max(now, Date.parse(challenge.expires ?? '') || now) + forgetAfterMs;
|
|
111
|
+
const paid = deposits.get(key);
|
|
112
|
+
if (paid && paid.until > now) {
|
|
113
|
+
paid.until = Math.max(paid.until, until);
|
|
114
|
+
return paid.hash;
|
|
115
|
+
}
|
|
116
|
+
const entry = { hash: pay(), until };
|
|
117
|
+
deposits.set(key, entry);
|
|
118
|
+
// Keep a sent deposit's hash; forget anything else so it can be retried.
|
|
119
|
+
entry.hash.catch((error) => {
|
|
120
|
+
if (error instanceof DepositUnconfirmedError)
|
|
121
|
+
entry.hash = Promise.resolve(error.hash);
|
|
122
|
+
else if (deposits.get(key) === entry)
|
|
123
|
+
deposits.delete(key);
|
|
124
|
+
});
|
|
125
|
+
return entry.hash;
|
|
78
126
|
})();
|
|
79
127
|
const source = parameters.source ??
|
|
80
128
|
(walletClient?.account?.address
|
|
@@ -160,13 +208,27 @@ async function broadcastEvmDeposit(walletClient, request) {
|
|
|
160
208
|
});
|
|
161
209
|
})();
|
|
162
210
|
// Present a *confirmed* hash when the client can wait for inclusion.
|
|
163
|
-
if (walletClient.waitForTransactionReceipt)
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
211
|
+
if (!walletClient.waitForTransactionReceipt)
|
|
212
|
+
return hash;
|
|
213
|
+
let receipt;
|
|
214
|
+
try {
|
|
215
|
+
receipt = await walletClient.waitForTransactionReceipt({ hash: hash });
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
throw new DepositUnconfirmedError(hash, error);
|
|
219
|
+
}
|
|
220
|
+
if (receipt.status && receipt.status !== 'success')
|
|
221
|
+
throw new Error(`nearintents-mpp-sdk: deposit transaction ${hash} reverted.`);
|
|
222
|
+
// A wallet speed-up mines a replacement under a new hash.
|
|
223
|
+
return receipt.transactionHash ?? hash;
|
|
224
|
+
}
|
|
225
|
+
/** @internal The deposit was broadcast, but waiting for its confirmation failed. */
|
|
226
|
+
class DepositUnconfirmedError extends Error {
|
|
227
|
+
name = 'DepositUnconfirmedError';
|
|
228
|
+
hash;
|
|
229
|
+
constructor(hash, cause) {
|
|
230
|
+
super(`nearintents-mpp-sdk: deposit ${hash} was broadcast but its confirmation failed; retrying presents it again.`, { cause });
|
|
231
|
+
this.hash = hash;
|
|
169
232
|
}
|
|
170
|
-
return hash;
|
|
171
233
|
}
|
|
172
234
|
//# sourceMappingURL=Charge.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Charge.js","sourceRoot":"","sources":["../../src/client/Charge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,MAAM,CAAA;AAE7D,OAAO,KAAK,OAAO,MAAM,eAAe,CAAA;AACxC,OAAO,KAAK,KAAK,MAAM,aAAa,CAAA;AAEpC
|
|
1
|
+
{"version":3,"file":"Charge.js","sourceRoot":"","sources":["../../src/client/Charge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,MAAM,CAAA;AAE7D,OAAO,KAAK,OAAO,MAAM,eAAe,CAAA;AACxC,OAAO,KAAK,KAAK,MAAM,aAAa,CAAA;AAEpC,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAA;AAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,MAAM,UAAU,MAAM,CAAC,aAAgC,EAAE;IACvD,sFAAsF;IACtF,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAoD,CAAA;IAC5E,IAAI,SAAS,GAAG,CAAC,CAAA;IAEjB,OAAO,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,EAA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,CAAC,KAAK,CAAC,oBAAoB,CAAC,QAAQ,CAAC,cAAc,CAAC;gBACtD,QAAQ,CAAC,cAAc,CAAkB;YAC3C,IAAI;SACL,CAAC,CAAA;IACJ,CAAC,CAAC,EAAE,CAAA;IAEJ,qEAAqE;IACrE,IAAI,CAAC,YAAY,CAAC,yBAAyB;QAAE,OAAO,IAAI,CAAA;IACxD,IAAI,OAA8E,CAAA;IAClF,IAAI,CAAC;QACH,OAAO,GAAG,MAAM,YAAY,CAAC,yBAAyB,CAAC,EAAE,IAAI,EAAE,IAAqB,EAAE,CAAC,CAAA;IACzF,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,uBAAuB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;IAChD,CAAC;IACD,IAAI,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,MAAM,KAAK,SAAS;QAChD,MAAM,IAAI,KAAK,CAAC,4CAA4C,IAAI,YAAY,CAAC,CAAA;IAC/E,0DAA0D;IAC1D,OAAO,OAAO,CAAC,eAAe,IAAI,IAAI,CAAA;AACxC,CAAC;AAED,oFAAoF;AACpF,MAAM,uBAAwB,SAAQ,KAAK;IACvB,IAAI,GAAG,yBAAyB,CAAA;IACzC,IAAI,CAAQ;IAErB,YAAY,IAAY,EAAE,KAAc;QACtC,KAAK,CACH,gCAAgC,IAAI,yEAAyE,EAC7G,EAAE,KAAK,EAAE,CACV,CAAA;QACD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defuse-protocol/nearintents-mpp-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Reference implementation of the `nearintents` payment method for MPP (Machine Payments Protocol) — cross-chain HTTP 402 payments settled by NEAR Intents. Extends mppx.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"zod": "^4.6.7"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"mppx": "0.
|
|
57
|
+
"mppx": "0.22.0",
|
|
58
58
|
"viem": "^2.0.0"
|
|
59
59
|
},
|
|
60
60
|
"peerDependenciesMeta": {
|
|
@@ -66,10 +66,10 @@
|
|
|
66
66
|
"@biomejs/biome": "^2.5.4",
|
|
67
67
|
"@changesets/cli": "^2.31.1",
|
|
68
68
|
"@types/node": "^26.1.1",
|
|
69
|
-
"mppx": "0.
|
|
69
|
+
"mppx": "0.22.0",
|
|
70
70
|
"tsx": "^4.23.1",
|
|
71
71
|
"typescript": "~6.0.3",
|
|
72
|
-
"viem": "^2.57.
|
|
72
|
+
"viem": "^2.57.20",
|
|
73
73
|
"vitest": "^4.1.10"
|
|
74
74
|
},
|
|
75
75
|
"scripts": {
|
package/src/client/Charge.ts
CHANGED
|
@@ -3,6 +3,8 @@ import { Credential, Errors, Expires, Method, z } from 'mppx'
|
|
|
3
3
|
import * as Methods from '../Methods.js'
|
|
4
4
|
import * as Types from '../Types.js'
|
|
5
5
|
|
|
6
|
+
const forgetAfterMs = 24 * 60 * 60 * 1000
|
|
7
|
+
|
|
6
8
|
/**
|
|
7
9
|
* Creates a `nearintents` charge method for usage on the client.
|
|
8
10
|
*
|
|
@@ -20,6 +22,16 @@ import * as Types from '../Types.js'
|
|
|
20
22
|
* `eip155:*` origins (native transfers and ERC-20 `transfer`); any viem
|
|
21
23
|
* WalletClient extended with public actions satisfies the interface.
|
|
22
24
|
*
|
|
25
|
+
* A method instance pays each deposit address (and memo) at most once per
|
|
26
|
+
* payer: a later challenge for the same address re-presents the first
|
|
27
|
+
* deposit's hash. After a non-success terminal, the server's retry challenge
|
|
28
|
+
* can still carry the spent quote, which mppx's fetch would otherwise pay
|
|
29
|
+
* again. The payer is the wallet's account for the built-in broadcast and the
|
|
30
|
+
* instance itself for `sendDeposit`; a wallet without an account address is
|
|
31
|
+
* not tracked. This holds only within one instance, so keep one for the life
|
|
32
|
+
* of the payer's session. A deposit is forgotten 24 hours after the latest
|
|
33
|
+
* `expires` of a challenge for it, long after any quote for it has expired.
|
|
34
|
+
*
|
|
23
35
|
* @example
|
|
24
36
|
* ```ts
|
|
25
37
|
* import { Mppx } from 'mppx/client'
|
|
@@ -40,6 +52,10 @@ import * as Types from '../Types.js'
|
|
|
40
52
|
* ```
|
|
41
53
|
*/
|
|
42
54
|
export function charge(parameters: charge.Parameters = {}) {
|
|
55
|
+
// ponytail: in-memory and per instance; persist it if a client must survive restarts.
|
|
56
|
+
const deposits = new Map<string, { hash: Promise<string>; until: number }>()
|
|
57
|
+
let nextSweep = 0
|
|
58
|
+
|
|
43
59
|
return Method.toClient(Methods.charge, {
|
|
44
60
|
context: z.object({
|
|
45
61
|
/** Tx hash of an already-broadcast deposit (push mode, bring-your-own-broadcast). */
|
|
@@ -70,13 +86,45 @@ export function charge(parameters: charge.Parameters = {}) {
|
|
|
70
86
|
assertPolicy(parameters.policy, request)
|
|
71
87
|
|
|
72
88
|
const walletClient = context?.walletClient ?? parameters.walletClient
|
|
73
|
-
const hash = await (
|
|
89
|
+
const hash = await (() => {
|
|
74
90
|
if (context?.hash) return context.hash
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
91
|
+
const pay = async () => {
|
|
92
|
+
if (parameters.sendDeposit) return parameters.sendDeposit({ challenge, request })
|
|
93
|
+
if (walletClient) return broadcastEvmDeposit(walletClient, request)
|
|
94
|
+
throw new Error(
|
|
95
|
+
'nearintents-mpp-sdk: no way to pay — provide context.hash, a sendDeposit callback, or a walletClient.',
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
const payer = parameters.sendDeposit ? '' : walletClient?.account?.address.toLowerCase()
|
|
99
|
+
if (payer === undefined) return pay()
|
|
100
|
+
|
|
101
|
+
const now = Date.now()
|
|
102
|
+
if (now >= nextSweep) {
|
|
103
|
+
for (const [entryKey, entry] of deposits)
|
|
104
|
+
if (entry.until <= now) deposits.delete(entryKey)
|
|
105
|
+
nextSweep = now + 60_000
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const key = JSON.stringify([
|
|
109
|
+
payer,
|
|
110
|
+
request.methodDetails.originNetwork,
|
|
111
|
+
request.recipient,
|
|
112
|
+
request.methodDetails.depositMemo ?? null,
|
|
113
|
+
])
|
|
114
|
+
const until = Math.max(now, Date.parse(challenge.expires ?? '') || now) + forgetAfterMs
|
|
115
|
+
const paid = deposits.get(key)
|
|
116
|
+
if (paid && paid.until > now) {
|
|
117
|
+
paid.until = Math.max(paid.until, until)
|
|
118
|
+
return paid.hash
|
|
119
|
+
}
|
|
120
|
+
const entry = { hash: pay(), until }
|
|
121
|
+
deposits.set(key, entry)
|
|
122
|
+
// Keep a sent deposit's hash; forget anything else so it can be retried.
|
|
123
|
+
entry.hash.catch((error) => {
|
|
124
|
+
if (error instanceof DepositUnconfirmedError) entry.hash = Promise.resolve(error.hash)
|
|
125
|
+
else if (deposits.get(key) === entry) deposits.delete(key)
|
|
126
|
+
})
|
|
127
|
+
return entry.hash
|
|
80
128
|
})()
|
|
81
129
|
|
|
82
130
|
const source =
|
|
@@ -147,10 +195,18 @@ export declare namespace charge {
|
|
|
147
195
|
data?: `0x${string}` | undefined
|
|
148
196
|
}) => Promise<string>
|
|
149
197
|
waitForTransactionReceipt?:
|
|
150
|
-
| ((args: { hash: `0x${string}` }) => Promise<{
|
|
198
|
+
| ((args: { hash: `0x${string}` }) => Promise<{
|
|
199
|
+
status?: string | undefined
|
|
200
|
+
transactionHash?: string | undefined
|
|
201
|
+
}>)
|
|
151
202
|
| undefined
|
|
152
203
|
}
|
|
153
204
|
|
|
205
|
+
/**
|
|
206
|
+
* Pays the deposit and resolves with its confirmed tx hash. Reject only when
|
|
207
|
+
* nothing was broadcast: a rejection lets the next challenge for this
|
|
208
|
+
* deposit address pay it again.
|
|
209
|
+
*/
|
|
154
210
|
type SendDeposit = (parameters: {
|
|
155
211
|
challenge: { id: string; expires?: string | undefined }
|
|
156
212
|
request: Types.ChargeRequest
|
|
@@ -159,7 +215,10 @@ export declare namespace charge {
|
|
|
159
215
|
type Parameters = {
|
|
160
216
|
/** Payment policy asserted before every deposit. */
|
|
161
217
|
policy?: Policy | undefined
|
|
162
|
-
/**
|
|
218
|
+
/**
|
|
219
|
+
* Pays the origin-chain leg and resolves with the confirmed tx hash. Reject
|
|
220
|
+
* only when nothing was broadcast, or the next challenge pays again.
|
|
221
|
+
*/
|
|
163
222
|
sendDeposit?: SendDeposit | undefined
|
|
164
223
|
/** EVM wallet for the built-in `eip155:*` broadcast. */
|
|
165
224
|
walletClient?: EvmWalletClient | undefined
|
|
@@ -271,13 +330,29 @@ async function broadcastEvmDeposit(
|
|
|
271
330
|
})()
|
|
272
331
|
|
|
273
332
|
// Present a *confirmed* hash when the client can wait for inclusion.
|
|
274
|
-
if (walletClient.waitForTransactionReceipt)
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
})
|
|
278
|
-
|
|
279
|
-
|
|
333
|
+
if (!walletClient.waitForTransactionReceipt) return hash
|
|
334
|
+
let receipt: { status?: string | undefined; transactionHash?: string | undefined }
|
|
335
|
+
try {
|
|
336
|
+
receipt = await walletClient.waitForTransactionReceipt({ hash: hash as `0x${string}` })
|
|
337
|
+
} catch (error) {
|
|
338
|
+
throw new DepositUnconfirmedError(hash, error)
|
|
280
339
|
}
|
|
340
|
+
if (receipt.status && receipt.status !== 'success')
|
|
341
|
+
throw new Error(`nearintents-mpp-sdk: deposit transaction ${hash} reverted.`)
|
|
342
|
+
// A wallet speed-up mines a replacement under a new hash.
|
|
343
|
+
return receipt.transactionHash ?? hash
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** @internal The deposit was broadcast, but waiting for its confirmation failed. */
|
|
347
|
+
class DepositUnconfirmedError extends Error {
|
|
348
|
+
override readonly name = 'DepositUnconfirmedError'
|
|
349
|
+
readonly hash: string
|
|
281
350
|
|
|
282
|
-
|
|
351
|
+
constructor(hash: string, cause: unknown) {
|
|
352
|
+
super(
|
|
353
|
+
`nearintents-mpp-sdk: deposit ${hash} was broadcast but its confirmation failed; retrying presents it again.`,
|
|
354
|
+
{ cause },
|
|
355
|
+
)
|
|
356
|
+
this.hash = hash
|
|
357
|
+
}
|
|
283
358
|
}
|