@avalabs/bitcoin-module 0.0.23 → 0.1.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.
Files changed (34) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +1 -1
  3. package/dist/index.cjs +4 -4
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.js +3 -3
  6. package/dist/index.js.map +1 -1
  7. package/package.json +10 -5
  8. package/.turbo/turbo-build.log +0 -22
  9. package/.turbo/turbo-lint.log +0 -4
  10. package/.turbo/turbo-test.log +0 -16
  11. package/CHANGELOG.md +0 -56
  12. package/jest.config.js +0 -10
  13. package/src/env.ts +0 -25
  14. package/src/handlers/get-address/get-address.ts +0 -37
  15. package/src/handlers/get-balances/get-balances.test.ts +0 -181
  16. package/src/handlers/get-balances/get-balances.ts +0 -110
  17. package/src/handlers/get-balances/index.ts +0 -1
  18. package/src/handlers/get-network-fee/get-network-fee.test.ts +0 -67
  19. package/src/handlers/get-network-fee/get-network-fee.ts +0 -34
  20. package/src/handlers/get-network-fee/index.ts +0 -1
  21. package/src/handlers/get-transaction-history/convert-btc-transaction.test.ts +0 -108
  22. package/src/handlers/get-transaction-history/convert-btc-transaction.ts +0 -36
  23. package/src/handlers/get-transaction-history/get-transaction-history.test.ts +0 -98
  24. package/src/handlers/get-transaction-history/get-transaction-history.ts +0 -23
  25. package/src/handlers/get-transaction-history/index.ts +0 -1
  26. package/src/index.ts +0 -1
  27. package/src/module.test.ts +0 -57
  28. package/src/module.ts +0 -77
  29. package/src/utils/extract-token-market-data.ts +0 -16
  30. package/src/utils/get-provider.test.ts +0 -55
  31. package/src/utils/get-provider.ts +0 -18
  32. package/tsconfig.jest.json +0 -7
  33. package/tsconfig.json +0 -14
  34. package/tsup.config.ts +0 -4
@@ -1,98 +0,0 @@
1
- import type { BitcoinHistoryTx, BitcoinProvider } from '@avalabs/wallets-sdk';
2
- import type { Network } from '@avalabs/vm-module-types';
3
-
4
- import { getProvider } from '../../utils/get-provider';
5
-
6
- import { getTransactionHistory } from './get-transaction-history';
7
- import { convertBtcTransaction } from './convert-btc-transaction';
8
-
9
- jest.mock('../../utils/get-provider');
10
-
11
- const proxyApiUrl = 'https://proxy.api/';
12
-
13
- const btcMain: Network = {
14
- isTestnet: false,
15
- chainId: 987654,
16
- networkToken: {
17
- decimals: 8,
18
- name: 'Bitcoin',
19
- symbol: 'BTC',
20
- },
21
- explorerUrl: 'https://btc.main',
22
- } as unknown as Network;
23
-
24
- const btcTest: Network = {
25
- isTestnet: true,
26
- chainId: 987653,
27
- networkToken: {
28
- decimals: 8,
29
- name: 'Bitcoin',
30
- symbol: 'BTC',
31
- },
32
- explorerUrl: 'https://btc.test',
33
- } as unknown as Network;
34
-
35
- const userAddress = 'b1-user-address';
36
- const txAddress = 'b1-tx-address';
37
-
38
- const rawHistory: BitcoinHistoryTx[] = [
39
- {
40
- addresses: [txAddress],
41
- amount: 15_000,
42
- fee: 600,
43
- block: 100_000,
44
- confirmations: 1,
45
- containsMultisig: false,
46
- hash: '0x1rstTxHash',
47
- isSender: true,
48
- receivedTime: 172139193,
49
- confirmedTime: 172139323,
50
- },
51
- ];
52
-
53
- describe('get-transaction-history', () => {
54
- const provider = { getTxHistory: jest.fn() } as unknown as BitcoinProvider;
55
-
56
- beforeEach(() => {
57
- jest.mocked(provider.getTxHistory).mockResolvedValue(rawHistory);
58
- jest.mocked(getProvider).mockReturnValue(provider);
59
- });
60
-
61
- it('should build the provider', async () => {
62
- await getTransactionHistory({
63
- address: userAddress,
64
- network: btcMain,
65
- proxyApiUrl,
66
- });
67
-
68
- expect(getProvider).toHaveBeenCalledWith({ isTestnet: false, proxyApiUrl });
69
-
70
- await getTransactionHistory({
71
- address: txAddress,
72
- network: btcTest,
73
- proxyApiUrl: 'https://proxy-dev.api/',
74
- });
75
-
76
- expect(getProvider).toHaveBeenCalledWith({ isTestnet: true, proxyApiUrl: 'https://proxy-dev.api/' });
77
- });
78
-
79
- it('calls getTxHistory() method', async () => {
80
- await getTransactionHistory({
81
- address: userAddress,
82
- network: btcTest,
83
- proxyApiUrl,
84
- });
85
-
86
- expect(provider.getTxHistory).toHaveBeenCalledWith(userAddress);
87
- });
88
-
89
- it('maps returned fee rates to known model', async () => {
90
- expect(
91
- await getTransactionHistory({
92
- address: userAddress,
93
- network: btcTest,
94
- proxyApiUrl,
95
- }),
96
- ).toEqual(rawHistory.map((tx) => convertBtcTransaction(tx, { address: userAddress, network: btcTest })));
97
- });
98
- });
@@ -1,23 +0,0 @@
1
- import type { Network } from '@avalabs/vm-module-types';
2
-
3
- import { getProvider } from '../../utils/get-provider';
4
-
5
- import { convertBtcTransaction } from './convert-btc-transaction';
6
-
7
- type GetBtcTransactionHistoryOptions = {
8
- network: Network;
9
- address: string;
10
- proxyApiUrl: string;
11
- };
12
-
13
- export const getTransactionHistory = async ({ address, network, proxyApiUrl }: GetBtcTransactionHistoryOptions) => {
14
- const provider = getProvider({ isTestnet: Boolean(network.isTestnet), proxyApiUrl });
15
- const rawHistory = await provider.getTxHistory(address);
16
-
17
- return rawHistory.map((tx) =>
18
- convertBtcTransaction(tx, {
19
- address,
20
- network,
21
- }),
22
- );
23
- };
@@ -1 +0,0 @@
1
- export * from './get-transaction-history';
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './module';
@@ -1,57 +0,0 @@
1
- import { Environment, type Network } from '@avalabs/vm-module-types';
2
-
3
- import { BitcoinModule } from './module';
4
- import { getNetworkFee } from './handlers/get-network-fee';
5
- import { getBalances } from './handlers/get-balances';
6
- import { devEnv, prodEnv } from './env';
7
-
8
- jest.mock('./handlers/get-network-fee');
9
- jest.mock('./handlers/get-balances');
10
-
11
- describe('bitcoin-module', () => {
12
- describe('getNetworkFee()', () => {
13
- it('uses the get-network-fee handler', async () => {
14
- const devModule = new BitcoinModule({ environment: Environment.DEV });
15
- await devModule.getNetworkFee({ isTestnet: true } as Network);
16
-
17
- expect(getNetworkFee).toHaveBeenCalledWith({
18
- isTestnet: true,
19
- proxyApiUrl: devEnv.proxyApiUrl,
20
- });
21
-
22
- const prodModule = new BitcoinModule({ environment: Environment.PRODUCTION });
23
- await prodModule.getNetworkFee({ isTestnet: false } as Network);
24
-
25
- expect(getNetworkFee).toHaveBeenCalledWith({
26
- isTestnet: false,
27
- proxyApiUrl: prodEnv.proxyApiUrl,
28
- });
29
- });
30
- });
31
-
32
- describe('getBalances()', () => {
33
- it('uses the get-balances handler', async () => {
34
- const params = {
35
- network: { isTestnet: true } as Network,
36
- addresses: ['address-1'],
37
- currency: 'USD',
38
- };
39
-
40
- const devModule = new BitcoinModule({ environment: Environment.DEV });
41
- await devModule.getBalances(params);
42
-
43
- expect(getBalances).toHaveBeenCalledWith({
44
- ...params,
45
- proxyApiUrl: devEnv.proxyApiUrl,
46
- });
47
-
48
- const prodModule = new BitcoinModule({ environment: Environment.PRODUCTION });
49
- await prodModule.getBalances(params);
50
-
51
- expect(getBalances).toHaveBeenCalledWith({
52
- ...params,
53
- proxyApiUrl: prodEnv.proxyApiUrl,
54
- });
55
- });
56
- });
57
- });
package/src/module.ts DELETED
@@ -1,77 +0,0 @@
1
- import type {
2
- Module,
3
- Manifest,
4
- NetworkFees,
5
- GetTransactionHistory,
6
- RpcRequest,
7
- Network,
8
- Environment,
9
- GetBalancesParams,
10
- GetAddressParams,
11
- GetAddressResponse,
12
- } from '@avalabs/vm-module-types';
13
- import { parseManifest } from '@avalabs/vm-module-types';
14
- import { rpcErrors } from '@metamask/rpc-errors';
15
- import { getEnv } from './env';
16
-
17
- import ManifestJson from '../manifest.json';
18
- import { getNetworkFee } from './handlers/get-network-fee';
19
- import { getTransactionHistory } from './handlers/get-transaction-history';
20
- import { getBalances } from './handlers/get-balances';
21
- import { getAddress } from './handlers/get-address/get-address';
22
-
23
- export class BitcoinModule implements Module {
24
- #proxyApiUrl: string;
25
-
26
- constructor({ environment }: { environment: Environment }) {
27
- const { proxyApiUrl } = getEnv(environment);
28
- this.#proxyApiUrl = proxyApiUrl;
29
- }
30
-
31
- getAddress({ accountIndex, xpub, isTestnet, walletType }: GetAddressParams): Promise<GetAddressResponse> {
32
- return getAddress({ accountIndex, xpub, isTestnet, walletType });
33
- }
34
-
35
- getBalances({ addresses, currency, network, storage }: GetBalancesParams) {
36
- return getBalances({
37
- addresses,
38
- currency,
39
- network,
40
- proxyApiUrl: this.#proxyApiUrl,
41
- storage,
42
- });
43
- }
44
-
45
- getManifest(): Manifest | undefined {
46
- const result = parseManifest(ManifestJson);
47
- return result.success ? result.data : undefined;
48
- }
49
-
50
- getNetworkFee(network: Network): Promise<NetworkFees> {
51
- return getNetworkFee({
52
- isTestnet: Boolean(network.isTestnet),
53
- proxyApiUrl: this.#proxyApiUrl,
54
- });
55
- }
56
-
57
- async getTransactionHistory({ address, network }: GetTransactionHistory) {
58
- return {
59
- transactions: await getTransactionHistory({
60
- address,
61
- network,
62
- proxyApiUrl: this.#proxyApiUrl,
63
- }),
64
- };
65
- }
66
-
67
- getTokens(_: Network) {
68
- return Promise.resolve([]);
69
- }
70
-
71
- async onRpcRequest(request: RpcRequest, _network: Network) {
72
- switch (request.method) {
73
- default:
74
- return { error: rpcErrors.methodNotSupported(`Method ${request.method} not supported`) };
75
- }
76
- }
77
- }
@@ -1,16 +0,0 @@
1
- import type { SimplePriceResponse, TokenMarketData } from '@avalabs/vm-module-types';
2
-
3
- export const extractTokenMarketData = (
4
- coinId: string,
5
- currency?: string,
6
- data?: SimplePriceResponse,
7
- ): TokenMarketData => {
8
- const coinData = data?.[coinId]?.[currency ?? ''] ?? {};
9
-
10
- return {
11
- priceInCurrency: coinData.price ?? undefined,
12
- marketCap: coinData.marketCap ?? undefined,
13
- vol24: coinData.vol24 ?? undefined,
14
- change24: coinData.change24 ?? undefined,
15
- };
16
- };
@@ -1,55 +0,0 @@
1
- import { BitcoinProvider } from '@avalabs/wallets-sdk';
2
-
3
- import { getProvider } from './get-provider';
4
-
5
- jest.mock('@avalabs/wallets-sdk');
6
-
7
- const PROXY_API_PROD = 'https://proxy.api';
8
- const PROXY_API_DEV = 'https://proxy-dev.api';
9
-
10
- describe('get-provider', () => {
11
- it.each([
12
- { isTestnet: true, proxyApiUrl: PROXY_API_DEV },
13
- { isTestnet: false, proxyApiUrl: PROXY_API_DEV },
14
- { isTestnet: false, proxyApiUrl: PROXY_API_PROD },
15
- ])('builds BitcoinProvider instance with proper params', (params) => {
16
- const result = getProvider(params);
17
-
18
- expect(BitcoinProvider).toHaveBeenCalledWith(
19
- !params.isTestnet,
20
- undefined,
21
- `${params.proxyApiUrl}/proxy/nownodes/${params.isTestnet ? 'btcbook-testnet' : 'btcbook'}`,
22
- `${params.proxyApiUrl}/proxy/nownodes/${params.isTestnet ? 'btc-testnet' : 'btc'}`,
23
- {},
24
- );
25
-
26
- expect(result).toBeInstanceOf(BitcoinProvider);
27
- });
28
-
29
- describe('when process.env.GLACIER_API_KEY is defined', () => {
30
- const env = process.env;
31
-
32
- beforeAll(() => {
33
- process.env = {
34
- ...env,
35
- GLACIER_API_KEY: 'glacier-api-key',
36
- };
37
- });
38
-
39
- afterAll(() => {
40
- process.env = env;
41
- });
42
-
43
- it('passes it to the constructor', () => {
44
- getProvider({ isTestnet: true, proxyApiUrl: PROXY_API_DEV });
45
-
46
- expect(BitcoinProvider).toHaveBeenCalledWith(
47
- false,
48
- undefined,
49
- `${PROXY_API_DEV}/proxy/nownodes/btcbook-testnet`,
50
- `${PROXY_API_DEV}/proxy/nownodes/btc-testnet`,
51
- { token: 'glacier-api-key' },
52
- );
53
- });
54
- });
55
- });
@@ -1,18 +0,0 @@
1
- import { BitcoinProvider } from '@avalabs/wallets-sdk';
2
-
3
- type ProviderParams = {
4
- isTestnet: boolean;
5
- proxyApiUrl: string;
6
- };
7
-
8
- export const getProvider = ({ isTestnet, proxyApiUrl }: ProviderParams): BitcoinProvider =>
9
- new BitcoinProvider(
10
- !isTestnet,
11
- undefined,
12
- `${proxyApiUrl}/proxy/nownodes/${isTestnet ? 'btcbook-testnet' : 'btcbook'}`,
13
- `${proxyApiUrl}/proxy/nownodes/${isTestnet ? 'btc-testnet' : 'btc'}`,
14
-
15
- // The Glacier API key is only needed in development to bypass rate limits.
16
- // It should never be used in production.
17
- process.env.GLACIER_API_KEY ? { token: process.env.GLACIER_API_KEY } : {},
18
- );
@@ -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);