sdk-triggerx 0.1.0 → 0.1.1
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 +7 -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 +1 -6
package/README.md
CHANGED
|
@@ -31,6 +31,9 @@ npm install sdk-triggerx
|
|
|
31
31
|
import { TriggerXClient } from 'sdk-triggerx';
|
|
32
32
|
|
|
33
33
|
const client = new TriggerXClient('YOUR_API_KEY');
|
|
34
|
+
|
|
35
|
+
//👉 To get your API key, visit: [https://app.triggerx.network/generate-api](https://app.triggerx.network/generate-api)
|
|
36
|
+
|
|
34
37
|
```
|
|
35
38
|
|
|
36
39
|
---
|
|
@@ -58,9 +61,6 @@ import { createJob, JobType, ArgType } from 'sdk-triggerx';
|
|
|
58
61
|
const jobInput = {
|
|
59
62
|
jobType: JobType.Time,
|
|
60
63
|
argType: ArgType.Static,
|
|
61
|
-
userAddress: '0x...',
|
|
62
|
-
etherBalance: 10000000000000000,
|
|
63
|
-
tokenBalance: 10000000000000000000,
|
|
64
64
|
jobTitle: 'My Time Job',
|
|
65
65
|
timeFrame: 36,
|
|
66
66
|
scheduleType: 'interval',
|
|
@@ -78,6 +78,8 @@ const jobInput = {
|
|
|
78
78
|
isImua: true,
|
|
79
79
|
arguments: ['3'],
|
|
80
80
|
dynamicArgumentsScriptUrl: '',
|
|
81
|
+
// if more TG needed auto top-up TG must true for automatially top up TG
|
|
82
|
+
autotopupTG: true,
|
|
81
83
|
};
|
|
82
84
|
|
|
83
85
|
const signer = /* ethers.Signer instance */;
|
|
@@ -93,9 +95,6 @@ console.log(result);
|
|
|
93
95
|
const jobInput = {
|
|
94
96
|
jobType: JobType.Event,
|
|
95
97
|
argType: ArgType.Dynamic,
|
|
96
|
-
userAddress: '0x...',
|
|
97
|
-
etherBalance: 10000000000000000,
|
|
98
|
-
tokenBalance: 10000000000000000000,
|
|
99
98
|
jobTitle: 'My Event Job',
|
|
100
99
|
timeFrame: 36,
|
|
101
100
|
recurring: true,
|
|
@@ -112,6 +111,7 @@ const jobInput = {
|
|
|
112
111
|
arguments: [],
|
|
113
112
|
dynamicArgumentsScriptUrl: 'https://your-ipfs-url',
|
|
114
113
|
isImua: true,
|
|
114
|
+
autotopupTG: true,
|
|
115
115
|
};
|
|
116
116
|
|
|
117
117
|
const result = await createJob(client, { jobInput, signer });
|
|
@@ -126,9 +126,6 @@ console.log(result);
|
|
|
126
126
|
const jobInput = {
|
|
127
127
|
jobType: JobType.Condition,
|
|
128
128
|
argType: ArgType.Static,
|
|
129
|
-
userAddress: '0x...',
|
|
130
|
-
etherBalance: 10000000000000000,
|
|
131
|
-
tokenBalance: 10000000000000000000,
|
|
132
129
|
jobTitle: 'My Condition Job',
|
|
133
130
|
timeFrame: 36,
|
|
134
131
|
recurring: false,
|
|
@@ -147,6 +144,7 @@ const jobInput = {
|
|
|
147
144
|
arguments: ['5'],
|
|
148
145
|
dynamicArgumentsScriptUrl: '',
|
|
149
146
|
isImua: true,
|
|
147
|
+
autotopupTG: true,
|
|
150
148
|
};
|
|
151
149
|
|
|
152
150
|
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
|
@@ -11,9 +11,6 @@ async function main() {
|
|
|
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,7 @@ async function main() {
|
|
|
37
34
|
// const jobInput: CreateJobInput = {
|
|
38
35
|
// jobType: JobType.Condition,
|
|
39
36
|
// argType: ArgType.Static,
|
|
40
|
-
// userAddress: '
|
|
41
|
-
// etherBalance: 50000000000000000,
|
|
42
|
-
// tokenBalance: 50000000000000000000,
|
|
37
|
+
// userAddress: '0x7Db951c0E6D8906687B459427eA3F3F2b456473A',
|
|
43
38
|
// jobTitle: 'SDK Test Condition Job',
|
|
44
39
|
// timeFrame: 48,
|
|
45
40
|
// conditionType: 'greaterThan',
|