Skip to content

Commit a5eded7

Browse files
authored
Prepare for allocator migration (#90)
* feat: prepare for allocator migration by allowing different allocator per chain id * chore: override base allocator on dev * fix: tweaks
1 parent d26211c commit a5eded7

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

src/api/queries/configs/v1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default {
3434
_req: FastifyRequestTypeBox<typeof Schema>,
3535
reply: FastifyReplyTypeBox<typeof Schema>
3636
) => {
37-
const { walletClient } = await getOnchainAllocator();
37+
const { walletClient } = await getOnchainAllocator("ethereum");
3838

3939
return reply.status(200).send({
4040
configs: {

src/services/request-handler/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class RequestHandlerService {
9898
case "ethereum-vm": {
9999
if (request.mode === "onchain") {
100100
const { contract, publicClient, walletClient } =
101-
await getOnchainAllocator();
101+
await getOnchainAllocator(request.chainId);
102102

103103
payloadParams = {
104104
chainId: chain.metadata.allocatorChainId!,
@@ -112,7 +112,7 @@ export class RequestHandlerService {
112112
};
113113

114114
// This is needed before being able to submit withdraw requests
115-
await handleOneTimeApproval();
115+
await handleOneTimeApproval(request.chainId);
116116

117117
const txHash = await contract.write.submitWithdrawRequest([
118118
payloadParams as any,
@@ -238,7 +238,7 @@ export class RequestHandlerService {
238238
case "solana-vm": {
239239
if (request.mode === "onchain") {
240240
const { contract, publicClient, walletClient } =
241-
await getOnchainAllocator();
241+
await getOnchainAllocator(request.chainId);
242242

243243
// The "solana-vm" payload builder expects addresses to be hex-encoded
244244
const toHexString = (address: string) =>
@@ -259,7 +259,7 @@ export class RequestHandlerService {
259259
};
260260

261261
// This is needed before being able to submit withdraw requests
262-
await handleOneTimeApproval();
262+
await handleOneTimeApproval(request.chainId);
263263

264264
const txHash = await contract.write.submitWithdrawRequest([
265265
payloadParams as any,
@@ -466,7 +466,7 @@ export class RequestHandlerService {
466466
case "hyperliquid-vm": {
467467
if (request.mode === "onchain") {
468468
const { contract, publicClient, walletClient } =
469-
await getOnchainAllocator();
469+
await getOnchainAllocator(request.chainId);
470470

471471
const isNativeCurrency =
472472
request.currency === getVmTypeNativeCurrency(chain.vmType);
@@ -509,7 +509,7 @@ export class RequestHandlerService {
509509
};
510510

511511
// This is needed before being able to submit withdraw requests
512-
await handleOneTimeApproval();
512+
await handleOneTimeApproval(request.chainId);
513513

514514
const txHash = await contract.write.submitWithdrawRequest([
515515
payloadParams as any,
@@ -716,7 +716,9 @@ export class RequestHandlerService {
716716
throw externalError("Withdrawal request not using 'onchain' mode");
717717
}
718718

719-
const { contract, publicClient } = await getOnchainAllocator();
719+
const { contract, publicClient } = await getOnchainAllocator(
720+
withdrawalRequest.chainId
721+
);
720722

721723
const payloadTimestamp = await contract.read.payloadTimestamps([
722724
withdrawalRequest.payloadId as Hex,

src/utils/onchain-allocator.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,16 @@ const getPayloadBuilder = async (address: string) => {
8585
};
8686
};
8787

88-
export const getOnchainAllocator = async () => {
89-
if (!config.onchainAllocator) {
88+
export const getOnchainAllocator = async (chainId: string) => {
89+
let allocator = config.onchainAllocator;
90+
if (
91+
process.env.SERVICE === "relay-protocol-hub-dev" &&
92+
["base"].includes(chainId)
93+
) {
94+
allocator = "0x45348c213bf7ddb8e45f34ca4f333307a78ecb9a";
95+
}
96+
97+
if (!allocator) {
9098
throw externalError("Onchain allocator not configured");
9199
}
92100

@@ -99,7 +107,7 @@ export const getOnchainAllocator = async () => {
99107
return {
100108
contract: getContract({
101109
client: walletClient,
102-
address: config.onchainAllocator as Address,
110+
address: allocator as Address,
103111
abi: parseAbi([
104112
`function submitWithdrawRequest(${PayloadParams} params) returns (bytes32)`,
105113
`function signWithdrawPayloadHash(${PayloadParams} params, bytes signature, ${GasSettings} gasSettings, uint32 index)`,
@@ -115,10 +123,12 @@ export const getOnchainAllocator = async () => {
115123
};
116124

117125
let _allowanceCache: bigint | undefined;
118-
export const handleOneTimeApproval = async () => {
126+
export const handleOneTimeApproval = async (chainId: string) => {
119127
const { walletClient } = await getPublicAndWalletClients();
120128

121-
const allocator = await getOnchainAllocator().then((a) => a.contract.address);
129+
const allocator = await getOnchainAllocator(chainId).then(
130+
(a) => a.contract.address
131+
);
122132

123133
const wNearContract = getContract({
124134
client: walletClient,
@@ -194,7 +204,7 @@ export const getSigner = async (chainId: string) => {
194204
}
195205
}
196206

197-
const { contract } = await getOnchainAllocator();
207+
const { contract } = await getOnchainAllocator(chainId);
198208

199209
const args = {
200210
domain_id: domainId,
@@ -252,7 +262,7 @@ export const getSignature = async (id: string) => {
252262
);
253263
}
254264

255-
const onchainAllocator = await getOnchainAllocator();
265+
const onchainAllocator = await getOnchainAllocator(chain.id);
256266
const payloadBuilderAddress =
257267
await onchainAllocator.contract.read.payloadBuilders([
258268
BigInt(chain.metadata.allocatorChainId),

0 commit comments

Comments
 (0)