Skip to content
This repository was archived by the owner on Dec 23, 2025. It is now read-only.

Commit 5aef37d

Browse files
authored
add getDecodedWithdrawalAmount (#20)
* add `getDecodedWithdrawalAmount` * supports btc
1 parent b4a9c4d commit 5aef37d

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reservoir0x/relay-protocol-sdk",
3-
"version": "0.0.51",
3+
"version": "0.0.52",
44
"description": "Relay protocol SDK",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
decodeWithdrawal,
2626
getDecodedWithdrawalId,
2727
getDecodedWithdrawalCurrency,
28+
getDecodedWithdrawalAmount,
2829
} from "./messages/v2.1/depository-withdrawal";
2930

3031
import {
@@ -95,6 +96,7 @@ export {
9596
decodeWithdrawal,
9697
getDecodedWithdrawalId,
9798
getDecodedWithdrawalCurrency,
99+
getDecodedWithdrawalAmount,
98100

99101
// SolverRefund
100102
SolverRefundMessage,

src/messages/v2.1/depository-withdrawal.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { bcs } from "@mysten/sui/bcs";
44
import { PublicKey, SystemProgram } from "@solana/web3.js";
55
import { sha256 } from "js-sha256";
66
import * as tronweb from "tronweb";
7+
import * as bitcoin from "bitcoinjs-lib";
78
import {
89
Address,
910
bytesToHex,
@@ -787,3 +788,74 @@ export const getDecodedWithdrawalCurrency = (
787788
}
788789
}
789790
};
791+
792+
const decodeERC20TransferAmount = (data: string) => {
793+
// ERC20 / TRC20 `transfer(address,uint256)` selector is 0xa9059cbb
794+
const TRANSFER_SELECTOR = "0xa9059cbb";
795+
if (data.toLowerCase().startsWith(TRANSFER_SELECTOR.toLowerCase())) {
796+
const paramsData = ("0x" + data.slice(TRANSFER_SELECTOR.length)) as Hex;
797+
const [amount] = decodeAbiParameters(
798+
parseAbiParameters(["address", "uint256"]),
799+
paramsData
800+
);
801+
return amount;
802+
} else {
803+
throw new Error(`Unsupported function call data: ${data}`);
804+
}
805+
};
806+
807+
export const getDecodedWithdrawalAmount = (
808+
decodedWithdrawal: DecodedWithdrawal
809+
): string => {
810+
switch (decodedWithdrawal.vmType) {
811+
case "ethereum-vm": {
812+
const firstCall = decodedWithdrawal.withdrawal.calls[0];
813+
if (firstCall.data === "0x") {
814+
return firstCall.value;
815+
} else {
816+
return decodeERC20TransferAmount(firstCall.data);
817+
}
818+
}
819+
820+
case "tron-vm": {
821+
const firstCall = decodedWithdrawal.withdrawal.calls[0];
822+
if (firstCall.data === "0x") {
823+
return firstCall.value;
824+
} else {
825+
return decodeERC20TransferAmount(firstCall.data);
826+
}
827+
}
828+
829+
case "solana-vm": {
830+
return decodedWithdrawal.withdrawal.amount;
831+
}
832+
833+
case "sui-vm": {
834+
return decodedWithdrawal.withdrawal.amount;
835+
}
836+
837+
case "bitcoin-vm": {
838+
try {
839+
const psbt = bitcoin.Psbt.fromHex(decodedWithdrawal.withdrawal.psbt);
840+
const tx = psbt.extractTransaction(false);
841+
const totalAmount = tx.outs.reduce((sum, output) => {
842+
return sum + output.value;
843+
}, 0);
844+
return totalAmount.toString();
845+
} catch (error) {
846+
throw new Error(
847+
`Failed to decode PSBT: ${
848+
error instanceof Error ? error.message : String(error)
849+
}`
850+
);
851+
}
852+
}
853+
854+
case "hyperliquid-vm": {
855+
return decodedWithdrawal.withdrawal.parameters.amount;
856+
}
857+
858+
default:
859+
throw new Error("Unsupported vm type");
860+
}
861+
};

0 commit comments

Comments
 (0)