@cryptorubic/web3 1.1.0-alpha-stellar.8 → 1.1.0-alpha-stellar.10
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 +15 -7
- package/src/lib/adapter/adapters/adapter-stellar/stellar-adapter.js +79 -54
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const FAKE_STELLAR_WALLET = "GA43DZREOL4ISMF3RE4ETB4DVJVTITWEWTKPGJRZW4EOZCOCNLNOUVVB";
|
|
@@ -16,21 +16,29 @@ export declare class StellarAdapter extends AbstractAdapter<StellarClient, Stell
|
|
|
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,71 +22,88 @@ 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
|
-
const operation = contract.call('balance', (0, stellar_sdk_1.nativeToScVal)(userAddress, { type: 'address' }));
|
|
28
|
-
const tx = new stellar_sdk_1.TransactionBuilder(new stellar_sdk_1.Account(userAddress, '0'), {
|
|
29
|
-
fee: stellar_sdk_1.BASE_FEE,
|
|
30
|
-
networkPassphrase: stellar_sdk_1.Networks.PUBLIC
|
|
31
|
-
})
|
|
32
|
-
.addOperation(operation)
|
|
33
|
-
.setTimeout(30)
|
|
34
|
-
.build();
|
|
35
|
-
try {
|
|
36
|
-
const resp = await this.public.simulateTransaction(tx);
|
|
37
|
-
//@ts-ignore
|
|
38
|
-
const rawRetval = resp.results.retval;
|
|
39
|
-
const balance = (0, stellar_sdk_1.scValToBigInt)(rawRetval);
|
|
40
|
-
return new bignumber_js_1.default(balance.toString());
|
|
41
|
-
}
|
|
42
|
-
catch (err) {
|
|
43
|
-
console.log(err);
|
|
44
|
-
return new bignumber_js_1.default('0');
|
|
45
|
-
}
|
|
46
|
-
});
|
|
30
|
+
return Promise.all(tokensAddresses.map((address) => this.getBalance(userAddress, address)));
|
|
31
|
+
}
|
|
32
|
+
async getBalance(userAddress, tokenAddress) {
|
|
47
33
|
try {
|
|
48
|
-
|
|
49
|
-
|
|
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());
|
|
50
40
|
}
|
|
51
|
-
catch
|
|
52
|
-
|
|
53
|
-
throw err;
|
|
41
|
+
catch {
|
|
42
|
+
return new bignumber_js_1.default(0);
|
|
54
43
|
}
|
|
55
44
|
}
|
|
56
|
-
callForTokensInfo(tokenAddresses) {
|
|
57
|
-
|
|
45
|
+
async callForTokensInfo(tokenAddresses) {
|
|
46
|
+
return Promise.all(tokenAddresses.map((token) => this.callForTokenInfo(token)));
|
|
58
47
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
48
|
+
async callForTokenInfo(tokenAddress) {
|
|
49
|
+
const metadata = ['name', 'symbol', 'decimals'];
|
|
50
|
+
const contract = new stellar_sdk_1.Contract(this.getTokenContractId(tokenAddress));
|
|
51
|
+
const builder = new stellar_sdk_1.TransactionBuilder(new stellar_sdk_1.Account(fake_stellar_wallet_1.FAKE_STELLAR_WALLET, '0'), {
|
|
52
|
+
fee: stellar_sdk_1.BASE_FEE,
|
|
53
|
+
networkPassphrase: stellar_sdk_1.Networks.PUBLIC
|
|
54
|
+
})
|
|
55
|
+
.setTimeout(30)
|
|
56
|
+
.build();
|
|
57
|
+
const [name, symbol, decimals] = await Promise.all(metadata.map(async (value) => {
|
|
58
|
+
builder.operations.pop();
|
|
59
|
+
//@ts-ignore
|
|
60
|
+
builder.operations.push(contract.call(value));
|
|
61
|
+
const resp = await this.public.simulateTransaction(builder);
|
|
62
|
+
if ('error' in resp) {
|
|
63
|
+
throw new Error(resp.error);
|
|
64
|
+
}
|
|
65
|
+
const rawValue = resp.result.retval;
|
|
66
|
+
return (0, stellar_sdk_1.scValToNative)(rawValue);
|
|
67
|
+
}));
|
|
68
|
+
return new core_1.Token({
|
|
69
|
+
address: tokenAddress,
|
|
70
|
+
blockchain: core_1.BLOCKCHAIN_NAME.STELLAR,
|
|
71
|
+
decimals,
|
|
72
|
+
name,
|
|
73
|
+
symbol
|
|
74
|
+
});
|
|
64
75
|
}
|
|
65
|
-
checkEnoughBalance(token, walletAddress) {
|
|
66
|
-
|
|
76
|
+
async checkEnoughBalance(token, walletAddress) {
|
|
77
|
+
const userBalance = await this.getBalance(walletAddress, token.address);
|
|
78
|
+
return userBalance.gte(token.weiAmount);
|
|
67
79
|
}
|
|
68
|
-
|
|
69
|
-
|
|
80
|
+
getTokenContractId(tokenAddress) {
|
|
81
|
+
if (web3_pure_1.Web3Pure.isNativeAddress(core_1.BLOCKCHAIN_NAME.STELLAR, tokenAddress)) {
|
|
82
|
+
return stellar_sdk_1.Asset.native().contractId(stellar_sdk_1.Networks.PUBLIC);
|
|
83
|
+
}
|
|
84
|
+
return this.convertTokenAddressToAsset(tokenAddress).contractId(stellar_sdk_1.Networks.PUBLIC);
|
|
70
85
|
}
|
|
71
|
-
|
|
72
|
-
|
|
86
|
+
convertTokenAddressToAsset(tokenAddress) {
|
|
87
|
+
const [code, issuer] = tokenAddress.split('-');
|
|
88
|
+
return new stellar_sdk_1.Asset(code, issuer);
|
|
73
89
|
}
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
90
|
+
async read(address, method, methodArgs = []) {
|
|
91
|
+
const contract = new stellar_sdk_1.Contract(address);
|
|
92
|
+
const args = methodArgs.map(({ value, type }) => (0, stellar_sdk_1.nativeToScVal)(value, { type }));
|
|
93
|
+
const operation = contract.call(method, ...args);
|
|
94
|
+
const tx = new stellar_sdk_1.TransactionBuilder(new stellar_sdk_1.Account(fake_stellar_wallet_1.FAKE_STELLAR_WALLET, '0'), {
|
|
95
|
+
fee: stellar_sdk_1.BASE_FEE,
|
|
96
|
+
networkPassphrase: stellar_sdk_1.Networks.PUBLIC
|
|
97
|
+
})
|
|
98
|
+
.addOperation(operation)
|
|
99
|
+
.setTimeout(30)
|
|
100
|
+
.build();
|
|
101
|
+
const resp = await this.public.simulateTransaction(tx);
|
|
102
|
+
if ('error' in resp) {
|
|
103
|
+
throw new Error(resp.error);
|
|
79
104
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
async read(address, abi, method, methodArgs = []) {
|
|
83
|
-
// const operation = Operation.invokeHostFunction({
|
|
84
|
-
// })
|
|
85
|
-
// const result = await
|
|
86
|
-
// return result as Promise<T>;
|
|
87
|
-
throw Error('Not implemented');
|
|
105
|
+
const rawValue = resp.result.retval;
|
|
106
|
+
return (0, stellar_sdk_1.scValToNative)(rawValue);
|
|
88
107
|
}
|
|
89
108
|
async simulateTransaction(config, from, timeout = 15000) {
|
|
90
109
|
try {
|
|
@@ -111,5 +130,11 @@ class StellarAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
111
130
|
return tx_status_1.TX_STATUS.PENDING;
|
|
112
131
|
}
|
|
113
132
|
}
|
|
133
|
+
async callContractMethod(_contractAddress, _contractAbi = [], _methodName, _methodArguments = [], _options) {
|
|
134
|
+
throw new Error('Method call is not supported');
|
|
135
|
+
}
|
|
136
|
+
multicallContractsMethods(contractAbi, contractsData) {
|
|
137
|
+
throw new Error('Method multicall is not supported');
|
|
138
|
+
}
|
|
114
139
|
}
|
|
115
140
|
exports.StellarAdapter = StellarAdapter;
|