@cryptorubic/web3 1.6.0-alpha-rub-2366.0 → 1.6.0

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.6.0-alpha-rub-2366.0",
3
+ "version": "1.6.0",
4
4
  "dependencies": {
5
5
  "@ethersproject/bignumber": "^5.8.0",
6
6
  "@mysten/sui": "^1.24.0",
@@ -16,12 +16,13 @@ import { ApprovableAdapter } from '../models/approve-adapter';
16
16
  import { TxStatus } from '../models/web3-public-models/tx-status';
17
17
  export declare class TronAdapter extends AbstractAdapter<TronWeb, TronWeb, TronBlockchainName> implements ApprovableAdapter<TronTransactionConfig> {
18
18
  private readonly rpcList;
19
+ private readonly clientParams?;
19
20
  readonly signer: TronAdapterSigner;
20
21
  private readonly multicallAddress;
21
22
  needPreswapAction(): Promise<boolean>;
22
23
  constructor(rpcList: (string | {
23
24
  fullHost: string;
24
- })[], httpClient: HttpClient, logger?: ICustomLogger, clientParams?: ClientAdaptersFactoryParams);
25
+ })[], httpClient: HttpClient, logger?: ICustomLogger, clientParams?: ClientAdaptersFactoryParams | undefined);
25
26
  initWeb3Client(): void;
26
27
  callContractMethod<T extends Web3PrimitiveType = string>(contractAddress: string, contractAbi: Abi, methodName: string, methodArguments?: unknown[]): Promise<T>;
27
28
  getTransactionStatus(srcTxHash: string): Promise<TxStatus>;
@@ -42,7 +43,6 @@ export declare class TronAdapter extends AbstractAdapter<TronWeb, TronWeb, TronB
42
43
  }>;
43
44
  checkEnoughBalance(token: TokenAmount | PriceTokenAmount, walletAddress: string): Promise<boolean>;
44
45
  private multicall;
45
- private multicallContractsMethodsByOne;
46
46
  callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token[]>;
47
47
  getBalance(userAddress: string, tokenAddress?: string): Promise<BigNumber>;
48
48
  getTokenBalance(userAddress: string, tokenAddress: string): Promise<BigNumber>;
@@ -20,12 +20,17 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
20
20
  constructor(rpcList, httpClient, logger, clientParams) {
21
21
  super(core_1.BLOCKCHAIN_NAME.TRON, logger);
22
22
  this.rpcList = rpcList;
23
- this.multicallAddress = 'T9ziQU4EBteJzjzMzhHELdhgWFqwzS5Vki';
23
+ this.clientParams = clientParams;
24
+ this.multicallAddress = 'TEazPvZwDjDtFeJupyo7QunvnrnUjPH8ED';
24
25
  this.signer = new tron_adapter_signer_1.TronAdapterSigner(this.publicRef, httpClient, logger, clientParams);
25
26
  }
26
27
  initWeb3Client() {
27
28
  const rpc = typeof this.rpcList[0] === 'string' ? this.rpcList[0] : this.rpcList[0].fullHost;
28
- this.public = new tronweb_1.TronWeb({ fullHost: rpc });
29
+ const headers = this.clientParams?.tronwebHeaders;
30
+ this.public = new tronweb_1.TronWeb({
31
+ fullHost: rpc,
32
+ ...(headers && { headers })
33
+ });
29
34
  }
30
35
  async callContractMethod(contractAddress, contractAbi, methodName, methodArguments = []) {
31
36
  this.public.setAddress(contractAddress);
@@ -57,7 +62,7 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
57
62
  tokensAddresses.splice(indexOfNativeCoin, 1);
58
63
  promises[1] = this.getBalance(userAddress);
59
64
  }
60
- promises[0] = this.batchMulticallTokenBalance(userAddress, tokensAddresses, 50);
65
+ promises[0] = this.batchMulticallTokenBalance(userAddress, tokensAddresses, 20);
61
66
  const results = await Promise.all(promises);
62
67
  const tokensBalances = results[0].map((tokenResults) => {
63
68
  const { success, output } = tokenResults[0];
@@ -69,37 +74,27 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
69
74
  return tokensBalances;
70
75
  }
71
76
  async batchMulticallTokenBalance(userAddress, tokensAddresses, batchSize) {
72
- if (tokensAddresses.length > 100) {
73
- const balances = [];
74
- for (let i = 0; i < tokensAddresses.length; i += batchSize) {
75
- const batchRequest = tokensAddresses.slice(i, i + batchSize);
76
- const batchResult = await this.multicallContractsMethods(trc_20_contract_abi_1.TRC20_CONTRACT_ABI, batchRequest.map((tokenAddress) => ({
77
- contractAddress: tokenAddress,
78
- methodsData: [
79
- {
80
- methodName: 'balanceOf',
81
- methodArguments: [userAddress]
82
- }
83
- ]
84
- })));
85
- balances.push(...batchResult);
86
- }
87
- return balances;
77
+ const balances = [];
78
+ for (let i = 0; i < tokensAddresses.length; i += batchSize) {
79
+ const batchRequest = tokensAddresses.slice(i, i + batchSize);
80
+ const batchResult = await this.multicallContractsMethods(trc_20_contract_abi_1.TRC20_CONTRACT_ABI, batchRequest.map((tokenAddress) => ({
81
+ contractAddress: tokenAddress,
82
+ methodsData: [
83
+ {
84
+ methodName: 'balanceOf',
85
+ methodArguments: [userAddress]
86
+ }
87
+ ]
88
+ })));
89
+ balances.push(...batchResult);
88
90
  }
89
- return this.multicallContractsMethods(trc_20_contract_abi_1.TRC20_CONTRACT_ABI, tokensAddresses.map((tokenAddress) => ({
90
- contractAddress: tokenAddress,
91
- methodsData: [
92
- {
93
- methodName: 'balanceOf',
94
- methodArguments: [userAddress]
95
- }
96
- ]
97
- })));
91
+ return balances;
98
92
  }
99
93
  async multicallContractsMethods(contractAbi, contractsData) {
100
94
  const calls = contractsData.map(({ contractAddress, methodsData }) => {
101
95
  return methodsData.map(({ methodName, methodArguments }) => [
102
96
  contractAddress,
97
+ true,
103
98
  tron_web3_pure_1.TronWeb3Pure.encodeFunctionCall(contractAbi, methodName, methodArguments)
104
99
  ]);
105
100
  });
@@ -107,17 +102,14 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
107
102
  const outputs = await this.multicall(calls.flat());
108
103
  let outputIndex = 0;
109
104
  return contractsData.map((contractData) => contractData.methodsData.map((methodData) => {
110
- const success = outputs.results[outputIndex];
111
- const returnData = outputs.returnData[outputIndex];
105
+ const output = outputs.returnData[outputIndex];
106
+ if (!output)
107
+ return { output: null, success: false };
108
+ const methodOutputAbi = contractAbi.find((funcSignature) => funcSignature.name === methodData.methodName).outputs;
112
109
  outputIndex++;
113
- const methodOutputAbi = contractAbi.find(
114
- // @ts-ignore
115
- (funcSignature) => funcSignature.name === methodData.methodName
116
- // @ts-ignore
117
- ).outputs;
118
110
  return {
119
- output: success ? tron_web3_pure_1.TronWeb3Pure.decodeMethodOutput(methodOutputAbi, returnData) : null,
120
- success
111
+ output: output.success ? tron_web3_pure_1.TronWeb3Pure.decodeMethodOutput(methodOutputAbi, output.returnData) : null,
112
+ success: output.success
121
113
  };
122
114
  }));
123
115
  }
@@ -125,7 +117,7 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
125
117
  this.logger
126
118
  ? this.logger.customError('[TronAdapter_multicallContractsMethods] Error: ', err)
127
119
  : console.debug('[TronAdapter_multicallContractsMethods] Error:', err);
128
- return this.multicallContractsMethodsByOne(contractAbi, contractsData);
120
+ return contractsData.map((contractData) => contractData.methodsData.map(() => ({ output: null, success: false })));
129
121
  }
130
122
  }
131
123
  async getBlockNumber() {
@@ -139,26 +131,7 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
139
131
  this.public.setAddress(this.multicallAddress);
140
132
  const contract = await this.public.contract(tron_multicall_abi_1.TRON_MULTICALL_ABI, this.multicallAddress);
141
133
  // @ts-ignore
142
- return contract.aggregateViewCalls(calls).call();
143
- }
144
- multicallContractsMethodsByOne(contractAbi, contractsData) {
145
- return Promise.all(contractsData.map((contractData) => {
146
- return Promise.all(contractData.methodsData.map(async (methodData) => {
147
- try {
148
- const output = (await this.read(contractData.contractAddress, contractAbi, methodData.methodName, methodData.methodArguments));
149
- return {
150
- output,
151
- success: true
152
- };
153
- }
154
- catch {
155
- return {
156
- output: null,
157
- success: false
158
- };
159
- }
160
- }));
161
- }));
134
+ return contract['aggregate3'](calls).call();
162
135
  }
163
136
  async callForTokensInfo(tokenAddresses) {
164
137
  const tokenFields = ['decimals', 'symbol', 'name'];
@@ -10,3 +10,4 @@ export declare const TRC20_CONTRACT_ABI: {
10
10
  stateMutability: string;
11
11
  type: string;
12
12
  }[];
13
+ export type TronAbiItem = (typeof TRC20_CONTRACT_ABI)[number];
@@ -7,18 +7,25 @@ exports.TRON_MULTICALL_ABI = [
7
7
  {
8
8
  components: [
9
9
  { internalType: 'address', name: 'target', type: 'address' },
10
+ { internalType: 'bool', name: 'allowFailure', type: 'bool' },
10
11
  { internalType: 'bytes', name: 'callData', type: 'bytes' }
11
12
  ],
12
- internalType: 'struct Multicall.Call[]',
13
+ internalType: 'struct Multicall3.Call3[]',
13
14
  name: 'calls',
14
15
  type: 'tuple[]'
15
16
  }
16
17
  ],
17
- name: 'aggregateViewCalls',
18
+ name: 'aggregate3',
18
19
  outputs: [
19
- { internalType: 'uint256', name: 'blockNumber', type: 'uint256' },
20
- { internalType: 'bytes[]', name: 'returnData', type: 'bytes[]' },
21
- { internalType: 'bool[]', name: 'results', type: 'bool[]' }
20
+ {
21
+ components: [
22
+ { internalType: 'bool', name: 'success', type: 'bool' },
23
+ { internalType: 'bytes', name: 'returnData', type: 'bytes' }
24
+ ],
25
+ internalType: 'struct Multicall3.Result[]',
26
+ name: 'returnData',
27
+ type: 'tuple[]'
28
+ }
22
29
  ],
23
30
  stateMutability: 'view',
24
31
  type: 'function'
@@ -16,6 +16,9 @@ export interface ClientAdaptersFactoryParams {
16
16
  envType: EnvType;
17
17
  viemConfig?: ViemChainConfig;
18
18
  lazyLoadWeb3?: boolean;
19
+ tronwebHeaders?: {
20
+ [key: string]: string;
21
+ };
19
22
  }
20
23
  export type ViemChainConfig = Partial<Record<EvmBlockchainName, ViemConfig>>;
21
24
  export interface ViemConfig {
@@ -1,5 +1,10 @@
1
1
  /**
2
- * First argument is `target`, that is contract address to execute on.
3
- * Second argument is `data`, that is encoded contract method.
2
+ * First argument is `target`, that is contract address to execute on
3
+ * Second argument is `allowFailure`. If false, the entire call will revert if the call fails.
4
+ * 3th argument is `callData`. Data to call on the target contract
4
5
  */
5
- export type TronCall = [string, string];
6
+ export type TronCall3Request = [string, boolean, string];
7
+ export type TronCall3Result = {
8
+ success: boolean;
9
+ returnData: string;
10
+ };