@cryptorubic/web3 1.5.0 → 1.5.1-alpha.tron.2
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 +1 -1
- package/src/lib/adapter/adapters/adapter-tron/tron-adapter.d.ts +2 -1
- package/src/lib/adapter/adapters/adapter-tron/tron-adapter.js +66 -16
- package/src/lib/adapter/adapters/constants/trc-20-contract-abi.d.ts +1 -0
- package/src/lib/adapter/adapters/constants/tron-multicall-abi.js +36 -6
- package/src/lib/adapter/models/create-factory-params.d.ts +1 -0
- package/src/lib/utils/models/tron-call.d.ts +18 -0
package/package.json
CHANGED
|
@@ -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>;
|
|
@@ -20,12 +20,18 @@ 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.
|
|
23
|
+
this.clientParams = clientParams;
|
|
24
|
+
// private readonly multicallAddress = 'T9ziQU4EBteJzjzMzhHELdhgWFqwzS5Vki';
|
|
25
|
+
this.multicallAddress = 'TEazPvZwDjDtFeJupyo7QunvnrnUjPH8ED';
|
|
24
26
|
this.signer = new tron_adapter_signer_1.TronAdapterSigner(this.publicRef, httpClient, logger, clientParams);
|
|
25
27
|
}
|
|
26
28
|
initWeb3Client() {
|
|
27
29
|
const rpc = typeof this.rpcList[0] === 'string' ? this.rpcList[0] : this.rpcList[0].fullHost;
|
|
28
|
-
|
|
30
|
+
const tronApiKey = this.clientParams?.tronApiKey;
|
|
31
|
+
this.public = new tronweb_1.TronWeb({
|
|
32
|
+
fullHost: rpc,
|
|
33
|
+
headers: { ...(tronApiKey && { 'TRON-PRO-API-KEY': tronApiKey }) }
|
|
34
|
+
});
|
|
29
35
|
}
|
|
30
36
|
async callContractMethod(contractAddress, contractAbi, methodName, methodArguments = []) {
|
|
31
37
|
this.public.setAddress(contractAddress);
|
|
@@ -96,32 +102,70 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
96
102
|
]
|
|
97
103
|
})));
|
|
98
104
|
}
|
|
105
|
+
// public async multicallContractsMethods<Output extends TronWeb3PrimitiveType>(
|
|
106
|
+
// contractAbi: any,
|
|
107
|
+
// contractsData: {
|
|
108
|
+
// contractAddress: string;
|
|
109
|
+
// methodsData: MethodData[];
|
|
110
|
+
// }[]
|
|
111
|
+
// ): Promise<ContractMulticallResponse<any>[][]> {
|
|
112
|
+
// const calls: TronCall[][] = contractsData.map(({ contractAddress, methodsData }) => {
|
|
113
|
+
// return methodsData.map(({ methodName, methodArguments }) => [
|
|
114
|
+
// contractAddress,
|
|
115
|
+
// TronWeb3Pure.encodeFunctionCall(contractAbi as any, methodName, methodArguments)
|
|
116
|
+
// ]);
|
|
117
|
+
// });
|
|
118
|
+
// try {
|
|
119
|
+
// const outputs = await this.multicall(calls.flat());
|
|
120
|
+
// let outputIndex = 0;
|
|
121
|
+
// return contractsData.map((contractData) =>
|
|
122
|
+
// contractData.methodsData.map((methodData) => {
|
|
123
|
+
// const success = outputs.results[outputIndex]!;
|
|
124
|
+
// const returnData = outputs.returnData[outputIndex]!;
|
|
125
|
+
// outputIndex++;
|
|
126
|
+
// const methodOutputAbi = contractAbi.find(
|
|
127
|
+
// // @ts-ignore
|
|
128
|
+
// (funcSignature) => funcSignature.name === methodData.methodName
|
|
129
|
+
// // @ts-ignore
|
|
130
|
+
// )!.outputs!;
|
|
131
|
+
// return {
|
|
132
|
+
// output: success ? (TronWeb3Pure.decodeMethodOutput(methodOutputAbi, returnData) as Output) : null,
|
|
133
|
+
// success
|
|
134
|
+
// };
|
|
135
|
+
// })
|
|
136
|
+
// );
|
|
137
|
+
// } catch (err) {
|
|
138
|
+
// this.logger
|
|
139
|
+
// ? this.logger.customError('[TronAdapter_multicallContractsMethods] Error: ', err)
|
|
140
|
+
// : console.debug('[TronAdapter_multicallContractsMethods] Error:', err);
|
|
141
|
+
// return this.multicallContractsMethodsByOne(contractAbi, contractsData);
|
|
142
|
+
// }
|
|
143
|
+
// }
|
|
99
144
|
async multicallContractsMethods(contractAbi, contractsData) {
|
|
100
145
|
const calls = contractsData.map(({ contractAddress, methodsData }) => {
|
|
101
|
-
return methodsData.map(({ methodName, methodArguments }) =>
|
|
102
|
-
contractAddress,
|
|
103
|
-
|
|
104
|
-
|
|
146
|
+
return methodsData.map(({ methodName, methodArguments }) => ({
|
|
147
|
+
target: contractAddress,
|
|
148
|
+
allowFailure: true,
|
|
149
|
+
callData: tron_web3_pure_1.TronWeb3Pure.encodeFunctionCall(contractAbi, methodName, methodArguments)
|
|
150
|
+
}));
|
|
105
151
|
});
|
|
152
|
+
console.log('[multicallContractsMethods] calls ==>', calls);
|
|
106
153
|
try {
|
|
107
154
|
const outputs = await this.multicall(calls.flat());
|
|
155
|
+
console.log('[multicallContractsMethods] outputs ==>', outputs);
|
|
108
156
|
let outputIndex = 0;
|
|
109
157
|
return contractsData.map((contractData) => contractData.methodsData.map((methodData) => {
|
|
110
|
-
const
|
|
111
|
-
const
|
|
158
|
+
const output = outputs[outputIndex];
|
|
159
|
+
const methodOutputAbi = contractAbi.find((funcSignature) => funcSignature.name === methodData.methodName).outputs;
|
|
112
160
|
outputIndex++;
|
|
113
|
-
const methodOutputAbi = contractAbi.find(
|
|
114
|
-
// @ts-ignore
|
|
115
|
-
(funcSignature) => funcSignature.name === methodData.methodName
|
|
116
|
-
// @ts-ignore
|
|
117
|
-
).outputs;
|
|
118
161
|
return {
|
|
119
|
-
output: success ? tron_web3_pure_1.TronWeb3Pure.decodeMethodOutput(methodOutputAbi, returnData) : null,
|
|
120
|
-
success
|
|
162
|
+
output: output.success ? tron_web3_pure_1.TronWeb3Pure.decodeMethodOutput(methodOutputAbi, output.returnData) : null,
|
|
163
|
+
success: output.success
|
|
121
164
|
};
|
|
122
165
|
}));
|
|
123
166
|
}
|
|
124
167
|
catch (err) {
|
|
168
|
+
console.log('[multicallContractsMethods] err ==>', err);
|
|
125
169
|
this.logger
|
|
126
170
|
? this.logger.customError('[TronAdapter_multicallContractsMethods] Error: ', err)
|
|
127
171
|
: console.debug('[TronAdapter_multicallContractsMethods] Error:', err);
|
|
@@ -135,11 +179,17 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
135
179
|
const balance = await this.getBalance(walletAddress, token.address);
|
|
136
180
|
return balance.gte(token.weiAmount);
|
|
137
181
|
}
|
|
182
|
+
// private async multicall(calls: TronCall[]): Promise<TronMulticallResponse> {
|
|
183
|
+
// this.public.setAddress(this.multicallAddress);
|
|
184
|
+
// const contract = await this.public.contract(TRON_MULTICALL_ABI as any, this.multicallAddress);
|
|
185
|
+
// // @ts-ignore
|
|
186
|
+
// return contract.aggregateViewCalls(calls).call();
|
|
187
|
+
// }
|
|
138
188
|
async multicall(calls) {
|
|
139
189
|
this.public.setAddress(this.multicallAddress);
|
|
140
190
|
const contract = await this.public.contract(tron_multicall_abi_1.TRON_MULTICALL_ABI, this.multicallAddress);
|
|
141
191
|
// @ts-ignore
|
|
142
|
-
return contract
|
|
192
|
+
return contract['aggregate3'](calls).call();
|
|
143
193
|
}
|
|
144
194
|
multicallContractsMethodsByOne(contractAbi, contractsData) {
|
|
145
195
|
return Promise.all(contractsData.map((contractData) => {
|
|
@@ -1,26 +1,56 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TRON_MULTICALL_ABI = void 0;
|
|
4
|
+
// export const TRON_MULTICALL_ABI = [
|
|
5
|
+
// {
|
|
6
|
+
// inputs: [
|
|
7
|
+
// {
|
|
8
|
+
// components: [
|
|
9
|
+
// { internalType: 'address', name: 'target', type: 'address' },
|
|
10
|
+
// { internalType: 'bytes', name: 'callData', type: 'bytes' }
|
|
11
|
+
// ],
|
|
12
|
+
// internalType: 'struct Multicall.Call[]',
|
|
13
|
+
// name: 'calls',
|
|
14
|
+
// type: 'tuple[]'
|
|
15
|
+
// }
|
|
16
|
+
// ],
|
|
17
|
+
// name: 'aggregateViewCalls',
|
|
18
|
+
// outputs: [
|
|
19
|
+
// { internalType: 'uint256', name: 'blockNumber', type: 'uint256' },
|
|
20
|
+
// { internalType: 'bytes[]', name: 'returnData', type: 'bytes[]' },
|
|
21
|
+
// { internalType: 'bool[]', name: 'results', type: 'bool[]' }
|
|
22
|
+
// ],
|
|
23
|
+
// stateMutability: 'view',
|
|
24
|
+
// type: 'function'
|
|
25
|
+
// }
|
|
26
|
+
// ] as AbiItem[];
|
|
4
27
|
exports.TRON_MULTICALL_ABI = [
|
|
5
28
|
{
|
|
6
29
|
inputs: [
|
|
7
30
|
{
|
|
8
31
|
components: [
|
|
9
32
|
{ internalType: 'address', name: 'target', type: 'address' },
|
|
33
|
+
{ internalType: 'bool', name: 'allowFailure', type: 'bool' },
|
|
10
34
|
{ internalType: 'bytes', name: 'callData', type: 'bytes' }
|
|
11
35
|
],
|
|
12
|
-
internalType: 'struct
|
|
36
|
+
internalType: 'struct Multicall3.Call3[]',
|
|
13
37
|
name: 'calls',
|
|
14
38
|
type: 'tuple[]'
|
|
15
39
|
}
|
|
16
40
|
],
|
|
17
|
-
name: '
|
|
41
|
+
name: 'aggregate3',
|
|
18
42
|
outputs: [
|
|
19
|
-
{
|
|
20
|
-
|
|
21
|
-
|
|
43
|
+
{
|
|
44
|
+
components: [
|
|
45
|
+
{ internalType: 'bool', name: 'success', type: 'bool' },
|
|
46
|
+
{ internalType: 'bytes', name: 'returnData', type: 'bytes' }
|
|
47
|
+
],
|
|
48
|
+
internalType: 'struct Multicall3.Result[]',
|
|
49
|
+
name: 'returnData',
|
|
50
|
+
type: 'tuple[]'
|
|
51
|
+
}
|
|
22
52
|
],
|
|
23
|
-
stateMutability: '
|
|
53
|
+
stateMutability: 'payable',
|
|
24
54
|
type: 'function'
|
|
25
55
|
}
|
|
26
56
|
];
|
|
@@ -16,6 +16,7 @@ export interface ClientAdaptersFactoryParams {
|
|
|
16
16
|
envType: EnvType;
|
|
17
17
|
viemConfig?: ViemChainConfig;
|
|
18
18
|
lazyLoadWeb3?: boolean;
|
|
19
|
+
tronApiKey?: string;
|
|
19
20
|
}
|
|
20
21
|
export type ViemChainConfig = Partial<Record<EvmBlockchainName, ViemConfig>>;
|
|
21
22
|
export interface ViemConfig {
|
|
@@ -3,3 +3,21 @@
|
|
|
3
3
|
* Second argument is `data`, that is encoded contract method.
|
|
4
4
|
*/
|
|
5
5
|
export type TronCall = [string, string];
|
|
6
|
+
export type TronCall3Request = {
|
|
7
|
+
/**
|
|
8
|
+
* that is contract address to execute on
|
|
9
|
+
*/
|
|
10
|
+
target: string;
|
|
11
|
+
/**
|
|
12
|
+
* If false, the entire call will revert if the call fails.
|
|
13
|
+
*/
|
|
14
|
+
allowFailure: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Data to call on the target contract
|
|
17
|
+
*/
|
|
18
|
+
callData: string;
|
|
19
|
+
};
|
|
20
|
+
export type TronCall3Result = {
|
|
21
|
+
success: boolean;
|
|
22
|
+
returnData: string;
|
|
23
|
+
};
|