@cryptorubic/web3 0.0.24 → 0.0.25
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/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/lib/adapter/adapters/abstract-adapter.d.ts +3 -0
- package/src/lib/adapter/adapters/abstract-adapter.js +12 -0
- package/src/lib/adapter/adapters/evm-adapter.d.ts +24 -2
- package/src/lib/adapter/adapters/evm-adapter.js +80 -2
- package/src/lib/adapter/adapters/models/approve-adapter.d.ts +10 -4
- package/src/lib/adapter/adapters/models/basic-transaction-options.d.ts +7 -0
- package/src/lib/adapter/adapters/models/basic-transaction-options.js +2 -0
- package/src/lib/adapter/adapters/models/gas-price.d.ts +22 -0
- package/src/lib/adapter/adapters/models/gas-price.js +2 -0
- package/src/lib/adapter/adapters/tron-adapter.d.ts +5 -6
- package/src/lib/adapter/adapters/tron-adapter.js +20 -18
- package/src/lib/adapter/blockchain-adapter-factory.service.d.ts +3 -1
- package/src/lib/adapter/blockchain-adapter-factory.service.js +4 -0
- package/src/lib/adapter/constants/chain-configs/chain-configs.js +1 -1
- package/src/lib/adapter/constants/viem-blockchain-mapping.js +2 -1
- package/src/lib/utils/web3-types/tron-web3-pure.d.ts +3 -0
- package/src/lib/utils/web3-types/tron-web3-pure.js +14 -0
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export * from './lib/adapter/adapters/models/ton-api-config';
|
|
|
22
22
|
export * from './lib/adapter/adapters/models/tonapi-models';
|
|
23
23
|
export * from './lib/adapter/adapters/models/toncenter-types';
|
|
24
24
|
export * from './lib/adapter/adapters/models/approve-adapter';
|
|
25
|
+
export * from './lib/adapter/adapters/models/gas-price';
|
|
25
26
|
export * from './lib/utils/web3-pure';
|
|
26
27
|
export * from './lib/utils/models/evm-transaction-config';
|
|
27
28
|
export * from './lib/utils/web3-types/bitcoin-web3-pure';
|
package/src/index.js
CHANGED
|
@@ -22,6 +22,7 @@ tslib_1.__exportStar(require("./lib/adapter/adapters/models/ton-api-config"), ex
|
|
|
22
22
|
tslib_1.__exportStar(require("./lib/adapter/adapters/models/tonapi-models"), exports);
|
|
23
23
|
tslib_1.__exportStar(require("./lib/adapter/adapters/models/toncenter-types"), exports);
|
|
24
24
|
tslib_1.__exportStar(require("./lib/adapter/adapters/models/approve-adapter"), exports);
|
|
25
|
+
tslib_1.__exportStar(require("./lib/adapter/adapters/models/gas-price"), exports);
|
|
25
26
|
// utils aka web3pure
|
|
26
27
|
tslib_1.__exportStar(require("./lib/utils/web3-pure"), exports);
|
|
27
28
|
tslib_1.__exportStar(require("./lib/utils/models/evm-transaction-config"), exports);
|
|
@@ -9,6 +9,9 @@ export declare abstract class AbstractAdapter<P, W, B extends BlockchainName> {
|
|
|
9
9
|
private _wallet;
|
|
10
10
|
set wallet(value: W | null);
|
|
11
11
|
get wallet(): W;
|
|
12
|
+
private _walletAddress;
|
|
13
|
+
set walletAddress(value: string | null);
|
|
14
|
+
get walletAddress(): string;
|
|
12
15
|
protected constructor(blockchain: B, logger?: ICustomLogger);
|
|
13
16
|
abstract callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token<BlockchainName>[]>;
|
|
14
17
|
needPreswapAction(token: TokenAmount, contractAddress: string, walletAddress: string, amount: string | BigNumber): Promise<boolean>;
|
|
@@ -25,9 +25,21 @@ class AbstractAdapter {
|
|
|
25
25
|
}
|
|
26
26
|
return this._wallet;
|
|
27
27
|
}
|
|
28
|
+
set walletAddress(value) {
|
|
29
|
+
this._walletAddress = value;
|
|
30
|
+
}
|
|
31
|
+
get walletAddress() {
|
|
32
|
+
if (!this._walletAddress) {
|
|
33
|
+
const msg = 'Trying to access undefined walletAddress';
|
|
34
|
+
this.logger?.customLog(msg);
|
|
35
|
+
throw new Error(msg);
|
|
36
|
+
}
|
|
37
|
+
return this._walletAddress;
|
|
38
|
+
}
|
|
28
39
|
constructor(blockchain, logger) {
|
|
29
40
|
this._public = null;
|
|
30
41
|
this._wallet = null;
|
|
42
|
+
this._walletAddress = null;
|
|
31
43
|
this.blockchain = blockchain;
|
|
32
44
|
if (logger) {
|
|
33
45
|
this.logger = logger;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Abi, MulticallResponse, MulticallParameters, PublicActions, PublicClient, WalletClient, WalletActions } from 'viem';
|
|
1
|
+
import { Abi, MulticallResponse, MulticallParameters, PublicActions, PublicClient, WalletClient, WalletActions, BlockNumber, GetBlockReturnType } from 'viem';
|
|
2
2
|
import { AbstractAdapter } from './abstract-adapter';
|
|
3
3
|
import { EvmBlockchainName, ICustomLogger, Token, TokenAmount, PriceTokenAmount } from '@cryptorubic/core';
|
|
4
4
|
import BigNumber from "bignumber.js";
|
|
@@ -6,7 +6,9 @@ import { EvmAdapterOptions } from "./models/evm-adapter-options";
|
|
|
6
6
|
import { EvmTransactionConfig } from "../../utils/models/evm-transaction-config";
|
|
7
7
|
import { ApproveAdapter } from "./models/approve-adapter";
|
|
8
8
|
import { AllowanceInfo } from "./models/common-types";
|
|
9
|
-
|
|
9
|
+
import { BlockTag } from "viem/_types/types/block";
|
|
10
|
+
import { GasPrice } from "./models/gas-price";
|
|
11
|
+
export declare class EvmAdapter extends AbstractAdapter<PublicActions & PublicClient, WalletClient & WalletActions, EvmBlockchainName> implements ApproveAdapter<EvmTransactionConfig> {
|
|
10
12
|
needPreswapAction(token: TokenAmount, contractAddress: string, walletAddress: string, amount: string): Promise<boolean>;
|
|
11
13
|
handlePreswap(contractAddress: string, walletAddress: string, tokenAmount: TokenAmount): Promise<void>;
|
|
12
14
|
constructor(adapterOptions: EvmAdapterOptions, logger?: ICustomLogger);
|
|
@@ -31,4 +33,24 @@ export declare class EvmAdapter extends AbstractAdapter<PublicActions & PublicCl
|
|
|
31
33
|
getAllowance(fromTokenAddress: string, walletAddress: string, spenderAddress: string): Promise<AllowanceInfo>;
|
|
32
34
|
encodeApprove(tokenAddress: string, spenderAddress: string, amount?: string): EvmTransactionConfig;
|
|
33
35
|
needApprove(token: TokenAmount, contractAddress: string, walletAddress: string, amount: string): Promise<boolean>;
|
|
36
|
+
approve(fromAddress: string, tokenAddress: string, spenderAddress: string, amount?: string): Promise<string>;
|
|
37
|
+
/**
|
|
38
|
+
* Calculates EIP-1559 specific gas details.
|
|
39
|
+
* @see {@link https://github.com/ethers-io/ethers.js/blob/master/packages/abstract-provider/src.ts/index.ts#L235}
|
|
40
|
+
* @returns block baseFee, average maxPriorityFeePerGas, and maxFeePerGas.
|
|
41
|
+
*/
|
|
42
|
+
getPriorityFeeGas(): Promise<GasPrice>;
|
|
43
|
+
/**
|
|
44
|
+
* Gets block by block id.
|
|
45
|
+
* @param [blockId] Block id: hash, number ... Default is 'latest'.
|
|
46
|
+
* @returns Block by blockId parameter.
|
|
47
|
+
*/
|
|
48
|
+
getBlock(blockType?: BlockNumber | BlockTag): Promise<GetBlockReturnType>;
|
|
49
|
+
/**
|
|
50
|
+
* Estimates average maxPriorityFeePerGas for EIP-1559 transactions based on last 20 blocks.
|
|
51
|
+
* @see {@link https://docs.alchemy.com/docs/how-to-build-a-gas-fee-estimator-using-eip-1559}
|
|
52
|
+
* @returns Average maxPriorityFeePerGas in wei
|
|
53
|
+
*/
|
|
54
|
+
getMaxPriorityFeePerGas(): Promise<number>;
|
|
55
|
+
getBaseFeePerGas(blockNumber: bigint): Promise<number>;
|
|
34
56
|
}
|
|
@@ -136,8 +136,7 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
136
136
|
to: config.to,
|
|
137
137
|
...(config.value && { value: BigInt(config.value) })
|
|
138
138
|
};
|
|
139
|
-
|
|
140
|
-
await this.core.call(callParams);
|
|
139
|
+
await this.public.call(callParams);
|
|
141
140
|
}
|
|
142
141
|
catch (err) {
|
|
143
142
|
this.logger?.customError('Error while simulating transaction', err);
|
|
@@ -243,5 +242,84 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
243
242
|
return allowanceWei.lt(token.weiAmount);
|
|
244
243
|
}
|
|
245
244
|
;
|
|
245
|
+
async approve(fromAddress, tokenAddress, spenderAddress, amount) {
|
|
246
|
+
const config = this.encodeApprove(tokenAddress, spenderAddress, amount);
|
|
247
|
+
return this.write(fromAddress, config.to, config.value, config.data);
|
|
248
|
+
}
|
|
249
|
+
;
|
|
250
|
+
/**
|
|
251
|
+
* Calculates EIP-1559 specific gas details.
|
|
252
|
+
* @see {@link https://github.com/ethers-io/ethers.js/blob/master/packages/abstract-provider/src.ts/index.ts#L235}
|
|
253
|
+
* @returns block baseFee, average maxPriorityFeePerGas, and maxFeePerGas.
|
|
254
|
+
*/
|
|
255
|
+
async getPriorityFeeGas() {
|
|
256
|
+
const block = await this.getBlock('latest');
|
|
257
|
+
let lastBaseFeePerGas = null;
|
|
258
|
+
let maxFeePerGas = null;
|
|
259
|
+
let maxPriorityFeePerGas = null;
|
|
260
|
+
if (block && block.baseFeePerGas) {
|
|
261
|
+
const baseFeePerGas = await this.getBaseFeePerGas(block.number);
|
|
262
|
+
try {
|
|
263
|
+
lastBaseFeePerGas = baseFeePerGas;
|
|
264
|
+
maxPriorityFeePerGas = await this.getMaxPriorityFeePerGas();
|
|
265
|
+
maxFeePerGas = baseFeePerGas * 2 + maxPriorityFeePerGas;
|
|
266
|
+
}
|
|
267
|
+
catch (err) {
|
|
268
|
+
console.debug(err);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return {
|
|
272
|
+
baseFee: lastBaseFeePerGas?.toFixed(),
|
|
273
|
+
maxFeePerGas: maxFeePerGas?.toFixed(),
|
|
274
|
+
maxPriorityFeePerGas: maxPriorityFeePerGas?.toFixed()
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Gets block by block id.
|
|
279
|
+
* @param [blockId] Block id: hash, number ... Default is 'latest'.
|
|
280
|
+
* @returns Block by blockId parameter.
|
|
281
|
+
*/
|
|
282
|
+
getBlock(blockType = 'latest') {
|
|
283
|
+
if (typeof blockType === 'string') {
|
|
284
|
+
// @TODO FIX TYPE
|
|
285
|
+
return this.public.getBlock({ blockTag: blockType });
|
|
286
|
+
}
|
|
287
|
+
return this.public.getBlock({ blockNumber: blockType });
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Estimates average maxPriorityFeePerGas for EIP-1559 transactions based on last 20 blocks.
|
|
291
|
+
* @see {@link https://docs.alchemy.com/docs/how-to-build-a-gas-fee-estimator-using-eip-1559}
|
|
292
|
+
* @returns Average maxPriorityFeePerGas in wei
|
|
293
|
+
*/
|
|
294
|
+
async getMaxPriorityFeePerGas() {
|
|
295
|
+
const HISTORICAL_BLOCKS = 20;
|
|
296
|
+
const feeHistory = await this.public.getFeeHistory({
|
|
297
|
+
blockCount: HISTORICAL_BLOCKS,
|
|
298
|
+
rewardPercentiles: [50],
|
|
299
|
+
blockTag: 'pending'
|
|
300
|
+
});
|
|
301
|
+
if (!feeHistory?.reward) {
|
|
302
|
+
throw new Error('No fee history found');
|
|
303
|
+
}
|
|
304
|
+
const blocks = feeHistory.reward.map(x => x.map(reward => Number(reward)));
|
|
305
|
+
const rewardSum = blocks
|
|
306
|
+
.map(x => x[0])
|
|
307
|
+
.reduce((acc, v) => acc + (v || 0), 0);
|
|
308
|
+
return Math.round(rewardSum / blocks.length);
|
|
309
|
+
}
|
|
310
|
+
async getBaseFeePerGas(blockNumber) {
|
|
311
|
+
const feeHistory = await this.public.getFeeHistory({
|
|
312
|
+
blockCount: 1,
|
|
313
|
+
rewardPercentiles: [50],
|
|
314
|
+
// blockTag: 'pending',
|
|
315
|
+
blockNumber: blockNumber
|
|
316
|
+
});
|
|
317
|
+
if (!feeHistory?.baseFeePerGas.length) {
|
|
318
|
+
throw new Error('No fee history found');
|
|
319
|
+
}
|
|
320
|
+
const feedSum = feeHistory.baseFeePerGas
|
|
321
|
+
.reduce((acc, v) => acc.plus(v.toString() || 0), new bignumber_js_1.default(0));
|
|
322
|
+
return feedSum.dividedBy(feeHistory.baseFeePerGas.length).toNumber();
|
|
323
|
+
}
|
|
246
324
|
}
|
|
247
325
|
exports.EvmAdapter = EvmAdapter;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { TokenAmount } from "@cryptorubic/core";
|
|
2
2
|
import { AllowanceInfo } from "./common-types";
|
|
3
|
-
|
|
4
|
-
export interface ApproveAdapter {
|
|
3
|
+
export interface ApproveAdapter<T> {
|
|
5
4
|
/**
|
|
6
5
|
* @param fromTokenAddress erc20 address of checked token
|
|
7
6
|
* @param walletAddress owner wallet address
|
|
@@ -10,12 +9,12 @@ export interface ApproveAdapter {
|
|
|
10
9
|
*/
|
|
11
10
|
getAllowance(fromTokenAddress: string, walletAddress: string, spenderAddress: string): Promise<AllowanceInfo>;
|
|
12
11
|
/**
|
|
13
|
-
*
|
|
12
|
+
* Get data for tokens approve
|
|
14
13
|
* @param tokenAddress erc20 token address
|
|
15
14
|
* @param spenderAddress spender address
|
|
16
15
|
* @param amount amount in non wei
|
|
17
16
|
*/
|
|
18
|
-
encodeApprove(tokenAddress: string, spenderAddress: string, amount?: string):
|
|
17
|
+
encodeApprove(tokenAddress: string, spenderAddress: string, amount?: string): T;
|
|
19
18
|
/**
|
|
20
19
|
* Check if approve needed.
|
|
21
20
|
* @param token
|
|
@@ -24,4 +23,11 @@ export interface ApproveAdapter {
|
|
|
24
23
|
* @param amount
|
|
25
24
|
*/
|
|
26
25
|
needApprove(token: TokenAmount, contractAddress: string, walletAddress: string, amount: string): Promise<boolean>;
|
|
26
|
+
/**
|
|
27
|
+
* Get data for tokens approve
|
|
28
|
+
* @param tokenAddress erc20 token address
|
|
29
|
+
* @param spenderAddress spender address
|
|
30
|
+
* @param amount amount in non wei
|
|
31
|
+
*/
|
|
32
|
+
approve(walletAddress: string, tokenAddress: string, spenderAddress: string, amount?: string): Promise<string>;
|
|
27
33
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
export type GasPrice = EIP1559Gas & SingleGasPrice;
|
|
3
|
+
export interface EIP1559Gas {
|
|
4
|
+
/**
|
|
5
|
+
* EIP-1559 Block base fee.
|
|
6
|
+
*/
|
|
7
|
+
baseFee?: string;
|
|
8
|
+
/**
|
|
9
|
+
* EIP-1559 Transaction maximum fee.
|
|
10
|
+
*/
|
|
11
|
+
maxFeePerGas?: string;
|
|
12
|
+
/**
|
|
13
|
+
* EIP-1559 Transaction miner's tip.
|
|
14
|
+
*/
|
|
15
|
+
maxPriorityFeePerGas?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface SingleGasPrice {
|
|
18
|
+
gasPrice?: string;
|
|
19
|
+
}
|
|
20
|
+
export type GasPriceBN = {
|
|
21
|
+
[P in keyof GasPrice]: BigNumber;
|
|
22
|
+
};
|
|
@@ -9,8 +9,7 @@ import { TronParameters } from "../../utils/models/tron-parameters";
|
|
|
9
9
|
import { Abi } from "viem";
|
|
10
10
|
import { ApproveAdapter } from "./models/approve-adapter";
|
|
11
11
|
import { AllowanceInfo } from "./models/common-types";
|
|
12
|
-
|
|
13
|
-
export declare class TronAdapter extends AbstractAdapter<TronWeb, TronWeb, TronBlockchainName> implements ApproveAdapter {
|
|
12
|
+
export declare class TronAdapter extends AbstractAdapter<TronWeb, TronWeb, TronBlockchainName> implements ApproveAdapter<TronTransactionConfig> {
|
|
14
13
|
private readonly multicallAddress;
|
|
15
14
|
needPreswapAction(token: TokenAmount, contractAddress: string, walletAddress: string, amount: string): Promise<boolean>;
|
|
16
15
|
constructor(rpcList: string[], logger?: ICustomLogger);
|
|
@@ -19,18 +18,18 @@ export declare class TronAdapter extends AbstractAdapter<TronWeb, TronWeb, TronB
|
|
|
19
18
|
methodsData: MethodData[];
|
|
20
19
|
}[]): Promise<ContractMulticallResponse<Output>[][]>;
|
|
21
20
|
private multicall;
|
|
22
|
-
|
|
21
|
+
read<T>(contractAddress: string, contractAbi: readonly AbiFragment[], methodName: string, methodArguments?: unknown[]): Promise<T>;
|
|
23
22
|
private multicallContractsMethodsByOne;
|
|
24
23
|
getTokensBalances(userAddress: string, tokensAddresses: string[]): Promise<BigNumber[]>;
|
|
25
24
|
callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token[]>;
|
|
26
25
|
getBalance(userAddress: string, tokenAddress?: string): Promise<BigNumber>;
|
|
27
26
|
getTokenBalance(userAddress: string, tokenAddress: string): Promise<BigNumber>;
|
|
28
27
|
convertTronAddressToHex(address: string): Promise<string>;
|
|
29
|
-
|
|
30
|
-
write(): Promise<string>;
|
|
28
|
+
write(contractAddress: string, methodSignature: string, parameters: TronParameters): Promise<string>;
|
|
31
29
|
static encodeMethodCall(contractAddress: string, contractAbi: Abi, methodName: string, methodArguments: TronParameters, callValue?: string, feeLimit?: number): TronTransactionConfig;
|
|
32
30
|
private static flattenTypesToString;
|
|
33
31
|
needApprove(from: TokenAmount | PriceTokenAmount, walletAddress: string, spender: string): Promise<boolean>;
|
|
34
|
-
encodeApprove(tokenAddress: string, spenderAddress: string, amount?: string):
|
|
32
|
+
encodeApprove(tokenAddress: string, spenderAddress: string, amount?: string): TronTransactionConfig;
|
|
35
33
|
getAllowance(fromTokenAddress: string, walletAddress: string, spender: string): Promise<AllowanceInfo>;
|
|
34
|
+
approve(fromAddress: string, tokenAddress: string, spenderAddress: string, amount?: string): Promise<string>;
|
|
36
35
|
}
|
|
@@ -51,7 +51,7 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
51
51
|
// @ts-ignore
|
|
52
52
|
return contract.aggregateViewCalls(calls).call();
|
|
53
53
|
}
|
|
54
|
-
async
|
|
54
|
+
async read(contractAddress, contractAbi, methodName, methodArguments = []) {
|
|
55
55
|
this.public.setAddress(contractAddress);
|
|
56
56
|
const contract = await this.public.contract(contractAbi, contractAddress);
|
|
57
57
|
const response = await contract[methodName](...methodArguments).call();
|
|
@@ -61,7 +61,7 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
61
61
|
return Promise.all(contractsData.map((contractData) => {
|
|
62
62
|
return Promise.all(contractData.methodsData.map(async (methodData) => {
|
|
63
63
|
try {
|
|
64
|
-
const output = (await this.
|
|
64
|
+
const output = (await this.read(contractData.contractAddress, contractAbi, methodData.methodName, methodData.methodArguments));
|
|
65
65
|
return {
|
|
66
66
|
output,
|
|
67
67
|
success: true
|
|
@@ -161,12 +161,11 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
161
161
|
async convertTronAddressToHex(address) {
|
|
162
162
|
return this.public.address.toHex(address);
|
|
163
163
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
throw new Error('Method is not implemented');
|
|
164
|
+
async write(contractAddress, methodSignature, parameters) {
|
|
165
|
+
const transaction = await this.wallet.transactionBuilder.triggerSmartContract(contractAddress, methodSignature, {}, parameters, this.walletAddress);
|
|
166
|
+
const signedTransaction = await this.wallet.trx.sign(transaction.transaction);
|
|
167
|
+
const receipt = await this.wallet.trx.sendRawTransaction(signedTransaction);
|
|
168
|
+
return receipt.transaction.txID;
|
|
170
169
|
}
|
|
171
170
|
;
|
|
172
171
|
static encodeMethodCall(contractAddress, contractAbi, methodName, methodArguments, callValue, feeLimit) {
|
|
@@ -201,22 +200,25 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
201
200
|
}
|
|
202
201
|
encodeApprove(tokenAddress, spenderAddress, amount) {
|
|
203
202
|
const amountWei = amount ?? new bignumber_js_1.default(2).pow(256).minus(1).toFixed();
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
value: '0'
|
|
209
|
-
};
|
|
203
|
+
return tron_web3_pure_1.TronWeb3Pure.encodeMethodCall(tokenAddress, trc_20_contract_abi_1.TRC20_CONTRACT_ABI, 'approve', [
|
|
204
|
+
{ type: 'address', value: spenderAddress },
|
|
205
|
+
{ type: 'uint256', value: amountWei }
|
|
206
|
+
]);
|
|
210
207
|
}
|
|
211
208
|
async getAllowance(fromTokenAddress, walletAddress, spender) {
|
|
212
|
-
const [contract, decimals] = await Promise.all([
|
|
209
|
+
const [contract, decimals, allowance] = await Promise.all([
|
|
213
210
|
this.public.contract(trc_20_contract_abi_1.TRC20_CONTRACT_ABI, fromTokenAddress),
|
|
214
|
-
this.
|
|
211
|
+
this.read(fromTokenAddress, trc_20_contract_abi_1.TRC20_CONTRACT_ABI, 'decimals'),
|
|
212
|
+
this.read(fromTokenAddress, trc_20_contract_abi_1.TRC20_CONTRACT_ABI, 'allowance', [spender])
|
|
215
213
|
]);
|
|
216
|
-
const
|
|
217
|
-
const allowanceWeiBN = new bignumber_js_1.default(allowance?.toString());
|
|
214
|
+
const allowanceWeiBN = new bignumber_js_1.default(allowance);
|
|
218
215
|
const allowanceNonWei = core_1.Token.fromWei(allowanceWeiBN, Number(decimals));
|
|
219
216
|
return { allowanceNonWei, allowanceWei: allowanceWeiBN };
|
|
220
217
|
}
|
|
218
|
+
async approve(fromAddress, tokenAddress, spenderAddress, amount) {
|
|
219
|
+
const config = this.encodeApprove(tokenAddress, spenderAddress, amount);
|
|
220
|
+
return this.write(config.to, config.signature, config.arguments);
|
|
221
|
+
}
|
|
222
|
+
;
|
|
221
223
|
}
|
|
222
224
|
exports.TronAdapter = TronAdapter;
|
|
@@ -2,7 +2,8 @@ import { SolanaAdapter } from './adapters/solana-adapter';
|
|
|
2
2
|
import { TronAdapter } from './adapters/tron-adapter';
|
|
3
3
|
import { AbstractAdapter } from './adapters/abstract-adapter';
|
|
4
4
|
import { WalletProvider } from "./constants/models/wallet-provider";
|
|
5
|
-
import { BlockchainName, EvmBlockchainName, SolanaBlockchainName, TronBlockchainName, ICustomLogger, HttpClient } from "@cryptorubic/core";
|
|
5
|
+
import { BlockchainName, EvmBlockchainName, SolanaBlockchainName, TronBlockchainName, ICustomLogger, HttpClient, TonBlockchainName } from "@cryptorubic/core";
|
|
6
|
+
import { TonAdapter } from "./adapters/ton-adapter";
|
|
6
7
|
import { TonAdapterConfig } from "./adapters/models/ton-adapter-config";
|
|
7
8
|
import { EvmAdapter } from "./adapters/evm-adapter";
|
|
8
9
|
export declare class BlockchainAdapterFactoryService {
|
|
@@ -17,6 +18,7 @@ export declare class BlockchainAdapterFactoryService {
|
|
|
17
18
|
getAdapter(blockchain: SolanaBlockchainName): SolanaAdapter;
|
|
18
19
|
getAdapter(blockchain: EvmBlockchainName): EvmAdapter;
|
|
19
20
|
getAdapter(blockchain: TronBlockchainName): TronAdapter;
|
|
21
|
+
getAdapter(blockchain: TonBlockchainName): TonAdapter;
|
|
20
22
|
private createStorage;
|
|
21
23
|
private createAdapter;
|
|
22
24
|
connectWallet(walletProvider: WalletProvider): void;
|
|
@@ -92,19 +92,23 @@ class BlockchainAdapterFactoryService {
|
|
|
92
92
|
chain: chain,
|
|
93
93
|
transport: (0, viem_1.custom)(provider)
|
|
94
94
|
});
|
|
95
|
+
adapter.walletAddress = walletProvider[core_1.CHAIN_TYPE.EVM].address;
|
|
95
96
|
});
|
|
96
97
|
}
|
|
97
98
|
if (walletProvider?.[core_1.CHAIN_TYPE.SOLANA]?.core && this.adapterStore?.[core_1.BLOCKCHAIN_NAME.SOLANA]) {
|
|
98
99
|
const store = this.adapterStore[core_1.BLOCKCHAIN_NAME.SOLANA];
|
|
99
100
|
store.wallet = walletProvider[core_1.CHAIN_TYPE.SOLANA].core;
|
|
101
|
+
store.walletAddress = walletProvider[core_1.CHAIN_TYPE.SOLANA].address;
|
|
100
102
|
}
|
|
101
103
|
if (walletProvider?.[core_1.CHAIN_TYPE.TRON]?.core && this.adapterStore?.[core_1.BLOCKCHAIN_NAME.TRON]) {
|
|
102
104
|
const store = this.adapterStore[core_1.BLOCKCHAIN_NAME.TRON];
|
|
103
105
|
store.wallet = walletProvider[core_1.CHAIN_TYPE.TRON].core;
|
|
106
|
+
store.walletAddress = walletProvider[core_1.CHAIN_TYPE.TRON].address;
|
|
104
107
|
}
|
|
105
108
|
if (walletProvider?.[core_1.CHAIN_TYPE.TON]?.core && this.adapterStore?.[core_1.BLOCKCHAIN_NAME.TON]) {
|
|
106
109
|
const store = this.adapterStore[core_1.BLOCKCHAIN_NAME.TON];
|
|
107
110
|
store.wallet = walletProvider[core_1.CHAIN_TYPE.TON].core;
|
|
111
|
+
store.walletAddress = walletProvider[core_1.CHAIN_TYPE.TON].address;
|
|
108
112
|
}
|
|
109
113
|
}
|
|
110
114
|
}
|
|
@@ -69,5 +69,6 @@ exports.viemBlockchainMapping = {
|
|
|
69
69
|
[core_1.BLOCKCHAIN_NAME.XLAYER]: chain_configs_1.viemConfig[core_1.BLOCKCHAIN_NAME.XLAYER], // viem xLayer config without multicall address
|
|
70
70
|
[core_1.BLOCKCHAIN_NAME.BAHAMUT]: chain_configs_1.viemConfig[core_1.BLOCKCHAIN_NAME.BAHAMUT],
|
|
71
71
|
[core_1.BLOCKCHAIN_NAME.BITLAYER]: chain_configs_1.viemConfig[core_1.BLOCKCHAIN_NAME.BITLAYER],
|
|
72
|
-
[core_1.BLOCKCHAIN_NAME.GRAVITY]: chains_1.gravity
|
|
72
|
+
[core_1.BLOCKCHAIN_NAME.GRAVITY]: chains_1.gravity,
|
|
73
|
+
[core_1.BLOCKCHAIN_NAME.SEI]: chains_1.sei
|
|
73
74
|
};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { CommonWeb3Pure } from "./common-web3-pure";
|
|
2
|
+
import { TronTransactionConfig } from "../models/tron-transaction-config";
|
|
2
3
|
import { TronParameters } from "../models/tron-parameters";
|
|
3
4
|
import { TronWeb3PrimitiveType, Web3PrimitiveType } from "../models/primitive-types";
|
|
4
5
|
import { FunctionFragment } from "tronweb/src/types/ABI";
|
|
6
|
+
import { AbiItem } from "../models/abi-types";
|
|
5
7
|
export declare class TronWeb3Pure extends CommonWeb3Pure {
|
|
6
8
|
constructor();
|
|
7
9
|
isAddressCorrect(address: string): Promise<boolean>;
|
|
@@ -15,6 +17,7 @@ export declare class TronWeb3Pure extends CommonWeb3Pure {
|
|
|
15
17
|
* @returns An ABI encoded function call. Means function signature + parameters.
|
|
16
18
|
*/
|
|
17
19
|
static encodeFunctionCall(contractAbi: FunctionFragment[], methodName: string, methodArguments: unknown[]): string;
|
|
20
|
+
static encodeMethodCall(contractAddress: string, contractAbi: AbiItem[], methodName: string, methodArguments: TronParameters, callValue?: string, feeLimit?: number): TronTransactionConfig;
|
|
18
21
|
static encodeMethodSignature(methodSignature: string, parameters: TronParameters): string;
|
|
19
22
|
/**
|
|
20
23
|
* Decodes method result using its JSON interface object and given parameters.
|
|
@@ -33,6 +33,20 @@ class TronWeb3Pure extends common_web3_pure_1.CommonWeb3Pure {
|
|
|
33
33
|
const encodedParameters = tronweb_1.utils.abi.encodeParamsV2ByABI(methodSignature, methodArguments);
|
|
34
34
|
return encodedMethodSignature + encodedParameters.slice(2);
|
|
35
35
|
}
|
|
36
|
+
static encodeMethodCall(contractAddress, contractAbi, methodName, methodArguments, callValue, feeLimit) {
|
|
37
|
+
const methodAbi = contractAbi.find(abiItem => abiItem.name === methodName);
|
|
38
|
+
if (!methodAbi) {
|
|
39
|
+
throw new Error('Encode fail. No method in ABI');
|
|
40
|
+
}
|
|
41
|
+
const signature = `${methodAbi.name}(${this.flattenTypesToString(methodAbi.inputs).join(',')})`;
|
|
42
|
+
return {
|
|
43
|
+
to: contractAddress,
|
|
44
|
+
arguments: methodArguments,
|
|
45
|
+
signature,
|
|
46
|
+
...(callValue && { callValue }),
|
|
47
|
+
...(feeLimit && { feeLimit })
|
|
48
|
+
};
|
|
49
|
+
}
|
|
36
50
|
static encodeMethodSignature(methodSignature, parameters) {
|
|
37
51
|
const encodedMethodSignature = tronweb_1.TronWeb.sha3(methodSignature).slice(0, 10);
|
|
38
52
|
const flattenedParameters = this.flattenParameters(parameters);
|