Skip to content

Commit a4950a2

Browse files
authored
Merge pull request #81 from relayprotocol/add-configs-api
feat: add configs api
2 parents f95a900 + e0925be commit a4950a2

File tree

7 files changed

+88
-18
lines changed

7 files changed

+88
-18
lines changed

src/api/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import actionsSolverRefundsV1 from "./actions/solver-refunds/v1";
1212
import queriesBalanceLocksV1 from "./queries/balance-locks/v1";
1313
import queriesBalancesV1 from "./queries/balances/v1";
1414
import queriesChainsV1 from "./queries/chains/v1";
15+
import queriesConfigsV1 from "./queries/configs/v1";
1516
import queriesWithdrawalRequestsV1 from "./queries/withdrawal-requests/v1";
1617

1718
// Requests
@@ -27,6 +28,7 @@ const endpoints = [
2728
queriesBalanceLocksV1,
2829
queriesBalancesV1,
2930
queriesChainsV1,
31+
queriesConfigsV1,
3032
queriesWithdrawalRequestsV1,
3133
requestsUnlocksV1,
3234
requestsWithdrawalsV1,

src/api/queries/chains/v1.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import {
66
FastifyReplyTypeBox,
77
FastifyRequestTypeBox,
88
} from "../../utils";
9-
import { getAllocatorForChain, getChains } from "../../../common/chains";
9+
import {
10+
getChains,
11+
getOffchainAllocatorForChain,
12+
getOnchainAllocatorForChain,
13+
} from "../../../common/chains";
1014

1115
const Schema = {
1216
response: {
@@ -34,6 +38,11 @@ const Schema = {
3438
allocator: Type.Optional(
3539
Type.String({ description: "The allocator address for the chain" })
3640
),
41+
allocatorMode: Type.Optional(
42+
Type.Union([Type.Literal("offchain"), Type.Literal("onchain")], {
43+
description: "The vm type of the chain",
44+
})
45+
),
3746
}),
3847
{
3948
description: "A list of supported chains",
@@ -57,13 +66,19 @@ export default {
5766
return reply.status(200).send({
5867
chains: await Promise.all(
5968
Object.keys(chains).map(async (id) => {
69+
const allocatorMode = chains[id].metadata.allocatorChainId
70+
? "onchain"
71+
: "offchain";
6072
return {
6173
id,
6274
vmType: chains[id].vmType,
6375
depository: chains[id].depository,
6476
allocator: chains[id].depository
65-
? await getAllocatorForChain(id)
77+
? allocatorMode === "offchain"
78+
? await getOffchainAllocatorForChain(id)
79+
: await getOnchainAllocatorForChain(id)
6680
: undefined,
81+
allocatorMode,
6782
};
6883
})
6984
),

src/api/queries/configs/v1.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Type } from "@fastify/type-provider-typebox";
2+
3+
import {
4+
Endpoint,
5+
ErrorResponse,
6+
FastifyReplyTypeBox,
7+
FastifyRequestTypeBox,
8+
} from "../../utils";
9+
import { getOnchainAllocator } from "../../../utils/onchain-allocator";
10+
11+
const Schema = {
12+
response: {
13+
200: Type.Object({
14+
configs: Type.Object(
15+
{
16+
onchainAllocatorSender: Type.String({
17+
description: "The wallet sending onchain allocation requests",
18+
}),
19+
},
20+
{
21+
description: "Current configuration settings",
22+
}
23+
),
24+
}),
25+
...ErrorResponse,
26+
},
27+
};
28+
29+
export default {
30+
method: "GET",
31+
url: "/queries/configs/v1",
32+
schema: Schema,
33+
handler: async (
34+
_req: FastifyRequestTypeBox<typeof Schema>,
35+
reply: FastifyReplyTypeBox<typeof Schema>
36+
) => {
37+
const { walletClient } = await getOnchainAllocator();
38+
39+
return reply.status(200).send({
40+
configs: {
41+
onchainAllocatorSender: walletClient.account.address.toLowerCase(),
42+
},
43+
});
44+
},
45+
} as Endpoint;

src/api/queries/withdrawal-requests/v1.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import {
66
FastifyReplyTypeBox,
77
FastifyRequestTypeBox,
88
} from "../../utils";
9-
import { getAllocatorForChain } from "../../../common/chains";
10-
import { getSignature, getSigner } from "../../../utils/onchain-allocator";
9+
import {
10+
getOffchainAllocatorForChain,
11+
getOnchainAllocatorForChain,
12+
} from "../../../common/chains";
13+
import { getSignature } from "../../../utils/onchain-allocator";
1114
import { getPendingWithdrawalRequestsByOwner } from "../../../models/withdrawal-requests";
1215

1316
const Schema = {
@@ -67,11 +70,11 @@ export default {
6770
let signature: string | undefined;
6871
if (w.payloadId) {
6972
// Signed using "onchain" mode, signature might be available onchain
70-
signer = await getSigner(w.chainId);
73+
signer = await getOnchainAllocatorForChain(w.chainId);
7174
signature = await getSignature(w.id);
7275
} else {
7376
// Signed using "offchain" mode, signature already available
74-
signer = await getAllocatorForChain(w.chainId);
77+
signer = await getOffchainAllocatorForChain(w.chainId);
7578
signature = w.signature;
7679
}
7780

src/common/aws.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Signer } from '@aws-sdk/rds-signer';
2-
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
1+
import { Signer } from "@aws-sdk/rds-signer";
2+
import { fromNodeProviderChain } from "@aws-sdk/credential-providers";
33

44
interface GetIamTokenParams {
55
host: string;
@@ -8,19 +8,19 @@ interface GetIamTokenParams {
88
region?: string;
99
}
1010

11-
export const getIamToken = async({
11+
export const getIamToken = async ({
1212
host,
1313
port = 5432,
1414
user,
15-
region = 'us-east-1',
15+
region = "us-east-1",
1616
}: GetIamTokenParams) => {
1717
const signer = new Signer({
1818
hostname: host,
1919
port,
2020
username: user,
2121
region,
22-
credentials: fromNodeProviderChain()
22+
credentials: fromNodeProviderChain(),
2323
});
2424

2525
return signer.getAuthToken();
26-
}
26+
};

src/common/chains.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import TronWeb from "tronweb";
1818
import { db } from "./db";
1919
import { externalError } from "./error";
2020
import { config } from "../config";
21+
import * as onchainAllocator from "../utils/onchain-allocator";
2122

2223
// Global chain metadata
2324
export type ChainMetadata = {
@@ -78,7 +79,11 @@ export const getChain = async (chainId: string) => {
7879
return chains[chainId];
7980
};
8081

81-
export const getAllocatorForChain = async (chainId: string) => {
82+
export const getOnchainAllocatorForChain = async (chainId: string) => {
83+
return onchainAllocator.getSigner(chainId);
84+
};
85+
86+
export const getOffchainAllocatorForChain = async (chainId: string) => {
8287
const ecdsaPk = config.ecdsaPrivateKey;
8388
const ed25519Pk = config.ed25519PrivateKey;
8489

src/services/request-handler/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import TronWeb from "tronweb";
2626
import {
2727
ChainMetadataEthereumVm,
2828
ChainMetadataTronVm,
29-
getAllocatorForChain,
29+
getOffchainAllocatorForChain,
30+
getOnchainAllocatorForChain,
3031
getChain,
3132
} from "../../common/chains";
3233
import { db } from "../../common/db";
@@ -35,7 +36,6 @@ import { logger } from "../../common/logger";
3536
import {
3637
getOnchainAllocator,
3738
getSignature,
38-
getSigner,
3939
handleOneTimeApproval,
4040
logBalance,
4141
} from "../../utils/onchain-allocator";
@@ -364,7 +364,7 @@ export class RequestHandlerService {
364364
// Start constructing the PSBT
365365
const psbt = new bitcoin.Psbt({ network: bitcoin.networks.bitcoin });
366366

367-
const allocator = await getAllocatorForChain(request.chainId);
367+
const allocator = await getOffchainAllocatorForChain(request.chainId);
368368

369369
// Add allocator input UTXOs
370370
for (const utxo of additionalData.allocatorUtxos) {
@@ -615,8 +615,8 @@ export class RequestHandlerService {
615615
signature,
616616
signer:
617617
request.mode === "onchain"
618-
? await getSigner(request.chainId)
619-
: await getAllocatorForChain(request.chainId),
618+
? await getOnchainAllocatorForChain(request.chainId)
619+
: await getOffchainAllocatorForChain(request.chainId),
620620
};
621621
}
622622

0 commit comments

Comments
 (0)