@yodlpay/payment-decoder 1.3.7 → 1.3.8

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.
Files changed (2) hide show
  1. package/dist/index.js +75 -41
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -31,7 +31,8 @@ import {
31
31
  erc20Abi as erc20Abi2,
32
32
  getAddress,
33
33
  isAddressEqual as isAddressEqual2,
34
- parseEventLogs
34
+ parseEventLogs,
35
+ zeroAddress as zeroAddress2
35
36
  } from "viem";
36
37
 
37
38
  // src/abi.ts
@@ -41,6 +42,9 @@ var yodlAbi = getRouterAbi("0.8");
41
42
  var relaySwapAbi = parseAbi([
42
43
  "event Swap(address indexed sender, address indexed recipient, address inputToken, address outputToken, uint256 inputAmount, uint256 outputAmount)"
43
44
  ]);
45
+ var fundsMovementAbi = parseAbi([
46
+ "event FundsMovement(address from, address to, address currency, uint256 amount, bytes metadata)"
47
+ ]);
44
48
 
45
49
  // src/utils.ts
46
50
  import { getTokenByAddress } from "@yodlpay/tokenlists";
@@ -153,6 +157,20 @@ function extractTokenTransfers(logs) {
153
157
  return [{ token: log.address, from, to, amount: value }];
154
158
  });
155
159
  }
160
+ function extractFundsMovements(logs) {
161
+ const parsed = parseEventLogs({
162
+ abi: fundsMovementAbi,
163
+ logs: [...logs],
164
+ eventName: "FundsMovement",
165
+ strict: false
166
+ });
167
+ return parsed.flatMap((log) => {
168
+ if (!log.args) return [];
169
+ const { from, to, currency, amount } = log.args;
170
+ if (!from || !to || !currency || amount === void 0) return [];
171
+ return [{ emitter: log.address, from, to, currency, amount }];
172
+ });
173
+ }
156
174
  function isKnownToken(address) {
157
175
  return tokenlist.some((t) => isAddressEqual2(t.address, address));
158
176
  }
@@ -221,52 +239,68 @@ function findInputTransfer(transfers, sender) {
221
239
  }
222
240
  function inferSwapFromTransfers(logs, sender, yodlToken) {
223
241
  const transfers = extractTokenTransfers(logs);
242
+ const tokenOutAmount = transfers.filter(
243
+ (t) => isAddressEqual2(t.to, sender) && isAddressEqual2(t.token, yodlToken)
244
+ ).reduce((sum, t) => sum + t.amount, 0n);
245
+ if (tokenOutAmount === 0n) return void 0;
224
246
  const outgoingDifferentToken = transfers.filter(
225
247
  (t) => isAddressEqual2(t.from, sender) && !isAddressEqual2(t.token, yodlToken)
226
248
  );
227
- if (outgoingDifferentToken.length === 0) return void 0;
228
- const byToken = /* @__PURE__ */ new Map();
229
- for (const t of outgoingDifferentToken) {
230
- const key = getAddress(t.token);
231
- const existing = byToken.get(key);
232
- if (existing) {
233
- existing.gross += t.amount;
234
- } else {
235
- byToken.set(key, { token: t.token, gross: t.amount, refund: 0n });
249
+ if (outgoingDifferentToken.length > 0) {
250
+ const byToken = /* @__PURE__ */ new Map();
251
+ for (const t of outgoingDifferentToken) {
252
+ const key = getAddress(t.token);
253
+ const existing = byToken.get(key);
254
+ if (existing) {
255
+ existing.gross += t.amount;
256
+ } else {
257
+ byToken.set(key, { token: t.token, gross: t.amount, refund: 0n });
258
+ }
236
259
  }
237
- }
238
- const incomingNonYodl = transfers.filter(
239
- (t) => isAddressEqual2(t.to, sender) && !isAddressEqual2(t.token, yodlToken)
240
- );
241
- for (const t of incomingNonYodl) {
242
- const key = getAddress(t.token);
243
- const existing = byToken.get(key);
244
- if (existing) {
245
- existing.refund += t.amount;
260
+ const incomingNonYodl = transfers.filter(
261
+ (t) => isAddressEqual2(t.to, sender) && !isAddressEqual2(t.token, yodlToken)
262
+ );
263
+ for (const t of incomingNonYodl) {
264
+ const key = getAddress(t.token);
265
+ const existing = byToken.get(key);
266
+ if (existing) {
267
+ existing.refund += t.amount;
268
+ }
269
+ }
270
+ let tokenIn;
271
+ let tokenInAmount = 0n;
272
+ for (const { token, gross, refund } of byToken.values()) {
273
+ const net = gross - refund;
274
+ if (net > tokenInAmount) {
275
+ tokenIn = token;
276
+ tokenInAmount = net;
277
+ }
246
278
  }
279
+ if (tokenIn && tokenInAmount > 0n) {
280
+ return {
281
+ tokenIn,
282
+ tokenOut: yodlToken,
283
+ tokenInAmount,
284
+ tokenOutAmount
285
+ };
286
+ }
287
+ return void 0;
247
288
  }
248
- let tokenIn;
249
- let tokenInAmount = 0n;
250
- for (const { token, gross, refund } of byToken.values()) {
251
- const net = gross - refund;
252
- if (net > tokenInAmount) {
253
- tokenIn = token;
254
- tokenInAmount = net;
289
+ let nativeInAmount = 0n;
290
+ for (const movement of extractFundsMovements(logs)) {
291
+ if (!isAddressEqual2(movement.currency, zeroAddress2)) continue;
292
+ if (isAddressEqual2(movement.from, sender)) {
293
+ nativeInAmount += movement.amount;
294
+ }
295
+ if (isAddressEqual2(movement.to, sender)) {
296
+ nativeInAmount -= movement.amount;
255
297
  }
256
298
  }
257
- if (!tokenIn || tokenInAmount <= 0n) return void 0;
258
- const incomingYodlToken = transfers.filter(
259
- (t) => isAddressEqual2(t.to, sender) && isAddressEqual2(t.token, yodlToken)
260
- );
261
- const tokenOutAmount = incomingYodlToken.reduce(
262
- (sum, t) => sum + t.amount,
263
- 0n
264
- );
265
- if (tokenOutAmount === 0n) return void 0;
299
+ if (nativeInAmount <= 0n) return void 0;
266
300
  return {
267
- tokenIn,
301
+ tokenIn: zeroAddress2,
268
302
  tokenOut: yodlToken,
269
- tokenInAmount,
303
+ tokenInAmount: nativeInAmount,
270
304
  tokenOutAmount
271
305
  };
272
306
  }
@@ -426,7 +460,7 @@ import {
426
460
  isAddress as isAddress2,
427
461
  isAddressEqual as isAddressEqual3,
428
462
  parseEventLogs as parseEventLogs2,
429
- zeroAddress as zeroAddress2
463
+ zeroAddress as zeroAddress3
430
464
  } from "viem";
431
465
  import { entryPoint08Abi, entryPoint08Address } from "viem/account-abstraction";
432
466
 
@@ -627,7 +661,7 @@ async function decodeRelayBridgePayment(hash, clients, options = {}) {
627
661
  clients
628
662
  );
629
663
  } catch (error) {
630
- if (error instanceof ExpectedDecodeError && inputToken && isAddressEqual3(inputToken, zeroAddress2) && inputAmountGross > 0n) {
664
+ if (error instanceof ExpectedDecodeError && inputToken && isAddressEqual3(inputToken, zeroAddress3) && inputAmountGross > 0n) {
631
665
  const swapEvent = decodeSwapFromLogs(destReceipt.logs);
632
666
  const tokenOutAmountGross = swapEvent ? swapEvent.tokenOutAmount : yodlEvent.amount;
633
667
  tokens = {
@@ -887,7 +921,7 @@ async function decodePayment(hash, chainId2, clients, cachedReceipt) {
887
921
  import {
888
922
  formatUnits,
889
923
  isAddressEqual as isAddressEqual5,
890
- zeroAddress as zeroAddress3
924
+ zeroAddress as zeroAddress4
891
925
  } from "viem";
892
926
  async function buildTokenInfo(params, clients) {
893
927
  const sameToken = isAddressEqual5(params.tokenIn, params.tokenOut) && params.inChainId === params.outChainId;
@@ -1003,7 +1037,7 @@ async function decodeYodlPayment(txHash2, chainId2, clients, cachedReceipt) {
1003
1037
  cachedReceipt
1004
1038
  );
1005
1039
  const firstWebhook = paymentInfo.webhooks[0];
1006
- const processorAddress = firstWebhook?.webhookAddress ?? zeroAddress3;
1040
+ const processorAddress = firstWebhook?.webhookAddress ?? zeroAddress4;
1007
1041
  const processorMemo = firstWebhook?.memo ?? "";
1008
1042
  const tokenInfo = await extractTokenInfo(
1009
1043
  paymentInfo,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yodlpay/payment-decoder",
3
- "version": "1.3.7",
3
+ "version": "1.3.8",
4
4
  "description": "Decode Yodl payment hashes into structured payment data",
5
5
  "keywords": [
6
6
  "yodl",