sdk-triggerx 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -9
- package/package.json +1 -1
- package/src/api/checkTgBalance.ts +6 -6
- package/src/api/jobs.ts +47 -19
- package/src/client.ts +1 -1
- package/src/types.ts +4 -11
- package/test/createJobExample.ts +3 -9
package/README.md
CHANGED
|
@@ -33,6 +33,8 @@ import { TriggerXClient } from 'sdk-triggerx';
|
|
|
33
33
|
const client = new TriggerXClient('YOUR_API_KEY');
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
- To get your API key, visit: [Generate API Key](https://app.triggerx.network/generate-api)
|
|
37
|
+
|
|
36
38
|
---
|
|
37
39
|
|
|
38
40
|
### 2. Create a Job
|
|
@@ -58,9 +60,6 @@ import { createJob, JobType, ArgType } from 'sdk-triggerx';
|
|
|
58
60
|
const jobInput = {
|
|
59
61
|
jobType: JobType.Time,
|
|
60
62
|
argType: ArgType.Static,
|
|
61
|
-
userAddress: '0x...',
|
|
62
|
-
etherBalance: 10000000000000000,
|
|
63
|
-
tokenBalance: 10000000000000000000,
|
|
64
63
|
jobTitle: 'My Time Job',
|
|
65
64
|
timeFrame: 36,
|
|
66
65
|
scheduleType: 'interval',
|
|
@@ -78,6 +77,8 @@ const jobInput = {
|
|
|
78
77
|
isImua: true,
|
|
79
78
|
arguments: ['3'],
|
|
80
79
|
dynamicArgumentsScriptUrl: '',
|
|
80
|
+
// if more TG needed auto top-up TG must true for automatially top up TG
|
|
81
|
+
autotopupTG: true,
|
|
81
82
|
};
|
|
82
83
|
|
|
83
84
|
const signer = /* ethers.Signer instance */;
|
|
@@ -93,9 +94,6 @@ console.log(result);
|
|
|
93
94
|
const jobInput = {
|
|
94
95
|
jobType: JobType.Event,
|
|
95
96
|
argType: ArgType.Dynamic,
|
|
96
|
-
userAddress: '0x...',
|
|
97
|
-
etherBalance: 10000000000000000,
|
|
98
|
-
tokenBalance: 10000000000000000000,
|
|
99
97
|
jobTitle: 'My Event Job',
|
|
100
98
|
timeFrame: 36,
|
|
101
99
|
recurring: true,
|
|
@@ -112,6 +110,7 @@ const jobInput = {
|
|
|
112
110
|
arguments: [],
|
|
113
111
|
dynamicArgumentsScriptUrl: 'https://your-ipfs-url',
|
|
114
112
|
isImua: true,
|
|
113
|
+
autotopupTG: true,
|
|
115
114
|
};
|
|
116
115
|
|
|
117
116
|
const result = await createJob(client, { jobInput, signer });
|
|
@@ -126,9 +125,6 @@ console.log(result);
|
|
|
126
125
|
const jobInput = {
|
|
127
126
|
jobType: JobType.Condition,
|
|
128
127
|
argType: ArgType.Static,
|
|
129
|
-
userAddress: '0x...',
|
|
130
|
-
etherBalance: 10000000000000000,
|
|
131
|
-
tokenBalance: 10000000000000000000,
|
|
132
128
|
jobTitle: 'My Condition Job',
|
|
133
129
|
timeFrame: 36,
|
|
134
130
|
recurring: false,
|
|
@@ -147,6 +143,7 @@ const jobInput = {
|
|
|
147
143
|
arguments: ['5'],
|
|
148
144
|
dynamicArgumentsScriptUrl: '',
|
|
149
145
|
isImua: true,
|
|
146
|
+
autotopupTG: true,
|
|
150
147
|
};
|
|
151
148
|
|
|
152
149
|
const result = await createJob(client, { jobInput, signer });
|
package/package.json
CHANGED
|
@@ -13,15 +13,15 @@ export const checkTgBalance = async (signer: ethers.Signer) => {
|
|
|
13
13
|
// We'll convert TGbalance from wei to ETH
|
|
14
14
|
// If balance is an array: [ethSpent, TGbalance]
|
|
15
15
|
// If balance is an object: { ethSpent, TGbalance }
|
|
16
|
-
let tgBalanceWei;
|
|
16
|
+
let tgBalanceWei: bigint;
|
|
17
17
|
if (Array.isArray(balance)) {
|
|
18
|
-
tgBalanceWei = balance[1];
|
|
18
|
+
tgBalanceWei = balance[1] as bigint;
|
|
19
19
|
} else if (balance && balance.TGbalance !== undefined) {
|
|
20
|
-
tgBalanceWei = balance.TGbalance;
|
|
20
|
+
tgBalanceWei = balance.TGbalance as bigint;
|
|
21
21
|
} else {
|
|
22
22
|
throw new Error('Unexpected balance format');
|
|
23
23
|
}
|
|
24
|
-
const
|
|
25
|
-
console.log('tgBalanceEth',
|
|
26
|
-
return
|
|
24
|
+
const tgBalance = ethers.formatEther(tgBalanceWei);
|
|
25
|
+
console.log('tgBalanceEth', tgBalance);
|
|
26
|
+
return { tgBalanceWei, tgBalance };
|
|
27
27
|
};
|
package/src/api/jobs.ts
CHANGED
|
@@ -15,12 +15,16 @@ const JOB_ID = '3009495282496655901782243134420405284093052736340975530671528358
|
|
|
15
15
|
const DYNAMIC_ARGS_URL = 'https://teal-random-koala-993.mypinata.cloud/ipfs/bafkreif426p7t7takzhw3g6we2h6wsvf27p5jxj3gaiynqf22p3jvhx4la';
|
|
16
16
|
const JOB_REGISTRY_ADDRESS = '0xdB66c11221234C6B19cCBd29868310c31494C21C'; // Set your fixed contract address here
|
|
17
17
|
|
|
18
|
-
export function toCreateJobDataFromTime(
|
|
18
|
+
export function toCreateJobDataFromTime(
|
|
19
|
+
input: TimeBasedJobInput,
|
|
20
|
+
balances: { etherBalance: bigint; tokenBalanceWei: bigint },
|
|
21
|
+
userAddress: string,
|
|
22
|
+
): CreateJobData {
|
|
19
23
|
return {
|
|
20
24
|
job_id: JOB_ID,
|
|
21
|
-
user_address:
|
|
22
|
-
ether_balance:
|
|
23
|
-
token_balance:
|
|
25
|
+
user_address: userAddress,
|
|
26
|
+
ether_balance: balances.etherBalance,
|
|
27
|
+
token_balance: balances.tokenBalanceWei,
|
|
24
28
|
job_title: input.jobTitle,
|
|
25
29
|
task_definition_id: input.dynamicArgumentsScriptUrl ? 2 : 1,
|
|
26
30
|
custom: true,
|
|
@@ -44,12 +48,16 @@ export function toCreateJobDataFromTime(input: TimeBasedJobInput): CreateJobData
|
|
|
44
48
|
};
|
|
45
49
|
}
|
|
46
50
|
|
|
47
|
-
export function toCreateJobDataFromEvent(
|
|
51
|
+
export function toCreateJobDataFromEvent(
|
|
52
|
+
input: EventBasedJobInput,
|
|
53
|
+
balances: { etherBalance: bigint; tokenBalanceWei: bigint },
|
|
54
|
+
userAddress: string,
|
|
55
|
+
): CreateJobData {
|
|
48
56
|
return {
|
|
49
57
|
job_id: JOB_ID,
|
|
50
|
-
user_address:
|
|
51
|
-
ether_balance:
|
|
52
|
-
token_balance:
|
|
58
|
+
user_address: userAddress,
|
|
59
|
+
ether_balance: balances.etherBalance,
|
|
60
|
+
token_balance: balances.tokenBalanceWei,
|
|
53
61
|
job_title: input.jobTitle,
|
|
54
62
|
task_definition_id: input.dynamicArgumentsScriptUrl ? 4 : 3,
|
|
55
63
|
custom: true,
|
|
@@ -72,12 +80,16 @@ export function toCreateJobDataFromEvent(input: EventBasedJobInput): CreateJobDa
|
|
|
72
80
|
};
|
|
73
81
|
}
|
|
74
82
|
|
|
75
|
-
export function toCreateJobDataFromCondition(
|
|
83
|
+
export function toCreateJobDataFromCondition(
|
|
84
|
+
input: ConditionBasedJobInput,
|
|
85
|
+
balances: { etherBalance: bigint; tokenBalanceWei: bigint },
|
|
86
|
+
userAddress: string,
|
|
87
|
+
): CreateJobData {
|
|
76
88
|
return {
|
|
77
89
|
job_id: JOB_ID,
|
|
78
|
-
user_address:
|
|
79
|
-
ether_balance:
|
|
80
|
-
token_balance:
|
|
90
|
+
user_address: userAddress,
|
|
91
|
+
ether_balance: balances.etherBalance,
|
|
92
|
+
token_balance: balances.tokenBalanceWei,
|
|
81
93
|
job_title: input.jobTitle,
|
|
82
94
|
task_definition_id: input.dynamicArgumentsScriptUrl ? 6 : 5,
|
|
83
95
|
custom: true,
|
|
@@ -152,6 +164,8 @@ export async function createJob(
|
|
|
152
164
|
// Use the API key from the client instance
|
|
153
165
|
const apiKey = client.getApiKey();
|
|
154
166
|
|
|
167
|
+
const userAddress = await signer.getAddress();
|
|
168
|
+
|
|
155
169
|
let jobTitle: string, timeFrame: number, targetContractAddress: string, jobType: number;
|
|
156
170
|
if ('jobTitle' in jobInput) jobTitle = jobInput.jobTitle;
|
|
157
171
|
if ('timeFrame' in jobInput) timeFrame = jobInput.timeFrame;
|
|
@@ -249,7 +263,7 @@ export async function createJob(
|
|
|
249
263
|
// If you want to automate, you can add a `proceed` flag to params in the future.
|
|
250
264
|
|
|
251
265
|
// Check if the user has enough TG to cover the job cost prediction
|
|
252
|
-
const tgBalance = await checkTgBalance(signer);
|
|
266
|
+
const { tgBalanceWei, tgBalance } = await checkTgBalance(signer);
|
|
253
267
|
if (Number(tgBalance) < job_cost_prediction) {
|
|
254
268
|
// Check if user has enabled auto topup
|
|
255
269
|
// For each job type, autotopupTG should be present in jobInput
|
|
@@ -258,9 +272,15 @@ export async function createJob(
|
|
|
258
272
|
throw new Error(`Insufficient TG balance. Job cost prediction is ${job_cost_prediction}. Current TG balance: ${tgBalance}. Please set autotopupTG: true in jobInput.`);
|
|
259
273
|
} else {
|
|
260
274
|
// autotopupTG is true, automatically top up
|
|
261
|
-
|
|
275
|
+
const requiredTG = Math.ceil(job_cost_prediction * 1000); // 1 TG = 0.001 ETH
|
|
276
|
+
await topupTg(requiredTG, signer);
|
|
262
277
|
}
|
|
263
|
-
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Compute balances to store with the job
|
|
281
|
+
const tokenBalanceWei = tgBalanceWei;
|
|
282
|
+
const etherBalance = tokenBalanceWei / 1000n;
|
|
283
|
+
|
|
264
284
|
// Patch jobInput with job_cost_prediction for downstream usage
|
|
265
285
|
(jobInput as any).jobCostPrediction = job_cost_prediction;
|
|
266
286
|
|
|
@@ -277,21 +297,29 @@ export async function createJob(
|
|
|
277
297
|
|
|
278
298
|
// 2. Convert input to CreateJobData
|
|
279
299
|
let jobData: CreateJobData;
|
|
300
|
+
const balances = { etherBalance, tokenBalanceWei };
|
|
280
301
|
if ('scheduleType' in jobInput) {
|
|
281
|
-
jobData = toCreateJobDataFromTime(jobInput as TimeBasedJobInput);
|
|
302
|
+
jobData = toCreateJobDataFromTime(jobInput as TimeBasedJobInput, balances, userAddress);
|
|
282
303
|
} else if ('triggerChainId' in jobInput) {
|
|
283
|
-
jobData = toCreateJobDataFromEvent(jobInput as EventBasedJobInput);
|
|
304
|
+
jobData = toCreateJobDataFromEvent(jobInput as EventBasedJobInput, balances, userAddress);
|
|
284
305
|
} else {
|
|
285
|
-
jobData = toCreateJobDataFromCondition(jobInput as ConditionBasedJobInput);
|
|
306
|
+
jobData = toCreateJobDataFromCondition(jobInput as ConditionBasedJobInput, balances, userAddress);
|
|
286
307
|
}
|
|
287
308
|
// 3. Set the job_id from contract
|
|
288
309
|
jobData.job_id = jobId;
|
|
289
310
|
|
|
290
311
|
// 4. Call the API
|
|
291
312
|
try {
|
|
313
|
+
// Ensure JSON-serializable payload (use numbers for balances)
|
|
314
|
+
const jobDataForApi = {
|
|
315
|
+
...jobData,
|
|
316
|
+
ether_balance: typeof jobData.ether_balance === 'bigint' ? Number(jobData.ether_balance) : Number(jobData.ether_balance),
|
|
317
|
+
token_balance: typeof jobData.token_balance === 'bigint' ? Number(jobData.token_balance) : Number(jobData.token_balance),
|
|
318
|
+
} as any;
|
|
319
|
+
|
|
292
320
|
const res = await client.post<any>(
|
|
293
321
|
'/api/jobs',
|
|
294
|
-
[
|
|
322
|
+
[jobDataForApi],
|
|
295
323
|
{
|
|
296
324
|
headers: { 'Content-Type': 'application/json', 'X-API-KEY': apiKey },
|
|
297
325
|
}
|
package/src/client.ts
CHANGED
|
@@ -9,7 +9,7 @@ export class TriggerXClient {
|
|
|
9
9
|
this.apiKey = apiKey; // Initialize the apiKey
|
|
10
10
|
const baseConfig = getConfig();
|
|
11
11
|
this.client = axios.create({
|
|
12
|
-
baseURL: config?.baseURL || baseConfig.apiUrl || 'http://localhost:9002',
|
|
12
|
+
baseURL: config?.baseURL || baseConfig.apiUrl || 'http://localhost:9002', //http://localhost:9002 , https://data.triggerx.network
|
|
13
13
|
headers: { 'Authorization': `Bearer ${this.apiKey}` }, // Set the API key here
|
|
14
14
|
...config,
|
|
15
15
|
});
|
package/src/types.ts
CHANGED
|
@@ -30,9 +30,6 @@ export type CreateJobInput =
|
|
|
30
30
|
|
|
31
31
|
// User-facing input types (without jobType/argType)
|
|
32
32
|
export interface TimeBasedJobInput {
|
|
33
|
-
userAddress: string;
|
|
34
|
-
etherBalance: BigInt | number;
|
|
35
|
-
tokenBalance: BigInt | number;
|
|
36
33
|
jobTitle: string;
|
|
37
34
|
timeFrame: number;
|
|
38
35
|
scheduleType: 'cron' | 'specific' | 'interval';
|
|
@@ -54,9 +51,6 @@ export interface TimeBasedJobInput {
|
|
|
54
51
|
}
|
|
55
52
|
|
|
56
53
|
export interface EventBasedJobInput {
|
|
57
|
-
userAddress: string;
|
|
58
|
-
etherBalance: BigInt | number;
|
|
59
|
-
tokenBalance: BigInt | number;
|
|
60
54
|
jobTitle: string;
|
|
61
55
|
timeFrame: number;
|
|
62
56
|
triggerChainId: string;
|
|
@@ -77,9 +71,6 @@ export interface EventBasedJobInput {
|
|
|
77
71
|
}
|
|
78
72
|
|
|
79
73
|
export interface ConditionBasedJobInput {
|
|
80
|
-
userAddress: string;
|
|
81
|
-
etherBalance: BigInt | number;
|
|
82
|
-
tokenBalance: BigInt | number;
|
|
83
74
|
jobTitle: string;
|
|
84
75
|
timeFrame: number;
|
|
85
76
|
conditionType: string;
|
|
@@ -199,6 +190,7 @@ export interface EventJobData {
|
|
|
199
190
|
trigger_chain_id: string;
|
|
200
191
|
trigger_contract_address: string;
|
|
201
192
|
trigger_event: string;
|
|
193
|
+
timezone: string;
|
|
202
194
|
target_chain_id: string;
|
|
203
195
|
target_contract_address: string;
|
|
204
196
|
target_function: string;
|
|
@@ -218,10 +210,11 @@ export interface ConditionJobData {
|
|
|
218
210
|
updated_at: Date; // Assuming time.Time is represented as Date
|
|
219
211
|
recurring: boolean;
|
|
220
212
|
condition_type: string;
|
|
221
|
-
upper_limit: number;
|
|
222
|
-
lower_limit: number;
|
|
213
|
+
upper_limit: number;
|
|
214
|
+
lower_limit: number;
|
|
223
215
|
value_source_type: string;
|
|
224
216
|
value_source_url: string;
|
|
217
|
+
timezone: string;
|
|
225
218
|
target_chain_id: string;
|
|
226
219
|
target_contract_address: string;
|
|
227
220
|
target_function: string;
|
package/test/createJobExample.ts
CHANGED
|
@@ -4,16 +4,13 @@ import { ethers } from 'ethers';
|
|
|
4
4
|
|
|
5
5
|
async function main() {
|
|
6
6
|
|
|
7
|
-
const apiKey = '
|
|
7
|
+
const apiKey = '';
|
|
8
8
|
const client = new TriggerXClient(apiKey);
|
|
9
9
|
|
|
10
10
|
// Example: Time-based static job
|
|
11
11
|
const jobInput: CreateJobInput = {
|
|
12
12
|
jobType: JobType.Time,
|
|
13
13
|
argType: ArgType.Static,
|
|
14
|
-
userAddress: '0x7Db951c0E6D8906687B459427eA3F3F2b456473B',
|
|
15
|
-
etherBalance: 50000000000000000,
|
|
16
|
-
tokenBalance: 50000000000000000000,
|
|
17
14
|
jobTitle: 'SDK Test Time Job',
|
|
18
15
|
timeFrame: 36,
|
|
19
16
|
scheduleType: 'interval',
|
|
@@ -37,9 +34,6 @@ async function main() {
|
|
|
37
34
|
// const jobInput: CreateJobInput = {
|
|
38
35
|
// jobType: JobType.Condition,
|
|
39
36
|
// argType: ArgType.Static,
|
|
40
|
-
// userAddress: '0x7Db951c0E6D8906687B459427eA3F3F2b456473B',
|
|
41
|
-
// etherBalance: 50000000000000000,
|
|
42
|
-
// tokenBalance: 50000000000000000000,
|
|
43
37
|
// jobTitle: 'SDK Test Condition Job',
|
|
44
38
|
// timeFrame: 48,
|
|
45
39
|
// conditionType: 'greaterThan',
|
|
@@ -68,8 +62,8 @@ async function main() {
|
|
|
68
62
|
// console.log('Condition Job creation result:', conditionResult);
|
|
69
63
|
|
|
70
64
|
// These would typically come from env/config/user input
|
|
71
|
-
const privateKey = '
|
|
72
|
-
const providerUrl = '
|
|
65
|
+
const privateKey = '';
|
|
66
|
+
const providerUrl = '';
|
|
73
67
|
const provider = new ethers.JsonRpcProvider(providerUrl);
|
|
74
68
|
const signer = new ethers.Wallet(privateKey, provider);
|
|
75
69
|
|