sdk-triggerx 0.1.36 → 0.1.38
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/jobs.js +8 -4
- package/dist/client.js +2 -1
- package/dist/config.js +5 -5
- package/package.json +1 -1
- package/dist/api/checkTgBalance.d.ts +0 -20
- package/dist/api/checkTgBalance.js +0 -85
- package/dist/api/topupTg.d.ts +0 -9
- package/dist/api/topupTg.js +0 -99
- package/dist/api/withdrawTg.d.ts +0 -15
- package/dist/api/withdrawTg.js +0 -88
package/dist/api/jobs.js
CHANGED
|
@@ -251,8 +251,9 @@ async function createJob(client, params) {
|
|
|
251
251
|
return (0, errors_1.createErrorResponse)(new errors_1.ContractError('Failed to configure Safe wallet', { originalError: err, safeAddress: jobInput.safeAddress }), 'Contract error');
|
|
252
252
|
}
|
|
253
253
|
// Auto-set module target; user does not need to pass targetContractAddress in safe mode
|
|
254
|
+
// New ABI: execJobFromHub(safeAddress, actionTarget, actionValue, actionData, operation, jobOwner)
|
|
254
255
|
jobInput.targetContractAddress = safeModule;
|
|
255
|
-
jobInput.targetFunction = 'execJobFromHub
|
|
256
|
+
jobInput.targetFunction = 'execJobFromHub';
|
|
256
257
|
jobInput.abi = JSON.stringify([
|
|
257
258
|
{
|
|
258
259
|
"type": "function", "name": "execJobFromHub", "stateMutability": "nonpayable", "inputs": [
|
|
@@ -260,7 +261,8 @@ async function createJob(client, params) {
|
|
|
260
261
|
{ "name": "actionTarget", "type": "address" },
|
|
261
262
|
{ "name": "actionValue", "type": "uint256" },
|
|
262
263
|
{ "name": "actionData", "type": "bytes" },
|
|
263
|
-
{ "name": "operation", "type": "uint8" }
|
|
264
|
+
{ "name": "operation", "type": "uint8" },
|
|
265
|
+
{ "name": "jobOwner", "type": "address" }
|
|
264
266
|
], "outputs": [{ "type": "bool", "name": "success" }]
|
|
265
267
|
}
|
|
266
268
|
]);
|
|
@@ -283,7 +285,8 @@ async function createJob(client, params) {
|
|
|
283
285
|
tx.to,
|
|
284
286
|
tx.value,
|
|
285
287
|
tx.data,
|
|
286
|
-
0 // CALL
|
|
288
|
+
0, // CALL
|
|
289
|
+
userAddress
|
|
287
290
|
];
|
|
288
291
|
}
|
|
289
292
|
else {
|
|
@@ -297,7 +300,8 @@ async function createJob(client, params) {
|
|
|
297
300
|
multisendCallOnly,
|
|
298
301
|
'0',
|
|
299
302
|
encodedMultisendData,
|
|
300
|
-
1 // DELEGATECALL
|
|
303
|
+
1, // DELEGATECALL
|
|
304
|
+
userAddress
|
|
301
305
|
];
|
|
302
306
|
}
|
|
303
307
|
}
|
package/dist/client.js
CHANGED
|
@@ -10,7 +10,8 @@ class TriggerXClient {
|
|
|
10
10
|
this.apiKey = apiKey; // Initialize the apiKey
|
|
11
11
|
// const baseConfig = getConfig();
|
|
12
12
|
this.client = axios_1.default.create({
|
|
13
|
-
baseURL: 'https://data.triggerx.network',
|
|
13
|
+
baseURL: 'https://data.triggerx.network',
|
|
14
|
+
// baseURL: 'http://192.168.1.50:9002', //'https://data.triggerx.network',
|
|
14
15
|
headers: { 'Authorization': `Bearer ${this.apiKey}` }, // Set the API key here
|
|
15
16
|
timeout: 120000, // 120 second timeout
|
|
16
17
|
...config,
|
package/dist/config.js
CHANGED
|
@@ -16,7 +16,7 @@ exports.CONTRACT_ADDRESSES_BY_CHAIN = {
|
|
|
16
16
|
gasRegistry: '0x248E9f1B99F1AC8068254233D1F271ed0e0903D6',
|
|
17
17
|
jobRegistry: '0x476ACc7949a95e31144cC84b8F6BC7abF0967E4b',
|
|
18
18
|
safeFactory: '0x04359eDC46Cd6C6BD7F6359512984222BE10F8Be',
|
|
19
|
-
safeModule: '
|
|
19
|
+
safeModule: '0x71BB931AfBE9a10a1030bAc1dc24fe45a2960b7a',
|
|
20
20
|
multisendCallOnly: '0x9641d764fc13c8B624c04430C7356C1C7C8102e2',
|
|
21
21
|
rpcUrl: 'https://sepolia.optimism.io',
|
|
22
22
|
},
|
|
@@ -25,7 +25,7 @@ exports.CONTRACT_ADDRESSES_BY_CHAIN = {
|
|
|
25
25
|
gasRegistry: '0x248E9f1B99F1AC8068254233D1F271ed0e0903D6',
|
|
26
26
|
jobRegistry: '0x476ACc7949a95e31144cC84b8F6BC7abF0967E4b',
|
|
27
27
|
safeFactory: '0xdf76E2A796a206D877086c717979054544B1D9Bc',
|
|
28
|
-
safeModule: '
|
|
28
|
+
safeModule: '0x71BB931AfBE9a10a1030bAc1dc24fe45a2960b7a',
|
|
29
29
|
multisendCallOnly: '0x9641d764fc13c8B624c04430C7356C1C7C8102e2',
|
|
30
30
|
rpcUrl: 'https://rpc.sepolia.org',
|
|
31
31
|
},
|
|
@@ -34,7 +34,7 @@ exports.CONTRACT_ADDRESSES_BY_CHAIN = {
|
|
|
34
34
|
gasRegistry: '0x248E9f1B99F1AC8068254233D1F271ed0e0903D6',
|
|
35
35
|
jobRegistry: '0x476ACc7949a95e31144cC84b8F6BC7abF0967E4b',
|
|
36
36
|
safeFactory: '0x04359eDC46Cd6C6BD7F6359512984222BE10F8Be',
|
|
37
|
-
safeModule: '
|
|
37
|
+
safeModule: '0x71BB931AfBE9a10a1030bAc1dc24fe45a2960b7a',
|
|
38
38
|
multisendCallOnly: '0x9641d764fc13c8B624c04430C7356C1C7C8102e2',
|
|
39
39
|
rpcUrl: 'https://sepolia-rollup.arbitrum.io/rpc',
|
|
40
40
|
// safeSingleton can be provided per deployment (Safe or SafeL2)
|
|
@@ -44,7 +44,7 @@ exports.CONTRACT_ADDRESSES_BY_CHAIN = {
|
|
|
44
44
|
gasRegistry: '0x248E9f1B99F1AC8068254233D1F271ed0e0903D6',
|
|
45
45
|
jobRegistry: '0x476ACc7949a95e31144cC84b8F6BC7abF0967E4b',
|
|
46
46
|
safeFactory: '0x04359eDC46Cd6C6BD7F6359512984222BE10F8Be',
|
|
47
|
-
safeModule: '
|
|
47
|
+
safeModule: '0x71BB931AfBE9a10a1030bAc1dc24fe45a2960b7a',
|
|
48
48
|
multisendCallOnly: '0x9641d764fc13c8B624c04430C7356C1C7C8102e2',
|
|
49
49
|
rpcUrl: 'https://sepolia.base.org',
|
|
50
50
|
},
|
|
@@ -54,7 +54,7 @@ exports.CONTRACT_ADDRESSES_BY_CHAIN = {
|
|
|
54
54
|
gasRegistry: '0xe2AC670F7D66c69D547A44D08F9bA1Fc0Fc0f991',
|
|
55
55
|
jobRegistry: '0xAf1189aFd1F1880F09AeC3Cbc32cf415c735C710',
|
|
56
56
|
safeFactory: '0x93528Df5984231059Cf5c5f2CE9b85c59250D096',
|
|
57
|
-
safeModule: '
|
|
57
|
+
safeModule: '0x91d317d9D4d9F1402ababD4456c28b5B1eC841E4',
|
|
58
58
|
multisendCallOnly: '0x9641d764fc13c8B624c04430C7356C1C7C8102e2',
|
|
59
59
|
rpcUrl: 'https://arb1.arbitrum.io/rpc',
|
|
60
60
|
},
|
package/package.json
CHANGED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { ethers } from 'ethers';
|
|
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<{
|
|
11
|
-
success: boolean;
|
|
12
|
-
data?: {
|
|
13
|
-
tgBalanceWei: bigint;
|
|
14
|
-
tgBalance: string;
|
|
15
|
-
};
|
|
16
|
-
error?: string;
|
|
17
|
-
errorCode?: string;
|
|
18
|
-
errorType?: string;
|
|
19
|
-
details?: any;
|
|
20
|
-
}>;
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.checkTgBalance = void 0;
|
|
7
|
-
const ethers_1 = require("ethers");
|
|
8
|
-
const GasRegistry_json_1 = __importDefault(require("../contracts/abi/GasRegistry.json"));
|
|
9
|
-
const contractUtils_1 = require("../contracts/contractUtils");
|
|
10
|
-
const errors_1 = require("../utils/errors");
|
|
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) => {
|
|
20
|
-
// Validate inputs
|
|
21
|
-
if (!signer) {
|
|
22
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ValidationError('signer', 'Signer is required'), 'Validation error');
|
|
23
|
-
}
|
|
24
|
-
try {
|
|
25
|
-
// Resolve chain ID (use provided chainId or resolve from signer)
|
|
26
|
-
let resolvedChainId;
|
|
27
|
-
try {
|
|
28
|
-
resolvedChainId = await (0, contractUtils_1.resolveChainId)(signer, chainId);
|
|
29
|
-
}
|
|
30
|
-
catch (configError) {
|
|
31
|
-
if (configError instanceof errors_1.ConfigurationError) {
|
|
32
|
-
return (0, errors_1.createErrorResponse)(configError, 'Configuration error');
|
|
33
|
-
}
|
|
34
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ConfigurationError('Failed to resolve chain ID', { originalError: configError }), 'Configuration error');
|
|
35
|
-
}
|
|
36
|
-
// Get contract address
|
|
37
|
-
let gasRegistryContractAddress;
|
|
38
|
-
try {
|
|
39
|
-
gasRegistryContractAddress = (0, contractUtils_1.getContractAddress)(resolvedChainId, 'gasRegistry');
|
|
40
|
-
}
|
|
41
|
-
catch (configError) {
|
|
42
|
-
if (configError instanceof errors_1.ConfigurationError) {
|
|
43
|
-
return (0, errors_1.createErrorResponse)(configError, 'Configuration error');
|
|
44
|
-
}
|
|
45
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ConfigurationError('Failed to get contract address', { originalError: configError }), 'Configuration error');
|
|
46
|
-
}
|
|
47
|
-
// Create contract instance with SDK RPC provider (read-only)
|
|
48
|
-
// This ensures we can read balance even if user's RPC fails
|
|
49
|
-
const contract = await (0, contractUtils_1.createContractWithSdkRpc)(gasRegistryContractAddress, GasRegistry_json_1.default, resolvedChainId, signer);
|
|
50
|
-
// Get address from signer (this doesn't require provider)
|
|
51
|
-
const address = await signer.getAddress();
|
|
52
|
-
// Read balance using our RPC provider
|
|
53
|
-
const balance = await contract.balances(address);
|
|
54
|
-
// balance is likely an array or object with ethSpent and TGbalance, both in wei
|
|
55
|
-
// We'll convert TGbalance from wei to ETH
|
|
56
|
-
// If balance is an array: [ethSpent, TGbalance]
|
|
57
|
-
// If balance is an object: { ethSpent, TGbalance }
|
|
58
|
-
let tgBalanceWei;
|
|
59
|
-
if (Array.isArray(balance)) {
|
|
60
|
-
tgBalanceWei = balance[1];
|
|
61
|
-
}
|
|
62
|
-
else if (balance && balance.TGbalance !== undefined) {
|
|
63
|
-
tgBalanceWei = balance.TGbalance;
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ContractError('Unexpected balance format from contract', { balance }), 'Contract error');
|
|
67
|
-
}
|
|
68
|
-
const tgBalance = ethers_1.ethers.formatEther(tgBalanceWei);
|
|
69
|
-
console.log('tgBalanceEth', tgBalance);
|
|
70
|
-
return { success: true, data: { tgBalanceWei, tgBalance } };
|
|
71
|
-
}
|
|
72
|
-
catch (error) {
|
|
73
|
-
console.error('Error checking TG balance:', error);
|
|
74
|
-
if (error instanceof Error) {
|
|
75
|
-
if (error.message.includes('network') || error.message.includes('timeout')) {
|
|
76
|
-
return (0, errors_1.createErrorResponse)(new errors_1.NetworkError('Network error during balance check', { originalError: error }), 'Network error');
|
|
77
|
-
}
|
|
78
|
-
else if (error.message.includes('contract') || error.message.includes('transaction')) {
|
|
79
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ContractError('Contract error during balance check', { originalError: error }), 'Contract error');
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return (0, errors_1.createErrorResponse)(error, 'Failed to check TG balance');
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
exports.checkTgBalance = checkTgBalance;
|
package/dist/api/topupTg.d.ts
DELETED
package/dist/api/topupTg.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.topupTg = void 0;
|
|
7
|
-
const GasRegistry_json_1 = __importDefault(require("../contracts/abi/GasRegistry.json"));
|
|
8
|
-
const contractUtils_1 = require("../contracts/contractUtils");
|
|
9
|
-
const errors_1 = require("../utils/errors");
|
|
10
|
-
const topupTg = async (tgAmount, signer) => {
|
|
11
|
-
console.log('topping up TG balance', tgAmount);
|
|
12
|
-
// Validate inputs
|
|
13
|
-
if (!tgAmount || tgAmount <= 0) {
|
|
14
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ValidationError('tgAmount', 'TG amount must be a positive number'), 'Validation error');
|
|
15
|
-
}
|
|
16
|
-
if (!signer) {
|
|
17
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ValidationError('signer', 'Signer is required'), 'Validation error');
|
|
18
|
-
}
|
|
19
|
-
try {
|
|
20
|
-
// Get contract address and create contract instances with SDK RPC provider
|
|
21
|
-
// This ensures we can interact with the contract even if user's RPC fails
|
|
22
|
-
let gasRegistryContractAddress;
|
|
23
|
-
let contract;
|
|
24
|
-
let contractWithSigner;
|
|
25
|
-
let rpcProvider;
|
|
26
|
-
let resolvedChainId;
|
|
27
|
-
let signerAddress;
|
|
28
|
-
try {
|
|
29
|
-
// Get signer address (this doesn't require the signer's provider to work)
|
|
30
|
-
signerAddress = await signer.getAddress();
|
|
31
|
-
// Resolve chain ID from signer
|
|
32
|
-
resolvedChainId = await (0, contractUtils_1.resolveChainId)(signer);
|
|
33
|
-
// Get contract address
|
|
34
|
-
gasRegistryContractAddress = (0, contractUtils_1.getContractAddress)(resolvedChainId, 'gasRegistry');
|
|
35
|
-
// Create contract instances with SDK RPC provider
|
|
36
|
-
const contractInstances = await (0, contractUtils_1.createContractWithSdkRpcAndSigner)(gasRegistryContractAddress, GasRegistry_json_1.default, signer, resolvedChainId);
|
|
37
|
-
contract = contractInstances.contract;
|
|
38
|
-
contractWithSigner = contractInstances.contractWithSigner;
|
|
39
|
-
rpcProvider = contractInstances.rpcProvider;
|
|
40
|
-
}
|
|
41
|
-
catch (configError) {
|
|
42
|
-
if (configError instanceof errors_1.ConfigurationError) {
|
|
43
|
-
return (0, errors_1.createErrorResponse)(configError, 'Configuration error');
|
|
44
|
-
}
|
|
45
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ConfigurationError('Failed to initialize contract', { originalError: configError }), 'Configuration error');
|
|
46
|
-
}
|
|
47
|
-
// Each TG costs 0.001 ETH, so calculate the ETH required for the given TG amount
|
|
48
|
-
console.log('tgAmount', tgAmount);
|
|
49
|
-
const amountInEthWei = tgAmount;
|
|
50
|
-
// const amountInEthWei = ethers.parseEther(amountInEth.toString());
|
|
51
|
-
console.log('amountInEthWei', amountInEthWei);
|
|
52
|
-
// Estimate gas for the transaction using SDK RPC provider
|
|
53
|
-
// This ensures gas estimation works even if user's RPC fails
|
|
54
|
-
let estimatedGas;
|
|
55
|
-
try {
|
|
56
|
-
console.log('Estimating gas using SDK RPC provider...');
|
|
57
|
-
// Use contract instance with SDK RPC provider for estimation
|
|
58
|
-
// Specify the signer's address in the estimation options
|
|
59
|
-
estimatedGas = await contract.purchaseTG.estimateGas(amountInEthWei, {
|
|
60
|
-
value: amountInEthWei,
|
|
61
|
-
from: signerAddress // Specify the sender address for accurate estimation
|
|
62
|
-
});
|
|
63
|
-
console.log('Estimated gas (using SDK RPC):', estimatedGas.toString());
|
|
64
|
-
// Add 10% buffer to ensure transaction doesn't fail
|
|
65
|
-
const gasWithBuffer = (estimatedGas * BigInt(110)) / BigInt(100);
|
|
66
|
-
console.log('Gas with 10% buffer:', gasWithBuffer.toString());
|
|
67
|
-
// Execute transaction using signer (for signing)
|
|
68
|
-
const tx = await contractWithSigner.purchaseTG(amountInEthWei, {
|
|
69
|
-
value: amountInEthWei,
|
|
70
|
-
gasLimit: gasWithBuffer
|
|
71
|
-
});
|
|
72
|
-
await (0, contractUtils_1.waitForTransactionReceiptWithRpcFallback)(tx, rpcProvider);
|
|
73
|
-
return { success: true, data: tx };
|
|
74
|
-
}
|
|
75
|
-
catch (gasEstimateError) {
|
|
76
|
-
// If gas estimation fails, try without gas limit (let provider estimate)
|
|
77
|
-
console.warn('Gas estimation failed (using SDK RPC), proceeding without gas limit:', gasEstimateError);
|
|
78
|
-
const tx = await contractWithSigner.purchaseTG(amountInEthWei, { value: amountInEthWei });
|
|
79
|
-
await (0, contractUtils_1.waitForTransactionReceiptWithRpcFallback)(tx, rpcProvider);
|
|
80
|
-
return { success: true, data: tx };
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
catch (error) {
|
|
84
|
-
console.error('Error topping up TG:', error);
|
|
85
|
-
if (error instanceof Error) {
|
|
86
|
-
if (error.message.includes('network') || error.message.includes('timeout')) {
|
|
87
|
-
return (0, errors_1.createErrorResponse)(new errors_1.NetworkError('Network error during TG top-up', { originalError: error, tgAmount }), 'Network error');
|
|
88
|
-
}
|
|
89
|
-
else if (error.message.includes('contract') || error.message.includes('transaction')) {
|
|
90
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ContractError('Contract error during TG top-up', { originalError: error, tgAmount }), 'Contract error');
|
|
91
|
-
}
|
|
92
|
-
else if (error.message.includes('insufficient funds') || error.message.includes('balance')) {
|
|
93
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ValidationError('balance', 'Insufficient funds for TG top-up', { originalError: error, tgAmount }), 'Validation error');
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return (0, errors_1.createErrorResponse)(error, 'Failed to top up TG');
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
exports.topupTg = topupTg;
|
package/dist/api/withdrawTg.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { ethers } from 'ethers';
|
|
2
|
-
/**
|
|
3
|
-
* Withdraw ETH in exchange for TG tokens.
|
|
4
|
-
* @param signer ethers.Signer instance
|
|
5
|
-
* @param amountTG The amount of TG tokens to withdraw (as a string or BigNumberish)
|
|
6
|
-
* @returns The transaction object or error response
|
|
7
|
-
*/
|
|
8
|
-
export declare const withdrawTg: (signer: ethers.Signer, amountTG: string | ethers.BigNumberish) => Promise<{
|
|
9
|
-
success: boolean;
|
|
10
|
-
data?: any;
|
|
11
|
-
error?: string;
|
|
12
|
-
errorCode?: string;
|
|
13
|
-
errorType?: string;
|
|
14
|
-
details?: any;
|
|
15
|
-
}>;
|
package/dist/api/withdrawTg.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.withdrawTg = void 0;
|
|
7
|
-
const ethers_1 = require("ethers");
|
|
8
|
-
const GasRegistry_json_1 = __importDefault(require("../contracts/abi/GasRegistry.json"));
|
|
9
|
-
const contractUtils_1 = require("../contracts/contractUtils");
|
|
10
|
-
const errors_1 = require("../utils/errors");
|
|
11
|
-
/**
|
|
12
|
-
* Withdraw ETH in exchange for TG tokens.
|
|
13
|
-
* @param signer ethers.Signer instance
|
|
14
|
-
* @param amountTG The amount of TG tokens to withdraw (as a string or BigNumberish)
|
|
15
|
-
* @returns The transaction object or error response
|
|
16
|
-
*/
|
|
17
|
-
const withdrawTg = async (signer, amountTG) => {
|
|
18
|
-
// Validate inputs
|
|
19
|
-
if (!signer) {
|
|
20
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ValidationError('signer', 'Signer is required'), 'Validation error');
|
|
21
|
-
}
|
|
22
|
-
if (!amountTG || (typeof amountTG === 'string' && amountTG.trim() === '') || Number(amountTG) <= 0) {
|
|
23
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ValidationError('amountTG', 'Amount must be a positive number'), 'Validation error');
|
|
24
|
-
}
|
|
25
|
-
try {
|
|
26
|
-
// Resolve chain ID and create contract instances with SDK RPC provider
|
|
27
|
-
let resolvedChainId;
|
|
28
|
-
let contract;
|
|
29
|
-
let contractWithSigner;
|
|
30
|
-
let rpcProvider;
|
|
31
|
-
let signerAddress;
|
|
32
|
-
try {
|
|
33
|
-
// Resolve chain ID from signer
|
|
34
|
-
signerAddress = await signer.getAddress();
|
|
35
|
-
resolvedChainId = await (0, contractUtils_1.resolveChainId)(signer);
|
|
36
|
-
// Get contract address
|
|
37
|
-
const gasRegistryContractAddress = (0, contractUtils_1.getContractAddress)(resolvedChainId, 'gasRegistry');
|
|
38
|
-
// Create contract instances with SDK RPC provider
|
|
39
|
-
const contractInstances = await (0, contractUtils_1.createContractWithSdkRpcAndSigner)(gasRegistryContractAddress, GasRegistry_json_1.default, signer, resolvedChainId);
|
|
40
|
-
contract = contractInstances.contract;
|
|
41
|
-
contractWithSigner = contractInstances.contractWithSigner;
|
|
42
|
-
rpcProvider = contractInstances.rpcProvider;
|
|
43
|
-
}
|
|
44
|
-
catch (configError) {
|
|
45
|
-
if (configError instanceof errors_1.ConfigurationError) {
|
|
46
|
-
return (0, errors_1.createErrorResponse)(configError, 'Configuration error');
|
|
47
|
-
}
|
|
48
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ConfigurationError('Failed to initialize contract', { originalError: configError }), 'Configuration error');
|
|
49
|
-
}
|
|
50
|
-
// Assumes the contract has a function: claimEthForTg(uint256 amount)
|
|
51
|
-
const amountTGWei = ethers_1.ethers.parseEther(amountTG.toString());
|
|
52
|
-
let tx;
|
|
53
|
-
try {
|
|
54
|
-
console.log('Estimating gas for claimETHForTG using SDK RPC provider...');
|
|
55
|
-
const estimatedGas = await contract.claimETHForTG.estimateGas(amountTGWei, {
|
|
56
|
-
from: signerAddress,
|
|
57
|
-
});
|
|
58
|
-
console.log('Estimated gas (claimETHForTG):', estimatedGas.toString());
|
|
59
|
-
const gasWithBuffer = (estimatedGas * BigInt(110)) / BigInt(100);
|
|
60
|
-
console.log('Gas with 10% buffer (claimETHForTG):', gasWithBuffer.toString());
|
|
61
|
-
tx = await contractWithSigner.claimETHForTG(amountTGWei, {
|
|
62
|
-
gasLimit: gasWithBuffer,
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
catch (gasEstimateError) {
|
|
66
|
-
console.warn('Gas estimation failed for claimETHForTG (using SDK RPC), proceeding without explicit gas limit:', gasEstimateError);
|
|
67
|
-
tx = await contractWithSigner.claimETHForTG(amountTGWei);
|
|
68
|
-
}
|
|
69
|
-
await (0, contractUtils_1.waitForTransactionReceiptWithRpcFallback)(tx, rpcProvider);
|
|
70
|
-
return { success: true, data: tx };
|
|
71
|
-
}
|
|
72
|
-
catch (error) {
|
|
73
|
-
console.error('Error withdrawing TG:', error);
|
|
74
|
-
if (error instanceof Error) {
|
|
75
|
-
if (error.message.includes('network') || error.message.includes('timeout')) {
|
|
76
|
-
return (0, errors_1.createErrorResponse)(new errors_1.NetworkError('Network error during TG withdrawal', { originalError: error, amountTG }), 'Network error');
|
|
77
|
-
}
|
|
78
|
-
else if (error.message.includes('contract') || error.message.includes('transaction')) {
|
|
79
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ContractError('Contract error during TG withdrawal', { originalError: error, amountTG }), 'Contract error');
|
|
80
|
-
}
|
|
81
|
-
else if (error.message.includes('insufficient') || error.message.includes('balance')) {
|
|
82
|
-
return (0, errors_1.createErrorResponse)(new errors_1.ValidationError('balance', 'Insufficient TG balance for withdrawal', { originalError: error, amountTG }), 'Validation error');
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return (0, errors_1.createErrorResponse)(error, 'Failed to withdraw TG');
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
exports.withdrawTg = withdrawTg;
|