@tomo-inc/chains-service 0.0.2

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/README.md +15 -0
  3. package/package.json +38 -0
  4. package/project.json +59 -0
  5. package/src/api/__tests__/config.ts +21 -0
  6. package/src/api/__tests__/token.test.ts +120 -0
  7. package/src/api/__tests__/transaction.test.ts +86 -0
  8. package/src/api/__tests__/user.test.ts +105 -0
  9. package/src/api/__tests__/wallet.test.ts +73 -0
  10. package/src/api/base.ts +52 -0
  11. package/src/api/index.ts +24 -0
  12. package/src/api/network-data.ts +572 -0
  13. package/src/api/network.ts +81 -0
  14. package/src/api/token.ts +182 -0
  15. package/src/api/transaction.ts +59 -0
  16. package/src/api/types/common.ts +35 -0
  17. package/src/api/types/index.ts +13 -0
  18. package/src/api/types/type.ts +283 -0
  19. package/src/api/user.ts +83 -0
  20. package/src/api/utils/index.ts +34 -0
  21. package/src/api/utils/signature.ts +60 -0
  22. package/src/api/wallet.ts +57 -0
  23. package/src/base/network.ts +55 -0
  24. package/src/base/service.ts +33 -0
  25. package/src/base/token.ts +43 -0
  26. package/src/base/transaction.ts +58 -0
  27. package/src/config.ts +21 -0
  28. package/src/dogecoin/base.ts +39 -0
  29. package/src/dogecoin/config.ts +43 -0
  30. package/src/dogecoin/rpc.ts +449 -0
  31. package/src/dogecoin/service.ts +451 -0
  32. package/src/dogecoin/type.ts +29 -0
  33. package/src/dogecoin/utils-doge.ts +105 -0
  34. package/src/dogecoin/utils.ts +601 -0
  35. package/src/evm/rpc.ts +68 -0
  36. package/src/evm/service.ts +403 -0
  37. package/src/evm/utils.ts +92 -0
  38. package/src/index.ts +28 -0
  39. package/src/solana/config.ts +5 -0
  40. package/src/solana/service.ts +312 -0
  41. package/src/solana/types.ts +91 -0
  42. package/src/solana/utils.ts +635 -0
  43. package/src/types/account.ts +58 -0
  44. package/src/types/dapp.ts +7 -0
  45. package/src/types/gas.ts +53 -0
  46. package/src/types/index.ts +81 -0
  47. package/src/types/network.ts +66 -0
  48. package/src/types/tx.ts +181 -0
  49. package/src/types/wallet.ts +49 -0
  50. package/src/wallet.ts +96 -0
  51. package/tsconfig.json +14 -0
  52. package/tsup.config.ts +18 -0
@@ -0,0 +1,53 @@
1
+ export type EVMTransaction = {
2
+ to: `0x${string}`;
3
+ value?: string;
4
+ data?: `${string}`;
5
+ nonce?: number;
6
+ gas?: string;
7
+ gasPrice?: string;
8
+ maxFeePerGas?: string;
9
+ maxPriorityFeePerGas?: string;
10
+ };
11
+
12
+ export interface QueryGasParams {
13
+ chainIndex: number | string;
14
+ callData: string;
15
+ gasLimitParam: {
16
+ from: string;
17
+ to: string;
18
+ value: `0x${string}`;
19
+ };
20
+ addressList?: string[];
21
+ }
22
+
23
+ export interface QueryGasResponse {
24
+ success: boolean;
25
+ message?: string;
26
+ fees?: {
27
+ low: string | number;
28
+ medium: string | number;
29
+ high: string | number;
30
+ };
31
+ gasFee?: string | number;
32
+ rentInfo?: {
33
+ result?: {
34
+ minTransferAmount: string;
35
+ createSplTokenFee?: string;
36
+ };
37
+ } | null;
38
+ baseFee?: string;
39
+ gasLimit?: string;
40
+ priorityFee?: {
41
+ low: string;
42
+ medium: string;
43
+ high: string;
44
+ };
45
+ }
46
+
47
+ export interface BtcGasFee {
48
+ fastestFee: number;
49
+ halfHourFee: number;
50
+ hourFee: number;
51
+ economyFee: number;
52
+ minimumFee: number;
53
+ }
@@ -0,0 +1,81 @@
1
+ import { CubeStage, TomoStage } from "@tomo-inc/wallet-utils";
2
+
3
+ export * from "./account";
4
+ export * from "./dapp";
5
+ export * from "./gas";
6
+ export * from "./network";
7
+ export * from "./tx";
8
+ export * from "./wallet";
9
+
10
+ export interface TomoAppInfo {
11
+ tomoStage: TomoStage;
12
+ tomoClientId: string;
13
+ apiKey: string;
14
+ apiSecret: string;
15
+ salt: string;
16
+ jwtToken?: string;
17
+ }
18
+
19
+ export type TokenInfo = any;
20
+ export type AccountToken = any;
21
+
22
+ export interface ServiceRes {
23
+ success: boolean;
24
+ method?: string;
25
+ data: any;
26
+ }
27
+
28
+ export enum SubmitParamsType {
29
+ SWAP = 1,
30
+ BRIDGE = 2,
31
+ TRANSFER = 3,
32
+ }
33
+
34
+ export interface CenterSubmitParams {
35
+ platform?: number;
36
+ type?: SubmitParamsType;
37
+ userId?: string;
38
+ fromTokenPrice?: number;
39
+ toTokenPrice?: number;
40
+ source?: any;
41
+ slippage?: any;
42
+ fromAddress?: string;
43
+ toAddress?: string;
44
+ fromChainIndex: string;
45
+ fromTokenAddress?: string;
46
+ fromAmount?: string;
47
+ toChainIndex: string;
48
+ toTokenAddress?: string;
49
+ toAmount?: any;
50
+ sourceDex?: string;
51
+ estimatedGas?: string;
52
+ failReason?: string;
53
+ tx?: string;
54
+ callData: string;
55
+ extParams?: any;
56
+ }
57
+
58
+ export interface SendRes {
59
+ success: boolean;
60
+ data: {
61
+ orderId: string;
62
+ hash: string;
63
+ };
64
+ }
65
+
66
+ export interface AddressItem {
67
+ address: string;
68
+ subType: string;
69
+ publicKey: string;
70
+ }
71
+
72
+ export type SendTransactionConfig = {
73
+ waitForHash?: boolean;
74
+ };
75
+
76
+ export interface IConfig {
77
+ cubeStage: CubeStage;
78
+ tomoSdkClientId: string;
79
+ cubeSalt: string;
80
+ cubeOrgId: string;
81
+ }
@@ -0,0 +1,66 @@
1
+ import { ChainTypes } from "@tomo-inc/wallet-utils";
2
+
3
+ export type NativeCurrency = {
4
+ symbol: string;
5
+ decimals: number;
6
+ name: string;
7
+ logoURI?: string;
8
+ };
9
+
10
+ export type RpcInfo = {
11
+ name: string;
12
+ url: string;
13
+ };
14
+
15
+ export type EVMNetwork = `${number}`; // "1", "56"
16
+ export type BTCNetwork = "mainnet" | "testnet" | "signet" | "regtest";
17
+ export type SolNetwork = "mainnet" | "testnet" | "devnet";
18
+ export type SuiNetwork = "mainnet" | "testnet" | "devnet";
19
+ export type DogeNetwork = "mainnet" | "testnet" | "devnet";
20
+ export type TonNetwork = "mainnet" | "testnet" | "devnet";
21
+ export type TronNetwork = "mainnet" | "testnet" | "devnet";
22
+
23
+ export type ChainTypeIds =
24
+ | `${ChainTypes.EVM}:${EVMNetwork}`
25
+ | `${ChainTypes.BTC}:${BTCNetwork}`
26
+ | `${ChainTypes.SOL}:${SolNetwork}`
27
+ | `${ChainTypes.SUI}:${SuiNetwork}`
28
+ | `${ChainTypes.DOGE}:${DogeNetwork}`
29
+ | `${ChainTypes.TON}:${TonNetwork}`
30
+ | `${ChainTypes.TRON}:${TronNetwork}`;
31
+
32
+ export interface RemoteChainInfo2 {
33
+ chainId: number;
34
+ chainIndex: number;
35
+ name: string;
36
+ chainName: string;
37
+ nativeCurrencyName: string;
38
+ nativeCurrencySymbol: string;
39
+ nativeCurrencyDecimals: number;
40
+ rpcUrls?: string[];
41
+ blockExplorerUrl?: string;
42
+ platformType: string;
43
+ isTestnet: boolean;
44
+ icon: string;
45
+ supportSwap: boolean;
46
+ supportGift: boolean;
47
+ supportHistory: boolean;
48
+ }
49
+
50
+ export type ChainID = string;
51
+ export interface INetwork {
52
+ id?: string;
53
+ type?: ChainTypeIds;
54
+ rpcInfos?: RpcInfo[]; // update field
55
+ rpcUrls?: string[];
56
+ explorerUrls?: string[]; // update field
57
+ platformType: string;
58
+ chainId: string | number;
59
+ sortIndex?: number;
60
+ nativeCurrency?: NativeCurrency; // update field
61
+ name: string;
62
+ icon: string;
63
+ // --- remote data ---
64
+ chainIndex?: number; // remote chain index
65
+ info?: Partial<RemoteChainInfo2>;
66
+ }
@@ -0,0 +1,181 @@
1
+ export interface IUtxo {
2
+ txid: string;
3
+ vout: number;
4
+ status: {
5
+ confirmed: boolean;
6
+ block_height: number;
7
+ block_hash: string;
8
+ block_time: number;
9
+ };
10
+ value: number;
11
+ }
12
+
13
+ export interface ITypedData {
14
+ types: any;
15
+ primaryType: string;
16
+ domain: any;
17
+ message: any;
18
+ }
19
+
20
+ export type BTCNetworkType = "MAINNET" | "TESTNET" | "SIGNET";
21
+ export type BTCNetworkAddressType = "P2PKH" | "P2WPKH" | "P2TR" | "P2SH";
22
+
23
+ export enum BTCNetworkTypeEnum {
24
+ MAINNET = "MAINNET",
25
+ TESTNET = "TESTNET",
26
+ SIGNET = "SIGNET",
27
+ }
28
+
29
+ export enum BTCNetworkAddressTypeEnum {
30
+ P2PKH = "P2PKH",
31
+ P2WPKH = "P2WPKH",
32
+ P2TR = "P2TR",
33
+ P2SH = "P2SH",
34
+ }
35
+
36
+ export interface BtcCreateSendBtcPsbt {
37
+ networkType: BTCNetworkType;
38
+ addressType: BTCNetworkAddressType | undefined;
39
+ toAddress: string;
40
+ amount: string;
41
+ }
42
+
43
+ export interface BtcSignPsbt {
44
+ networkType: BTCNetworkType;
45
+ addressType: BTCNetworkAddressType;
46
+ autoFinalized: boolean;
47
+ psbtHex: string;
48
+ }
49
+
50
+ export interface BtcPushTx {
51
+ networkType: BTCNetworkType;
52
+ rawTransaction: string;
53
+ }
54
+
55
+ export type BtcAddressType =
56
+ | "bitcoinP2pkhAddress"
57
+ | "bitcoinP2shAddress"
58
+ | "bitcoinP2trAddress"
59
+ | "bitcoinP2wpkhAddress";
60
+
61
+ export type ISwapTokensParams = {
62
+ chain: number | undefined;
63
+ page: number | undefined;
64
+ size: number | undefined;
65
+ keyword: string | undefined;
66
+ };
67
+
68
+ export enum SubmitParamsType {
69
+ SWAP = 1,
70
+ BRIDGE = 2,
71
+ TRANSFER = 3,
72
+ }
73
+
74
+ export interface UTXO {
75
+ // hash of transaction that holds the UTXO
76
+ txid: string;
77
+ // index of the output in the transaction
78
+ vout: number;
79
+ // amount of satoshis the UTXO holds
80
+ value: number;
81
+ // the script that the UTXO contains
82
+ scriptPubKey: string;
83
+ }
84
+
85
+ export type Fees = {
86
+ // fee for inclusion in the next block
87
+ fastestFee: number;
88
+ // fee for inclusion in a block in 30 mins
89
+ halfHourFee: number;
90
+ // fee for inclusion in a block in 1 hour
91
+ hourFee: number;
92
+ // economy fee: inclusion not guaranteed
93
+ economyFee: number;
94
+ // minimum fee: the minimum fee of the network
95
+ minimumFee: number;
96
+ };
97
+
98
+ export interface ICreatePsbtArgs {
99
+ from: string;
100
+ pubKey: Uint8Array;
101
+ to: string;
102
+ amount: string;
103
+ addressType: BTCNetworkAddressTypeEnum;
104
+ networkType: BTCNetworkTypeEnum;
105
+ }
106
+
107
+ export interface UnspentOutput {
108
+ txId: string;
109
+ outputIndex: number;
110
+ satoshis: number;
111
+ scriptPk: string;
112
+ addressType?: BTCNetworkAddressTypeEnum;
113
+ address: string;
114
+ }
115
+
116
+ export interface TxInput {
117
+ data: {
118
+ hash: string;
119
+ index: number;
120
+ witnessUtxo: { value: number; script: Uint8Array };
121
+ tapInternalKey?: Uint8Array;
122
+ };
123
+ utxo: UnspentOutput;
124
+ }
125
+
126
+ export type TransactionParams = Omit<any, "rpcUrl">;
127
+
128
+ export interface EvmTransaction {
129
+ data: string | undefined;
130
+ gas: string | undefined;
131
+ gasPrice: string | undefined;
132
+ maxFeePerGas: string | undefined;
133
+ maxPriorityFeePerGas: string | undefined;
134
+ nonce: number | undefined;
135
+ to: string | undefined;
136
+ from: string | undefined;
137
+ value?: string | undefined;
138
+ }
139
+
140
+ export interface EvmTransactionParams {
141
+ transaction: EvmTransaction;
142
+ chainId: number;
143
+ }
144
+
145
+ export interface EvmTransactionWithTypes {
146
+ data: {
147
+ data: string | undefined;
148
+ gas: string | undefined;
149
+ gasPrice: string | undefined;
150
+ maxFeePerGas: string | undefined;
151
+ maxPriorityFeePerGas: string | undefined;
152
+ nonce: number | undefined;
153
+ to: string | undefined;
154
+ from: string | undefined;
155
+ chainId: number;
156
+ };
157
+ types: {
158
+ [key: string]: string;
159
+ };
160
+ }
161
+
162
+ export interface SendTransactionParams {
163
+ chainId: string;
164
+ accountId?: string;
165
+ amount: number;
166
+ data?: string;
167
+ decimals?: number;
168
+ fee: number;
169
+ from: string;
170
+ gasFeeUsd?: string;
171
+ maxFee?: string;
172
+ to: string;
173
+ tokenAddress: string;
174
+ submitType?: SubmitParamsType;
175
+ value?: string;
176
+ nonce?: number;
177
+ maxFeePerGas?: string;
178
+ maxPriorityFeePerGas?: string;
179
+ gas?: string;
180
+ spendableUtxos: any[];
181
+ }
@@ -0,0 +1,49 @@
1
+ import { ChainTypes } from "@tomo-inc/wallet-utils";
2
+
3
+ export enum TxTypes {
4
+ swap = 1,
5
+ bridge = 2,
6
+ receive = 31,
7
+ send = 32,
8
+ approve = 4,
9
+ contractInteraction = 5,
10
+ redPocket = 6,
11
+ }
12
+
13
+ export interface TransactionsParams {
14
+ walletId?: string;
15
+ tokenAddress?: string;
16
+ chainId?: string;
17
+ typeList?: TxTypes[];
18
+ cursor?: string;
19
+ pageLimit?: number;
20
+ }
21
+
22
+ export interface TransactionItem {
23
+ chainType: ChainTypes;
24
+ chainInfo: {
25
+ chainId: string;
26
+ name: string;
27
+ icon: string;
28
+ };
29
+ nativeCurrency: {
30
+ symbol: string;
31
+ decimals: number;
32
+ name: string;
33
+ };
34
+ tokenInfo: {
35
+ tokenAddress: string;
36
+ symbol: string;
37
+ logo: string;
38
+ riskLevel: number;
39
+ };
40
+ txHash: `0x${string}`;
41
+ txDetail: any;
42
+ txDetailLink: `https://${string}`;
43
+ txTime: number;
44
+ }
45
+
46
+ export interface TransactionsResponse {
47
+ cursor: string;
48
+ transactionList: TransactionItem[];
49
+ }
package/src/wallet.ts ADDED
@@ -0,0 +1,96 @@
1
+ //types
2
+ import { ChainTypes, getExplorerUrl } from "@tomo-inc/wallet-utils";
3
+
4
+ import { INetwork, NativeCurrency, TomoAppInfo, TransactionsParams, TransactionsResponse } from "./types";
5
+ import { BaseService } from "./base/service";
6
+
7
+ export class TomoWallet extends BaseService {
8
+ private static instance: TomoWallet;
9
+ private walletId: string;
10
+
11
+ public constructor(walletId: string, tomoAppInfo: TomoAppInfo) {
12
+ super(tomoAppInfo);
13
+ this.walletId = walletId;
14
+ }
15
+
16
+ public getInstance(walletId: string, tomoAppInfo: TomoAppInfo) {
17
+ if (!TomoWallet.instance) {
18
+ TomoWallet.instance = new TomoWallet(walletId, tomoAppInfo);
19
+ }
20
+ return new TomoWallet(walletId, tomoAppInfo);
21
+ }
22
+
23
+ public async supportedChains(chainType?: ChainTypes | ""): Promise<INetwork[]> {
24
+ const networks = await this.networks.getNetworks(chainType || "");
25
+ return networks;
26
+ }
27
+
28
+ public async getChainInfo(chainType: ChainTypes, chainId: string): Promise<INetwork> {
29
+ this.networks.setChainType(chainType);
30
+ const network = await this.networks.getNetworkByChainId(chainId);
31
+ return network;
32
+ }
33
+
34
+ //evm chains
35
+ public async isChainSupported(chainType: ChainTypes, chainId: string): Promise<boolean> {
36
+ this.networks.setChainType(chainType);
37
+ const chainInfo = await this.networks.getNetworkByChainId(chainId);
38
+ if (!chainInfo) {
39
+ return false;
40
+ }
41
+ return !!chainInfo.chainId;
42
+ }
43
+
44
+ public async getTransactions({
45
+ tokenAddress = "",
46
+ chainId = "",
47
+ typeList = [],
48
+ pageLimit = 20,
49
+ cursor,
50
+ }: TransactionsParams): Promise<TransactionsResponse> {
51
+ if (!this.walletId) {
52
+ throw new Error("walletId is required");
53
+ }
54
+ try {
55
+ const params: any = {
56
+ tokenAddress,
57
+ pageLimit: pageLimit + 1,
58
+ walletId: this.walletId,
59
+ cursor,
60
+ };
61
+ if (typeList.length > 0) {
62
+ params.typeList = typeList.join(",");
63
+ }
64
+ if (chainId) {
65
+ const chainInfo: any = await this.networks.getNetworkByChainId(chainId);
66
+ const chainIndex = chainInfo.chainIndex;
67
+ params.chainIndex = chainIndex;
68
+ }
69
+ const { cursor: newCursor = "", transactionList } = await this.transactions.getTransactions(params);
70
+
71
+ const txList = transactionList.map(async (item: any) => {
72
+ const chainIndex = item.chainIndex;
73
+ const chainInfo = (await this.networks.getNetworkByChainIndex(chainIndex)) as INetwork;
74
+ return {
75
+ chainType: chainInfo.platformType.toLowerCase() as ChainTypes,
76
+ chainInfo: {
77
+ chainId: chainInfo.chainId.toString(),
78
+ name: chainInfo.name,
79
+ icon: chainInfo.icon,
80
+ },
81
+ nativeCurrency: chainInfo?.nativeCurrency as NativeCurrency,
82
+ tokenInfo: item.tokenInfo,
83
+ txHash: item.txHash as `0x${string}`,
84
+ txDetail: item,
85
+ txDetailLink: getExplorerUrl(chainIndex, { txId: item.txHash }) as `https://${string}`,
86
+ txTime: item.txTime,
87
+ };
88
+ });
89
+ const resolvedTxList = await Promise.all(txList);
90
+ return { cursor: newCursor, transactionList: resolvedTxList };
91
+ } catch (error) {
92
+ console.error("Failed to send transaction via RPC:", error);
93
+ throw error;
94
+ }
95
+ }
96
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "baseUrl": "./src",
5
+ "target": "ES2022",
6
+ "module": "ESNext",
7
+ "skipLibCheck": true,
8
+ "moduleResolution": "node",
9
+ "allowSyntheticDefaultImports": true,
10
+ "esModuleInterop": true,
11
+ "types": []
12
+ },
13
+ "include": ["src"]
14
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/index.ts"],
5
+ format: ["esm", "cjs"],
6
+ dts: true,
7
+ treeshake: true,
8
+ clean: true,
9
+ target: "es2022",
10
+ platform: "browser",
11
+ tsconfig: "tsconfig.json",
12
+ external: ["viem", "crypto-js", "axios", "bignumber.js", "crypto", "@tomo-wallet/web3", "bitcoinjs-lib"],
13
+ outExtension({ format }) {
14
+ return {
15
+ js: format === "esm" ? ".js" : ".cjs",
16
+ };
17
+ },
18
+ });