@pioneer-platform/pioneer-sdk 8.12.2 → 8.12.4

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/index.cjs CHANGED
@@ -1079,7 +1079,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
1079
1079
  case "gas": {
1080
1080
  let gasLimit;
1081
1081
  if (isThorchainSwap) {
1082
- gasLimit = BigInt(120000);
1082
+ gasLimit = BigInt(1e5);
1083
1083
  console.log(tag, "Using higher gas limit for THORChain swap:", gasLimit.toString());
1084
1084
  } else {
1085
1085
  gasLimit = chainId === 1 ? BigInt(21000) : BigInt(25000);
@@ -1099,10 +1099,30 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
1099
1099
  console.log(tag, "isMax calculation - balance:", balance.toString(), "gasFee:", gasFee.toString(), "buffer:", buffer.toString(), "amountWei:", amountWei.toString());
1100
1100
  } else {
1101
1101
  amountWei = BigInt(Math.round(amount * 1000000000000000000));
1102
- if (amountWei + gasFee > balance) {
1103
- throw new Error("Insufficient funds for the transaction amount and gas fees");
1102
+ const totalNeeded = amountWei + gasFee;
1103
+ if (totalNeeded > balance) {
1104
+ const availableForSwap = balance > gasFee ? balance - gasFee : 0n;
1105
+ const balanceEth2 = (Number(balance) / 1000000000000000000).toFixed(6);
1106
+ const amountEth = (Number(amountWei) / 1000000000000000000).toFixed(6);
1107
+ const gasFeeEth = (Number(gasFee) / 1000000000000000000).toFixed(6);
1108
+ const availableEth = (Number(availableForSwap) / 1000000000000000000).toFixed(6);
1109
+ const swapType = isThorchainSwap ? "THORChain swap" : "transfer";
1110
+ throw new Error(`Insufficient funds for the transaction amount and gas fees.
1111
+ ` + `Balance: ${balanceEth2} ETH
1112
+ ` + `Attempting to ${swapType}: ${amountEth} ETH
1113
+ ` + `Estimated gas fee: ${gasFeeEth} ETH (${gasLimit.toString()} gas limit)
1114
+ ` + `Total needed: ${(Number(totalNeeded) / 1000000000000000000).toFixed(6)} ETH
1115
+ ` + `Maximum swappable: ${availableEth} ETH (after gas fees)`);
1104
1116
  }
1105
1117
  }
1118
+ console.log(tag, "Transaction calculation:", {
1119
+ balance: balance.toString() + " wei (" + (Number(balance) / 1000000000000000000).toFixed(6) + " ETH)",
1120
+ amountWei: amountWei.toString() + " wei (" + (Number(amountWei) / 1000000000000000000).toFixed(6) + " ETH)",
1121
+ gasFee: gasFee.toString() + " wei (" + (Number(gasFee) / 1000000000000000000).toFixed(6) + " ETH)",
1122
+ gasLimit: gasLimit.toString(),
1123
+ gasPrice: gasPrice.toString() + " wei (" + (Number(gasPrice) / 1e9).toFixed(2) + " gwei)",
1124
+ isThorchainSwap
1125
+ });
1106
1126
  let txData = "0x";
1107
1127
  if (isThorchainSwap) {
1108
1128
  console.log(tag, "Detected THORChain swap, encoding deposit data for memo:", memo);
@@ -1324,7 +1344,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
1324
1344
  expiry: expiryTime,
1325
1345
  dataLength: data.length
1326
1346
  });
1327
- gasLimit = BigInt(300000);
1347
+ gasLimit = BigInt(200000);
1328
1348
  } else {
1329
1349
  data = encodeTransferData(to, amountWei);
1330
1350
  finalTo = contractAddress;
@@ -5121,5 +5141,29 @@ class SDK {
5121
5141
  }
5122
5142
  };
5123
5143
  }
5144
+ CheckERC20Allowance = async (params) => {
5145
+ const tag = TAG9 + " | CheckERC20Allowance | ";
5146
+ try {
5147
+ console.log(tag, "Checking ERC20 allowance:", params);
5148
+ const result = await this.pioneer.GetTokenAllowance(params);
5149
+ console.log(tag, "Allowance result:", result);
5150
+ return result.data;
5151
+ } catch (e) {
5152
+ console.error(tag, "Error checking ERC20 allowance:", e);
5153
+ throw e;
5154
+ }
5155
+ };
5156
+ BuildERC20ApprovalTx = async (params) => {
5157
+ const tag = TAG9 + " | BuildERC20ApprovalTx | ";
5158
+ try {
5159
+ console.log(tag, "Building ERC20 approval transaction:", params);
5160
+ const result = await this.pioneer.BuildApprovalTransaction(params);
5161
+ console.log(tag, "Approval tx built:", result);
5162
+ return result.data;
5163
+ } catch (e) {
5164
+ console.error(tag, "Error building approval transaction:", e);
5165
+ throw e;
5166
+ }
5167
+ };
5124
5168
  }
5125
5169
  var src_default = SDK;
package/dist/index.es.js CHANGED
@@ -1255,7 +1255,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
1255
1255
  case "gas": {
1256
1256
  let gasLimit;
1257
1257
  if (isThorchainSwap) {
1258
- gasLimit = BigInt(120000);
1258
+ gasLimit = BigInt(1e5);
1259
1259
  console.log(tag, "Using higher gas limit for THORChain swap:", gasLimit.toString());
1260
1260
  } else {
1261
1261
  gasLimit = chainId === 1 ? BigInt(21000) : BigInt(25000);
@@ -1275,10 +1275,30 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
1275
1275
  console.log(tag, "isMax calculation - balance:", balance.toString(), "gasFee:", gasFee.toString(), "buffer:", buffer.toString(), "amountWei:", amountWei.toString());
1276
1276
  } else {
1277
1277
  amountWei = BigInt(Math.round(amount * 1000000000000000000));
1278
- if (amountWei + gasFee > balance) {
1279
- throw new Error("Insufficient funds for the transaction amount and gas fees");
1278
+ const totalNeeded = amountWei + gasFee;
1279
+ if (totalNeeded > balance) {
1280
+ const availableForSwap = balance > gasFee ? balance - gasFee : 0n;
1281
+ const balanceEth2 = (Number(balance) / 1000000000000000000).toFixed(6);
1282
+ const amountEth = (Number(amountWei) / 1000000000000000000).toFixed(6);
1283
+ const gasFeeEth = (Number(gasFee) / 1000000000000000000).toFixed(6);
1284
+ const availableEth = (Number(availableForSwap) / 1000000000000000000).toFixed(6);
1285
+ const swapType = isThorchainSwap ? "THORChain swap" : "transfer";
1286
+ throw new Error(`Insufficient funds for the transaction amount and gas fees.
1287
+ ` + `Balance: ${balanceEth2} ETH
1288
+ ` + `Attempting to ${swapType}: ${amountEth} ETH
1289
+ ` + `Estimated gas fee: ${gasFeeEth} ETH (${gasLimit.toString()} gas limit)
1290
+ ` + `Total needed: ${(Number(totalNeeded) / 1000000000000000000).toFixed(6)} ETH
1291
+ ` + `Maximum swappable: ${availableEth} ETH (after gas fees)`);
1280
1292
  }
1281
1293
  }
1294
+ console.log(tag, "Transaction calculation:", {
1295
+ balance: balance.toString() + " wei (" + (Number(balance) / 1000000000000000000).toFixed(6) + " ETH)",
1296
+ amountWei: amountWei.toString() + " wei (" + (Number(amountWei) / 1000000000000000000).toFixed(6) + " ETH)",
1297
+ gasFee: gasFee.toString() + " wei (" + (Number(gasFee) / 1000000000000000000).toFixed(6) + " ETH)",
1298
+ gasLimit: gasLimit.toString(),
1299
+ gasPrice: gasPrice.toString() + " wei (" + (Number(gasPrice) / 1e9).toFixed(2) + " gwei)",
1300
+ isThorchainSwap
1301
+ });
1282
1302
  let txData = "0x";
1283
1303
  if (isThorchainSwap) {
1284
1304
  console.log(tag, "Detected THORChain swap, encoding deposit data for memo:", memo);
@@ -1500,7 +1520,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
1500
1520
  expiry: expiryTime,
1501
1521
  dataLength: data.length
1502
1522
  });
1503
- gasLimit = BigInt(300000);
1523
+ gasLimit = BigInt(200000);
1504
1524
  } else {
1505
1525
  data = encodeTransferData(to, amountWei);
1506
1526
  finalTo = contractAddress;
@@ -5297,6 +5317,30 @@ class SDK {
5297
5317
  }
5298
5318
  };
5299
5319
  }
5320
+ CheckERC20Allowance = async (params) => {
5321
+ const tag = TAG9 + " | CheckERC20Allowance | ";
5322
+ try {
5323
+ console.log(tag, "Checking ERC20 allowance:", params);
5324
+ const result = await this.pioneer.GetTokenAllowance(params);
5325
+ console.log(tag, "Allowance result:", result);
5326
+ return result.data;
5327
+ } catch (e) {
5328
+ console.error(tag, "Error checking ERC20 allowance:", e);
5329
+ throw e;
5330
+ }
5331
+ };
5332
+ BuildERC20ApprovalTx = async (params) => {
5333
+ const tag = TAG9 + " | BuildERC20ApprovalTx | ";
5334
+ try {
5335
+ console.log(tag, "Building ERC20 approval transaction:", params);
5336
+ const result = await this.pioneer.BuildApprovalTransaction(params);
5337
+ console.log(tag, "Approval tx built:", result);
5338
+ return result.data;
5339
+ } catch (e) {
5340
+ console.error(tag, "Error building approval transaction:", e);
5341
+ throw e;
5342
+ }
5343
+ };
5300
5344
  }
5301
5345
  var src_default = SDK;
5302
5346
  export {
package/dist/index.js CHANGED
@@ -1255,7 +1255,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
1255
1255
  case "gas": {
1256
1256
  let gasLimit;
1257
1257
  if (isThorchainSwap) {
1258
- gasLimit = BigInt(120000);
1258
+ gasLimit = BigInt(1e5);
1259
1259
  console.log(tag, "Using higher gas limit for THORChain swap:", gasLimit.toString());
1260
1260
  } else {
1261
1261
  gasLimit = chainId === 1 ? BigInt(21000) : BigInt(25000);
@@ -1275,10 +1275,30 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
1275
1275
  console.log(tag, "isMax calculation - balance:", balance.toString(), "gasFee:", gasFee.toString(), "buffer:", buffer.toString(), "amountWei:", amountWei.toString());
1276
1276
  } else {
1277
1277
  amountWei = BigInt(Math.round(amount * 1000000000000000000));
1278
- if (amountWei + gasFee > balance) {
1279
- throw new Error("Insufficient funds for the transaction amount and gas fees");
1278
+ const totalNeeded = amountWei + gasFee;
1279
+ if (totalNeeded > balance) {
1280
+ const availableForSwap = balance > gasFee ? balance - gasFee : 0n;
1281
+ const balanceEth2 = (Number(balance) / 1000000000000000000).toFixed(6);
1282
+ const amountEth = (Number(amountWei) / 1000000000000000000).toFixed(6);
1283
+ const gasFeeEth = (Number(gasFee) / 1000000000000000000).toFixed(6);
1284
+ const availableEth = (Number(availableForSwap) / 1000000000000000000).toFixed(6);
1285
+ const swapType = isThorchainSwap ? "THORChain swap" : "transfer";
1286
+ throw new Error(`Insufficient funds for the transaction amount and gas fees.
1287
+ ` + `Balance: ${balanceEth2} ETH
1288
+ ` + `Attempting to ${swapType}: ${amountEth} ETH
1289
+ ` + `Estimated gas fee: ${gasFeeEth} ETH (${gasLimit.toString()} gas limit)
1290
+ ` + `Total needed: ${(Number(totalNeeded) / 1000000000000000000).toFixed(6)} ETH
1291
+ ` + `Maximum swappable: ${availableEth} ETH (after gas fees)`);
1280
1292
  }
1281
1293
  }
1294
+ console.log(tag, "Transaction calculation:", {
1295
+ balance: balance.toString() + " wei (" + (Number(balance) / 1000000000000000000).toFixed(6) + " ETH)",
1296
+ amountWei: amountWei.toString() + " wei (" + (Number(amountWei) / 1000000000000000000).toFixed(6) + " ETH)",
1297
+ gasFee: gasFee.toString() + " wei (" + (Number(gasFee) / 1000000000000000000).toFixed(6) + " ETH)",
1298
+ gasLimit: gasLimit.toString(),
1299
+ gasPrice: gasPrice.toString() + " wei (" + (Number(gasPrice) / 1e9).toFixed(2) + " gwei)",
1300
+ isThorchainSwap
1301
+ });
1282
1302
  let txData = "0x";
1283
1303
  if (isThorchainSwap) {
1284
1304
  console.log(tag, "Detected THORChain swap, encoding deposit data for memo:", memo);
@@ -1500,7 +1520,7 @@ async function createUnsignedEvmTx(caip, to, amount, memo, pubkeys, pioneer, pub
1500
1520
  expiry: expiryTime,
1501
1521
  dataLength: data.length
1502
1522
  });
1503
- gasLimit = BigInt(300000);
1523
+ gasLimit = BigInt(200000);
1504
1524
  } else {
1505
1525
  data = encodeTransferData(to, amountWei);
1506
1526
  finalTo = contractAddress;
@@ -5297,6 +5317,30 @@ class SDK {
5297
5317
  }
5298
5318
  };
5299
5319
  }
5320
+ CheckERC20Allowance = async (params) => {
5321
+ const tag = TAG9 + " | CheckERC20Allowance | ";
5322
+ try {
5323
+ console.log(tag, "Checking ERC20 allowance:", params);
5324
+ const result = await this.pioneer.GetTokenAllowance(params);
5325
+ console.log(tag, "Allowance result:", result);
5326
+ return result.data;
5327
+ } catch (e) {
5328
+ console.error(tag, "Error checking ERC20 allowance:", e);
5329
+ throw e;
5330
+ }
5331
+ };
5332
+ BuildERC20ApprovalTx = async (params) => {
5333
+ const tag = TAG9 + " | BuildERC20ApprovalTx | ";
5334
+ try {
5335
+ console.log(tag, "Building ERC20 approval transaction:", params);
5336
+ const result = await this.pioneer.BuildApprovalTransaction(params);
5337
+ console.log(tag, "Approval tx built:", result);
5338
+ return result.data;
5339
+ } catch (e) {
5340
+ console.error(tag, "Error building approval transaction:", e);
5341
+ throw e;
5342
+ }
5343
+ };
5300
5344
  }
5301
5345
  var src_default = SDK;
5302
5346
  export {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "highlander",
3
3
  "name": "@pioneer-platform/pioneer-sdk",
4
- "version": "8.12.2",
4
+ "version": "8.12.4",
5
5
  "dependencies": {
6
6
  "@keepkey/keepkey-sdk": "^0.2.62",
7
7
  "@pioneer-platform/loggerdog": "^8.11.0",
package/src/index.ts CHANGED
@@ -2624,6 +2624,55 @@ export class SDK {
2624
2624
  }
2625
2625
  };
2626
2626
  }
2627
+
2628
+ /**
2629
+ * Check ERC20 token allowance for a spender
2630
+ * Used to verify if approval is needed before THORChain swaps
2631
+ */
2632
+ public CheckERC20Allowance = async (params: {
2633
+ networkId: string;
2634
+ contractAddress: string;
2635
+ ownerAddress: string;
2636
+ spenderAddress: string;
2637
+ }) => {
2638
+ const tag = TAG + ' | CheckERC20Allowance | ';
2639
+ try {
2640
+ console.log(tag, 'Checking ERC20 allowance:', params);
2641
+
2642
+ const result = await this.pioneer.GetTokenAllowance(params);
2643
+
2644
+ console.log(tag, 'Allowance result:', result);
2645
+ return result.data;
2646
+ } catch (e) {
2647
+ console.error(tag, 'Error checking ERC20 allowance:', e);
2648
+ throw e;
2649
+ }
2650
+ };
2651
+
2652
+ /**
2653
+ * Build an ERC20 approval transaction
2654
+ * Required before THORChain router can transfer tokens
2655
+ */
2656
+ public BuildERC20ApprovalTx = async (params: {
2657
+ networkId: string;
2658
+ contractAddress: string;
2659
+ spenderAddress: string;
2660
+ ownerAddress: string;
2661
+ amount: string;
2662
+ }) => {
2663
+ const tag = TAG + ' | BuildERC20ApprovalTx | ';
2664
+ try {
2665
+ console.log(tag, 'Building ERC20 approval transaction:', params);
2666
+
2667
+ const result = await this.pioneer.BuildApprovalTransaction(params);
2668
+
2669
+ console.log(tag, 'Approval tx built:', result);
2670
+ return result.data;
2671
+ } catch (e) {
2672
+ console.error(tag, 'Error building approval transaction:', e);
2673
+ throw e;
2674
+ }
2675
+ };
2627
2676
  }
2628
2677
 
2629
2678
  // Export fee-related types for consumers
@@ -104,6 +104,14 @@ async function fetchTokenPriceInUsd(pioneer: any, caip: string) {
104
104
  }
105
105
 
106
106
  // Create an unsigned EVM transaction
107
+ /**
108
+ * Create an unsigned EVM transaction
109
+ *
110
+ * @param feeLevel - Fee speed preference:
111
+ * - 1-2: Slow (80% of base gas price)
112
+ * - 3-7: Average (100% of base gas price) [DEFAULT: 5]
113
+ * - 8-10: Fast (150% of base gas price)
114
+ */
107
115
  export async function createUnsignedEvmTx(
108
116
  caip,
109
117
  to,
@@ -113,7 +121,7 @@ export async function createUnsignedEvmTx(
113
121
  pioneer,
114
122
  pubkeyContext,
115
123
  isMax,
116
- feeLevel = 5, // Added feeLevel parameter with default of 5 (average)
124
+ feeLevel = 5, // Default: average fee level (100% of base)
117
125
  ) {
118
126
  const tag = TAG + ' | createUnsignedEvmTx | ';
119
127
 
@@ -292,9 +300,9 @@ export async function createUnsignedEvmTx(
292
300
  // Use the top-level isThorchainSwap check
293
301
  let gasLimit;
294
302
  if (isThorchainSwap) {
295
- // THORChain depositWithExpiry requires more gas (90-120k typical)
296
- // Use 120000 to be safe for all network conditions
297
- gasLimit = BigInt(120000);
303
+ // THORChain depositWithExpiry requires more gas (90-100k typical)
304
+ // Use 100000 with 10% buffer for safety (reduced from 120000 to minimize gas fees)
305
+ gasLimit = BigInt(100000);
298
306
  console.log(tag, 'Using higher gas limit for THORChain swap:', gasLimit.toString());
299
307
  } else {
300
308
  // Standard gas limit for ETH transfer
@@ -323,12 +331,35 @@ export async function createUnsignedEvmTx(
323
331
  console.log(tag, 'isMax calculation - balance:', balance.toString(), 'gasFee:', gasFee.toString(), 'buffer:', buffer.toString(), 'amountWei:', amountWei.toString());
324
332
  } else {
325
333
  amountWei = BigInt(Math.round(amount * 1e18));
326
- if (amountWei + gasFee > balance) {
327
- throw new Error('Insufficient funds for the transaction amount and gas fees');
334
+ const totalNeeded = amountWei + gasFee;
335
+
336
+ if (totalNeeded > balance) {
337
+ const availableForSwap = balance > gasFee ? balance - gasFee : 0n;
338
+ const balanceEth = (Number(balance) / 1e18).toFixed(6);
339
+ const amountEth = (Number(amountWei) / 1e18).toFixed(6);
340
+ const gasFeeEth = (Number(gasFee) / 1e18).toFixed(6);
341
+ const availableEth = (Number(availableForSwap) / 1e18).toFixed(6);
342
+ const swapType = isThorchainSwap ? 'THORChain swap' : 'transfer';
343
+
344
+ throw new Error(
345
+ `Insufficient funds for the transaction amount and gas fees.\n` +
346
+ `Balance: ${balanceEth} ETH\n` +
347
+ `Attempting to ${swapType}: ${amountEth} ETH\n` +
348
+ `Estimated gas fee: ${gasFeeEth} ETH (${gasLimit.toString()} gas limit)\n` +
349
+ `Total needed: ${(Number(totalNeeded) / 1e18).toFixed(6)} ETH\n` +
350
+ `Maximum swappable: ${availableEth} ETH (after gas fees)`
351
+ );
328
352
  }
329
353
  }
330
354
 
331
- //console.log(tag, 'amountWei:', amountWei.toString());
355
+ console.log(tag, 'Transaction calculation:', {
356
+ balance: balance.toString() + ' wei (' + (Number(balance) / 1e18).toFixed(6) + ' ETH)',
357
+ amountWei: amountWei.toString() + ' wei (' + (Number(amountWei) / 1e18).toFixed(6) + ' ETH)',
358
+ gasFee: gasFee.toString() + ' wei (' + (Number(gasFee) / 1e18).toFixed(6) + ' ETH)',
359
+ gasLimit: gasLimit.toString(),
360
+ gasPrice: gasPrice.toString() + ' wei (' + (Number(gasPrice) / 1e9).toFixed(2) + ' gwei)',
361
+ isThorchainSwap: isThorchainSwap,
362
+ });
332
363
 
333
364
  // Use the top-level isThorchainSwap check (defined before switch statement)
334
365
  let txData = '0x';
@@ -694,7 +725,8 @@ export async function createUnsignedEvmTx(
694
725
  });
695
726
 
696
727
  // Increase gas limit for router call (more complex than simple transfer)
697
- gasLimit = BigInt(300000); // Router calls require more gas than simple transfers
728
+ // ERC20 depositWithExpiry typically uses 150-200k gas
729
+ gasLimit = BigInt(200000); // Reduced from 300k - router calls with 33% buffer
698
730
  } else {
699
731
  // Regular ERC20 transfer (non-THORChain)
700
732
  data = encodeTransferData(to, amountWei);