@avalabs/avalanche-module 0.0.22 → 0.1.0

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.
@@ -1,31 +0,0 @@
1
- import {
2
- PrimaryNetworkChainName,
3
- type ListCChainAtomicTransactionsResponse,
4
- type ListPChainTransactionsResponse,
5
- type ListXChainTransactionsResponse,
6
- } from '@avalabs/glacier-sdk';
7
- import Big from 'big.js';
8
-
9
- export const isPChainTransactions = (
10
- value: ListPChainTransactionsResponse | ListXChainTransactionsResponse | ListCChainAtomicTransactionsResponse,
11
- ): value is ListPChainTransactionsResponse => {
12
- return value.chainInfo.chainName === PrimaryNetworkChainName.P_CHAIN;
13
- };
14
-
15
- export const isXChainTransactions = (
16
- value: ListPChainTransactionsResponse | ListXChainTransactionsResponse | ListCChainAtomicTransactionsResponse,
17
- ): value is ListXChainTransactionsResponse => {
18
- return value.chainInfo.chainName === PrimaryNetworkChainName.X_CHAIN;
19
- };
20
-
21
- export function getExplorerAddressByNetwork(
22
- explorerUrl: string,
23
- hash: string,
24
- hashType: 'address' | 'tx' = 'tx',
25
- ): string {
26
- return `${explorerUrl}/${hashType}/${hash}`;
27
- }
28
-
29
- export function getTokenValue({ amount, decimals }: { decimals: number; amount?: number }): Big {
30
- return amount === undefined ? new Big(0) : new Big(amount / 10 ** decimals);
31
- }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './module';
2
- export * from './handlers/get-balances/typeguards';
package/src/module.ts DELETED
@@ -1,68 +0,0 @@
1
- import type {
2
- Module,
3
- Manifest,
4
- NetworkFees,
5
- GetTransactionHistory,
6
- RpcRequest,
7
- Network,
8
- GetBalancesParams,
9
- GetBalancesResponse,
10
- Environment,
11
- } from '@avalabs/vm-module-types';
12
- import { parseManifest } from '@avalabs/vm-module-types';
13
- import { rpcErrors } from '@metamask/rpc-errors';
14
- import ManifestJson from '../manifest.json';
15
- import { getNetworkFee } from './handlers/get-network-fee/get-network-fee';
16
- import { getTransactionHistory } from './handlers/get-transaction-history/get-transaction-history';
17
- import { getEnv } from './env';
18
- import { AvalancheGlacierService } from './services/glacier-service/glacier-service';
19
- import { hashBlockchainId, TokenService } from '@internal/utils';
20
- import { getBalances } from './handlers/get-balances/get-balances';
21
-
22
- export class AvalancheModule implements Module {
23
- #glacierService: AvalancheGlacierService;
24
- #proxyApiUrl: string;
25
-
26
- constructor({ environment }: { environment: Environment }) {
27
- const { glacierApiUrl, proxyApiUrl } = getEnv(environment);
28
- this.#glacierService = new AvalancheGlacierService({ glacierApiUrl });
29
- this.#proxyApiUrl = proxyApiUrl;
30
- }
31
-
32
- getAddress(): Promise<string> {
33
- return Promise.resolve('Avalanche address');
34
- }
35
-
36
- getBalances({ addresses, network, storage, currency }: GetBalancesParams): Promise<GetBalancesResponse> {
37
- const tokenService = new TokenService({ storage, proxyApiUrl: this.#proxyApiUrl });
38
- return getBalances({ addresses, currency, network, glacierService: this.#glacierService, tokenService });
39
- }
40
-
41
- getManifest(): Manifest | undefined {
42
- const result = parseManifest(ManifestJson);
43
- return result.success ? result.data : undefined;
44
- }
45
-
46
- getNetworkFee(_: Network): Promise<NetworkFees> {
47
- return getNetworkFee();
48
- }
49
-
50
- getTransactionHistory({ network, address, nextPageToken, offset }: GetTransactionHistory) {
51
- return getTransactionHistory({ network, address, nextPageToken, offset, glacierService: this.#glacierService });
52
- }
53
-
54
- getTokens(_: Network) {
55
- return Promise.resolve([]);
56
- }
57
-
58
- async onRpcRequest(request: RpcRequest, _network: Network) {
59
- switch (request.method) {
60
- default:
61
- return { error: rpcErrors.methodNotSupported(`Method ${request.method} not supported`) };
62
- }
63
- }
64
-
65
- static getHashedBlockchainId({ blockchainId, isTestnet }: { blockchainId: string; isTestnet?: boolean }): string {
66
- return hashBlockchainId({ blockchainId, isTestnet });
67
- }
68
- }
@@ -1,75 +0,0 @@
1
- import {
2
- BlockchainId,
3
- Glacier,
4
- type ListCChainAtomicBalancesResponse,
5
- type ListCChainAtomicTransactionsResponse,
6
- type ListPChainBalancesResponse,
7
- type ListPChainTransactionsResponse,
8
- type ListXChainBalancesResponse,
9
- type ListXChainTransactionsResponse,
10
- Network,
11
- PrimaryNetworkTxType,
12
- SortOrder,
13
- } from '@avalabs/glacier-sdk';
14
-
15
- class GlacierUnhealthyError extends Error {
16
- override message = 'Glacier is unhealthy. Try again later.';
17
- }
18
-
19
- export class AvalancheGlacierService {
20
- glacierSdk: Glacier;
21
- isGlacierHealthy = true;
22
-
23
- constructor({ glacierApiUrl }: { glacierApiUrl: string }) {
24
- this.glacierSdk = new Glacier({ BASE: glacierApiUrl });
25
- }
26
-
27
- isHealthy = (): boolean => this.isGlacierHealthy;
28
-
29
- setGlacierToUnhealthy(): void {
30
- this.isGlacierHealthy = false;
31
- setTimeout(
32
- () => {
33
- this.isGlacierHealthy = true;
34
- },
35
- 5 * 60 * 1000,
36
- ); // 5 minutes
37
- }
38
-
39
- async listLatestPrimaryNetworkTransactions(params: {
40
- blockchainId: BlockchainId;
41
- network: Network;
42
- addresses?: string;
43
- txTypes?: Array<PrimaryNetworkTxType>;
44
- startTimestamp?: number;
45
- endTimestamp?: number;
46
- pageToken?: string;
47
- pageSize?: number;
48
- sortOrder?: SortOrder;
49
- }): Promise<ListPChainTransactionsResponse | ListXChainTransactionsResponse | ListCChainAtomicTransactionsResponse> {
50
- try {
51
- return this.glacierSdk.primaryNetworkTransactions.listLatestPrimaryNetworkTransactions(params);
52
- } catch (error) {
53
- if (error instanceof GlacierUnhealthyError) {
54
- this.setGlacierToUnhealthy();
55
- }
56
- throw error;
57
- }
58
- }
59
-
60
- async getChainBalance(params: {
61
- blockchainId: BlockchainId;
62
- network: Network;
63
- blockTimestamp?: number;
64
- addresses?: string;
65
- }): Promise<ListPChainBalancesResponse | ListXChainBalancesResponse | ListCChainAtomicBalancesResponse> {
66
- try {
67
- return this.glacierSdk.primaryNetworkBalances.getBalancesByAddresses(params);
68
- } catch (error) {
69
- if (error instanceof GlacierUnhealthyError) {
70
- this.setGlacierToUnhealthy();
71
- }
72
- throw error;
73
- }
74
- }
75
- }
@@ -1,7 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "verbatimModuleSyntax": false,
5
- "esModuleInterop": true
6
- }
7
- }
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "extends": "@internal/tsconfig/tsconfig.base.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "declaration": false,
6
- "incremental": false // Need to turn off because of tsup dts
7
- },
8
- "include": ["src"],
9
- "references": [
10
- {
11
- "path": "../../packages-internal/utils/tsconfig.json"
12
- }
13
- ]
14
- }
package/tsup.config.ts DELETED
@@ -1,4 +0,0 @@
1
- import { defineConfig } from 'tsup';
2
- import { baseConfig } from '@internal/tsup-config';
3
-
4
- export default defineConfig(baseConfig);