sdk-triggerx 0.1.27 → 0.1.28
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/dist/api/checkTgBalance.d.ts +9 -1
- package/dist/api/checkTgBalance.js +38 -6
- package/dist/api/jobs.js +5 -6
- package/dist/config.d.ts +10 -0
- package/dist/config.js +21 -0
- package/package.json +1 -1
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { ethers } from 'ethers';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Check TG balance for a given signer using SDK-provided RPC
|
|
4
|
+
* This function uses our own RPC provider to ensure reliable connection
|
|
5
|
+
* even if the user's RPC fails
|
|
6
|
+
* @param signer - ethers.Signer instance (used to get wallet address)
|
|
7
|
+
* @param chainId - Optional chain ID. If not provided, will try to get from signer's provider
|
|
8
|
+
* @returns Balance information or error response
|
|
9
|
+
*/
|
|
10
|
+
export declare const checkTgBalance: (signer: ethers.Signer, chainId?: string | number) => Promise<{
|
|
3
11
|
success: boolean;
|
|
4
12
|
data?: {
|
|
5
13
|
tgBalanceWei: bigint;
|
|
@@ -8,21 +8,53 @@ const ethers_1 = require("ethers");
|
|
|
8
8
|
const GasRegistry_json_1 = __importDefault(require("../contracts/abi/GasRegistry.json"));
|
|
9
9
|
const config_1 = require("../config");
|
|
10
10
|
const errors_1 = require("../utils/errors");
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Check TG balance for a given signer using SDK-provided RPC
|
|
13
|
+
* This function uses our own RPC provider to ensure reliable connection
|
|
14
|
+
* even if the user's RPC fails
|
|
15
|
+
* @param signer - ethers.Signer instance (used to get wallet address)
|
|
16
|
+
* @param chainId - Optional chain ID. If not provided, will try to get from signer's provider
|
|
17
|
+
* @returns Balance information or error response
|
|
18
|
+
*/
|
|
19
|
+
const checkTgBalance = async (signer, chainId) => {
|
|
12
20
|
// Validate inputs
|
|
13
21
|
if (!signer) {
|
|
14
22
|
return (0, errors_1.createErrorResponse)(new errors_1.ValidationError('signer', 'Signer is required'), 'Validation error');
|
|
15
23
|
}
|
|
16
24
|
try {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
25
|
+
// Try to get chainId from signer's provider if not provided
|
|
26
|
+
// If signer's provider fails, we'll use the provided chainId or return error
|
|
27
|
+
let resolvedChainId = chainId?.toString();
|
|
28
|
+
if (!resolvedChainId) {
|
|
29
|
+
try {
|
|
30
|
+
const network = await signer.provider?.getNetwork();
|
|
31
|
+
resolvedChainId = network?.chainId ? network.chainId.toString() : undefined;
|
|
32
|
+
}
|
|
33
|
+
catch (providerError) {
|
|
34
|
+
// If user's RPC fails, we can't get chainId from it
|
|
35
|
+
// This is expected in cases where user's RPC is down
|
|
36
|
+
console.warn('Could not get network from signer provider, using provided chainId or will fail:', providerError);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (!resolvedChainId) {
|
|
40
|
+
return (0, errors_1.createErrorResponse)(new errors_1.ConfigurationError('Chain ID is required. Please provide chainId parameter or ensure signer has a working provider.'), 'Configuration error');
|
|
41
|
+
}
|
|
42
|
+
const { gasRegistry } = (0, config_1.getChainAddresses)(resolvedChainId);
|
|
20
43
|
const gasRegistryContractAddress = gasRegistry;
|
|
21
44
|
if (!gasRegistryContractAddress) {
|
|
22
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ConfigurationError(`GasRegistry address not configured for chain ID: ${
|
|
45
|
+
return (0, errors_1.createErrorResponse)(new errors_1.ConfigurationError(`GasRegistry address not configured for chain ID: ${resolvedChainId}`), 'Configuration error');
|
|
46
|
+
}
|
|
47
|
+
// Use SDK-provided RPC provider instead of user's provider
|
|
48
|
+
// This ensures we can read balance even if user's RPC fails
|
|
49
|
+
const rpcProvider = (0, config_1.getRpcProvider)(resolvedChainId);
|
|
50
|
+
if (!rpcProvider) {
|
|
51
|
+
return (0, errors_1.createErrorResponse)(new errors_1.ConfigurationError(`RPC URL not configured for chain ID: ${resolvedChainId}. Cannot check balance without RPC provider.`), 'Configuration error');
|
|
23
52
|
}
|
|
24
|
-
|
|
53
|
+
// Create contract instance with our RPC provider (read-only)
|
|
54
|
+
const contract = new ethers_1.ethers.Contract(gasRegistryContractAddress, GasRegistry_json_1.default, rpcProvider);
|
|
55
|
+
// Get address from signer (this doesn't require provider)
|
|
25
56
|
const address = await signer.getAddress();
|
|
57
|
+
// Read balance using our RPC provider
|
|
26
58
|
const balance = await contract.balances(address);
|
|
27
59
|
// balance is likely an array or object with ethSpent and TGbalance, both in wei
|
|
28
60
|
// We'll convert TGbalance from wei to ETH
|
package/dist/api/jobs.js
CHANGED
|
@@ -16,8 +16,6 @@ const config_1 = require("../config");
|
|
|
16
16
|
const validation_1 = require("../utils/validation");
|
|
17
17
|
const errors_1 = require("../utils/errors");
|
|
18
18
|
const SafeWallet_1 = require("../contracts/safe/SafeWallet");
|
|
19
|
-
const JOB_ID = '300949528249665590178224313442040528409305273634097553067152835846309150732';
|
|
20
|
-
const DYNAMIC_ARGS_URL = 'https://teal-random-koala-993.mypinata.cloud/ipfs/bafkreif426p7t7takzhw3g6we2h6wsvf27p5jxj3gaiynqf22p3jvhx4la';
|
|
21
19
|
// Helper function to encode multisend batch transactions
|
|
22
20
|
function encodeMultisendData(transactions) {
|
|
23
21
|
// Multisend format: for each transaction, encode as:
|
|
@@ -55,7 +53,7 @@ function encodeMultisendData(transactions) {
|
|
|
55
53
|
}
|
|
56
54
|
function toCreateJobDataFromTime(input, balances, userAddress, jobCostPrediction) {
|
|
57
55
|
return {
|
|
58
|
-
job_id:
|
|
56
|
+
job_id: "",
|
|
59
57
|
user_address: userAddress,
|
|
60
58
|
ether_balance: balances.etherBalance,
|
|
61
59
|
token_balance: balances.tokenBalanceWei,
|
|
@@ -87,7 +85,7 @@ function toCreateJobDataFromTime(input, balances, userAddress, jobCostPrediction
|
|
|
87
85
|
}
|
|
88
86
|
function toCreateJobDataFromEvent(input, balances, userAddress, jobCostPrediction) {
|
|
89
87
|
return {
|
|
90
|
-
job_id:
|
|
88
|
+
job_id: "",
|
|
91
89
|
user_address: userAddress,
|
|
92
90
|
ether_balance: balances.etherBalance,
|
|
93
91
|
token_balance: balances.tokenBalanceWei,
|
|
@@ -118,7 +116,7 @@ function toCreateJobDataFromEvent(input, balances, userAddress, jobCostPredictio
|
|
|
118
116
|
}
|
|
119
117
|
function toCreateJobDataFromCondition(input, balances, userAddress, jobCostPrediction) {
|
|
120
118
|
return {
|
|
121
|
-
job_id:
|
|
119
|
+
job_id: "",
|
|
122
120
|
user_address: userAddress,
|
|
123
121
|
ether_balance: balances.etherBalance,
|
|
124
122
|
token_balance: balances.tokenBalanceWei,
|
|
@@ -408,9 +406,10 @@ async function createJob(client, params) {
|
|
|
408
406
|
// We'll throw an error with the fee and let the caller handle the prompt/confirmation.
|
|
409
407
|
// If you want to automate, you can add a `proceed` flag to params in the future.
|
|
410
408
|
// Check if the user has enough TG to cover the job cost prediction
|
|
409
|
+
// Use chainId if available, so we can use SDK RPC provider even if user's RPC fails
|
|
411
410
|
let tgBalanceWei, tgBalance;
|
|
412
411
|
try {
|
|
413
|
-
const balanceResult = await (0, checkTgBalance_1.checkTgBalance)(signer);
|
|
412
|
+
const balanceResult = await (0, checkTgBalance_1.checkTgBalance)(signer, chainIdStr);
|
|
414
413
|
if (!balanceResult.success || !balanceResult.data) {
|
|
415
414
|
return (0, errors_1.createErrorResponse)(new errors_1.BalanceError('Failed to check TG balance', balanceResult.details), 'Balance check error');
|
|
416
415
|
}
|
package/dist/config.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
1
2
|
export interface SDKConfig {
|
|
2
3
|
apiKey: string;
|
|
3
4
|
apiUrl: string;
|
|
@@ -9,8 +10,16 @@ export declare const CONTRACT_ADDRESSES_BY_CHAIN: Record<string, {
|
|
|
9
10
|
safeModule?: string;
|
|
10
11
|
safeSingleton?: string;
|
|
11
12
|
multisendCallOnly?: string;
|
|
13
|
+
rpcUrl?: string;
|
|
12
14
|
}>;
|
|
13
15
|
export declare function getConfig(): SDKConfig;
|
|
16
|
+
/**
|
|
17
|
+
* Get RPC provider for a given chain ID using SDK-configured RPC URLs
|
|
18
|
+
* This ensures reliable connection even if user's RPC fails
|
|
19
|
+
* @param chainId - Chain ID as string or number
|
|
20
|
+
* @returns ethers.JsonRpcProvider instance or null if chain not supported
|
|
21
|
+
*/
|
|
22
|
+
export declare function getRpcProvider(chainId: string | number | undefined): ethers.JsonRpcProvider | null;
|
|
14
23
|
export declare function getChainAddresses(chainId: string | number | undefined): {
|
|
15
24
|
gasRegistry: string;
|
|
16
25
|
jobRegistry: string;
|
|
@@ -18,4 +27,5 @@ export declare function getChainAddresses(chainId: string | number | undefined):
|
|
|
18
27
|
safeModule: string | undefined;
|
|
19
28
|
safeSingleton: string | undefined;
|
|
20
29
|
multisendCallOnly: string | undefined;
|
|
30
|
+
rpcUrl: string | undefined;
|
|
21
31
|
};
|
package/dist/config.js
CHANGED
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.CONTRACT_ADDRESSES_BY_CHAIN = void 0;
|
|
6
6
|
exports.getConfig = getConfig;
|
|
7
|
+
exports.getRpcProvider = getRpcProvider;
|
|
7
8
|
exports.getChainAddresses = getChainAddresses;
|
|
9
|
+
const ethers_1 = require("ethers");
|
|
8
10
|
// Contract addresses per chain
|
|
9
11
|
// Keyed by chainId as string to avoid bigint conversions throughout the SDK
|
|
10
12
|
exports.CONTRACT_ADDRESSES_BY_CHAIN = {
|
|
@@ -16,6 +18,7 @@ exports.CONTRACT_ADDRESSES_BY_CHAIN = {
|
|
|
16
18
|
safeFactory: '0x04359eDC46Cd6C6BD7F6359512984222BE10F8Be',
|
|
17
19
|
safeModule: '0xa0bC1477cfc452C05786262c377DE51FB8bc4669',
|
|
18
20
|
multisendCallOnly: '0x9641d764fc13c8B624c04430C7356C1C7C8102e2',
|
|
21
|
+
rpcUrl: 'https://sepolia.optimism.io',
|
|
19
22
|
},
|
|
20
23
|
// Ethereum Sepolia (11155111) - Ethereum Sepolia Testnet
|
|
21
24
|
'11155111': {
|
|
@@ -24,6 +27,7 @@ exports.CONTRACT_ADDRESSES_BY_CHAIN = {
|
|
|
24
27
|
safeFactory: '0xdf76E2A796a206D877086c717979054544B1D9Bc',
|
|
25
28
|
safeModule: '0xa0bC1477cfc452C05786262c377DE51FB8bc4669',
|
|
26
29
|
multisendCallOnly: '0x9641d764fc13c8B624c04430C7356C1C7C8102e2',
|
|
30
|
+
rpcUrl: 'https://rpc.sepolia.org',
|
|
27
31
|
},
|
|
28
32
|
// Arbitrum Sepolia (421614) - Arbitrum Sepolia Testnet
|
|
29
33
|
'421614': {
|
|
@@ -32,6 +36,7 @@ exports.CONTRACT_ADDRESSES_BY_CHAIN = {
|
|
|
32
36
|
safeFactory: '0x04359eDC46Cd6C6BD7F6359512984222BE10F8Be',
|
|
33
37
|
safeModule: '0xa0bC1477cfc452C05786262c377DE51FB8bc4669',
|
|
34
38
|
multisendCallOnly: '0x9641d764fc13c8B624c04430C7356C1C7C8102e2',
|
|
39
|
+
rpcUrl: 'https://sepolia-rollup.arbitrum.io/rpc',
|
|
35
40
|
// safeSingleton can be provided per deployment (Safe or SafeL2)
|
|
36
41
|
},
|
|
37
42
|
// Base Sepolia (84532) - Base Sepolia Testnet
|
|
@@ -41,6 +46,7 @@ exports.CONTRACT_ADDRESSES_BY_CHAIN = {
|
|
|
41
46
|
safeFactory: '0x04359eDC46Cd6C6BD7F6359512984222BE10F8Be',
|
|
42
47
|
safeModule: '0xa0bC1477cfc452C05786262c377DE51FB8bc4669',
|
|
43
48
|
multisendCallOnly: '0x9641d764fc13c8B624c04430C7356C1C7C8102e2',
|
|
49
|
+
rpcUrl: 'https://sepolia.base.org',
|
|
44
50
|
},
|
|
45
51
|
// MAINNET CONFIGURATIONS
|
|
46
52
|
// Arbitrum One (42161) - Mainnet
|
|
@@ -48,6 +54,7 @@ exports.CONTRACT_ADDRESSES_BY_CHAIN = {
|
|
|
48
54
|
gasRegistry: '0x93dDB2307F3Af5df85F361E5Cddd898Acd3d132d',
|
|
49
55
|
jobRegistry: '0xAf1189aFd1F1880F09AeC3Cbc32cf415c735C710',
|
|
50
56
|
multisendCallOnly: '0x9641d764fc13c8B624c04430C7356C1C7C8102e2',
|
|
57
|
+
rpcUrl: 'https://arb1.arbitrum.io/rpc',
|
|
51
58
|
},
|
|
52
59
|
// Default/fallbacks can be extended as needed for other networks
|
|
53
60
|
};
|
|
@@ -57,6 +64,19 @@ function getConfig() {
|
|
|
57
64
|
apiUrl: '',
|
|
58
65
|
};
|
|
59
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Get RPC provider for a given chain ID using SDK-configured RPC URLs
|
|
69
|
+
* This ensures reliable connection even if user's RPC fails
|
|
70
|
+
* @param chainId - Chain ID as string or number
|
|
71
|
+
* @returns ethers.JsonRpcProvider instance or null if chain not supported
|
|
72
|
+
*/
|
|
73
|
+
function getRpcProvider(chainId) {
|
|
74
|
+
const { rpcUrl } = getChainAddresses(chainId);
|
|
75
|
+
if (!rpcUrl) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
return new ethers_1.ethers.JsonRpcProvider(rpcUrl);
|
|
79
|
+
}
|
|
60
80
|
function getChainAddresses(chainId) {
|
|
61
81
|
const chainKey = String(chainId ?? '');
|
|
62
82
|
const mapped = exports.CONTRACT_ADDRESSES_BY_CHAIN[chainKey];
|
|
@@ -67,5 +87,6 @@ function getChainAddresses(chainId) {
|
|
|
67
87
|
safeModule: mapped ? mapped.safeModule : '',
|
|
68
88
|
safeSingleton: mapped ? mapped.safeSingleton : '',
|
|
69
89
|
multisendCallOnly: mapped ? mapped.multisendCallOnly : '',
|
|
90
|
+
rpcUrl: mapped ? mapped.rpcUrl : '',
|
|
70
91
|
};
|
|
71
92
|
}
|