Skip to content

Commit 656e16b

Browse files
authored
allocator: withdraw request helpers (#23)
1 parent 98075e6 commit 656e16b

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-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.56",
3+
"version": "0.0.57",
44
"description": "Relay protocol SDK",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ import {
6060
getVmTypeNativeCurrency,
6161
} from "./utils";
6262

63+
import {
64+
SubmitWithdrawRequest,
65+
getSubmitWithdrawRequestHash,
66+
getWithdrawalAddress,
67+
} from "./messages/v2.2/allocator";
68+
6369
export {
6470
// Order
6571
Order,
@@ -119,4 +125,9 @@ export {
119125
getExecutionMessageId,
120126
encodeAction,
121127
decodeAction,
128+
129+
// Allocator
130+
SubmitWithdrawRequest,
131+
getSubmitWithdrawRequestHash,
132+
getWithdrawalAddress,
122133
};

src/messages/v2.2/allocator.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Hex, Address, encodePacked, keccak256 } from "viem";
2+
3+
export interface SubmitWithdrawRequest {
4+
chainId: string; // chainId of the destination chain on which the user will withdraw
5+
depository: string; // address of the depository account
6+
currency: string;
7+
amount: string; // Amount to withdraw
8+
spender: string; // address of the account that owns the balance in the Hub contract (can be an alias)
9+
receiver: string; // Address of the account on the destination chain
10+
data: string; // add tional data
11+
nonce: string; // Nonce for replay protection
12+
}
13+
14+
export const getSubmitWithdrawRequestHash = (
15+
request: SubmitWithdrawRequest
16+
) => {
17+
// EIP712 type from RelayAllocator
18+
const PAYLOAD_TYPEHASH = keccak256(
19+
"SubmitWithdrawRequest(uint256 chainId,string depository,string currency,uint256 amount,address spender,string receiver,bytes data,bytes32 nonce)" as Hex
20+
);
21+
22+
// Create EIP712 digest
23+
const digest = keccak256(
24+
encodePacked(
25+
[
26+
"bytes32",
27+
"uint256",
28+
"bytes32",
29+
"bytes32",
30+
"uint256",
31+
"address",
32+
"bytes32",
33+
"bytes32",
34+
"bytes32",
35+
],
36+
[
37+
PAYLOAD_TYPEHASH,
38+
BigInt(request.chainId),
39+
keccak256(request.depository as Hex),
40+
keccak256(request.currency as Hex),
41+
BigInt(request.amount),
42+
request.spender as Address,
43+
keccak256(request.receiver as Hex),
44+
keccak256(request.data as Hex),
45+
request.nonce as Hex,
46+
]
47+
)
48+
);
49+
50+
// The withdrawal address is the digest itself (as a hex string)
51+
return digest;
52+
};
53+
54+
export function getWithdrawalAddress(request: SubmitWithdrawRequest): string {
55+
const withdrawalHash = getSubmitWithdrawRequestHash(request);
56+
const withdrawalAddress = withdrawalHash.slice(2).slice(-40);
57+
return `0x${withdrawalAddress}` as `0x${string}`;
58+
}

0 commit comments

Comments
 (0)