-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.ts
More file actions
242 lines (231 loc) · 8.15 KB
/
Copy pathindex.ts
File metadata and controls
242 lines (231 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { Address } from "viem";
import axios, { AxiosInstance } from "axios";
import {
AutomationSubscriptionLimits,
PreComputedAddressData,
TaskStatusData,
TransferCalldataResponse,
PrecomputeResponse,
} from "./types";
const routes = {
// Public Deployer Routes
fetchPreComputeData: "/deployer/public-strategy/precompute",
fetchDeployerSignature: "/deployer/public-strategy/signature",
deployPublicStrategy: "/deployer/public-strategy/deploy",
fetchTaskStatus: "/relayer/tasks/status",
};
export class PublicDeployer {
private readonly axiosInstance: AxiosInstance;
constructor(apiKey: string, baseURL: string) {
this.axiosInstance = axios.create({
baseURL,
headers: {
"x-api-key": apiKey,
},
});
}
/**
* Fetches the precomputed address data for a given owner, chain ID, and fee token.
* @param {Address} owner - The address of the owner.
* @param {number} chainId - Chain ID for connected network.
* @param {Address} feeToken - The address of the fee token.
* @returns {Promise<PreComputedAddressData | null>} The precomputed address data or null if an error occurs.
*/
async fetchPreComputeData(
owner: Address,
chainId: number,
feeToken: Address
): Promise<PreComputedAddressData | null> {
const payload = { owner, chainID: chainId, feeToken };
try {
const response = await this.axiosInstance.post(
routes.fetchPreComputeData,
payload
);
return response.data.data;
} catch (err) {
console.error("Error fetching precompute address:", err);
return null;
}
}
/**
* Generates an automation sub-account for a given set of parameters.
* @param {Address} owner - The address of the owner.
* @param {Address} precomputedConsoleAddress - The precomputed account address (from /pre-compute response)
* @param {number} chainID - Chain ID for connected network.
* @param {string} registryID - The registry ID. (Hardcoded)
* @param {Address} feeToken - The address of the fee token. (from /pre-compute response)
* @param {string} feeEstimate - The estimated fee.
* @param {Address[]} tokens - The list of token addresses.
* @param {string[]} amounts - The list of token amounts.
* @param {AutomationSubscriptionLimits} automationSubscriptionLimits - The subscription limits.
* @param {Record<string, unknown>} metadata - Additional metadata.
* @param {Address} multisendAddress - The address of the multisend contract.
* @returns {Promise<TransferCalldataResponse | null>} The transfer calldata response or null if an error occurs.
*/
async generateAutomationSubAccount(
owner: Address,
precomputedConsoleAddress: Address,
chainID: number,
registryID: string,
feeToken: Address,
feeEstimate: string,
tokens: Address[],
amounts: string[],
automationSubscriptionLimits: AutomationSubscriptionLimits,
metadata: Record<string, unknown>,
multisendAddress?: Address
): Promise<TransferCalldataResponse | null> {
const payload = {
owner,
precomputedConsoleAddress,
automationSubscriptionLimits,
chainID,
registryID,
feeToken,
feeEstimate,
tokens,
amounts,
metadata,
multisendAddress,
};
try {
const response = await this.axiosInstance.post(
routes.fetchDeployerSignature,
payload
);
return response.data.data;
} catch (err) {
console.error("Error generating automation sub-account:", err);
return null;
}
}
/**
* Computes the deployment addresses for account and sub-account setup.
* @param {Address} owner - The address of the owner.
* @param {number} chainID - Chain ID for connected network.
* @param {string} registryID - The registry ID (Hardcoded).
* @param {string} subscriptionDraftID - The subscription draft ID (from /signature response).
* @param {string} subAccountPolicyCommit - The sub-account policy commit (from /signature response).
* @param {Address} feeToken - The address of the fee token.
* @param {Address[]} tokens - The list of token addresses.
* @param {string[]} amounts - The list of token amounts.
* @param {string} subAccountChainerSignature - The sub-account chainer signature (from /signature response).
* @param {string} feeEstimateSignature - The fee estimate signature (from /pre-compute response).
* @param {string} feeEstimate - The estimated fee.
* @param {Record<string, unknown>} metadata - Additional metadata.
* @returns {Promise<PrecomputeResponse | null>} The precomputed deployment addresses or null if an error occurs.
*/
async computeDeploymentAddresses(
owner: Address,
chainID: number,
registryID: string,
subscriptionDraftID: string,
subAccountPolicyCommit: string,
feeToken: Address,
tokens: Address[],
amounts: string[],
subAccountChainerSignature: string,
feeEstimateSignature: string,
feeEstimate: string,
metadata: Record<string, unknown>
): Promise<PrecomputeResponse | null> {
const payload = {
owner,
chainID,
registryID,
subscriptionDraftID,
subAccountPolicyCommit,
feeToken,
tokens,
amounts,
subAccountChainerSignature,
feeEstimateSignature,
feeEstimate,
metadata,
preComputeAddresses: true,
};
try {
const response = await this.axiosInstance.post(
routes.deployPublicStrategy,
payload
);
return response.data.data;
} catch (err) {
console.error("Error deploying account and sub-account:", err);
return null;
}
}
/**
* Deploys a account and sub-account with the given parameters.
* @param {Address} owner - The address of the owner.
* @param {number} chainID - The ID of the blockchain network.
* @param {string} registryID - The registry ID. (Hardcoded)
* @param {string} subscriptionDraftID - The subscription draft ID. (from /signature response)
* @param {string} subAccountPolicyCommit - The sub-account policy commit. (from /signature response)
* @param {Address} feeToken - The address of the fee token.
* @param {Address[]} tokens - The list of token addresses.
* @param {string[]} amounts - The list of token amounts.
* @param {string} subAccountChainerSignature - The sub-account chainer signature. (from /signature response)
* @param {string} feeEstimateSignature - The fee estimate signature. (from /pre-compute response)
* @param {string} feeEstimate - The estimated fee. (from /pre-compute response)
* @param {Record<string, unknown>} metadata - Additional metadata.
* @returns {Promise<{ taskId: string } | null>} The task ID or null if an error occurs.
*/
async deployBrahmaAccount(
owner: Address,
chainID: number,
registryID: string,
subscriptionDraftID: string,
subAccountPolicyCommit: string,
feeToken: Address,
tokens: Address[],
amounts: string[],
subAccountChainerSignature: string,
feeEstimateSignature: string,
feeEstimate: string,
metadata: Record<string, unknown>
): Promise<{ taskId: string } | null> {
const payload = {
owner,
chainID,
registryID,
subscriptionDraftID,
subAccountPolicyCommit,
feeToken,
tokens,
amounts,
subAccountChainerSignature,
feeEstimateSignature,
feeEstimate,
metadata,
};
try {
const response = await this.axiosInstance.post(
routes.deployPublicStrategy,
payload
);
return response.data.data;
} catch (err) {
console.error("Error deploying account and sub-account:", err);
return null;
}
}
/**
* Fetches the status of a task by its ID.
* @param {string} taskId - The ID of the task. (from /deploy endpoint)
* @returns {Promise<TaskStatusData>} The task status data.
* @throws Will throw an error if the request fails.
*/
async fetchDeploymentStatus(taskId: string): Promise<TaskStatusData> {
try {
const response = await this.axiosInstance.get(
`${routes.fetchTaskStatus}/${taskId}`
);
return response.data.data;
} catch (err) {
console.error("Error fetching task status:", err);
throw err;
}
}
}