@cryptorubic/web3 0.0.34 → 0.0.35
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 -3
- package/src/lib/adapter/adapters/evm-adapter.js +81 -2
- package/src/lib/adapter/adapters/models/approve-adapter.d.ts +10 -4
- 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 +22 -20
- 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/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, BlockTag, 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,8 @@ 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 { GasPrice } from "./models/gas-price";
|
|
10
|
+
export declare class EvmAdapter extends AbstractAdapter<PublicActions & PublicClient, WalletClient & WalletActions, EvmBlockchainName> implements ApproveAdapter<EvmTransactionConfig> {
|
|
10
11
|
needPreswapAction(token: TokenAmount, contractAddress: string, walletAddress: string, amount: string): Promise<boolean>;
|
|
11
12
|
handlePreswap(contractAddress: string, walletAddress: string, tokenAmount: TokenAmount): Promise<void>;
|
|
12
13
|
constructor(adapterOptions: EvmAdapterOptions, logger?: ICustomLogger);
|
|
@@ -17,7 +18,7 @@ export declare class EvmAdapter extends AbstractAdapter<PublicActions & PublicCl
|
|
|
17
18
|
multicallByContract<T>(contracts: MulticallParameters, allowErrors?: boolean): Promise<MulticallResponse<T>[]>;
|
|
18
19
|
multicallByAddress<T>(address: string, abi: Abi, method: string, methodArgs?: unknown[][], allowErrors?: boolean): Promise<MulticallResponse<T>[]>;
|
|
19
20
|
static encodeMethodCall(contractAddress: string, contractAbi: Abi, method: string, parameters?: unknown[], value?: string): EvmTransactionConfig;
|
|
20
|
-
simulateTransaction(config: EvmTransactionConfig, from: string): Promise<
|
|
21
|
+
simulateTransaction(config: EvmTransactionConfig, from: string): Promise<string>;
|
|
21
22
|
callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token<EvmBlockchainName>[]>;
|
|
22
23
|
getGasPrice(): Promise<string>;
|
|
23
24
|
checkEnoughBalance(token: TokenAmount | PriceTokenAmount, walletAddress: string, amount?: BigNumber): Promise<boolean>;
|
|
@@ -31,4 +32,24 @@ export declare class EvmAdapter extends AbstractAdapter<PublicActions & PublicCl
|
|
|
31
32
|
getAllowance(fromTokenAddress: string, walletAddress: string, spenderAddress: string): Promise<AllowanceInfo>;
|
|
32
33
|
encodeApprove(tokenAddress: string, spenderAddress: string, amount?: string): EvmTransactionConfig;
|
|
33
34
|
needApprove(token: TokenAmount, contractAddress: string, walletAddress: string, amount: string): Promise<boolean>;
|
|
35
|
+
approve(fromAddress: string, tokenAddress: string, spenderAddress: string, amount?: string): Promise<string>;
|
|
36
|
+
/**
|
|
37
|
+
* Calculates EIP-1559 specific gas details.
|
|
38
|
+
* @see {@link https://github.com/ethers-io/ethers.js/blob/master/packages/abstract-provider/src.ts/index.ts#L235}
|
|
39
|
+
* @returns block baseFee, average maxPriorityFeePerGas, and maxFeePerGas.
|
|
40
|
+
*/
|
|
41
|
+
getPriorityFeeGas(): Promise<GasPrice>;
|
|
42
|
+
/**
|
|
43
|
+
* Gets block by block id.
|
|
44
|
+
* @param [blockId] Block id: hash, number ... Default is 'latest'.
|
|
45
|
+
* @returns Block by blockId parameter.
|
|
46
|
+
*/
|
|
47
|
+
getBlock(blockType?: BlockNumber | BlockTag): Promise<GetBlockReturnType>;
|
|
48
|
+
/**
|
|
49
|
+
* Estimates average maxPriorityFeePerGas for EIP-1559 transactions based on last 20 blocks.
|
|
50
|
+
* @see {@link https://docs.alchemy.com/docs/how-to-build-a-gas-fee-estimator-using-eip-1559}
|
|
51
|
+
* @returns Average maxPriorityFeePerGas in wei
|
|
52
|
+
*/
|
|
53
|
+
getMaxPriorityFeePerGas(): Promise<number>;
|
|
54
|
+
getBaseFeePerGas(blockNumber: bigint): Promise<number>;
|
|
34
55
|
}
|
|
@@ -136,8 +136,8 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
136
136
|
to: config.to,
|
|
137
137
|
...(config.value && { value: BigInt(config.value) })
|
|
138
138
|
};
|
|
139
|
-
|
|
140
|
-
|
|
139
|
+
const gasLimitWei = await this.public.estimateGas(callParams);
|
|
140
|
+
return gasLimitWei.toString();
|
|
141
141
|
}
|
|
142
142
|
catch (err) {
|
|
143
143
|
this.logger?.customError('Error while simulating transaction', err);
|
|
@@ -243,5 +243,84 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
243
243
|
return allowanceWei.lt(token.weiAmount);
|
|
244
244
|
}
|
|
245
245
|
;
|
|
246
|
+
async approve(fromAddress, tokenAddress, spenderAddress, amount) {
|
|
247
|
+
const config = this.encodeApprove(tokenAddress, spenderAddress, amount);
|
|
248
|
+
return this.write(fromAddress, config.to, config.value, config.data);
|
|
249
|
+
}
|
|
250
|
+
;
|
|
251
|
+
/**
|
|
252
|
+
* Calculates EIP-1559 specific gas details.
|
|
253
|
+
* @see {@link https://github.com/ethers-io/ethers.js/blob/master/packages/abstract-provider/src.ts/index.ts#L235}
|
|
254
|
+
* @returns block baseFee, average maxPriorityFeePerGas, and maxFeePerGas.
|
|
255
|
+
*/
|
|
256
|
+
async getPriorityFeeGas() {
|
|
257
|
+
const block = await this.getBlock('latest');
|
|
258
|
+
let lastBaseFeePerGas = null;
|
|
259
|
+
let maxFeePerGas = null;
|
|
260
|
+
let maxPriorityFeePerGas = null;
|
|
261
|
+
if (block && block.baseFeePerGas) {
|
|
262
|
+
const baseFeePerGas = await this.getBaseFeePerGas(block.number);
|
|
263
|
+
try {
|
|
264
|
+
lastBaseFeePerGas = baseFeePerGas;
|
|
265
|
+
maxPriorityFeePerGas = await this.getMaxPriorityFeePerGas();
|
|
266
|
+
maxFeePerGas = baseFeePerGas * 2 + maxPriorityFeePerGas;
|
|
267
|
+
}
|
|
268
|
+
catch (err) {
|
|
269
|
+
console.debug(err);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
baseFee: lastBaseFeePerGas?.toFixed(),
|
|
274
|
+
maxFeePerGas: maxFeePerGas?.toFixed(),
|
|
275
|
+
maxPriorityFeePerGas: maxPriorityFeePerGas?.toFixed()
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Gets block by block id.
|
|
280
|
+
* @param [blockId] Block id: hash, number ... Default is 'latest'.
|
|
281
|
+
* @returns Block by blockId parameter.
|
|
282
|
+
*/
|
|
283
|
+
getBlock(blockType = 'latest') {
|
|
284
|
+
if (typeof blockType === 'string') {
|
|
285
|
+
// @TODO FIX TYPE
|
|
286
|
+
return this.public.getBlock({ blockTag: blockType });
|
|
287
|
+
}
|
|
288
|
+
return this.public.getBlock({ blockNumber: blockType });
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Estimates average maxPriorityFeePerGas for EIP-1559 transactions based on last 20 blocks.
|
|
292
|
+
* @see {@link https://docs.alchemy.com/docs/how-to-build-a-gas-fee-estimator-using-eip-1559}
|
|
293
|
+
* @returns Average maxPriorityFeePerGas in wei
|
|
294
|
+
*/
|
|
295
|
+
async getMaxPriorityFeePerGas() {
|
|
296
|
+
const HISTORICAL_BLOCKS = 20;
|
|
297
|
+
const feeHistory = await this.public.getFeeHistory({
|
|
298
|
+
blockCount: HISTORICAL_BLOCKS,
|
|
299
|
+
rewardPercentiles: [50],
|
|
300
|
+
blockTag: 'pending'
|
|
301
|
+
});
|
|
302
|
+
if (!feeHistory?.reward) {
|
|
303
|
+
throw new Error('No fee history found');
|
|
304
|
+
}
|
|
305
|
+
const blocks = feeHistory.reward.map(x => x.map(reward => Number(reward)));
|
|
306
|
+
const rewardSum = blocks
|
|
307
|
+
.map(x => x[0])
|
|
308
|
+
.reduce((acc, v) => acc + (v || 0), 0);
|
|
309
|
+
return Math.round(rewardSum / blocks.length);
|
|
310
|
+
}
|
|
311
|
+
async getBaseFeePerGas(blockNumber) {
|
|
312
|
+
const feeHistory = await this.public.getFeeHistory({
|
|
313
|
+
blockCount: 1,
|
|
314
|
+
rewardPercentiles: [50],
|
|
315
|
+
// blockTag: 'pending',
|
|
316
|
+
blockNumber: blockNumber
|
|
317
|
+
});
|
|
318
|
+
if (!feeHistory?.baseFeePerGas.length) {
|
|
319
|
+
throw new Error('No fee history found');
|
|
320
|
+
}
|
|
321
|
+
const feedSum = feeHistory.baseFeePerGas
|
|
322
|
+
.reduce((acc, v) => acc.plus(v.toString() || 0), new bignumber_js_1.default(0));
|
|
323
|
+
return feedSum.dividedBy(feeHistory.baseFeePerGas.length).toNumber();
|
|
324
|
+
}
|
|
246
325
|
}
|
|
247
326
|
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
|
-
read<T>(): Promise<T>;
|
|
30
|
-
write(): Promise<string>;
|
|
31
28
|
static encodeMethodCall(contractAddress: string, contractAbi: Abi, methodName: string, methodArguments: TronParameters, callValue?: string, feeLimit?: number): TronTransactionConfig;
|
|
32
29
|
private static flattenTypesToString;
|
|
33
30
|
needApprove(from: TokenAmount | PriceTokenAmount, walletAddress: string, spender: string): Promise<boolean>;
|
|
34
|
-
encodeApprove(tokenAddress: string, spenderAddress: string, amount?: string):
|
|
31
|
+
encodeApprove(tokenAddress: string, spenderAddress: string, amount?: string): TronTransactionConfig;
|
|
35
32
|
getAllowance(fromTokenAddress: string, walletAddress: string, spender: string): Promise<AllowanceInfo>;
|
|
33
|
+
write(contractAddress: string, methodSignature: string, parameters: TronParameters): Promise<string>;
|
|
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,14 +161,6 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
161
161
|
async convertTronAddressToHex(address) {
|
|
162
162
|
return this.public.address.toHex(address);
|
|
163
163
|
}
|
|
164
|
-
read() {
|
|
165
|
-
throw new Error('Method is not implemented');
|
|
166
|
-
}
|
|
167
|
-
;
|
|
168
|
-
write() {
|
|
169
|
-
throw new Error('Method is not implemented');
|
|
170
|
-
}
|
|
171
|
-
;
|
|
172
164
|
static encodeMethodCall(contractAddress, contractAbi, methodName, methodArguments, callValue, feeLimit) {
|
|
173
165
|
const methodAbi = contractAbi.find((abiItem) => abiItem.type === 'function' && abiItem.name === methodName);
|
|
174
166
|
if (!methodAbi) {
|
|
@@ -201,22 +193,32 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
201
193
|
}
|
|
202
194
|
encodeApprove(tokenAddress, spenderAddress, amount) {
|
|
203
195
|
const amountWei = amount ?? new bignumber_js_1.default(2).pow(256).minus(1).toFixed();
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
value: '0'
|
|
209
|
-
};
|
|
196
|
+
return tron_web3_pure_1.TronWeb3Pure.encodeMethodCall(tokenAddress, trc_20_contract_abi_1.TRC20_CONTRACT_ABI, 'approve', [
|
|
197
|
+
{ type: 'address', value: spenderAddress },
|
|
198
|
+
{ type: 'uint256', value: amountWei }
|
|
199
|
+
]);
|
|
210
200
|
}
|
|
211
201
|
async getAllowance(fromTokenAddress, walletAddress, spender) {
|
|
212
|
-
const [contract, decimals] = await Promise.all([
|
|
202
|
+
const [contract, decimals, allowance] = await Promise.all([
|
|
213
203
|
this.public.contract(trc_20_contract_abi_1.TRC20_CONTRACT_ABI, fromTokenAddress),
|
|
214
|
-
this.
|
|
204
|
+
this.read(fromTokenAddress, trc_20_contract_abi_1.TRC20_CONTRACT_ABI, 'decimals'),
|
|
205
|
+
this.read(fromTokenAddress, trc_20_contract_abi_1.TRC20_CONTRACT_ABI, 'allowance', [spender])
|
|
215
206
|
]);
|
|
216
|
-
const
|
|
217
|
-
const allowanceWeiBN = new bignumber_js_1.default(allowance?.toString());
|
|
207
|
+
const allowanceWeiBN = new bignumber_js_1.default(allowance);
|
|
218
208
|
const allowanceNonWei = core_1.Token.fromWei(allowanceWeiBN, Number(decimals));
|
|
219
209
|
return { allowanceNonWei, allowanceWei: allowanceWeiBN };
|
|
220
210
|
}
|
|
211
|
+
async write(contractAddress, methodSignature, parameters) {
|
|
212
|
+
const transaction = await this.wallet.transactionBuilder.triggerSmartContract(contractAddress, methodSignature, {}, parameters, this.walletAddress);
|
|
213
|
+
const signedTransaction = await this.wallet.trx.sign(transaction.transaction);
|
|
214
|
+
const receipt = await this.wallet.trx.sendRawTransaction(signedTransaction);
|
|
215
|
+
return receipt.transaction.txID;
|
|
216
|
+
}
|
|
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
|
}
|
|
@@ -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>;
|
|
@@ -27,4 +29,5 @@ export declare class TronWeb3Pure extends CommonWeb3Pure {
|
|
|
27
29
|
private static flattenTypesToArray;
|
|
28
30
|
private static flattenParameters;
|
|
29
31
|
static flattenParameterToPrimitive(parameter: TronWeb3PrimitiveType): Web3PrimitiveType;
|
|
32
|
+
static encodeMethodCall(contractAddress: string, contractAbi: AbiItem[], methodName: string, methodArguments: TronParameters, callValue?: string, feeLimit?: number): TronTransactionConfig;
|
|
30
33
|
}
|
|
@@ -97,5 +97,19 @@ class TronWeb3Pure extends common_web3_pure_1.CommonWeb3Pure {
|
|
|
97
97
|
};
|
|
98
98
|
}, {});
|
|
99
99
|
}
|
|
100
|
+
static encodeMethodCall(contractAddress, contractAbi, methodName, methodArguments, callValue, feeLimit) {
|
|
101
|
+
const methodAbi = contractAbi.find(abiItem => abiItem.name === methodName);
|
|
102
|
+
if (!methodAbi) {
|
|
103
|
+
throw new Error('Encode fail. No method in ABI');
|
|
104
|
+
}
|
|
105
|
+
const signature = `${methodAbi.name}(${this.flattenTypesToString(methodAbi.inputs).join(',')})`;
|
|
106
|
+
return {
|
|
107
|
+
to: contractAddress,
|
|
108
|
+
arguments: methodArguments,
|
|
109
|
+
signature,
|
|
110
|
+
...(callValue && { callValue }),
|
|
111
|
+
...(feeLimit && { feeLimit })
|
|
112
|
+
};
|
|
113
|
+
}
|
|
100
114
|
}
|
|
101
115
|
exports.TronWeb3Pure = TronWeb3Pure;
|