@cryptorubic/web3 1.0.0-alpha.no-sdk.15 → 1.0.0-alpha.no-sdk.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cryptorubic/web3",
3
- "version": "1.0.0-alpha.no-sdk.15",
3
+ "version": "1.0.0-alpha.no-sdk.16",
4
4
  "dependencies": {
5
5
  "@ethersproject/bignumber": "^5.8.0",
6
6
  "@mysten/sui": "^1.24.0",
@@ -15,7 +15,15 @@ export declare class EvmAdapterClient extends AbstractAdapterClient<WalletClient
15
15
  getBlockchainName(): Promise<BlockchainName | undefined>;
16
16
  getAllowance(fromTokenAddress: string, walletAddress: string, spenderAddress: string): Promise<AllowanceInfo>;
17
17
  needApprove(token: TokenAmount, contractAddress: string, walletAddress: string, amount: string): Promise<boolean>;
18
- approve(fromAddress: string, tokenAddress: string, spenderAddress: string, amount?: string): Promise<string>;
18
+ /**
19
+ * Executes approve method in ERC-20 token contract.
20
+ * @param tokenAddress Address of the smart-contract corresponding to the token.
21
+ * @param spenderAddress Wallet or contract address to approve.
22
+ * @param amount Token amount to approve in wei.
23
+ * @param [options] Additional options.
24
+ * @returns Approval transaction hash.
25
+ */
26
+ approveTokens(tokenAddress: string, spenderAddress: string, amount?: BigNumber | 'infinity', options?: EvmTransactionOptions): Promise<string>;
19
27
  /**
20
28
  * Build encoded approve transaction config.
21
29
  * @param tokenAddress Address of the smart-contract corresponding to the token.
@@ -78,5 +86,5 @@ export declare class EvmAdapterClient extends AbstractAdapterClient<WalletClient
78
86
  private getChainAndAccount;
79
87
  read<T>(address: string, abi: Abi, method: string, methodArgs?: unknown[]): Promise<T>;
80
88
  writeContract(address: string, abi: Abi, method: string, value?: string, methodArgs?: unknown[]): Promise<string>;
81
- write(address: string, to: string, value: string, data: string): Promise<string>;
89
+ write(address: string, to: string, value: string, data: string, options?: EvmTransactionOptions): Promise<string>;
82
90
  }
@@ -14,6 +14,7 @@ const parse_evm_error_1 = require("../utils/parse-evm-error");
14
14
  const rubic_sdk_error_1 = require("../../../../errors/rubic-sdk.error");
15
15
  const evm_adapter_1 = require("../evm-adapter");
16
16
  const tx_status_1 = require("../../models/web3-public-models/tx-status");
17
+ const insufficient_funds_gas_price_value_error_1 = require("packages/web3/src/lib/errors/cross-chain/insufficient-funds-gas-price-value.error");
17
18
  class EvmAdapterClient extends abstract_adapter_client_1.AbstractAdapterClient {
18
19
  constructor(evmPublic, httpClient, logger, clientParams) {
19
20
  super(httpClient, logger, clientParams);
@@ -42,9 +43,29 @@ class EvmAdapterClient extends abstract_adapter_client_1.AbstractAdapterClient {
42
43
  const { allowanceWei } = await this.getAllowance(fromTokenAddress, walletAddress, contractAddress);
43
44
  return allowanceWei.lt(token.weiAmount);
44
45
  }
45
- async approve(fromAddress, tokenAddress, spenderAddress, amount) {
46
- const config = this.encodeApprove(tokenAddress, spenderAddress, amount);
47
- return this.write(fromAddress, config.to, config.value, config.data);
46
+ /**
47
+ * Executes approve method in ERC-20 token contract.
48
+ * @param tokenAddress Address of the smart-contract corresponding to the token.
49
+ * @param spenderAddress Wallet or contract address to approve.
50
+ * @param amount Token amount to approve in wei.
51
+ * @param [options] Additional options.
52
+ * @returns Approval transaction hash.
53
+ */
54
+ async approveTokens(tokenAddress, spenderAddress, amount = 'infinity', options = {}) {
55
+ try {
56
+ const rawValue = amount === 'infinity' ? new bignumber_js_1.default(2).pow(256).minus(1) : amount;
57
+ const gasOptions = options.gasPriceOptions ? (0, options_1.getGasOptions)(options) : {};
58
+ const txOptions = { ...options, ...gasOptions };
59
+ const receipt = await this.executeContractMethod(tokenAddress, erc20_token_abi_1.erc20TokenAbi, 'approve', [spenderAddress, BigInt(rawValue.toFixed(0))], txOptions);
60
+ return receipt.transactionHash;
61
+ }
62
+ catch (err) {
63
+ if (err?.message?.includes('gas required exceeds allowance') || err?.message?.includes('insufficient balance to pay for gas')) {
64
+ throw new insufficient_funds_gas_price_value_error_1.InsufficientFundsGasPriceValueError();
65
+ }
66
+ console.debug(err);
67
+ throw err;
68
+ }
48
69
  }
49
70
  /**
50
71
  * Build encoded approve transaction config.
@@ -326,13 +347,15 @@ class EvmAdapterClient extends abstract_adapter_client_1.AbstractAdapterClient {
326
347
  });
327
348
  return result;
328
349
  }
329
- async write(address, to, value, data) {
350
+ async write(address, to, value, data, options = {}) {
330
351
  const result = await this.clientWallet.sendTransaction({
331
352
  account: address,
332
353
  to: to,
333
354
  value: (0, viem_1.parseEther)(value),
334
355
  data: data,
335
- chain: this.evmPublic.chain
356
+ chain: this.evmPublic.chain,
357
+ ...(options.gas && { gas: BigInt(options.gas) }),
358
+ ...(0, options_1.getViemGasOptions)(options)
336
359
  });
337
360
  return result;
338
361
  }
@@ -1,8 +1,9 @@
1
1
  import { TronWeb } from 'tronweb';
2
+ import BigNumber from 'bignumber.js';
2
3
  import { AbstractAdapterClient } from '../../common/client/abstract-adapter-client';
3
4
  import { BlockchainName, HttpClient, ICustomLogger, TokenAmount } from '@cryptorubic/core';
4
5
  import { ClientAdaptersFactoryParams } from '../../../models/create-factory-params';
5
- import { TronSendTxParams } from '../models/tron-send-tx-params';
6
+ import { TronSendTxParams, TronTransactionOptions } from '../models/tron-send-tx-params';
6
7
  import { TxStatus } from '../../models/web3-public-models/tx-status';
7
8
  import { TronTransactionConfig } from '../../../../utils/models/tron-transaction-config';
8
9
  import { ApproveAdapterClient } from '../../models/approve-adapter';
@@ -16,7 +17,7 @@ export declare class TronAdapterClient extends AbstractAdapterClient<TronWeb, Tr
16
17
  getBlockchainName(): Promise<BlockchainName>;
17
18
  getAllowance(fromTokenAddress: string, walletAddress: string, spender: string): Promise<AllowanceInfo>;
18
19
  needApprove(from: TokenAmount, walletAddress: string, spender: string): Promise<boolean>;
19
- approve(fromAddress: string, tokenAddress: string, spenderAddress: string, amount?: string): Promise<string>;
20
+ approveTokens(tokenAddress: string, spenderAddress: string, amount?: BigNumber | 'infinity', options?: TronTransactionOptions): Promise<string>;
20
21
  encodeApprove(tokenAddress: string, spenderAddress: string, amount?: string): TronTransactionConfig;
21
22
  getTransactionStatus(srcTxHash: string): Promise<TxStatus>;
22
23
  /**
@@ -9,6 +9,7 @@ const tx_status_1 = require("../../models/web3-public-models/tx-status");
9
9
  const parse_tron_error_1 = require("../utils/parse-tron-error");
10
10
  const tron_adapter_1 = require("../tron-adapter");
11
11
  const tron_web3_pure_1 = require("../../../../utils/web3-types/tron-web3-pure");
12
+ const options_1 = require("../../utils/options");
12
13
  class TronAdapterClient extends abstract_adapter_client_1.AbstractAdapterClient {
13
14
  constructor(tronWeb, httpClient, logger, clientParams) {
14
15
  super(httpClient, logger, clientParams);
@@ -45,9 +46,24 @@ class TronAdapterClient extends abstract_adapter_client_1.AbstractAdapterClient
45
46
  const { allowanceWei } = await this.getAllowance(from.address, walletAddress, spender);
46
47
  return allowanceWei.lt(from.weiAmount);
47
48
  }
48
- async approve(fromAddress, tokenAddress, spenderAddress, amount) {
49
- const config = this.encodeApprove(tokenAddress, spenderAddress, amount);
50
- return this.write(config.to, config.signature, config.arguments);
49
+ async approveTokens(tokenAddress, spenderAddress, amount = 'infinity', options = {}) {
50
+ try {
51
+ const contract = await this.tronWeb.contract(trc_20_contract_abi_1.TRC20_CONTRACT_ABI, tokenAddress);
52
+ const rawValue = amount === 'infinity' ? new bignumber_js_1.default(2).pow(256).minus(1) : amount;
53
+ const transactionHash = await contract.approve(spenderAddress, rawValue.toFixed(0)).send({
54
+ ...(options.feeLimit && {
55
+ feeLimit: (0, options_1.stringifyAmount)(options.feeLimit)
56
+ })
57
+ });
58
+ if (options.onTransactionHash) {
59
+ options.onTransactionHash(transactionHash);
60
+ }
61
+ return transactionHash;
62
+ }
63
+ catch (err) {
64
+ console.error('Approve execution error: ', err);
65
+ throw (0, parse_tron_error_1.parseTronError)(err);
66
+ }
51
67
  }
52
68
  encodeApprove(tokenAddress, spenderAddress, amount) {
53
69
  const amountWei = amount ?? new bignumber_js_1.default(2).pow(256).minus(1).toFixed();
@@ -3,6 +3,8 @@ import { AllowanceInfo } from './common-types';
3
3
  import { EvmTransactionConfig } from '../../../utils/models/evm-transaction-config';
4
4
  import { TronTransactionConfig } from '../../../utils/models/tron-transaction-config';
5
5
  import { AbstractAdapterClient } from '../common/client/abstract-adapter-client';
6
+ import { BasicSendTransactionOptions } from './basic-transaction-options';
7
+ import BigNumber from 'bignumber.js';
6
8
  export interface ApproveAdapterClient<T> extends AbstractAdapterClient<{}, {}, {}> {
7
9
  /**
8
10
  * @param fromTokenAddress erc20 address of checked token
@@ -27,11 +29,13 @@ export interface ApproveAdapterClient<T> extends AbstractAdapterClient<{}, {}, {
27
29
  */
28
30
  needApprove(token: TokenAmount, contractAddress: string, walletAddress: string, amount: string): Promise<boolean>;
29
31
  /**
30
- * Get data for tokens approve
31
- * @param tokenAddress erc20 token address
32
- * @param spenderAddress spender address
33
- * @param amount amount in wei
32
+ * Executes approve method in ERC-20 token contract.
33
+ * @param tokenAddress Address of the smart-contract corresponding to the token.
34
+ * @param spenderAddress Wallet or contract address to approve.
35
+ * @param amount Token amount to approve in wei.
36
+ * @param [options] Additional options.
37
+ * @returns Approval transaction receipt.
34
38
  */
35
- approve(walletAddress: string, tokenAddress: string, spenderAddress: string, amount?: string): Promise<string>;
39
+ approveTokens(tokenAddress: string, spenderAddress: string, amount: BigNumber | 'infinity', options: BasicSendTransactionOptions): Promise<string>;
36
40
  }
37
41
  export declare function isApprovableAdapterClient(adapterClient: AbstractAdapterClient<{}, {}, {}>): adapterClient is ApproveAdapterClient<EvmTransactionConfig | TronTransactionConfig>;