@cryptorubic/web3 1.1.0-alpha-stellar.9 → 1.1.0-alpha-stellar.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/lib/adapter/adapters/adapter-stellar/constants/fake-stellar-wallet.d.ts +1 -0
- package/src/lib/adapter/adapters/adapter-stellar/constants/fake-stellar-wallet.js +4 -0
- package/src/lib/adapter/adapters/adapter-stellar/stellar-adapter.d.ts +18 -10
- package/src/lib/adapter/adapters/adapter-stellar/stellar-adapter.js +75 -56
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const FAKE_STELLAR_WALLET = "GA43DZREOL4ISMF3RE4ETB4DVJVTITWEWTKPGJRZW4EOZCOCNLNOUVVB";
|
|
@@ -3,34 +3,42 @@ import { AbstractAdapter } from '../common/abstract-adapter';
|
|
|
3
3
|
import { StellarClient } from './models/stellar-client';
|
|
4
4
|
import { StellarWallet } from './models/stellar-wallet';
|
|
5
5
|
import { TxStatus } from '../models/web3-public-models/tx-status';
|
|
6
|
-
import { StellarTransactionConfig } from '../../../utils/models/stellar-transaction-config';
|
|
7
6
|
import { ClientAdaptersFactoryParams } from '../../models/create-factory-params';
|
|
8
7
|
import { StellarAdapterSigner } from './signer/stellar-adapter-signer';
|
|
9
8
|
import BigNumber from 'bignumber.js';
|
|
10
|
-
import { Abi } from 'viem';
|
|
11
9
|
import { Web3PrimitiveType } from '../../../utils/models/primitive-types';
|
|
12
|
-
import {
|
|
10
|
+
import { Abi } from 'viem';
|
|
13
11
|
import { MethodData } from '../models/web3-public-models/method-data';
|
|
12
|
+
import { ContractMulticallResponse } from '../models/web3-public-models/contract-multicall-response';
|
|
13
|
+
import { StellarTransactionConfig } from '../../../utils/models/stellar-transaction-config';
|
|
14
14
|
export declare class StellarAdapter extends AbstractAdapter<StellarClient, StellarWallet, StellarBlockchainName> {
|
|
15
15
|
private readonly rpcList;
|
|
16
16
|
readonly signer: StellarAdapterSigner;
|
|
17
17
|
constructor(rpcList: string[], httpClient: HttpClient, logger?: ICustomLogger, clientParams?: ClientAdaptersFactoryParams);
|
|
18
18
|
initWeb3Client(): void;
|
|
19
|
-
getTokensBalances(userAddress: string, tokensAddresses: string[]): Promise<BigNumber[]>;
|
|
20
|
-
callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token<BlockchainName>[]>;
|
|
21
19
|
getBlockNumber(): Promise<number | {
|
|
22
20
|
blockNumber: number;
|
|
23
21
|
workchain: number;
|
|
24
22
|
}>;
|
|
23
|
+
getTokensBalances(userAddress: string, tokensAddresses: string[]): Promise<BigNumber[]>;
|
|
25
24
|
getBalance(userAddress: string, tokenAddress?: string): Promise<BigNumber>;
|
|
25
|
+
callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token<BlockchainName>[]>;
|
|
26
|
+
callForTokenInfo(tokenAddress: string): Promise<Token<BlockchainName>>;
|
|
26
27
|
checkEnoughBalance(token: TokenAmount, walletAddress: string): Promise<boolean>;
|
|
27
|
-
|
|
28
|
+
getTokenContractId(tokenAddress: string): string;
|
|
29
|
+
private convertTokenAddressToAsset;
|
|
30
|
+
read<T>(address: string, method: string, methodArgs?: {
|
|
31
|
+
value: string;
|
|
32
|
+
type: string;
|
|
33
|
+
}[]): Promise<T>;
|
|
34
|
+
simulateTransaction(config: StellarTransactionConfig, from: string, timeout?: number): Promise<string>;
|
|
35
|
+
getTransactionStatus(srcTxHash: string): Promise<TxStatus>;
|
|
36
|
+
callContractMethod<T extends Web3PrimitiveType = string>(_contractAddress: string, _contractAbi: Abi | undefined, _methodName: string, _methodArguments?: {
|
|
37
|
+
value: string;
|
|
38
|
+
type: string;
|
|
39
|
+
}[], _options?: object): Promise<T>;
|
|
28
40
|
multicallContractsMethods<Output extends Web3PrimitiveType>(contractAbi: Abi, contractsData: {
|
|
29
41
|
contractAddress: string;
|
|
30
42
|
methodsData: MethodData[];
|
|
31
43
|
}[]): Promise<ContractMulticallResponse<Output>[][]>;
|
|
32
|
-
private convertTokenAddressToSAC;
|
|
33
|
-
read<T>(address: string, abi: any, method: string, methodArgs?: unknown[]): Promise<T>;
|
|
34
|
-
simulateTransaction(config: StellarTransactionConfig, from: string, timeout?: number): Promise<string>;
|
|
35
|
-
getTransactionStatus(srcTxHash: string): Promise<TxStatus>;
|
|
36
44
|
}
|
|
@@ -8,6 +8,8 @@ const stellar_sdk_1 = require("@stellar/stellar-sdk");
|
|
|
8
8
|
const stellar_adapter_signer_1 = require("./signer/stellar-adapter-signer");
|
|
9
9
|
const stellar_sdk_2 = require("@stellar/stellar-sdk");
|
|
10
10
|
const bignumber_js_1 = require("bignumber.js");
|
|
11
|
+
const web3_pure_1 = require("../../../utils/web3-pure");
|
|
12
|
+
const fake_stellar_wallet_1 = require("./constants/fake-stellar-wallet");
|
|
11
13
|
class StellarAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
12
14
|
constructor(rpcList, httpClient, logger, clientParams) {
|
|
13
15
|
super(core_1.BLOCKCHAIN_NAME.STELLAR, logger);
|
|
@@ -20,75 +22,86 @@ class StellarAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
20
22
|
timeout: 5000
|
|
21
23
|
});
|
|
22
24
|
}
|
|
25
|
+
async getBlockNumber() {
|
|
26
|
+
const { sequence } = await this.public.getLatestLedger();
|
|
27
|
+
return sequence;
|
|
28
|
+
}
|
|
23
29
|
async getTokensBalances(userAddress, tokensAddresses) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
return Promise.all(tokensAddresses.map((address) => this.getBalance(userAddress, address)));
|
|
31
|
+
}
|
|
32
|
+
async getBalance(userAddress, tokenAddress) {
|
|
33
|
+
try {
|
|
34
|
+
if (!tokenAddress || web3_pure_1.Web3Pure.isNativeAddress(core_1.BLOCKCHAIN_NAME.STELLAR, tokenAddress)) {
|
|
35
|
+
const resp = await this.public.getAccountEntry(userAddress);
|
|
36
|
+
return new bignumber_js_1.default(resp.balance().toString());
|
|
37
|
+
}
|
|
38
|
+
const resp = await this.public.getTrustline(userAddress, this.convertTokenAddressToAsset(tokenAddress));
|
|
39
|
+
return new bignumber_js_1.default(resp.balance().toString());
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return new bignumber_js_1.default(0);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async callForTokensInfo(tokenAddresses) {
|
|
46
|
+
return Promise.all(tokenAddresses.map((token) => this.callForTokenInfo(token)));
|
|
47
|
+
}
|
|
48
|
+
async callForTokenInfo(tokenAddress) {
|
|
49
|
+
const metadata = ['name', 'symbol', 'decimals'];
|
|
50
|
+
const contract = new stellar_sdk_1.Contract(this.getTokenContractId(tokenAddress));
|
|
51
|
+
const [name, symbol, decimals] = await Promise.all(metadata.map(async (method) => {
|
|
52
|
+
const tx = new stellar_sdk_1.TransactionBuilder(new stellar_sdk_1.Account(fake_stellar_wallet_1.FAKE_STELLAR_WALLET, '0'), {
|
|
29
53
|
fee: stellar_sdk_1.BASE_FEE,
|
|
30
54
|
networkPassphrase: stellar_sdk_1.Networks.PUBLIC
|
|
31
55
|
})
|
|
32
|
-
.addOperation(
|
|
56
|
+
.addOperation(contract.call(method))
|
|
33
57
|
.setTimeout(30)
|
|
34
58
|
.build();
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (resp.error) {
|
|
39
|
-
return new bignumber_js_1.default(0);
|
|
40
|
-
}
|
|
41
|
-
//@ts-ignore
|
|
42
|
-
const rawRetval = resp.result.retval;
|
|
43
|
-
const balance = (0, stellar_sdk_1.scValToBigInt)(rawRetval);
|
|
44
|
-
return new bignumber_js_1.default(balance.toString());
|
|
45
|
-
}
|
|
46
|
-
catch (err) {
|
|
47
|
-
console.log(err);
|
|
48
|
-
return new bignumber_js_1.default('0');
|
|
59
|
+
const resp = await this.public.simulateTransaction(tx);
|
|
60
|
+
if ('error' in resp) {
|
|
61
|
+
throw new Error(resp.error);
|
|
49
62
|
}
|
|
63
|
+
const rawValue = resp.result.retval;
|
|
64
|
+
return (0, stellar_sdk_1.scValToNative)(rawValue);
|
|
65
|
+
}));
|
|
66
|
+
return new core_1.Token({
|
|
67
|
+
address: tokenAddress,
|
|
68
|
+
blockchain: core_1.BLOCKCHAIN_NAME.STELLAR,
|
|
69
|
+
decimals,
|
|
70
|
+
name,
|
|
71
|
+
symbol
|
|
50
72
|
});
|
|
51
|
-
try {
|
|
52
|
-
const resp = await Promise.all(balancePromises);
|
|
53
|
-
return resp;
|
|
54
|
-
}
|
|
55
|
-
catch (err) {
|
|
56
|
-
console.error(err);
|
|
57
|
-
throw err;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
callForTokensInfo(tokenAddresses) {
|
|
61
|
-
throw new Error('Not Implemented ');
|
|
62
|
-
}
|
|
63
|
-
getBlockNumber() {
|
|
64
|
-
throw new Error('Not Implemented ');
|
|
65
|
-
}
|
|
66
|
-
getBalance(userAddress, tokenAddress) {
|
|
67
|
-
throw new Error('Not Implemented ');
|
|
68
73
|
}
|
|
69
|
-
checkEnoughBalance(token, walletAddress) {
|
|
70
|
-
|
|
74
|
+
async checkEnoughBalance(token, walletAddress) {
|
|
75
|
+
const userBalance = await this.getBalance(walletAddress, token.address);
|
|
76
|
+
return userBalance.gte(token.weiAmount);
|
|
71
77
|
}
|
|
72
|
-
|
|
73
|
-
|
|
78
|
+
getTokenContractId(tokenAddress) {
|
|
79
|
+
if (web3_pure_1.Web3Pure.isNativeAddress(core_1.BLOCKCHAIN_NAME.STELLAR, tokenAddress)) {
|
|
80
|
+
return stellar_sdk_1.Asset.native().contractId(stellar_sdk_1.Networks.PUBLIC);
|
|
81
|
+
}
|
|
82
|
+
return this.convertTokenAddressToAsset(tokenAddress).contractId(stellar_sdk_1.Networks.PUBLIC);
|
|
74
83
|
}
|
|
75
|
-
|
|
76
|
-
|
|
84
|
+
convertTokenAddressToAsset(tokenAddress) {
|
|
85
|
+
const [code, issuer] = tokenAddress.split('-');
|
|
86
|
+
return new stellar_sdk_1.Asset(code, issuer);
|
|
77
87
|
}
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
88
|
+
async read(address, method, methodArgs = []) {
|
|
89
|
+
const contract = new stellar_sdk_1.Contract(address);
|
|
90
|
+
const args = methodArgs.map(({ value, type }) => (0, stellar_sdk_1.nativeToScVal)(value, { type }));
|
|
91
|
+
const operation = contract.call(method, ...args);
|
|
92
|
+
const tx = new stellar_sdk_1.TransactionBuilder(new stellar_sdk_1.Account(fake_stellar_wallet_1.FAKE_STELLAR_WALLET, '0'), {
|
|
93
|
+
fee: stellar_sdk_1.BASE_FEE,
|
|
94
|
+
networkPassphrase: stellar_sdk_1.Networks.PUBLIC
|
|
95
|
+
})
|
|
96
|
+
.addOperation(operation)
|
|
97
|
+
.setTimeout(30)
|
|
98
|
+
.build();
|
|
99
|
+
const resp = await this.public.simulateTransaction(tx);
|
|
100
|
+
if ('error' in resp) {
|
|
101
|
+
throw new Error(resp.error);
|
|
83
102
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
async read(address, abi, method, methodArgs = []) {
|
|
87
|
-
// const operation = Operation.invokeHostFunction({
|
|
88
|
-
// })
|
|
89
|
-
// const result = await
|
|
90
|
-
// return result as Promise<T>;
|
|
91
|
-
throw Error('Not implemented');
|
|
103
|
+
const rawValue = resp.result.retval;
|
|
104
|
+
return (0, stellar_sdk_1.scValToNative)(rawValue);
|
|
92
105
|
}
|
|
93
106
|
async simulateTransaction(config, from, timeout = 15000) {
|
|
94
107
|
try {
|
|
@@ -115,5 +128,11 @@ class StellarAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
115
128
|
return tx_status_1.TX_STATUS.PENDING;
|
|
116
129
|
}
|
|
117
130
|
}
|
|
131
|
+
async callContractMethod(_contractAddress, _contractAbi = [], _methodName, _methodArguments = [], _options) {
|
|
132
|
+
throw new Error('Method call is not supported');
|
|
133
|
+
}
|
|
134
|
+
multicallContractsMethods(contractAbi, contractsData) {
|
|
135
|
+
throw new Error('Method multicall is not supported');
|
|
136
|
+
}
|
|
118
137
|
}
|
|
119
138
|
exports.StellarAdapter = StellarAdapter;
|