Skip to content

Commit af6b2d4

Browse files
authored
feat: preview utxo for helper calls (#19)
* feat: preview utxo for helper calls * preview update * adjust call * add thread note * fix types * fix format * update description * test: fix
1 parent 0fc2e47 commit af6b2d4

File tree

7 files changed

+813
-2
lines changed

7 files changed

+813
-2
lines changed

packages/sdk/src/mintlayer-connect-sdk.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,7 @@ class Client {
19891989
const transaction_size_in_bytes = BigInt(Math.ceil(BINRepresentation.transactionsize));
19901990
const fee_amount_per_kb = BigInt('100000000000'); // TODO: Get the current feerate from the network
19911991

1992-
const nextPreciseFee = ((fee_amount_per_kb * transaction_size_in_bytes) + BigInt(999)) / BigInt(1000)
1992+
const nextPreciseFee = (fee_amount_per_kb * transaction_size_in_bytes + BigInt(999)) / BigInt(1000);
19931993

19941994
if (nextPreciseFee === preciseFee || nextPreciseFee === previousFee) {
19951995
const transaction = encode_transaction(
@@ -2844,6 +2844,52 @@ class Client {
28442844
return { spent, created };
28452845
}
28462846

2847+
/**
2848+
* Decorates a function with UTXO fetching logic.
2849+
* ⚠️ Not thread-safe.
2850+
* Do not use in parallel for the same client instance.
2851+
* Intended to get utxo changes for transactions one by one.
2852+
* @param func The function to decorate.
2853+
* @return A promise that resolves to an object containing the result of the function **and** UTXO changes.
2854+
*/
2855+
async decorateWithUtxoFetch<T>(func: () => Promise<T>): Promise<{ result: T; utxo: { created: any; spent: any } }> {
2856+
this.ensureInitialized();
2857+
2858+
const originalBuildTransaction = this.buildTransaction;
2859+
2860+
let txresult: Transaction | undefined = undefined;
2861+
this.buildTransaction = new Proxy(this.buildTransaction, {
2862+
apply: async (target, thisArg, args) => {
2863+
const result = (await Reflect.apply(target, thisArg, args)) as Transaction;
2864+
txresult = result; // pull the result of the buildTransaction
2865+
return result;
2866+
},
2867+
});
2868+
2869+
// Call this function in parallel is not allowed due to Proxy usage BUT also due to the fact that we need to
2870+
// ensure that utxo results are getting one by one, parallel not makes sense here.
2871+
if ((this as any).__decoratorLock__) {
2872+
throw new Error('decorateWithUtxoFetch already running — cannot run in parallel.');
2873+
}
2874+
2875+
(this as any).__decoratorLock__ = true;
2876+
2877+
try {
2878+
const t = await func();
2879+
2880+
if (!txresult) {
2881+
throw new Error('Failed to decorate with UtxoFetch');
2882+
}
2883+
2884+
const { created, spent } = this.previewUtxoChange(txresult);
2885+
2886+
return { result: t, utxo: { created, spent } };
2887+
} finally {
2888+
(this as any).__decoratorLock__ = false;
2889+
this.buildTransaction = originalBuildTransaction;
2890+
}
2891+
}
2892+
28472893
async getXPub(): Promise<string> {
28482894
this.ensureInitialized();
28492895
console.warn('[Mintlayer SDK] Warning: Sharing xPub exposes all derived addresses. Use with caution.');
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`issue token - snapshot 1`] = `
4+
{
5+
"BINRepresentation": {
6+
"inputs": [
7+
Uint8Array [
8+
0,
9+
0,
10+
175,
11+
59,
12+
95,
13+
173,
14+
32,
15+
246,
16+
249,
17+
126,
18+
178,
19+
16,
20+
147,
21+
78,
22+
148,
23+
33,
24+
118,
25+
247,
26+
247,
27+
208,
28+
247,
29+
4,
30+
35,
31+
89,
32+
6,
33+
89,
34+
238,
35+
14,
36+
2,
37+
23,
38+
5,
39+
58,
40+
124,
41+
171,
42+
1,
43+
0,
44+
0,
45+
0,
46+
],
47+
],
48+
"outputs": [
49+
Uint8Array [
50+
7,
51+
1,
52+
16,
53+
88,
54+
89,
55+
90,
56+
50,
57+
8,
58+
92,
59+
105,
60+
112,
61+
102,
62+
115,
63+
58,
64+
47,
65+
47,
66+
81,
67+
109,
68+
69,
69+
120,
70+
97,
71+
109,
72+
112,
73+
108,
74+
101,
75+
72,
76+
97,
77+
115,
78+
104,
79+
49,
80+
50,
81+
51,
82+
0,
83+
19,
84+
0,
85+
0,
86+
232,
87+
137,
88+
4,
89+
35,
90+
199,
91+
138,
92+
1,
93+
118,
94+
148,
95+
121,
96+
186,
97+
231,
98+
213,
99+
53,
100+
208,
101+
151,
102+
222,
103+
253,
104+
255,
105+
15,
106+
78,
107+
113,
108+
1,
109+
102,
110+
157,
111+
74,
112+
25,
113+
0,
114+
],
115+
Uint8Array [
116+
0,
117+
0,
118+
15,
119+
224,
120+
163,
121+
26,
122+
138,
123+
240,
124+
3,
125+
6,
126+
1,
127+
134,
128+
236,
129+
69,
130+
4,
131+
87,
132+
208,
133+
154,
134+
217,
135+
128,
136+
115,
137+
147,
138+
216,
139+
156,
140+
236,
141+
156,
142+
113,
143+
214,
144+
32,
145+
96,
146+
33,
147+
],
148+
],
149+
"transactionsize": 241,
150+
},
151+
"HEXRepresentation_unsigned": "0100040000af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab010000000807011058595a32085c697066733a2f2f516d4578616d706c654861736831323300130000e8890423c78a01769479bae7d535d097defdff0f4e7101669d4a190000000fe0a31a8af003060186ec450457d09ad9807393d89cec9c71d6206021",
152+
"JSONRepresentation": {
153+
"fee": {
154+
"atoms": "10024100000000",
155+
"decimal": "100.241",
156+
},
157+
"id": "2a8037b03f67e8164e0e7773287be3ff8ca133e94ce4af577147a5bc841c458a",
158+
"inputs": [
159+
{
160+
"input": {
161+
"index": 1,
162+
"input_type": "UTXO",
163+
"source_id": "af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab",
164+
"source_type": "Transaction",
165+
},
166+
"utxo": {
167+
"destination": "tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6",
168+
"type": "Transfer",
169+
"value": {
170+
"amount": {
171+
"atoms": "1703205604300000",
172+
"decimal": "17032.056043",
173+
},
174+
"type": "Coin",
175+
},
176+
},
177+
},
178+
],
179+
"outputs": [
180+
{
181+
"authority": "tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc",
182+
"is_freezable": false,
183+
"metadata_uri": {
184+
"hex": "697066733a2f2f516d4578616d706c6548617368313233",
185+
"string": "ipfs://QmExampleHash123",
186+
},
187+
"number_of_decimals": 8,
188+
"token_ticker": {
189+
"hex": "58595a32",
190+
"string": "XYZ2",
191+
},
192+
"total_supply": {
193+
"amount": {
194+
"atoms": "10000000000000000000",
195+
"decimal": "100000000000",
196+
},
197+
"type": "Fixed",
198+
},
199+
"type": "IssueFungibleToken",
200+
},
201+
{
202+
"destination": "tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6",
203+
"type": "Transfer",
204+
"value": {
205+
"amount": {
206+
"atoms": "1693181504300000",
207+
"decimal": "16931.815043",
208+
},
209+
"type": "Coin",
210+
},
211+
},
212+
],
213+
},
214+
"transaction_id": "2a8037b03f67e8164e0e7773287be3ff8ca133e94ce4af577147a5bc841c458a",
215+
}
216+
`;

0 commit comments

Comments
 (0)