@cryptorubic/web3 1.0.2-alpha.deposits.1 → 1.1.0-alpha-stellar.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cryptorubic/web3",
3
- "version": "1.0.2-alpha.deposits.1",
3
+ "version": "1.1.0-alpha-stellar.1",
4
4
  "dependencies": {
5
5
  "@ethersproject/bignumber": "^5.8.0",
6
6
  "@mysten/sui": "^1.24.0",
@@ -0,0 +1,2 @@
1
+ import { rpc } from '@stellar/stellar-sdk';
2
+ export type StellarClient = rpc.Server;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ import { BasicSendTransactionOptions } from '../../models/basic-transaction-options';
2
+ export interface StellarSendTxParams {
3
+ txOptions: StellarTransactionOptions;
4
+ }
5
+ export interface StellarTransactionOptions extends BasicSendTransactionOptions {
6
+ transaction: string;
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,87 @@
1
+ export interface StellarWallet {
2
+ /**
3
+ * Function used to request the public key from the active account or
4
+ * specific path on a wallet.
5
+ *
6
+ * @param params
7
+ * @param params.path - The path to tell the wallet which position to ask. This is commonly used in hardware wallets.
8
+ * @param params.skipRequestAccess - Freighter separates the access request from the logic of getting the address, if this is set to true we will avoid requesting the access
9
+ *
10
+ * @return Promise<{ address: string }>
11
+ */
12
+ getAddress(params?: {
13
+ path?: string;
14
+ skipRequestAccess?: boolean;
15
+ }): Promise<{
16
+ address: string;
17
+ }>;
18
+ /**
19
+ * A function to request a wallet to sign a built transaction in its XDR mode
20
+ *
21
+ * @param xdr - A Transaction or a FeeBumpTransaction
22
+ * @param opts - Options compatible with https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0043.md#signtransaction
23
+ * @param opts.networkPassphrase - The Stellar network to use when signing
24
+ * @param opts.address - The public key of the account that should be used to sign
25
+ * @param opts.path - This options is added for special cases like Hardware wallets
26
+ *
27
+ * @return Promise<{ signedTxXdr: string; signerAddress: string }>
28
+ */
29
+ signTransaction(xdr: string, opts?: {
30
+ networkPassphrase?: string;
31
+ address?: string;
32
+ path?: string;
33
+ submit?: boolean;
34
+ submitUrl?: string;
35
+ }): Promise<{
36
+ signedTxXdr: string;
37
+ signerAddress?: string;
38
+ }>;
39
+ /**
40
+ * A function to request a wallet to sign an AuthEntry XDR.
41
+ *
42
+ * @param authEntry - An XDR string version of `HashIdPreimageSorobanAuthorization`
43
+ * @param opts - Options compatible with https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0043.md#signauthentry
44
+ * @param opts.networkPassphrase - The Stellar network to use when signing
45
+ * @param opts.address - The public key of the account that should be used to sign
46
+ * @param opts.path - This options is added for special cases like Hardware wallets
47
+ *
48
+ * @return - Promise<{ signedAuthEntry: string; signerAddress: string }>
49
+ */
50
+ signAuthEntry(authEntry: string, opts?: {
51
+ networkPassphrase?: string;
52
+ address?: string;
53
+ path?: string;
54
+ }): Promise<{
55
+ signedAuthEntry: string;
56
+ signerAddress?: string;
57
+ }>;
58
+ /**
59
+ * A function to request a wallet to sign an AuthEntry XDR.
60
+ *
61
+ * @param message - An arbitrary string rather than a transaction or auth entry
62
+ * @param opts - Options compatible with https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0043.md#signmessage
63
+ * @param opts.networkPassphrase - The Stellar network to use when signing
64
+ * @param opts.address - The public key of the account that should be used to sign
65
+ * @param opts.path - This options is added for special cases like Hardware wallets
66
+ *
67
+ * @return - Promise<{ signedMessage: string; signerAddress: string }>
68
+ */
69
+ signMessage(message: string, opts?: {
70
+ networkPassphrase?: string;
71
+ address?: string;
72
+ path?: string;
73
+ }): Promise<{
74
+ signedMessage: string;
75
+ signerAddress?: string;
76
+ }>;
77
+ /**
78
+ * A function to request the current selected network in the wallet. This comes
79
+ * handy when you are dealing with a wallet that doesn't allow you to specify which network to use (For example Lobstr and Rabet)
80
+ *
81
+ * @return - Promise<{ network: string; networkPassphrase: string }>
82
+ */
83
+ getNetwork(): Promise<{
84
+ network: string;
85
+ networkPassphrase: string;
86
+ }>;
87
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,16 @@
1
+ import { BlockchainName, HttpClient, ICustomLogger } from '@cryptorubic/core';
2
+ import { AbstractAdapterSigner } from '../../common/signer/abstract-adapter-signer';
3
+ import { StellarClient } from '../models/stellar-client';
4
+ import { StellarSendTxParams } from '../models/stellar-send-tx-params';
5
+ import { StellarWallet } from '../models/stellar-wallet';
6
+ import { ClientAdaptersFactoryParams } from '../../../models/create-factory-params';
7
+ export declare class StellarAdapterSigner extends AbstractAdapterSigner<StellarWallet, StellarSendTxParams, string> {
8
+ private readonly publicRef;
9
+ get publicClient(): StellarClient;
10
+ constructor(publicRef: {
11
+ public: StellarClient | null;
12
+ }, httpClient: HttpClient, logger?: ICustomLogger, clientParams?: ClientAdaptersFactoryParams);
13
+ write<T>(): Promise<T>;
14
+ getBlockchainName(): Promise<BlockchainName>;
15
+ sendTransaction(params: StellarSendTxParams): Promise<string>;
16
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StellarAdapterSigner = void 0;
4
+ const core_1 = require("@cryptorubic/core");
5
+ const abstract_adapter_signer_1 = require("../../common/signer/abstract-adapter-signer");
6
+ const stellar_sdk_1 = require("@stellar/stellar-sdk");
7
+ class StellarAdapterSigner extends abstract_adapter_signer_1.AbstractAdapterSigner {
8
+ get publicClient() {
9
+ if (!this.publicRef.public) {
10
+ const msg = 'Trying to access undefined publicClient in StellarAdapterSigner';
11
+ this.logger?.customLog(msg);
12
+ throw new Error(msg);
13
+ }
14
+ return this.publicRef.public;
15
+ }
16
+ constructor(publicRef, httpClient, logger, clientParams) {
17
+ super(httpClient, logger, clientParams);
18
+ this.publicRef = publicRef;
19
+ }
20
+ async write() {
21
+ throw new Error('Method write is not supported');
22
+ }
23
+ getBlockchainName() {
24
+ return Promise.resolve(core_1.BLOCKCHAIN_NAME.STELLAR);
25
+ }
26
+ async sendTransaction(params) {
27
+ try {
28
+ const preparedTransaction = await this.publicClient.prepareTransaction(stellar_sdk_1.TransactionBuilder.fromXDR(params.txOptions.transaction, stellar_sdk_1.Networks.PUBLIC));
29
+ const { signedTxXdr } = await this.wallet.signTransaction(preparedTransaction.toXDR(), {
30
+ networkPassphrase: stellar_sdk_1.Networks.PUBLIC,
31
+ address: this.walletAddress
32
+ });
33
+ const resp = await this.publicClient.sendTransaction(stellar_sdk_1.TransactionBuilder.fromXDR(signedTxXdr, stellar_sdk_1.Networks.PUBLIC));
34
+ if (resp.status === 'ERROR') {
35
+ throw new Error('Failed to execute transaction', { cause: resp.errorResult });
36
+ }
37
+ params.txOptions?.onTransactionHash?.(resp.hash);
38
+ return resp.hash;
39
+ }
40
+ catch (err) {
41
+ console.error('Send transaction error', err);
42
+ throw err;
43
+ }
44
+ }
45
+ }
46
+ exports.StellarAdapterSigner = StellarAdapterSigner;
@@ -0,0 +1,36 @@
1
+ import { BlockchainName, HttpClient, ICustomLogger, StellarBlockchainName, Token, TokenAmount } from '@cryptorubic/core';
2
+ import { AbstractAdapter } from '../common/abstract-adapter';
3
+ import { StellarClient } from './models/stellar-client';
4
+ import { StellarWallet } from './models/stellar-wallet';
5
+ import { TxStatus } from '../models/web3-public-models/tx-status';
6
+ import { StellarTransactionConfig } from '../../../utils/models/stellar-transaction-config';
7
+ import { ClientAdaptersFactoryParams } from '../../models/create-factory-params';
8
+ import { StellarAdapterSigner } from './signer/stellar-adapter-signer';
9
+ import BigNumber from 'bignumber.js';
10
+ import { Abi } from 'viem';
11
+ import { Web3PrimitiveType } from '../../../utils/models/primitive-types';
12
+ import { ContractMulticallResponse } from '../models/web3-public-models/contract-multicall-response';
13
+ import { MethodData } from '../models/web3-public-models/method-data';
14
+ export declare class StellarAdapter extends AbstractAdapter<StellarClient, StellarWallet, StellarBlockchainName> {
15
+ private readonly rpcList;
16
+ readonly signer: StellarAdapterSigner;
17
+ constructor(rpcList: string[], httpClient: HttpClient, logger?: ICustomLogger, clientParams?: ClientAdaptersFactoryParams);
18
+ initWeb3Client(): void;
19
+ getTokensBalances(userAddress: string, tokensAddresses: string[]): Promise<BigNumber[]>;
20
+ callForTokensInfo(tokenAddresses: string[] | ReadonlyArray<string>): Promise<Token<BlockchainName>[]>;
21
+ getBlockNumber(): Promise<number | {
22
+ blockNumber: number;
23
+ workchain: number;
24
+ }>;
25
+ getBalance(userAddress: string, tokenAddress?: string): Promise<BigNumber>;
26
+ checkEnoughBalance(token: TokenAmount, walletAddress: string): Promise<boolean>;
27
+ callContractMethod<T extends Web3PrimitiveType = string>(contractAddress: string, contractAbi: Abi, methodName: string, methodArguments?: unknown[], options?: object): Promise<T>;
28
+ multicallContractsMethods<Output extends Web3PrimitiveType>(contractAbi: Abi, contractsData: {
29
+ contractAddress: string;
30
+ methodsData: MethodData[];
31
+ }[]): Promise<ContractMulticallResponse<Output>[][]>;
32
+ private convertClassicTokenAddressToSAC;
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
+ }
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StellarAdapter = void 0;
4
+ const core_1 = require("@cryptorubic/core");
5
+ const abstract_adapter_1 = require("../common/abstract-adapter");
6
+ const tx_status_1 = require("../models/web3-public-models/tx-status");
7
+ const stellar_sdk_1 = require("@stellar/stellar-sdk");
8
+ const stellar_adapter_signer_1 = require("./signer/stellar-adapter-signer");
9
+ const stellar_sdk_2 = require("@stellar/stellar-sdk");
10
+ const bignumber_js_1 = require("bignumber.js");
11
+ class StellarAdapter extends abstract_adapter_1.AbstractAdapter {
12
+ constructor(rpcList, httpClient, logger, clientParams) {
13
+ super(core_1.BLOCKCHAIN_NAME.STELLAR, logger);
14
+ this.rpcList = rpcList;
15
+ this.signer = new stellar_adapter_signer_1.StellarAdapterSigner(this.publicRef, httpClient, logger, clientParams);
16
+ }
17
+ initWeb3Client() {
18
+ this.public = new stellar_sdk_2.rpc.Server(this.rpcList[0], {
19
+ allowHttp: true,
20
+ timeout: 5000
21
+ });
22
+ }
23
+ async getTokensBalances(userAddress, tokensAddresses) {
24
+ const sacTokenAddresses = tokensAddresses.map((address) => address.startsWith('C') ? address : this.convertClassicTokenAddressToSAC(address));
25
+ const balancePromises = sacTokenAddresses.map(async (contractId) => {
26
+ const contract = new stellar_sdk_1.Contract(contractId);
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')).addOperation(operation).setTimeout(30).build();
29
+ const resp = await this.public.simulateTransaction(tx);
30
+ //@ts-ignore
31
+ const balanceScVal = resp.results[0].retval;
32
+ return new bignumber_js_1.default(balanceScVal.toString());
33
+ });
34
+ return Promise.all(balancePromises);
35
+ }
36
+ callForTokensInfo(tokenAddresses) {
37
+ throw new Error('Not Implemented ');
38
+ }
39
+ getBlockNumber() {
40
+ throw new Error('Not Implemented ');
41
+ }
42
+ getBalance(userAddress, tokenAddress) {
43
+ throw new Error('Not Implemented ');
44
+ }
45
+ checkEnoughBalance(token, walletAddress) {
46
+ throw new Error('Not Implemented ');
47
+ }
48
+ callContractMethod(contractAddress, contractAbi, methodName, methodArguments, options) {
49
+ throw new Error('Not Implemented ');
50
+ }
51
+ multicallContractsMethods(contractAbi, contractsData) {
52
+ throw new Error('Not Implemented ');
53
+ }
54
+ convertClassicTokenAddressToSAC(tokenAddress) {
55
+ const [code, address] = tokenAddress === '0x0000000000000000000000000000000000000000' ? [stellar_sdk_1.Asset.native().code, undefined] : tokenAddress.split('-');
56
+ const classicAsset = new stellar_sdk_1.Asset(code, address);
57
+ return classicAsset.contractId(stellar_sdk_1.Networks.PUBLIC);
58
+ }
59
+ async read(address, abi, method, methodArgs = []) {
60
+ // const operation = Operation.invokeHostFunction({
61
+ // })
62
+ // const result = await
63
+ // return result as Promise<T>;
64
+ throw Error('Not implemented');
65
+ }
66
+ async simulateTransaction(config, from, timeout = 15000) {
67
+ try {
68
+ await this.public.simulateTransaction(stellar_sdk_1.TransactionBuilder.fromXDR(config.transaction, stellar_sdk_1.Networks.PUBLIC));
69
+ return '0';
70
+ }
71
+ catch (err) {
72
+ this.logger?.customError('Error while simulating transaction', err);
73
+ throw err;
74
+ }
75
+ }
76
+ async getTransactionStatus(srcTxHash) {
77
+ try {
78
+ const resp = await this.public.pollTransaction(srcTxHash);
79
+ if (resp.status === 'SUCCESS') {
80
+ return tx_status_1.TX_STATUS.SUCCESS;
81
+ }
82
+ if (resp.status === 'FAILED') {
83
+ return tx_status_1.TX_STATUS.FAIL;
84
+ }
85
+ return tx_status_1.TX_STATUS.PENDING;
86
+ }
87
+ catch {
88
+ return tx_status_1.TX_STATUS.PENDING;
89
+ }
90
+ }
91
+ }
92
+ exports.StellarAdapter = StellarAdapter;
@@ -1,6 +1,6 @@
1
1
  import { AbstractAdapter } from './adapters/common/abstract-adapter';
2
2
  import { WalletProviderCore } from './constants/models/wallet-provider';
3
- import { BlockchainName, EvmBlockchainName, SolanaBlockchainName, TronBlockchainName, ICustomLogger, TonBlockchainName, BitcoinBlockchainName, SuiBlockchainName } from '@cryptorubic/core';
3
+ import { BlockchainName, EvmBlockchainName, SolanaBlockchainName, TronBlockchainName, ICustomLogger, TonBlockchainName, BitcoinBlockchainName, SuiBlockchainName, StellarBlockchainName } from '@cryptorubic/core';
4
4
  import { TonAdapter } from './adapters/adapter-ton/ton-adapter';
5
5
  import { EvmAdapter } from './adapters/adapter-evm/evm-adapter';
6
6
  import { BitcoinAdapter } from './adapters/adapter-bitcoin/bitcoin-adapter';
@@ -8,6 +8,7 @@ import { SolanaAdapter } from './adapters/adapter-solana/solana-adapter';
8
8
  import { TronAdapter } from './adapters/adapter-tron/tron-adapter';
9
9
  import { SuiAdapter } from './adapters/adapter-sui/sui-adapter';
10
10
  import { AdapterFactoryParams } from './models/create-factory-params';
11
+ import { StellarAdapter } from './adapters/adapter-stellar/stellar-adapter';
11
12
  export declare class BlockchainAdapterFactoryService {
12
13
  private readonly rpcList;
13
14
  private readonly httpClient;
@@ -25,6 +26,7 @@ export declare class BlockchainAdapterFactoryService {
25
26
  getAdapter(blockchain: TonBlockchainName): TonAdapter;
26
27
  getAdapter(blockchain: BitcoinBlockchainName): BitcoinAdapter;
27
28
  getAdapter(blockchain: SuiBlockchainName): SuiAdapter;
29
+ getAdapter(blockchain: StellarBlockchainName): StellarAdapter;
28
30
  private createStorage;
29
31
  private createAdapter;
30
32
  connectWallet(blockchain: BlockchainName, wallet: WalletProviderCore): void;
@@ -9,6 +9,7 @@ const bitcoin_adapter_1 = require("./adapters/adapter-bitcoin/bitcoin-adapter");
9
9
  const solana_adapter_1 = require("./adapters/adapter-solana/solana-adapter");
10
10
  const tron_adapter_1 = require("./adapters/adapter-tron/tron-adapter");
11
11
  const sui_adapter_1 = require("./adapters/adapter-sui/sui-adapter");
12
+ const stellar_adapter_1 = require("./adapters/adapter-stellar/stellar-adapter");
12
13
  class BlockchainAdapterFactoryService {
13
14
  constructor(rpcList, httpClient, createLogger, tonParams, clientParams) {
14
15
  this.rpcList = rpcList;
@@ -80,6 +81,9 @@ class BlockchainAdapterFactoryService {
80
81
  if (blockchainType === core_1.CHAIN_TYPE.SOLANA) {
81
82
  return new solana_adapter_1.SolanaAdapter(rpcs, this.httpClient, this.createLogger?.(`SOLANA_ADAPTER`), this.clientParams);
82
83
  }
84
+ if (blockchain === core_1.BLOCKCHAIN_NAME.STELLAR) {
85
+ return new stellar_adapter_1.StellarAdapter(rpcs, this.httpClient, this.logger, this.clientParams);
86
+ }
83
87
  }
84
88
  if (blockchain === core_1.BLOCKCHAIN_NAME.TON && this.tonParams?.tonApiConfig && this.tonParams?.tonClientConfig) {
85
89
  return new ton_adapter_1.TonAdapter(this.httpClient, this.tonParams, this.createLogger?.(`TON_ADAPTER`), this.clientParams);
@@ -1,9 +1,9 @@
1
- import { EvmBlockchainName, HttpClient, ICustomLogger, SolanaBlockchainName, SuiBlockchainName, TronBlockchainName } from '@cryptorubic/core';
1
+ import { EvmBlockchainName, HttpClient, ICustomLogger, SolanaBlockchainName, StellarBlockchainName, SuiBlockchainName, TronBlockchainName } from '@cryptorubic/core';
2
2
  import { EnvType } from '../constants/models/env-type';
3
3
  import { TronWebProvider } from '../adapters/adapter-tron/models/tron-web-provider';
4
4
  import { TonAdapterConfig } from '../adapters/models/ton-adapter-config';
5
5
  import { FallbackTransportConfig, HttpTransportConfig } from 'viem';
6
- export type RpcListType = Record<EvmBlockchainName, string[]> & Record<TronBlockchainName, TronWebProvider[]> & Record<SolanaBlockchainName, string[]> & Record<SuiBlockchainName, string[]>;
6
+ export type RpcListType = Record<EvmBlockchainName, string[]> & Record<TronBlockchainName, TronWebProvider[]> & Record<SolanaBlockchainName, string[]> & Record<SuiBlockchainName, string[]> & Record<StellarBlockchainName, string[]>;
7
7
  export interface AdapterFactoryParams {
8
8
  rpcList: RpcListType;
9
9
  httpClient?: HttpClient;
@@ -0,0 +1,3 @@
1
+ export interface StellarTransactionConfig {
2
+ transaction: string;
3
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });