@cryptorubic/web3 0.3.4 → 0.3.6-alpha-id-990.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/package.json CHANGED
@@ -1,17 +1,16 @@
1
1
  {
2
2
  "name": "@cryptorubic/web3",
3
- "version": "v0.3.4",
3
+ "version": "0.3.6-alpha-id-990.1",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.0",
6
6
  "bignumber.js": "9.1.2",
7
- "@cryptorubic/core": "v0.3.4",
8
- "viem": "^2.19.1",
7
+ "@cryptorubic/core": "0.3.6-alpha-id-990.1",
9
8
  "web3-utils": "^4.3.1",
10
9
  "@ton/ton": "^15.1.0",
11
10
  "@solana/web3.js": "1.95.3",
12
11
  "@solflare-wallet/utl-sdk": "^1.4.0",
13
12
  "@ethersproject/bignumber": "^5.7.0",
14
- "@cryptorubic/tron-types": "v0.3.4",
13
+ "@cryptorubic/tron-types": "0.3.6-alpha-id-990.1",
15
14
  "bitcoin-address-validation": "^2.2.3",
16
15
  "axios": "0.27.2",
17
16
  "crc-32": "^1.2.2",
@@ -0,0 +1,2 @@
1
+ import { Abi } from 'viem';
2
+ export declare const UNI_V3_PERMIT_2_ABI: Abi;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UNI_V3_PERMIT_2_ABI = void 0;
4
+ exports.UNI_V3_PERMIT_2_ABI = [
5
+ {
6
+ inputs: [
7
+ {
8
+ internalType: 'address',
9
+ name: 'owner',
10
+ type: 'address',
11
+ },
12
+ {
13
+ internalType: 'address',
14
+ name: 'token',
15
+ type: 'address',
16
+ },
17
+ {
18
+ internalType: 'address',
19
+ name: 'spender',
20
+ type: 'address',
21
+ },
22
+ ],
23
+ name: 'allowance',
24
+ outputs: [
25
+ {
26
+ internalType: 'uint160',
27
+ name: 'amount',
28
+ type: 'uint160',
29
+ },
30
+ {
31
+ internalType: 'uint48',
32
+ name: 'expiration',
33
+ type: 'uint48',
34
+ },
35
+ {
36
+ internalType: 'uint48',
37
+ name: 'nonce',
38
+ type: 'uint48',
39
+ },
40
+ ],
41
+ stateMutability: 'view',
42
+ type: 'function',
43
+ },
44
+ {
45
+ inputs: [
46
+ {
47
+ internalType: 'address',
48
+ name: 'token',
49
+ type: 'address',
50
+ },
51
+ {
52
+ internalType: 'address',
53
+ name: 'spender',
54
+ type: 'address',
55
+ },
56
+ {
57
+ internalType: 'uint160',
58
+ name: 'amount',
59
+ type: 'uint160',
60
+ },
61
+ {
62
+ internalType: 'uint48',
63
+ name: 'expiration',
64
+ type: 'uint48',
65
+ },
66
+ ],
67
+ name: 'approve',
68
+ outputs: [],
69
+ stateMutability: 'nonpayable',
70
+ type: 'function',
71
+ },
72
+ ];
@@ -18,7 +18,7 @@ export declare class EvmAdapter extends AbstractAdapter<PublicActions & PublicCl
18
18
  multicallByContract<T>(contracts: MulticallParameters, allowErrors?: boolean): Promise<MulticallResponse<T>[]>;
19
19
  multicallByAddress<T>(address: string, abi: Abi, method: string, methodArgs?: unknown[][], allowErrors?: boolean): Promise<MulticallResponse<T>[]>;
20
20
  static encodeMethodCall(contractAddress: string, contractAbi: Abi, method: string, parameters?: unknown[], value?: string): EvmTransactionConfig;
21
- simulateTransaction(config: EvmTransactionConfig, from: string): Promise<string>;
21
+ simulateTransaction(config: EvmTransactionConfig, from: string, timeout?: number): Promise<string>;
22
22
  callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token<EvmBlockchainName>[]>;
23
23
  getGasPrice(): Promise<string>;
24
24
  checkEnoughBalance(token: TokenAmount | PriceTokenAmount, walletAddress: string, amount?: BigNumber): Promise<boolean>;
@@ -33,6 +33,9 @@ export declare class EvmAdapter extends AbstractAdapter<PublicActions & PublicCl
33
33
  encodeApprove(tokenAddress: string, spenderAddress: string, amount?: string): EvmTransactionConfig;
34
34
  needApprove(token: TokenAmount, contractAddress: string, walletAddress: string, amount: string): Promise<boolean>;
35
35
  approve(fromAddress: string, tokenAddress: string, spenderAddress: string, amount?: string): Promise<string>;
36
+ needPermit2Approve(token: TokenAmount, permit2Address: string, walletAddress: string, spenderAddress: string): Promise<boolean>;
37
+ encodePermit2Approve(tokenAddress: string, spenderAddress: string, permit2Address: string, expiration: string, amount: string): EvmTransactionConfig;
38
+ approveOnPermit2(fromAddress: string, tokenAddress: string, permit2Address: string, spenderAddress: string, deadline?: BigNumber, amount?: BigNumber): Promise<string>;
36
39
  /**
37
40
  * Calculates EIP-1559 specific gas details.
38
41
  * @see {@link https://github.com/ethers-io/ethers.js/blob/master/packages/abstract-provider/src.ts/index.ts#L235}
@@ -9,6 +9,7 @@ const core_1 = require("@cryptorubic/core");
9
9
  const bignumber_js_1 = require("bignumber.js");
10
10
  const web3_pure_1 = require("../../utils/web3-pure");
11
11
  const possible_errors_1 = require("./constants/possible-errors");
12
+ const uni_v3_permit_abi_1 = require("./constants/uni-v3-permit-abi");
12
13
  class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
13
14
  async needPreswapAction(token, contractAddress, walletAddress, amount) {
14
15
  return this.needApprove(token, contractAddress, walletAddress, amount);
@@ -137,7 +138,7 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
137
138
  throw err;
138
139
  }
139
140
  }
140
- async simulateTransaction(config, from) {
141
+ async simulateTransaction(config, from, timeout = 15000) {
141
142
  try {
142
143
  const callParams = {
143
144
  account: from,
@@ -145,8 +146,11 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
145
146
  to: config.to,
146
147
  ...(config.value && { value: BigInt(config.value) }),
147
148
  };
148
- const gasLimitWei = await this.public.estimateGas(callParams);
149
- return gasLimitWei.toString();
149
+ const gasLimitWei = await Promise.race([
150
+ this.public.estimateGas(callParams),
151
+ new Promise((_, reject) => setTimeout(() => reject('Timeout'), timeout)),
152
+ ]);
153
+ return gasLimitWei?.toString();
150
154
  }
151
155
  catch (err) {
152
156
  this.logger?.customError('Error while simulating transaction', err);
@@ -262,6 +266,26 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
262
266
  const config = this.encodeApprove(tokenAddress, spenderAddress, amount);
263
267
  return this.write(fromAddress, config.to, config.value, config.data);
264
268
  }
269
+ async needPermit2Approve(token, permit2Address, walletAddress, spenderAddress) {
270
+ const res = await this.read(permit2Address, uni_v3_permit_abi_1.UNI_V3_PERMIT_2_ABI, 'allowance', [walletAddress, token.address, spenderAddress]);
271
+ const [amount, expiration, _nonce] = res;
272
+ return (token.weiAmount.gt(amount) ||
273
+ new bignumber_js_1.default(Date.now()).gt(Number(expiration) * 1000));
274
+ }
275
+ encodePermit2Approve(tokenAddress, spenderAddress, permit2Address, expiration, amount) {
276
+ const tx = EvmAdapter.encodeMethodCall(permit2Address, uni_v3_permit_abi_1.UNI_V3_PERMIT_2_ABI, 'approve', [tokenAddress, spenderAddress, amount, expiration]);
277
+ return {
278
+ data: tx.data,
279
+ value: tx.value,
280
+ to: tx.to,
281
+ };
282
+ }
283
+ async approveOnPermit2(fromAddress, tokenAddress, permit2Address, spenderAddress, deadline = new bignumber_js_1.default(1000000), amount) {
284
+ const approveAmount = amount ? amount : new bignumber_js_1.default(2).pow(256).minus(1);
285
+ const expiration = new bignumber_js_1.default(Date.now()).plus(deadline).toFixed();
286
+ const tx = this.encodePermit2Approve(tokenAddress, spenderAddress, permit2Address, expiration, approveAmount.toFixed());
287
+ return this.write(fromAddress, tx.to, tx.value, tx.data);
288
+ }
265
289
  /**
266
290
  * Calculates EIP-1559 specific gas details.
267
291
  * @see {@link https://github.com/ethers-io/ethers.js/blob/master/packages/abstract-provider/src.ts/index.ts#L235}
@@ -0,0 +1 @@
1
+ export type Permit2AllowanceContractResponse = [string, number, number];
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -34,8 +34,13 @@ class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
34
34
  }
35
35
  async simulateTransaction(_config, _from) { }
36
36
  async callForTokensInfo(tokenAddresses) {
37
+ const nativeToken = core_1.nativeTokensList[core_1.BLOCKCHAIN_NAME.SOLANA];
37
38
  const nativeTokenIndex = tokenAddresses.findIndex((address) => web3_pure_1.Web3Pure.isNativeAddress(core_1.CHAIN_TYPE.SOLANA, address));
38
39
  const filteredTokenAddresses = tokenAddresses.filter((_, index) => index !== nativeTokenIndex);
40
+ // only native token in tokenAddresses
41
+ if (!filteredTokenAddresses.length) {
42
+ return [nativeToken];
43
+ }
39
44
  const mints = filteredTokenAddresses.map((address) => new web3_js_1.PublicKey(address));
40
45
  const tokenSdk = new utl_sdk_1.Client();
41
46
  const tokensMint = await tokenSdk.fetchMints(mints);
@@ -51,8 +56,7 @@ class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
51
56
  if (nativeTokenIndex === -1) {
52
57
  return tokens;
53
58
  }
54
- const blockchainNativeToken = core_1.nativeTokensList[core_1.BLOCKCHAIN_NAME.SOLANA];
55
- tokens.splice(nativeTokenIndex, 0, blockchainNativeToken);
59
+ tokens.splice(nativeTokenIndex, 0, nativeToken);
56
60
  return tokens;
57
61
  }
58
62
  }