@cryptorubic/web3 0.6.4 → 0.6.5
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 +29 -28
- package/src/lib/adapter/adapters/abstract-adapter.d.ts +2 -1
- package/src/lib/adapter/adapters/bitcoin-adapter.d.ts +2 -1
- package/src/lib/adapter/adapters/bitcoin-adapter.js +4 -0
- package/src/lib/adapter/adapters/evm-adapter.js +19 -25
- package/src/lib/adapter/adapters/models/solana-web3-types.d.ts +20 -0
- package/src/lib/adapter/adapters/models/solana-web3-types.js +2 -0
- package/src/lib/adapter/adapters/solana-adapter.d.ts +11 -1
- package/src/lib/adapter/adapters/solana-adapter.js +37 -1
- package/src/lib/adapter/adapters/ton-adapter.d.ts +1 -1
- package/src/lib/adapter/adapters/ton-adapter.js +8 -8
- package/src/lib/adapter/adapters/tron-adapter.d.ts +1 -0
- package/src/lib/adapter/adapters/tron-adapter.js +8 -11
- package/src/lib/adapter/adapters/utils/ton-api.service.js +1 -2
- package/src/lib/adapter/blockchain-adapter-factory.service.js +6 -15
- package/src/lib/adapter/constants/chain-configs/chain-configs.js +1 -5
- package/src/lib/utils/web3-types/icp-web3-pure.js +1 -2
- package/src/lib/utils/web3-types/tron-web3-pure.js +1 -3
package/package.json
CHANGED
|
@@ -1,30 +1,31 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
2
|
+
"name": "@cryptorubic/web3",
|
|
3
|
+
"version": "0.6.5",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"tslib": "^2.3.0",
|
|
6
|
+
"bignumber.js": "9.1.2",
|
|
7
|
+
"@cryptorubic/core": "0.6.5",
|
|
8
|
+
"viem": "^2.19.1",
|
|
9
|
+
"web3-utils": "^4.3.1",
|
|
10
|
+
"@ton/ton": "^15.1.0",
|
|
11
|
+
"@solana/web3.js": "1.95.3",
|
|
12
|
+
"@solflare-wallet/utl-sdk": "^1.4.0",
|
|
13
|
+
"@ethersproject/bignumber": "^5.7.0",
|
|
14
|
+
"@cryptorubic/tron-types": "0.6.5",
|
|
15
|
+
"bitcoin-address-validation": "^2.2.3",
|
|
16
|
+
"axios": "0.27.2",
|
|
17
|
+
"crc-32": "^1.2.2",
|
|
18
|
+
"tronweb": "^6.0.0-beta.4",
|
|
19
|
+
"@solana/spl-token": "0.2.0"
|
|
20
|
+
},
|
|
21
|
+
"type": "commonjs",
|
|
22
|
+
"main": "./src/index.js",
|
|
23
|
+
"typings": "./src/index.d.ts",
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"author": {
|
|
28
|
+
"name": "Sleta Dmitrii"
|
|
29
|
+
},
|
|
30
|
+
"types": "./src/index.d.ts"
|
|
30
31
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import BigNumber from 'bignumber.js';
|
|
2
|
-
import { ICustomLogger, Token, TokenAmount, BlockchainName } from '@cryptorubic/core';
|
|
2
|
+
import { ICustomLogger, Token, TokenAmount, BlockchainName, PriceTokenAmount } from '@cryptorubic/core';
|
|
3
3
|
export declare abstract class AbstractAdapter<P, W, B extends BlockchainName> {
|
|
4
4
|
protected readonly logger?: ICustomLogger;
|
|
5
5
|
protected readonly blockchain: B;
|
|
@@ -13,6 +13,7 @@ export declare abstract class AbstractAdapter<P, W, B extends BlockchainName> {
|
|
|
13
13
|
set walletAddress(value: string | null);
|
|
14
14
|
get walletAddress(): string;
|
|
15
15
|
protected constructor(blockchain: B, logger?: ICustomLogger);
|
|
16
|
+
abstract checkEnoughBalance(token: TokenAmount | PriceTokenAmount, walletAddress: string): Promise<boolean>;
|
|
16
17
|
abstract callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token<BlockchainName>[]>;
|
|
17
18
|
needPreswapAction(token: TokenAmount, contractAddress: string, walletAddress: string, amount: string | BigNumber): Promise<boolean>;
|
|
18
19
|
handlePreswap(contractAddress: string, walletAddress: string, tokenAmount: TokenAmount): Promise<void>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BitcoinBlockchainName, BlockchainName, HttpClient, ICustomLogger, Token } from '@cryptorubic/core';
|
|
1
|
+
import { BitcoinBlockchainName, BlockchainName, HttpClient, ICustomLogger, PriceTokenAmount, Token, TokenAmount } from '@cryptorubic/core';
|
|
2
2
|
import BigNumber from 'bignumber.js';
|
|
3
3
|
import { AbstractAdapter } from './abstract-adapter';
|
|
4
4
|
import { BasicTransactionOptions } from './models/basic-transaction-options';
|
|
@@ -7,6 +7,7 @@ export declare class BitcoinAdapter extends AbstractAdapter<BtcWallet, BtcWallet
|
|
|
7
7
|
private readonly httpClient;
|
|
8
8
|
constructor(httpClient: HttpClient, logger?: ICustomLogger);
|
|
9
9
|
getBalance(userAddress: string): Promise<BigNumber>;
|
|
10
|
+
checkEnoughBalance(token: TokenAmount | PriceTokenAmount, walletAddress: string): Promise<boolean>;
|
|
10
11
|
callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token<BlockchainName>[]>;
|
|
11
12
|
read<T>(args: unknown): Promise<T>;
|
|
12
13
|
write(args: unknown): Promise<string>;
|
|
@@ -14,6 +14,10 @@ class BitcoinAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
14
14
|
const response = await this.httpClient.get(url);
|
|
15
15
|
return new bignumber_js_1.default(response.final_balance);
|
|
16
16
|
}
|
|
17
|
+
async checkEnoughBalance(token, walletAddress) {
|
|
18
|
+
const balance = await this.getBalance(walletAddress);
|
|
19
|
+
return balance.gte(token.tokenAmount);
|
|
20
|
+
}
|
|
17
21
|
async callForTokensInfo(tokenAddresses) {
|
|
18
22
|
return [core_1.nativeTokensList[core_1.BLOCKCHAIN_NAME.BITCOIN]];
|
|
19
23
|
}
|
|
@@ -15,17 +15,14 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
15
15
|
return this.needApprove(token, contractAddress, walletAddress, amount);
|
|
16
16
|
}
|
|
17
17
|
async handlePreswap(contractAddress, walletAddress, tokenAmount) {
|
|
18
|
-
const approveAmount = this.blockchain === core_1.BLOCKCHAIN_NAME.GNOSIS ||
|
|
19
|
-
this.blockchain === core_1.BLOCKCHAIN_NAME.CRONOS
|
|
18
|
+
const approveAmount = this.blockchain === core_1.BLOCKCHAIN_NAME.GNOSIS || this.blockchain === core_1.BLOCKCHAIN_NAME.CRONOS
|
|
20
19
|
? tokenAmount.stringWeiAmount
|
|
21
20
|
: tokenAmount.stringTokenAmount;
|
|
22
21
|
const isNative = web3_pure_1.Web3Pure.isNativeAddress(this.blockchain, tokenAmount.address);
|
|
23
22
|
const fromTokenAddress = isNative && tokenAmount.blockchain === core_1.BLOCKCHAIN_NAME.METIS
|
|
24
23
|
? '0xdeaddeaddeaddeaddeaddeaddeaddeaddead0000'
|
|
25
24
|
: tokenAmount.blockchain;
|
|
26
|
-
await this.writeContract(fromTokenAddress, erc20_token_abi_1.erc20TokenAbi, 'approve', '0', [
|
|
27
|
-
approveAmount
|
|
28
|
-
]);
|
|
25
|
+
await this.writeContract(fromTokenAddress, erc20_token_abi_1.erc20TokenAbi, 'approve', '0', [approveAmount]);
|
|
29
26
|
}
|
|
30
27
|
constructor(adapterOptions, logger) {
|
|
31
28
|
super(adapterOptions.blockchain, logger);
|
|
@@ -97,9 +94,7 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
97
94
|
}
|
|
98
95
|
const hasErrors = results.find((result) => {
|
|
99
96
|
if (result?.error || result.status !== 'success') {
|
|
100
|
-
this.logger?.customWarn('Multicall error', result?.error?.data ||
|
|
101
|
-
result?.error?.message ||
|
|
102
|
-
result?.error);
|
|
97
|
+
this.logger?.customWarn('Multicall error', result?.error?.data || result?.error?.message || result?.error);
|
|
103
98
|
return true;
|
|
104
99
|
}
|
|
105
100
|
return false;
|
|
@@ -232,10 +227,7 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
232
227
|
async getAllowance(fromTokenAddress, walletAddress, spenderAddress) {
|
|
233
228
|
const [decimals, allowance] = await Promise.all([
|
|
234
229
|
this.read(fromTokenAddress, erc20_token_abi_1.erc20TokenAbi, 'decimals'),
|
|
235
|
-
this.read(fromTokenAddress, erc20_token_abi_1.erc20TokenAbi, 'allowance', [
|
|
236
|
-
walletAddress,
|
|
237
|
-
spenderAddress
|
|
238
|
-
])
|
|
230
|
+
this.read(fromTokenAddress, erc20_token_abi_1.erc20TokenAbi, 'allowance', [walletAddress, spenderAddress])
|
|
239
231
|
]);
|
|
240
232
|
const allowanceWeiBN = new bignumber_js_1.default(allowance);
|
|
241
233
|
const allowanceNonWei = core_1.Token.fromWei(allowanceWeiBN, Number(decimals));
|
|
@@ -256,9 +248,7 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
256
248
|
return false;
|
|
257
249
|
}
|
|
258
250
|
// Special native address for METIS native coin
|
|
259
|
-
const fromTokenAddress = token.isNative && token.blockchain === core_1.BLOCKCHAIN_NAME.METIS
|
|
260
|
-
? '0xdeaddeaddeaddeaddeaddeaddeaddeaddead0000'
|
|
261
|
-
: token.address;
|
|
251
|
+
const fromTokenAddress = token.isNative && token.blockchain === core_1.BLOCKCHAIN_NAME.METIS ? '0xdeaddeaddeaddeaddeaddeaddeaddeaddead0000' : token.address;
|
|
262
252
|
const { allowanceWei } = await this.getAllowance(fromTokenAddress, walletAddress, contractAddress);
|
|
263
253
|
return allowanceWei.lt(token.weiAmount);
|
|
264
254
|
}
|
|
@@ -267,13 +257,21 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
267
257
|
return this.write(fromAddress, config.to, config.value, config.data);
|
|
268
258
|
}
|
|
269
259
|
async needPermit2Approve(token, permit2Address, walletAddress, spenderAddress) {
|
|
270
|
-
const res = await this.read(permit2Address, uni_v3_permit_abi_1.UNI_V3_PERMIT_2_ABI, 'allowance', [
|
|
260
|
+
const res = await this.read(permit2Address, uni_v3_permit_abi_1.UNI_V3_PERMIT_2_ABI, 'allowance', [
|
|
261
|
+
walletAddress,
|
|
262
|
+
token.address,
|
|
263
|
+
spenderAddress
|
|
264
|
+
]);
|
|
271
265
|
const [amount, expiration, _nonce] = res;
|
|
272
|
-
return
|
|
273
|
-
new bignumber_js_1.default(Date.now()).gt(Number(expiration) * 1000));
|
|
266
|
+
return token.weiAmount.gt(amount) || new bignumber_js_1.default(Date.now()).gt(Number(expiration) * 1000);
|
|
274
267
|
}
|
|
275
268
|
encodePermit2Approve(tokenAddress, spenderAddress, permit2Address, expiration, amount) {
|
|
276
|
-
const tx = EvmAdapter.encodeMethodCall(permit2Address, uni_v3_permit_abi_1.UNI_V3_PERMIT_2_ABI, 'approve', [
|
|
269
|
+
const tx = EvmAdapter.encodeMethodCall(permit2Address, uni_v3_permit_abi_1.UNI_V3_PERMIT_2_ABI, 'approve', [
|
|
270
|
+
tokenAddress,
|
|
271
|
+
spenderAddress,
|
|
272
|
+
amount,
|
|
273
|
+
expiration
|
|
274
|
+
]);
|
|
277
275
|
return {
|
|
278
276
|
data: tx.data,
|
|
279
277
|
value: tx.value,
|
|
@@ -341,9 +339,7 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
341
339
|
throw new Error('No fee history found');
|
|
342
340
|
}
|
|
343
341
|
const blocks = feeHistory.reward.map((x) => x.map((reward) => Number(reward)));
|
|
344
|
-
const rewardSum = blocks
|
|
345
|
-
.map((x) => x[0])
|
|
346
|
-
.reduce((acc, v) => acc + (v || 0), 0);
|
|
342
|
+
const rewardSum = blocks.map((x) => x[0]).reduce((acc, v) => acc + (v || 0), 0);
|
|
347
343
|
return Math.round(rewardSum / blocks.length);
|
|
348
344
|
}
|
|
349
345
|
async getBaseFeePerGas(blockNumber) {
|
|
@@ -361,9 +357,7 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
361
357
|
}
|
|
362
358
|
async handlePossibleError(response) {
|
|
363
359
|
const body = JSON.stringify(response.body);
|
|
364
|
-
const isError = !response.ok ||
|
|
365
|
-
'error' in response ||
|
|
366
|
-
possible_errors_1.possibleErrors.some((error) => body.includes(error));
|
|
360
|
+
const isError = !response.ok || 'error' in response || possible_errors_1.possibleErrors.some((error) => body.includes(error));
|
|
367
361
|
if (isError) {
|
|
368
362
|
this.logger?.customError('Rpc fetch error', {
|
|
369
363
|
blockchain: this.blockchain,
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AccountInfo, PublicKey, RpcResponseAndContext } from '@solana/web3.js';
|
|
2
|
+
/**
|
|
3
|
+
* RPC response value.
|
|
4
|
+
*/
|
|
5
|
+
export type ReturnValue = Promise<{
|
|
6
|
+
result: RpcResponseAndContext<Array<{
|
|
7
|
+
pubkey: PublicKey;
|
|
8
|
+
account: AccountInfo<{
|
|
9
|
+
parsed: {
|
|
10
|
+
info: {
|
|
11
|
+
tokenAmount: {
|
|
12
|
+
amount: number;
|
|
13
|
+
decimals: number;
|
|
14
|
+
};
|
|
15
|
+
mint: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
}>;
|
|
19
|
+
}>>;
|
|
20
|
+
}>;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Abi, MulticallResponse, MulticallParameters } from 'viem';
|
|
2
2
|
import { AbstractAdapter } from './abstract-adapter';
|
|
3
3
|
import { Connection } from '@solana/web3.js';
|
|
4
|
-
import { ICustomLogger, SolanaBlockchainName, Token } from '@cryptorubic/core';
|
|
4
|
+
import { ICustomLogger, PriceTokenAmount, SolanaBlockchainName, Token, TokenAmount } from '@cryptorubic/core';
|
|
5
5
|
import { EvmTransactionConfig } from '../../utils/models/evm-transaction-config';
|
|
6
|
+
import BigNumber from 'bignumber.js';
|
|
7
|
+
export declare const NATIVE_SOLANA_MINT_ADDRESS = "So11111111111111111111111111111111111111111";
|
|
6
8
|
export declare class SolanaAdapter extends AbstractAdapter<Connection, Connection, SolanaBlockchainName> {
|
|
7
9
|
constructor(rpcList: string[], logger?: ICustomLogger);
|
|
8
10
|
private createPublicClient;
|
|
@@ -11,5 +13,13 @@ export declare class SolanaAdapter extends AbstractAdapter<Connection, Connectio
|
|
|
11
13
|
multicallByContract<T>(_contracts: MulticallParameters, _allowErrors?: boolean): Promise<MulticallResponse<T>[]>;
|
|
12
14
|
multicallByAddress<T>(_address: string, _abi: Abi, _method: string, _methodArgs?: unknown[][], _allowErrors?: boolean): Promise<MulticallResponse<T>[]>;
|
|
13
15
|
simulateTransaction(_config: EvmTransactionConfig, _from: string): Promise<void>;
|
|
16
|
+
checkEnoughBalance(token: TokenAmount | PriceTokenAmount, walletAddress: string): Promise<boolean>;
|
|
17
|
+
getBalance(userAddress: string, tokenAddress: string): Promise<BigNumber>;
|
|
18
|
+
/**
|
|
19
|
+
* Gets balance of multiple tokens.
|
|
20
|
+
* @param address Wallet address.
|
|
21
|
+
* @param tokensAddresses Tokens addresses.
|
|
22
|
+
*/
|
|
23
|
+
getTokensBalances(address: string, tokensAddresses: string[]): Promise<BigNumber[]>;
|
|
14
24
|
callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token<SolanaBlockchainName>[]>;
|
|
15
25
|
}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SolanaAdapter = void 0;
|
|
3
|
+
exports.SolanaAdapter = exports.NATIVE_SOLANA_MINT_ADDRESS = void 0;
|
|
4
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
4
5
|
const abstract_adapter_1 = require("./abstract-adapter");
|
|
5
6
|
const web3_js_1 = require("@solana/web3.js");
|
|
6
7
|
const utl_sdk_1 = require("@solflare-wallet/utl-sdk");
|
|
7
8
|
const core_1 = require("@cryptorubic/core");
|
|
8
9
|
const web3_pure_1 = require("../../utils/web3-pure");
|
|
10
|
+
const bignumber_js_1 = require("bignumber.js");
|
|
11
|
+
exports.NATIVE_SOLANA_MINT_ADDRESS = 'So11111111111111111111111111111111111111111';
|
|
9
12
|
class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
10
13
|
constructor(rpcList, logger) {
|
|
11
14
|
super(core_1.BLOCKCHAIN_NAME.SOLANA, logger);
|
|
@@ -33,6 +36,39 @@ class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
33
36
|
throw new Error('Method multicall is not supported');
|
|
34
37
|
}
|
|
35
38
|
async simulateTransaction(_config, _from) { }
|
|
39
|
+
async checkEnoughBalance(token, walletAddress) {
|
|
40
|
+
const balance = await this.getBalance(walletAddress, token.address);
|
|
41
|
+
return balance.gte(token.tokenAmount);
|
|
42
|
+
}
|
|
43
|
+
async getBalance(userAddress, tokenAddress) {
|
|
44
|
+
const isToken = tokenAddress && !web3_pure_1.Web3Pure.isNativeAddress(core_1.CHAIN_TYPE.SOLANA, tokenAddress);
|
|
45
|
+
if (isToken) {
|
|
46
|
+
const balance = await this.getTokensBalances(userAddress, [tokenAddress]);
|
|
47
|
+
return balance?.[0] || new bignumber_js_1.default(0);
|
|
48
|
+
}
|
|
49
|
+
const balance = await this.public.getBalanceAndContext(new web3_js_1.PublicKey(userAddress), 'confirmed');
|
|
50
|
+
return new bignumber_js_1.default(balance.value.toString());
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Gets balance of multiple tokens.
|
|
54
|
+
* @param address Wallet address.
|
|
55
|
+
* @param tokensAddresses Tokens addresses.
|
|
56
|
+
*/
|
|
57
|
+
async getTokensBalances(address, tokensAddresses) {
|
|
58
|
+
const resp = await this.public._rpcRequest('getTokenAccountsByOwner', [address, { programId: spl_token_1.TOKEN_PROGRAM_ID }, { encoding: 'jsonParsed' }]);
|
|
59
|
+
const tokenInfo = new Map(resp.result.value.map((el) => {
|
|
60
|
+
const { mint, tokenAmount } = el.account.data.parsed.info;
|
|
61
|
+
return [mint, tokenAmount.amount];
|
|
62
|
+
}));
|
|
63
|
+
const nativeSolBalance = await this.public.getBalanceAndContext(new web3_js_1.PublicKey(address), 'confirmed');
|
|
64
|
+
return tokensAddresses.map((tokenAddress) => {
|
|
65
|
+
if (tokenAddress === exports.NATIVE_SOLANA_MINT_ADDRESS) {
|
|
66
|
+
return new bignumber_js_1.default(nativeSolBalance.value.toString());
|
|
67
|
+
}
|
|
68
|
+
const tokenWithBalance = tokenInfo.get(tokenAddress);
|
|
69
|
+
return new bignumber_js_1.default(tokenWithBalance || NaN);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
36
72
|
async callForTokensInfo(tokenAddresses) {
|
|
37
73
|
const nativeToken = core_1.nativeTokensList[core_1.BLOCKCHAIN_NAME.SOLANA];
|
|
38
74
|
const nativeTokenIndex = tokenAddresses.findIndex((address) => web3_pure_1.Web3Pure.isNativeAddress(core_1.CHAIN_TYPE.SOLANA, address));
|
|
@@ -14,7 +14,7 @@ export declare class TonAdapter extends AbstractAdapter<TonClient, TonClient, To
|
|
|
14
14
|
write(args: unknown): Promise<string>;
|
|
15
15
|
getAllFormatsOfAddress(walletAddress: string): Promise<TonApiParseAddressResp>;
|
|
16
16
|
getWalletAddress(address: Address, contractAddress: Address): Promise<Address>;
|
|
17
|
-
|
|
17
|
+
checkEnoughBalance(token: TokenAmount | PriceTokenAmount, walletAddress: string): Promise<boolean>;
|
|
18
18
|
getBalance(walletAddress: string, tokenAddress?: string): Promise<BigNumber>;
|
|
19
19
|
getTransferEncodedConfig(tokenAddress: string, walletAddress: string, receiver: string, amount: string): Promise<TonTransactionConfig>;
|
|
20
20
|
private getTransferNativeConfig;
|
|
@@ -42,21 +42,21 @@ 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', [
|
|
45
|
+
const addressResult = await this.tonClient.runMethod(contractAddress, 'get_wallet_address', [
|
|
46
|
+
{ cell: (0, ton_1.beginCell)().storeAddress(address).endCell(), type: 'slice' }
|
|
47
|
+
]);
|
|
46
48
|
return addressResult.stack.readAddress();
|
|
47
49
|
}
|
|
48
|
-
async
|
|
49
|
-
const weiBalance = await this.getBalance(walletAddress, token.
|
|
50
|
+
async checkEnoughBalance(token, walletAddress) {
|
|
51
|
+
const weiBalance = await this.getBalance(walletAddress, token.address);
|
|
50
52
|
const balance = core_1.Token.fromWei(weiBalance, token.decimals);
|
|
51
|
-
return balance.gte(
|
|
53
|
+
return balance.gte(token.tokenAmount);
|
|
52
54
|
}
|
|
53
55
|
async getBalance(walletAddress, tokenAddress) {
|
|
54
|
-
const isNative = !tokenAddress ||
|
|
55
|
-
web3_pure_1.Web3Pure.isNativeAddress(core_1.BLOCKCHAIN_NAME.TON, tokenAddress);
|
|
56
|
+
const isNative = !tokenAddress || web3_pure_1.Web3Pure.isNativeAddress(core_1.BLOCKCHAIN_NAME.TON, tokenAddress);
|
|
56
57
|
const tokenBalance = isNative
|
|
57
58
|
? (await this.tonApi.fetchAccountInfo(walletAddress)).balance
|
|
58
|
-
: (await this.tonApi.fetchTokenInfoForWallet(walletAddress, tokenAddress))
|
|
59
|
-
.balance;
|
|
59
|
+
: (await this.tonApi.fetchTokenInfoForWallet(walletAddress, tokenAddress)).balance;
|
|
60
60
|
return new bignumber_js_1.default(tokenBalance || 0);
|
|
61
61
|
}
|
|
62
62
|
getTransferEncodedConfig(tokenAddress, walletAddress, receiver, amount) {
|
|
@@ -13,6 +13,7 @@ export declare class TronAdapter extends AbstractAdapter<TronWeb, TronWeb, TronB
|
|
|
13
13
|
private readonly multicallAddress;
|
|
14
14
|
needPreswapAction(token: TokenAmount, contractAddress: string, walletAddress: string, amount: string): Promise<boolean>;
|
|
15
15
|
constructor(rpcList: string[], logger?: ICustomLogger);
|
|
16
|
+
checkEnoughBalance(token: TokenAmount | PriceTokenAmount, walletAddress: string): Promise<boolean>;
|
|
16
17
|
multicallContractsMethods<Output extends TronWeb3PrimitiveType>(contractAbi: readonly AbiFragment[], contractsData: {
|
|
17
18
|
contractAddress: string;
|
|
18
19
|
methodsData: MethodData[];
|
|
@@ -19,6 +19,10 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
19
19
|
this.multicallAddress = 'T9ziQU4EBteJzjzMzhHELdhgWFqwzS5Vki';
|
|
20
20
|
this.public = new tronweb_1.TronWeb({ fullHost: rpcList[0] });
|
|
21
21
|
}
|
|
22
|
+
async checkEnoughBalance(token, walletAddress) {
|
|
23
|
+
const balance = await this.getBalance(walletAddress, token.address);
|
|
24
|
+
return balance.gte(token.tokenAmount);
|
|
25
|
+
}
|
|
22
26
|
async multicallContractsMethods(contractAbi, contractsData) {
|
|
23
27
|
const calls = contractsData.map(({ contractAddress, methodsData }) => {
|
|
24
28
|
return methodsData.map(({ methodName, methodArguments }) => [
|
|
@@ -39,9 +43,7 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
39
43
|
// @ts-ignore
|
|
40
44
|
).outputs;
|
|
41
45
|
return {
|
|
42
|
-
output: success
|
|
43
|
-
? tron_web3_pure_1.TronWeb3Pure.decodeMethodOutput(methodOutputAbi, returnData)
|
|
44
|
-
: null,
|
|
46
|
+
output: success ? tron_web3_pure_1.TronWeb3Pure.decodeMethodOutput(methodOutputAbi, returnData) : null,
|
|
45
47
|
success
|
|
46
48
|
};
|
|
47
49
|
}));
|
|
@@ -118,9 +120,7 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
118
120
|
methodName
|
|
119
121
|
}))
|
|
120
122
|
}));
|
|
121
|
-
const results = contractsData.length
|
|
122
|
-
? await this.multicallContractsMethods(trc_20_contract_abi_1.TRC20_CONTRACT_ABI, contractsData)
|
|
123
|
-
: [];
|
|
123
|
+
const results = contractsData.length ? await this.multicallContractsMethods(trc_20_contract_abi_1.TRC20_CONTRACT_ABI, contractsData) : [];
|
|
124
124
|
const tokens = results.map((tokenFieldsResults, tokenIndex) => {
|
|
125
125
|
const tokenAddress = tokenAddresses[tokenIndex];
|
|
126
126
|
const possibleToken = tokenFieldsResults.reduce((acc, field, fieldIndex) => {
|
|
@@ -149,8 +149,7 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
149
149
|
}
|
|
150
150
|
async getBalance(userAddress, tokenAddress) {
|
|
151
151
|
let balance;
|
|
152
|
-
if (tokenAddress &&
|
|
153
|
-
!web3_pure_1.Web3Pure.isNativeAddress(core_1.CHAIN_TYPE.TRON, tokenAddress)) {
|
|
152
|
+
if (tokenAddress && !web3_pure_1.Web3Pure.isNativeAddress(core_1.CHAIN_TYPE.TRON, tokenAddress)) {
|
|
154
153
|
balance = await this.getTokenBalance(userAddress, tokenAddress);
|
|
155
154
|
}
|
|
156
155
|
else {
|
|
@@ -213,9 +212,7 @@ class TronAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
213
212
|
const [contract, decimals, allowance] = await Promise.all([
|
|
214
213
|
this.public.contract(trc_20_contract_abi_1.TRC20_CONTRACT_ABI, fromTokenAddress),
|
|
215
214
|
this.read(fromTokenAddress, trc_20_contract_abi_1.TRC20_CONTRACT_ABI, 'decimals'),
|
|
216
|
-
this.read(fromTokenAddress, trc_20_contract_abi_1.TRC20_CONTRACT_ABI, 'allowance', [
|
|
217
|
-
spender
|
|
218
|
-
])
|
|
215
|
+
this.read(fromTokenAddress, trc_20_contract_abi_1.TRC20_CONTRACT_ABI, 'allowance', [spender])
|
|
219
216
|
]);
|
|
220
217
|
const allowanceWeiBN = new bignumber_js_1.default(allowance);
|
|
221
218
|
const allowanceNonWei = core_1.Token.fromWei(allowanceWeiBN, Number(decimals));
|
|
@@ -6,8 +6,7 @@ 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 =
|
|
10
|
-
params.tonCenterV3Url || 'https://toncenter.com/api/v3';
|
|
9
|
+
this.tonCenterV3Url = params.tonCenterV3Url || 'https://toncenter.com/api/v3';
|
|
11
10
|
}
|
|
12
11
|
/**
|
|
13
12
|
* @param walletAddress in any format: raw or friendly
|
|
@@ -48,9 +48,7 @@ class BlockchainAdapterFactoryService {
|
|
|
48
48
|
});
|
|
49
49
|
const tonAdapter = this.createAdapter(core_1.BLOCKCHAIN_NAME.TON, []);
|
|
50
50
|
const btcAdapter = this.createAdapter(core_1.BLOCKCHAIN_NAME.BITCOIN, []);
|
|
51
|
-
adapters.push(tonAdapter
|
|
52
|
-
? [core_1.BLOCKCHAIN_NAME.TON, tonAdapter]
|
|
53
|
-
: [core_1.BLOCKCHAIN_NAME.TON, null]);
|
|
51
|
+
adapters.push(tonAdapter ? [core_1.BLOCKCHAIN_NAME.TON, tonAdapter] : [core_1.BLOCKCHAIN_NAME.TON, null]);
|
|
54
52
|
const activeAdapters = adapters.filter(([blockchain, adapter]) => {
|
|
55
53
|
const adapterExists = Boolean(adapter);
|
|
56
54
|
const message = adapterExists
|
|
@@ -81,10 +79,7 @@ class BlockchainAdapterFactoryService {
|
|
|
81
79
|
return new solana_adapter_1.SolanaAdapter(rpcs, this.createLogger?.(`SOLANA_ADAPTER`));
|
|
82
80
|
}
|
|
83
81
|
}
|
|
84
|
-
if (blockchain === core_1.BLOCKCHAIN_NAME.TON &&
|
|
85
|
-
this.httpClient &&
|
|
86
|
-
this.tonParams?.tonApiConfig &&
|
|
87
|
-
this.tonParams?.tonClientConfig) {
|
|
82
|
+
if (blockchain === core_1.BLOCKCHAIN_NAME.TON && this.httpClient && this.tonParams?.tonApiConfig && this.tonParams?.tonClientConfig) {
|
|
88
83
|
return new ton_adapter_1.TonAdapter(this.httpClient, this.tonParams, this.createLogger?.(`TON_ADAPTER`));
|
|
89
84
|
}
|
|
90
85
|
if (blockchain === core_1.BLOCKCHAIN_NAME.BITCOIN && this.httpClient) {
|
|
@@ -104,26 +99,22 @@ class BlockchainAdapterFactoryService {
|
|
|
104
99
|
adapter.walletAddress = walletProvider[core_1.CHAIN_TYPE.EVM].address;
|
|
105
100
|
});
|
|
106
101
|
}
|
|
107
|
-
if (walletProvider?.[core_1.CHAIN_TYPE.SOLANA]?.core &&
|
|
108
|
-
this.adapterStore?.[core_1.BLOCKCHAIN_NAME.SOLANA]) {
|
|
102
|
+
if (walletProvider?.[core_1.CHAIN_TYPE.SOLANA]?.core && this.adapterStore?.[core_1.BLOCKCHAIN_NAME.SOLANA]) {
|
|
109
103
|
const store = this.adapterStore[core_1.BLOCKCHAIN_NAME.SOLANA];
|
|
110
104
|
store.wallet = walletProvider[core_1.CHAIN_TYPE.SOLANA].core;
|
|
111
105
|
store.walletAddress = walletProvider[core_1.CHAIN_TYPE.SOLANA].address;
|
|
112
106
|
}
|
|
113
|
-
if (walletProvider?.[core_1.CHAIN_TYPE.TRON]?.core &&
|
|
114
|
-
this.adapterStore?.[core_1.BLOCKCHAIN_NAME.TRON]) {
|
|
107
|
+
if (walletProvider?.[core_1.CHAIN_TYPE.TRON]?.core && this.adapterStore?.[core_1.BLOCKCHAIN_NAME.TRON]) {
|
|
115
108
|
const store = this.adapterStore[core_1.BLOCKCHAIN_NAME.TRON];
|
|
116
109
|
store.wallet = walletProvider[core_1.CHAIN_TYPE.TRON].core;
|
|
117
110
|
store.walletAddress = walletProvider[core_1.CHAIN_TYPE.TRON].address;
|
|
118
111
|
}
|
|
119
|
-
if (walletProvider?.[core_1.CHAIN_TYPE.TON]?.core &&
|
|
120
|
-
this.adapterStore?.[core_1.BLOCKCHAIN_NAME.TON]) {
|
|
112
|
+
if (walletProvider?.[core_1.CHAIN_TYPE.TON]?.core && this.adapterStore?.[core_1.BLOCKCHAIN_NAME.TON]) {
|
|
121
113
|
const store = this.adapterStore[core_1.BLOCKCHAIN_NAME.TON];
|
|
122
114
|
store.wallet = walletProvider[core_1.CHAIN_TYPE.TON].core;
|
|
123
115
|
store.walletAddress = walletProvider[core_1.CHAIN_TYPE.TON].address;
|
|
124
116
|
}
|
|
125
|
-
if (walletProvider?.[core_1.CHAIN_TYPE.BITCOIN]?.core &&
|
|
126
|
-
this.adapterStore?.[core_1.BLOCKCHAIN_NAME.BITCOIN]) {
|
|
117
|
+
if (walletProvider?.[core_1.CHAIN_TYPE.BITCOIN]?.core && this.adapterStore?.[core_1.BLOCKCHAIN_NAME.BITCOIN]) {
|
|
127
118
|
const store = this.adapterStore[core_1.BLOCKCHAIN_NAME.BITCOIN];
|
|
128
119
|
store.wallet = walletProvider[core_1.CHAIN_TYPE.BITCOIN].core;
|
|
129
120
|
store.walletAddress = walletProvider[core_1.CHAIN_TYPE.BITCOIN].address;
|
|
@@ -1288,11 +1288,7 @@ exports.viemConfig = {
|
|
|
1288
1288
|
nativeCurrency: { decimals: 18, name: 'STRK', symbol: 'STRK' },
|
|
1289
1289
|
rpcUrls: {
|
|
1290
1290
|
default: {
|
|
1291
|
-
http: [
|
|
1292
|
-
'https://rpc.starknet.lava.build',
|
|
1293
|
-
'https://starknet-mainnet.public.blastapi.io',
|
|
1294
|
-
'https://data.voyager.online/'
|
|
1295
|
-
]
|
|
1291
|
+
http: ['https://rpc.starknet.lava.build', 'https://starknet-mainnet.public.blastapi.io', 'https://data.voyager.online/']
|
|
1296
1292
|
}
|
|
1297
1293
|
},
|
|
1298
1294
|
testnet: false
|
|
@@ -13,8 +13,7 @@ class IcpWeb3Pure extends common_web3_pure_1.CommonWeb3Pure {
|
|
|
13
13
|
}
|
|
14
14
|
const buffer = this.base16Decode(address);
|
|
15
15
|
const hash = CRC32.buf(buffer.slice(4));
|
|
16
|
-
return
|
|
17
|
-
((buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]));
|
|
16
|
+
return hash === ((buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]);
|
|
18
17
|
}
|
|
19
18
|
base16Decode(str) {
|
|
20
19
|
const buffer = [];
|
|
@@ -83,9 +83,7 @@ class TronWeb3Pure extends common_web3_pure_1.CommonWeb3Pure {
|
|
|
83
83
|
return [types, values];
|
|
84
84
|
}
|
|
85
85
|
static flattenParameterToPrimitive(parameter) {
|
|
86
|
-
if (typeof parameter === 'number' ||
|
|
87
|
-
parameter instanceof bignumber_1.BigNumber ||
|
|
88
|
-
typeof parameter === 'bigint') {
|
|
86
|
+
if (typeof parameter === 'number' || parameter instanceof bignumber_1.BigNumber || typeof parameter === 'bigint') {
|
|
89
87
|
return parameter.toString();
|
|
90
88
|
}
|
|
91
89
|
if (typeof parameter === 'string' || typeof parameter === 'boolean') {
|