Skip to content
This repository was archived by the owner on Dec 23, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reservoir0x/relay-protocol-sdk",
"version": "0.0.51",
"version": "0.0.52",
"description": "Relay protocol SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
decodeWithdrawal,
getDecodedWithdrawalId,
getDecodedWithdrawalCurrency,
getDecodedWithdrawalAmount,
} from "./messages/v2.1/depository-withdrawal";

import {
Expand Down Expand Up @@ -95,6 +96,7 @@ export {
decodeWithdrawal,
getDecodedWithdrawalId,
getDecodedWithdrawalCurrency,
getDecodedWithdrawalAmount,

// SolverRefund
SolverRefundMessage,
Expand Down
72 changes: 72 additions & 0 deletions src/messages/v2.1/depository-withdrawal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { bcs } from "@mysten/sui/bcs";
import { PublicKey, SystemProgram } from "@solana/web3.js";
import { sha256 } from "js-sha256";
import * as tronweb from "tronweb";
import * as bitcoin from "bitcoinjs-lib";
import {
Address,
bytesToHex,
Expand Down Expand Up @@ -787,3 +788,74 @@ export const getDecodedWithdrawalCurrency = (
}
}
};

const decodeERC20TransferAmount = (data: string) => {
// ERC20 / TRC20 `transfer(address,uint256)` selector is 0xa9059cbb
const TRANSFER_SELECTOR = "0xa9059cbb";
if (data.toLowerCase().startsWith(TRANSFER_SELECTOR.toLowerCase())) {
const paramsData = ("0x" + data.slice(TRANSFER_SELECTOR.length)) as Hex;
const [amount] = decodeAbiParameters(
parseAbiParameters(["address", "uint256"]),
paramsData
);
return amount;
} else {
throw new Error(`Unsupported function call data: ${data}`);
}
};

export const getDecodedWithdrawalAmount = (
decodedWithdrawal: DecodedWithdrawal
): string => {
switch (decodedWithdrawal.vmType) {
case "ethereum-vm": {
const firstCall = decodedWithdrawal.withdrawal.calls[0];
if (firstCall.data === "0x") {
return firstCall.value;
} else {
return decodeERC20TransferAmount(firstCall.data);
}
}

case "tron-vm": {
const firstCall = decodedWithdrawal.withdrawal.calls[0];
if (firstCall.data === "0x") {
return firstCall.value;
} else {
return decodeERC20TransferAmount(firstCall.data);
}
}

case "solana-vm": {
return decodedWithdrawal.withdrawal.amount;
}

case "sui-vm": {
return decodedWithdrawal.withdrawal.amount;
}

case "bitcoin-vm": {
try {
const psbt = bitcoin.Psbt.fromHex(decodedWithdrawal.withdrawal.psbt);
const tx = psbt.extractTransaction(false);
const totalAmount = tx.outs.reduce((sum, output) => {
return sum + output.value;
}, 0);
return totalAmount.toString();
} catch (error) {
throw new Error(
`Failed to decode PSBT: ${
error instanceof Error ? error.message : String(error)
}`
);
}
}

case "hyperliquid-vm": {
return decodedWithdrawal.withdrawal.parameters.amount;
}

default:
throw new Error("Unsupported vm type");
}
};