@cryptorubic/web3 0.0.34 → 0.2.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 +15 -2
- package/src/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/lib/adapter/adapters/abstract-adapter.d.ts +5 -2
- package/src/lib/adapter/adapters/abstract-adapter.js +12 -2
- package/src/lib/adapter/adapters/constants/erc20-token-abi.js +12 -12
- package/src/lib/adapter/adapters/constants/trc-20-contract-abi.js +15 -15
- package/src/lib/adapter/adapters/constants/tron-multicall-abi.js +6 -6
- package/src/lib/adapter/adapters/evm-adapter.d.ts +30 -9
- package/src/lib/adapter/adapters/evm-adapter.js +121 -28
- package/src/lib/adapter/adapters/models/approve-adapter.d.ts +12 -6
- package/src/lib/adapter/adapters/models/evm-adapter-options.d.ts +1 -1
- 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/models/ton-adapter-config.d.ts +2 -2
- package/src/lib/adapter/adapters/solana-adapter.d.ts +3 -3
- package/src/lib/adapter/adapters/solana-adapter.js +1 -1
- package/src/lib/adapter/adapters/ton-adapter.d.ts +6 -6
- package/src/lib/adapter/adapters/ton-adapter.js +8 -8
- package/src/lib/adapter/adapters/tron-adapter.d.ts +13 -14
- package/src/lib/adapter/adapters/tron-adapter.js +54 -39
- package/src/lib/adapter/adapters/utils/ton-api.service.d.ts +3 -3
- package/src/lib/adapter/adapters/utils/ton-api.service.js +4 -3
- package/src/lib/adapter/blockchain-adapter-factory.service.d.ts +6 -4
- package/src/lib/adapter/blockchain-adapter-factory.service.js +24 -13
- package/src/lib/adapter/constants/chain-configs/chain-configs.d.ts +1 -1
- package/src/lib/adapter/constants/chain-configs/chain-configs.js +589 -299
- package/src/lib/adapter/constants/models/wallet-provider.d.ts +3 -3
- package/src/lib/adapter/constants/viem-blockchain-mapping.d.ts +1 -1
- package/src/lib/adapter/constants/viem-blockchain-mapping.js +1 -1
- package/src/lib/utils/constants/changenow-api-blockchain.js +2 -2
- package/src/lib/utils/constants/web3-pure-store.d.ts +2 -2
- package/src/lib/utils/constants/web3-pure-store.js +33 -33
- package/src/lib/utils/models/contract-multicall-response.d.ts +1 -1
- package/src/lib/utils/models/primitive-types.d.ts +1 -1
- package/src/lib/utils/models/tron-transaction-config.d.ts +1 -1
- package/src/lib/utils/web3-pure.d.ts +2 -2
- package/src/lib/utils/web3-pure.js +6 -7
- package/src/lib/utils/web3-types/bitcoin-web3-pure.d.ts +1 -1
- package/src/lib/utils/web3-types/common-web3-pure.js +0 -2
- package/src/lib/utils/web3-types/evm-web3-pure.d.ts +1 -1
- package/src/lib/utils/web3-types/icp-web3-pure.d.ts +1 -1
- package/src/lib/utils/web3-types/icp-web3-pure.js +2 -1
- package/src/lib/utils/web3-types/solana-web3-pure.d.ts +1 -1
- package/src/lib/utils/web3-types/solana-web3-pure.js +1 -1
- package/src/lib/utils/web3-types/ton-web3-pure.d.ts +1 -1
- package/src/lib/utils/web3-types/ton-web3-pure.js +1 -1
- package/src/lib/utils/web3-types/tron-web3-pure.d.ts +7 -4
- package/src/lib/utils/web3-types/tron-web3-pure.js +22 -6
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { TokenAmount } from
|
|
2
|
-
import { AllowanceInfo } from
|
|
3
|
-
|
|
4
|
-
export interface ApproveAdapter {
|
|
1
|
+
import { TokenAmount } from '@cryptorubic/core';
|
|
2
|
+
import { AllowanceInfo } from './common-types';
|
|
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
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { TonApiConfig } from
|
|
2
|
-
import { TonClientParameters } from
|
|
1
|
+
import { TonApiConfig } from './ton-api-config';
|
|
2
|
+
import { TonClientParameters } from '@ton/ton/dist/client/TonClient';
|
|
3
3
|
export interface TonAdapterConfig {
|
|
4
4
|
tonApiConfig: TonApiConfig;
|
|
5
5
|
tonClientConfig: TonClientParameters;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Abi, MulticallResponse, MulticallParameters } from 'viem';
|
|
2
2
|
import { AbstractAdapter } from './abstract-adapter';
|
|
3
|
-
import { Connection } from
|
|
4
|
-
import { ICustomLogger, SolanaBlockchainName, Token } from
|
|
5
|
-
import { EvmTransactionConfig } from
|
|
3
|
+
import { Connection } from '@solana/web3.js';
|
|
4
|
+
import { ICustomLogger, SolanaBlockchainName, Token } from '@cryptorubic/core';
|
|
5
|
+
import { EvmTransactionConfig } from '../../utils/models/evm-transaction-config';
|
|
6
6
|
export declare class SolanaAdapter extends AbstractAdapter<Connection, Connection, SolanaBlockchainName> {
|
|
7
7
|
constructor(rpcList: string[], logger?: ICustomLogger);
|
|
8
8
|
private createPublicClient;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { BlockchainName, HttpClient, ICustomLogger, PriceTokenAmount, Token, TokenAmount, TonBlockchainName } from '@cryptorubic/core';
|
|
2
|
-
import { Address, TonClient } from
|
|
3
|
-
import { TonApiParseAddressResp } from
|
|
4
|
-
import { TonAdapterConfig } from
|
|
5
|
-
import BigNumber from
|
|
6
|
-
import { TonTransactionConfig } from
|
|
7
|
-
import { AbstractAdapter } from
|
|
2
|
+
import { Address, TonClient } from '@ton/ton';
|
|
3
|
+
import { TonApiParseAddressResp } from './models/tonapi-models';
|
|
4
|
+
import { TonAdapterConfig } from './models/ton-adapter-config';
|
|
5
|
+
import BigNumber from 'bignumber.js';
|
|
6
|
+
import { TonTransactionConfig } from '../../utils/models/ton-transaction-config';
|
|
7
|
+
import { AbstractAdapter } from './abstract-adapter';
|
|
8
8
|
export declare class TonAdapter extends AbstractAdapter<TonClient, TonClient, TonBlockchainName> {
|
|
9
9
|
private readonly tonApi;
|
|
10
10
|
readonly tonClient: TonClient;
|
|
@@ -28,7 +28,7 @@ class TonAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
28
28
|
blockchain: core_1.BLOCKCHAIN_NAME.TON,
|
|
29
29
|
decimals: Number(decimals),
|
|
30
30
|
name,
|
|
31
|
-
symbol
|
|
31
|
+
symbol,
|
|
32
32
|
}));
|
|
33
33
|
return necessaryInfo;
|
|
34
34
|
}
|
|
@@ -42,9 +42,7 @@ class TonAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
42
42
|
return this.tonApi.getAllFormatsOfAddress(walletAddress);
|
|
43
43
|
}
|
|
44
44
|
async getWalletAddress(address, contractAddress) {
|
|
45
|
-
const addressResult = await this.tonClient.runMethod(contractAddress, 'get_wallet_address', [
|
|
46
|
-
{ cell: (0, ton_1.beginCell)().storeAddress(address).endCell(), type: 'slice' }
|
|
47
|
-
]);
|
|
45
|
+
const addressResult = await this.tonClient.runMethod(contractAddress, 'get_wallet_address', [{ cell: (0, ton_1.beginCell)().storeAddress(address).endCell(), type: 'slice' }]);
|
|
48
46
|
return addressResult.stack.readAddress();
|
|
49
47
|
}
|
|
50
48
|
async checkEnoughTokenBalance(token, walletAddress, amount = token.tokenAmount) {
|
|
@@ -53,10 +51,12 @@ class TonAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
53
51
|
return balance.gte(amount);
|
|
54
52
|
}
|
|
55
53
|
async getBalance(walletAddress, tokenAddress) {
|
|
56
|
-
const isNative = !tokenAddress ||
|
|
54
|
+
const isNative = !tokenAddress ||
|
|
55
|
+
web3_pure_1.Web3Pure.isNativeAddress(core_1.BLOCKCHAIN_NAME.TON, tokenAddress);
|
|
57
56
|
const tokenBalance = isNative
|
|
58
57
|
? (await this.tonApi.fetchAccountInfo(walletAddress)).balance
|
|
59
|
-
: (await this.tonApi.fetchTokenInfoForWallet(walletAddress, tokenAddress))
|
|
58
|
+
: (await this.tonApi.fetchTokenInfoForWallet(walletAddress, tokenAddress))
|
|
59
|
+
.balance;
|
|
60
60
|
return new bignumber_js_1.default(tokenBalance || 0);
|
|
61
61
|
}
|
|
62
62
|
getTransferEncodedConfig(tokenAddress, walletAddress, receiver, amount) {
|
|
@@ -69,7 +69,7 @@ class TonAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
69
69
|
const transferAmount = BigInt(amount);
|
|
70
70
|
const encodeConfig = {
|
|
71
71
|
address: receiver,
|
|
72
|
-
amount: transferAmount.toString()
|
|
72
|
+
amount: transferAmount.toString(),
|
|
73
73
|
};
|
|
74
74
|
return encodeConfig;
|
|
75
75
|
}
|
|
@@ -92,7 +92,7 @@ class TonAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
92
92
|
const encodeConfig = {
|
|
93
93
|
address: jettonWalletAddress.toRawString(),
|
|
94
94
|
amount: (0, ton_1.toNano)('0.05').toString(),
|
|
95
|
-
payload: body.toBoc().toString('base64')
|
|
95
|
+
payload: body.toBoc().toString('base64'),
|
|
96
96
|
};
|
|
97
97
|
return encodeConfig;
|
|
98
98
|
}
|
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import BigNumber from 'bignumber.js';
|
|
2
2
|
import { AbstractAdapter } from './abstract-adapter';
|
|
3
|
-
import { ContractMulticallResponse, MethodData, TronWeb3PrimitiveType } from
|
|
3
|
+
import { ContractMulticallResponse, MethodData, TronWeb3PrimitiveType } from '@cryptorubic/tron-types';
|
|
4
4
|
import { TronWeb } from 'tronweb';
|
|
5
|
-
import { AbiFragment } from
|
|
6
|
-
import { ICustomLogger, PriceTokenAmount, Token, TokenAmount, TronBlockchainName } from
|
|
7
|
-
import { TronTransactionConfig } from
|
|
8
|
-
import { TronParameters } from
|
|
9
|
-
import { Abi } from
|
|
10
|
-
import { ApproveAdapter } from
|
|
11
|
-
import { AllowanceInfo } from
|
|
12
|
-
|
|
13
|
-
export declare class TronAdapter extends AbstractAdapter<TronWeb, TronWeb, TronBlockchainName> implements ApproveAdapter {
|
|
5
|
+
import { AbiFragment } from 'tronweb/lib/commonjs/types';
|
|
6
|
+
import { ICustomLogger, PriceTokenAmount, Token, TokenAmount, TronBlockchainName } from '@cryptorubic/core';
|
|
7
|
+
import { TronTransactionConfig } from '../../utils/models/tron-transaction-config';
|
|
8
|
+
import { TronParameters } from '../../utils/models/tron-parameters';
|
|
9
|
+
import { Abi } from 'viem';
|
|
10
|
+
import { ApproveAdapter } from './models/approve-adapter';
|
|
11
|
+
import { AllowanceInfo } from './models/common-types';
|
|
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
|
}
|
|
@@ -23,7 +23,7 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
23
23
|
const calls = contractsData.map(({ contractAddress, methodsData }) => {
|
|
24
24
|
return methodsData.map(({ methodName, methodArguments }) => [
|
|
25
25
|
contractAddress,
|
|
26
|
-
tron_web3_pure_1.TronWeb3Pure.encodeFunctionCall(contractAbi, methodName, methodArguments)
|
|
26
|
+
tron_web3_pure_1.TronWeb3Pure.encodeFunctionCall(contractAbi, methodName, methodArguments),
|
|
27
27
|
]);
|
|
28
28
|
});
|
|
29
29
|
try {
|
|
@@ -33,11 +33,16 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
33
33
|
const success = outputs.results[outputIndex];
|
|
34
34
|
const returnData = outputs.returnData[outputIndex];
|
|
35
35
|
outputIndex++;
|
|
36
|
+
const methodOutputAbi = contractAbi.find(
|
|
36
37
|
// @ts-ignore
|
|
37
|
-
|
|
38
|
+
(funcSignature) => funcSignature.name === methodData.methodName
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
).outputs;
|
|
38
41
|
return {
|
|
39
|
-
output: success
|
|
40
|
-
|
|
42
|
+
output: success
|
|
43
|
+
? tron_web3_pure_1.TronWeb3Pure.decodeMethodOutput(methodOutputAbi, returnData)
|
|
44
|
+
: null,
|
|
45
|
+
success,
|
|
41
46
|
};
|
|
42
47
|
}));
|
|
43
48
|
}
|
|
@@ -51,7 +56,7 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
51
56
|
// @ts-ignore
|
|
52
57
|
return contract.aggregateViewCalls(calls).call();
|
|
53
58
|
}
|
|
54
|
-
async
|
|
59
|
+
async read(contractAddress, contractAbi, methodName, methodArguments = []) {
|
|
55
60
|
this.public.setAddress(contractAddress);
|
|
56
61
|
const contract = await this.public.contract(contractAbi, contractAddress);
|
|
57
62
|
const response = await contract[methodName](...methodArguments).call();
|
|
@@ -61,16 +66,16 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
61
66
|
return Promise.all(contractsData.map((contractData) => {
|
|
62
67
|
return Promise.all(contractData.methodsData.map(async (methodData) => {
|
|
63
68
|
try {
|
|
64
|
-
const output = (await this.
|
|
69
|
+
const output = (await this.read(contractData.contractAddress, contractAbi, methodData.methodName, methodData.methodArguments));
|
|
65
70
|
return {
|
|
66
71
|
output,
|
|
67
|
-
success: true
|
|
72
|
+
success: true,
|
|
68
73
|
};
|
|
69
74
|
}
|
|
70
75
|
catch {
|
|
71
76
|
return {
|
|
72
77
|
output: null,
|
|
73
|
-
success: false
|
|
78
|
+
success: false,
|
|
74
79
|
};
|
|
75
80
|
}
|
|
76
81
|
}));
|
|
@@ -88,9 +93,9 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
88
93
|
methodsData: [
|
|
89
94
|
{
|
|
90
95
|
methodArguments: [userAddress],
|
|
91
|
-
methodName: 'balanceOf'
|
|
92
|
-
}
|
|
93
|
-
]
|
|
96
|
+
methodName: 'balanceOf',
|
|
97
|
+
},
|
|
98
|
+
],
|
|
94
99
|
})));
|
|
95
100
|
const results = await Promise.all(promises);
|
|
96
101
|
const tokensBalances = results[0].map((tokenResults) => {
|
|
@@ -110,10 +115,12 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
110
115
|
contractAddress,
|
|
111
116
|
methodsData: tokenFields.map((methodName) => ({
|
|
112
117
|
methodArguments: [],
|
|
113
|
-
methodName
|
|
114
|
-
}))
|
|
118
|
+
methodName,
|
|
119
|
+
})),
|
|
115
120
|
}));
|
|
116
|
-
const results = contractsData.length
|
|
121
|
+
const results = contractsData.length
|
|
122
|
+
? await this.multicallContractsMethods(trc_20_contract_abi_1.TRC20_CONTRACT_ABI, contractsData)
|
|
123
|
+
: [];
|
|
117
124
|
const tokens = results.map((tokenFieldsResults, tokenIndex) => {
|
|
118
125
|
const tokenAddress = tokenAddresses[tokenIndex];
|
|
119
126
|
const possibleToken = tokenFieldsResults.reduce((acc, field, fieldIndex) => {
|
|
@@ -122,7 +129,9 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
122
129
|
}
|
|
123
130
|
return {
|
|
124
131
|
...acc,
|
|
125
|
-
[tokenFields[fieldIndex]]: field.success
|
|
132
|
+
[tokenFields[fieldIndex]]: field.success
|
|
133
|
+
? field.output
|
|
134
|
+
: undefined,
|
|
126
135
|
};
|
|
127
136
|
}, {});
|
|
128
137
|
return new core_1.Token({
|
|
@@ -130,7 +139,7 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
130
139
|
blockchain: core_1.BLOCKCHAIN_NAME.TRON,
|
|
131
140
|
decimals: Number(possibleToken?.decimals),
|
|
132
141
|
name: possibleToken?.name,
|
|
133
|
-
symbol: possibleToken?.symbol
|
|
142
|
+
symbol: possibleToken?.symbol,
|
|
134
143
|
});
|
|
135
144
|
});
|
|
136
145
|
if (nativeTokenIndex === -1) {
|
|
@@ -142,7 +151,8 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
142
151
|
}
|
|
143
152
|
async getBalance(userAddress, tokenAddress) {
|
|
144
153
|
let balance;
|
|
145
|
-
if (tokenAddress &&
|
|
154
|
+
if (tokenAddress &&
|
|
155
|
+
!web3_pure_1.Web3Pure.isNativeAddress(core_1.CHAIN_TYPE.TRON, tokenAddress)) {
|
|
146
156
|
balance = await this.getTokenBalance(userAddress, tokenAddress);
|
|
147
157
|
}
|
|
148
158
|
else {
|
|
@@ -154,21 +164,15 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
154
164
|
async getTokenBalance(userAddress, tokenAddress) {
|
|
155
165
|
this.public.setAddress(userAddress);
|
|
156
166
|
const contract = await this.public.contract(trc_20_contract_abi_1.TRC20_CONTRACT_ABI, tokenAddress);
|
|
157
|
-
|
|
158
|
-
|
|
167
|
+
const balance = await contract
|
|
168
|
+
// @ts-ignore
|
|
169
|
+
.balanceOf(userAddress)
|
|
170
|
+
.call();
|
|
159
171
|
return new bignumber_js_1.default(balance?.toString());
|
|
160
172
|
}
|
|
161
173
|
async convertTronAddressToHex(address) {
|
|
162
174
|
return this.public.address.toHex(address);
|
|
163
175
|
}
|
|
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
176
|
static encodeMethodCall(contractAddress, contractAbi, methodName, methodArguments, callValue, feeLimit) {
|
|
173
177
|
const methodAbi = contractAbi.find((abiItem) => abiItem.type === 'function' && abiItem.name === methodName);
|
|
174
178
|
if (!methodAbi) {
|
|
@@ -180,14 +184,15 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
180
184
|
signature,
|
|
181
185
|
to: contractAddress,
|
|
182
186
|
...(callValue && { callValue }),
|
|
183
|
-
...(feeLimit && { feeLimit })
|
|
187
|
+
...(feeLimit && { feeLimit }),
|
|
184
188
|
};
|
|
185
189
|
}
|
|
186
190
|
static flattenTypesToString(abiInputs) {
|
|
187
191
|
return (abiInputs?.map((abiInput) => {
|
|
188
192
|
if (abiInput.type === 'tuple' || abiInput.type.includes('tuple')) {
|
|
193
|
+
const flattenedComponents = this.flattenTypesToString(
|
|
189
194
|
//@ts-ignore
|
|
190
|
-
|
|
195
|
+
abiInput.components);
|
|
191
196
|
return `(${flattenedComponents.join(',')})`;
|
|
192
197
|
}
|
|
193
198
|
return abiInput.type;
|
|
@@ -201,22 +206,32 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
201
206
|
}
|
|
202
207
|
encodeApprove(tokenAddress, spenderAddress, amount) {
|
|
203
208
|
const amountWei = amount ?? new bignumber_js_1.default(2).pow(256).minus(1).toFixed();
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
value: '0'
|
|
209
|
-
};
|
|
209
|
+
return tron_web3_pure_1.TronWeb3Pure.encodeMethodCall(tokenAddress, trc_20_contract_abi_1.TRC20_CONTRACT_ABI, 'approve', [
|
|
210
|
+
{ type: 'address', value: spenderAddress },
|
|
211
|
+
{ type: 'uint256', value: amountWei },
|
|
212
|
+
]);
|
|
210
213
|
}
|
|
211
214
|
async getAllowance(fromTokenAddress, walletAddress, spender) {
|
|
212
|
-
const [contract, decimals] = await Promise.all([
|
|
215
|
+
const [contract, decimals, allowance] = await Promise.all([
|
|
213
216
|
this.public.contract(trc_20_contract_abi_1.TRC20_CONTRACT_ABI, fromTokenAddress),
|
|
214
|
-
this.
|
|
217
|
+
this.read(fromTokenAddress, trc_20_contract_abi_1.TRC20_CONTRACT_ABI, 'decimals'),
|
|
218
|
+
this.read(fromTokenAddress, trc_20_contract_abi_1.TRC20_CONTRACT_ABI, 'allowance', [
|
|
219
|
+
spender,
|
|
220
|
+
]),
|
|
215
221
|
]);
|
|
216
|
-
const
|
|
217
|
-
const allowanceWeiBN = new bignumber_js_1.default(allowance?.toString());
|
|
222
|
+
const allowanceWeiBN = new bignumber_js_1.default(allowance);
|
|
218
223
|
const allowanceNonWei = core_1.Token.fromWei(allowanceWeiBN, Number(decimals));
|
|
219
224
|
return { allowanceNonWei, allowanceWei: allowanceWeiBN };
|
|
220
225
|
}
|
|
226
|
+
async write(contractAddress, methodSignature, parameters) {
|
|
227
|
+
const transaction = await this.wallet.transactionBuilder.triggerSmartContract(contractAddress, methodSignature, {}, parameters, this.walletAddress);
|
|
228
|
+
const signedTransaction = await this.wallet.trx.sign(transaction.transaction);
|
|
229
|
+
const receipt = await this.wallet.trx.sendRawTransaction(signedTransaction);
|
|
230
|
+
return receipt.transaction.txID;
|
|
231
|
+
}
|
|
232
|
+
async approve(fromAddress, tokenAddress, spenderAddress, amount) {
|
|
233
|
+
const config = this.encodeApprove(tokenAddress, spenderAddress, amount);
|
|
234
|
+
return this.write(config.to, config.signature, config.arguments);
|
|
235
|
+
}
|
|
221
236
|
}
|
|
222
237
|
exports.TronAdapter = TronAdapter;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { TonApiAccountInfoResp, TonApiAllNonNullableTokenInfoForWalletResp, TonApiParseAddressResp, TonApiTokenInfoForWalletResp, TonApiTokenInfoResp, TonApiTxDataByBocResp } from '../models/tonapi-models';
|
|
2
2
|
import { TonCenterBlockInfo } from '../models/toncenter-types';
|
|
3
|
-
import { HttpClient } from
|
|
4
|
-
import { TonApiConfig } from
|
|
5
|
-
import { Web3PrimitiveType } from
|
|
3
|
+
import { HttpClient } from '@cryptorubic/core';
|
|
4
|
+
import { TonApiConfig } from '../models/ton-api-config';
|
|
5
|
+
import { Web3PrimitiveType } from '../../../utils/models/primitive-types';
|
|
6
6
|
export declare class TonApiService {
|
|
7
7
|
private readonly httpClient;
|
|
8
8
|
private readonly tonApiUrl;
|
|
@@ -6,7 +6,8 @@ class TonApiService {
|
|
|
6
6
|
this.httpClient = httpClient;
|
|
7
7
|
this.apiKey = params.tonApiKey;
|
|
8
8
|
this.tonApiUrl = params?.tonApiUrl || 'https://tonapi.io';
|
|
9
|
-
this.tonCenterV3Url =
|
|
9
|
+
this.tonCenterV3Url =
|
|
10
|
+
params.tonCenterV3Url || 'https://toncenter.com/api/v3';
|
|
10
11
|
}
|
|
11
12
|
/**
|
|
12
13
|
* @param walletAddress in any format: raw or friendly
|
|
@@ -97,8 +98,8 @@ class TonApiService {
|
|
|
97
98
|
params: {
|
|
98
99
|
limit: 1,
|
|
99
100
|
offset: 0,
|
|
100
|
-
sort: 'desc'
|
|
101
|
-
}
|
|
101
|
+
sort: 'desc',
|
|
102
|
+
},
|
|
102
103
|
});
|
|
103
104
|
if ('detail' in res) {
|
|
104
105
|
throw new Error(`[TonApiService] Error in fetchWalletSeqno - ${res.detail[0]?.msg}`);
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { SolanaAdapter } from './adapters/solana-adapter';
|
|
2
2
|
import { TronAdapter } from './adapters/tron-adapter';
|
|
3
3
|
import { AbstractAdapter } from './adapters/abstract-adapter';
|
|
4
|
-
import { WalletProvider } from
|
|
5
|
-
import { BlockchainName, EvmBlockchainName, SolanaBlockchainName, TronBlockchainName, ICustomLogger, HttpClient } from
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
4
|
+
import { WalletProvider } from './constants/models/wallet-provider';
|
|
5
|
+
import { BlockchainName, EvmBlockchainName, SolanaBlockchainName, TronBlockchainName, ICustomLogger, HttpClient, TonBlockchainName } from '@cryptorubic/core';
|
|
6
|
+
import { TonAdapter } from './adapters/ton-adapter';
|
|
7
|
+
import { TonAdapterConfig } from './adapters/models/ton-adapter-config';
|
|
8
|
+
import { EvmAdapter } from './adapters/evm-adapter';
|
|
8
9
|
export declare class BlockchainAdapterFactoryService {
|
|
9
10
|
private readonly rpcList;
|
|
10
11
|
private readonly createLogger?;
|
|
@@ -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;
|
|
@@ -23,7 +23,7 @@ class BlockchainAdapterFactoryService {
|
|
|
23
23
|
static async createFactory(rpcList, httpClient, tonParams, createLogger) {
|
|
24
24
|
// @TODO Add default logger
|
|
25
25
|
const loggerFn = createLogger || undefined;
|
|
26
|
-
const resolvedHttpClient = httpClient ?? await Promise.resolve().then(() => require('axios'));
|
|
26
|
+
const resolvedHttpClient = httpClient ?? (await Promise.resolve().then(() => require('axios')));
|
|
27
27
|
return new this(rpcList, createLogger, resolvedHttpClient, tonParams);
|
|
28
28
|
}
|
|
29
29
|
getAdapter(blockchain) {
|
|
@@ -35,8 +35,7 @@ class BlockchainAdapterFactoryService {
|
|
|
35
35
|
return adapter;
|
|
36
36
|
}
|
|
37
37
|
createStorage() {
|
|
38
|
-
const adapters = Object.entries(this.rpcList)
|
|
39
|
-
.map(([blockchain, rpcs]) => {
|
|
38
|
+
const adapters = Object.entries(this.rpcList).map(([blockchain, rpcs]) => {
|
|
40
39
|
const adapter = this.createAdapter(blockchain, rpcs || []);
|
|
41
40
|
return [blockchain, adapter || null];
|
|
42
41
|
if (adapter) {
|
|
@@ -47,10 +46,14 @@ class BlockchainAdapterFactoryService {
|
|
|
47
46
|
return [blockchain, null];
|
|
48
47
|
});
|
|
49
48
|
const tonAdapter = this.createAdapter(core_1.BLOCKCHAIN_NAME.TON, []);
|
|
50
|
-
adapters.push(tonAdapter
|
|
49
|
+
adapters.push(tonAdapter
|
|
50
|
+
? [core_1.BLOCKCHAIN_NAME.TON, tonAdapter]
|
|
51
|
+
: [core_1.BLOCKCHAIN_NAME.TON, null]);
|
|
51
52
|
const activeAdapters = adapters.filter(([blockchain, adapter]) => {
|
|
52
53
|
const adapterExists = Boolean(adapter);
|
|
53
|
-
const message = adapterExists
|
|
54
|
+
const message = adapterExists
|
|
55
|
+
? `Creating adapter for ${blockchain} blockchain`
|
|
56
|
+
: `Failed to create adapter for ${blockchain} blockchain`;
|
|
54
57
|
this.logger?.customLog(message);
|
|
55
58
|
return adapterExists;
|
|
56
59
|
});
|
|
@@ -65,7 +68,7 @@ class BlockchainAdapterFactoryService {
|
|
|
65
68
|
if (viem_blockchain_mapping_1.viemBlockchainMapping?.[blockchain]) {
|
|
66
69
|
return new evm_adapter_1.EvmAdapter({
|
|
67
70
|
blockchain: blockchain,
|
|
68
|
-
rpcList: rpcs
|
|
71
|
+
rpcList: rpcs,
|
|
69
72
|
}, this.createLogger?.(`EVM_ADAPTER_${blockchain}`));
|
|
70
73
|
}
|
|
71
74
|
}
|
|
@@ -76,11 +79,12 @@ class BlockchainAdapterFactoryService {
|
|
|
76
79
|
return new solana_adapter_1.SolanaAdapter(rpcs, this.createLogger?.(`SOLANA_ADAPTER`));
|
|
77
80
|
}
|
|
78
81
|
}
|
|
79
|
-
if (blockchain === core_1.BLOCKCHAIN_NAME.TON &&
|
|
82
|
+
if (blockchain === core_1.BLOCKCHAIN_NAME.TON &&
|
|
83
|
+
this.httpClient &&
|
|
84
|
+
this.tonParams?.tonApiConfig &&
|
|
85
|
+
this.tonParams?.tonClientConfig) {
|
|
80
86
|
return new ton_adapter_1.TonAdapter(this.httpClient, this.tonParams, this.createLogger?.(`TON_ADAPTER`));
|
|
81
87
|
}
|
|
82
|
-
else {
|
|
83
|
-
}
|
|
84
88
|
return null;
|
|
85
89
|
}
|
|
86
90
|
connectWallet(walletProvider) {
|
|
@@ -90,21 +94,28 @@ class BlockchainAdapterFactoryService {
|
|
|
90
94
|
const chain = viem_blockchain_mapping_1.viemBlockchainMapping[blockchain];
|
|
91
95
|
adapter.wallet = (0, viem_1.createWalletClient)({
|
|
92
96
|
chain: chain,
|
|
93
|
-
transport: (0, viem_1.custom)(provider)
|
|
97
|
+
transport: (0, viem_1.custom)(provider),
|
|
94
98
|
});
|
|
99
|
+
adapter.walletAddress = walletProvider[core_1.CHAIN_TYPE.EVM].address;
|
|
95
100
|
});
|
|
96
101
|
}
|
|
97
|
-
if (walletProvider?.[core_1.CHAIN_TYPE.SOLANA]?.core &&
|
|
102
|
+
if (walletProvider?.[core_1.CHAIN_TYPE.SOLANA]?.core &&
|
|
103
|
+
this.adapterStore?.[core_1.BLOCKCHAIN_NAME.SOLANA]) {
|
|
98
104
|
const store = this.adapterStore[core_1.BLOCKCHAIN_NAME.SOLANA];
|
|
99
105
|
store.wallet = walletProvider[core_1.CHAIN_TYPE.SOLANA].core;
|
|
106
|
+
store.walletAddress = walletProvider[core_1.CHAIN_TYPE.SOLANA].address;
|
|
100
107
|
}
|
|
101
|
-
if (walletProvider?.[core_1.CHAIN_TYPE.TRON]?.core &&
|
|
108
|
+
if (walletProvider?.[core_1.CHAIN_TYPE.TRON]?.core &&
|
|
109
|
+
this.adapterStore?.[core_1.BLOCKCHAIN_NAME.TRON]) {
|
|
102
110
|
const store = this.adapterStore[core_1.BLOCKCHAIN_NAME.TRON];
|
|
103
111
|
store.wallet = walletProvider[core_1.CHAIN_TYPE.TRON].core;
|
|
112
|
+
store.walletAddress = walletProvider[core_1.CHAIN_TYPE.TRON].address;
|
|
104
113
|
}
|
|
105
|
-
if (walletProvider?.[core_1.CHAIN_TYPE.TON]?.core &&
|
|
114
|
+
if (walletProvider?.[core_1.CHAIN_TYPE.TON]?.core &&
|
|
115
|
+
this.adapterStore?.[core_1.BLOCKCHAIN_NAME.TON]) {
|
|
106
116
|
const store = this.adapterStore[core_1.BLOCKCHAIN_NAME.TON];
|
|
107
117
|
store.wallet = walletProvider[core_1.CHAIN_TYPE.TON].core;
|
|
118
|
+
store.walletAddress = walletProvider[core_1.CHAIN_TYPE.TON].address;
|
|
108
119
|
}
|
|
109
120
|
}
|
|
110
121
|
}
|