@stacks/network 4.0.2 → 4.2.0-beta.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.
- package/README.md +56 -5
- package/dist/esm/fetch.d.ts +31 -0
- package/dist/esm/fetch.js +72 -0
- package/dist/esm/fetch.js.map +1 -0
- package/dist/esm/index.d.ts +2 -54
- package/dist/esm/index.js +2 -106
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/network.d.ts +57 -0
- package/dist/esm/network.js +122 -0
- package/dist/esm/network.js.map +1 -0
- package/dist/fetch.d.ts +31 -0
- package/dist/fetch.js +81 -0
- package/dist/fetch.js.map +1 -0
- package/dist/index.d.ts +2 -54
- package/dist/index.js +15 -110
- package/dist/index.js.map +1 -1
- package/dist/network.d.ts +57 -0
- package/dist/network.js +129 -0
- package/dist/network.js.map +1 -0
- package/dist/polyfill/index.js +1 -1
- package/dist/umd/index.js +1 -1
- package/dist/umd/index.js.map +1 -1
- package/package.json +4 -3
- package/src/fetch.ts +175 -0
- package/src/index.ts +2 -146
- package/src/network.ts +178 -0
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ npm install @stacks/network
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
### Create a Stacks mainnet, testnet or mocknet network
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
16
|
import { StacksMainnet, StacksTestnet, StacksMocknet } from '@stacks/network';
|
|
@@ -22,19 +22,19 @@ const testnet = new StacksTestnet();
|
|
|
22
22
|
const mocknet = new StacksMocknet();
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
### Set a custom node URL
|
|
26
26
|
|
|
27
27
|
```typescript
|
|
28
28
|
const network = new StacksMainnet({ url: 'https://www.mystacksnode.com/' });
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
Check if network is mainnet
|
|
31
|
+
### Check if network is mainnet
|
|
32
32
|
|
|
33
33
|
```typescript
|
|
34
34
|
const isMainnet = network.isMainnet();
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
### Network usage in transaction building
|
|
38
38
|
|
|
39
39
|
```typescript
|
|
40
40
|
import { makeSTXTokenTransfer } from '@stacks/transactions';
|
|
@@ -49,7 +49,58 @@ const txOptions = {
|
|
|
49
49
|
const transaction = await makeSTXTokenTransfer(txOptions);
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
### Use the built-in API key middleware
|
|
53
|
+
|
|
54
|
+
Some Stacks APIs make use API keys to provide less rate-limited plans.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { createApiKeyMiddleware, createFetchFn, StacksMainnet } from '@stacks/network';
|
|
58
|
+
import { broadcastResponse, getNonce, makeSTXTokenTransfer } from '@stacks/transactions';
|
|
59
|
+
|
|
60
|
+
const myApiMiddleware = createApiKeyMiddleware('example_e8e044a3_41d8b0fe_3dd3988ef302');
|
|
61
|
+
const myFetchFn = createFetchFn(apiMiddleware); // middlewares can be used to create a new fetch function
|
|
62
|
+
const myMainnet = new StacksMainnet({ fetchFn: myFetchFn }); // the fetchFn options can be passed to a StacksNetwork to override the default fetch function
|
|
63
|
+
|
|
64
|
+
const txOptions = {
|
|
65
|
+
recipient: 'SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159',
|
|
66
|
+
amount: 12345n,
|
|
67
|
+
senderKey: 'b244296d5907de9864c0b0d51f98a13c52890be0404e83f273144cd5b9960eed01',
|
|
68
|
+
memo: 'some memo',
|
|
69
|
+
anchorMode: AnchorMode.Any,
|
|
70
|
+
network: myMainnet, // make sure to pass in the custom network object
|
|
71
|
+
};
|
|
72
|
+
const transaction = await makeSTXTokenTransfer(txOptions); // fee-estimation will use the custom fetchFn
|
|
73
|
+
|
|
74
|
+
const response = await broadcastResponse(transaction, myMainnet); // make sure to broadcast via the custom network object
|
|
75
|
+
|
|
76
|
+
// stacks.js functions, which take a StacksNetwork object will use the custom fetchFn
|
|
77
|
+
const nonce = await getNonce('SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159', myMainnet);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Use custom middleware
|
|
81
|
+
|
|
82
|
+
Middleware can be used to hook into network calls before sending a request or after receiving a response.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { createFetchFn, RequestContext, ResponseContext, StacksTestnet } from '@stacks/network';
|
|
86
|
+
import { broadcastResponse, getNonce, makeSTXTokenTransfer } from '@stacks/transactions';
|
|
87
|
+
|
|
88
|
+
const preMiddleware = (ctx: RequestContext) => {
|
|
89
|
+
ctx.init.headers = new Headers();
|
|
90
|
+
ctx.init.headers.set('x-foo', 'bar'); // override headers and set new `x-foo` header
|
|
91
|
+
};
|
|
92
|
+
const postMiddleware = (ctx: ResponseContext) => {
|
|
93
|
+
console.log(await ctx.response.json()); // log response body as json
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const fetchFn = createFetchFn({ pre: preMiddleware, post: preMiddleware }); // a middleware can contain `pre`, `post`, or both
|
|
97
|
+
const network = new StacksTestnet({ fetchFn });
|
|
98
|
+
|
|
99
|
+
// stacks.js functions, which take a StacksNetwork object will use the custom fetchFn
|
|
100
|
+
const nonce = await getNonce('SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159', network);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Get various API URLs
|
|
53
104
|
|
|
54
105
|
```typescript
|
|
55
106
|
const txBroadcastUrl = network.getBroadcastApiUrl();
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import 'cross-fetch/polyfill';
|
|
2
|
+
export declare const getFetchOptions: () => RequestInit;
|
|
3
|
+
export declare const setFetchOptions: (ops: RequestInit) => RequestInit;
|
|
4
|
+
export declare type FetchFn = (url: string, init?: RequestInit) => Promise<Response>;
|
|
5
|
+
export interface RequestContext {
|
|
6
|
+
fetch: FetchFn;
|
|
7
|
+
url: string;
|
|
8
|
+
init: RequestInit;
|
|
9
|
+
}
|
|
10
|
+
export interface ResponseContext {
|
|
11
|
+
fetch: FetchFn;
|
|
12
|
+
url: string;
|
|
13
|
+
init: RequestInit;
|
|
14
|
+
response: Response;
|
|
15
|
+
}
|
|
16
|
+
export interface FetchParams {
|
|
17
|
+
url: string;
|
|
18
|
+
init: RequestInit;
|
|
19
|
+
}
|
|
20
|
+
export interface FetchMiddleware {
|
|
21
|
+
pre?: (context: RequestContext) => PromiseLike<FetchParams | void> | FetchParams | void;
|
|
22
|
+
post?: (context: ResponseContext) => Promise<Response | void> | Response | void;
|
|
23
|
+
}
|
|
24
|
+
export interface ApiKeyMiddlewareOpts {
|
|
25
|
+
host?: RegExp | string;
|
|
26
|
+
httpHeader?: string;
|
|
27
|
+
apiKey: string;
|
|
28
|
+
}
|
|
29
|
+
export declare function createApiKeyMiddleware({ apiKey, host, httpHeader, }: ApiKeyMiddlewareOpts): FetchMiddleware;
|
|
30
|
+
export declare function createFetchFn(fetchLib: FetchFn, ...middleware: FetchMiddleware[]): FetchFn;
|
|
31
|
+
export declare function createFetchFn(...middleware: FetchMiddleware[]): FetchFn;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import 'cross-fetch/polyfill';
|
|
2
|
+
const defaultFetchOpts = {
|
|
3
|
+
referrerPolicy: 'origin',
|
|
4
|
+
};
|
|
5
|
+
export const getFetchOptions = () => {
|
|
6
|
+
return defaultFetchOpts;
|
|
7
|
+
};
|
|
8
|
+
export const setFetchOptions = (ops) => {
|
|
9
|
+
return Object.assign(defaultFetchOpts, ops);
|
|
10
|
+
};
|
|
11
|
+
export async function fetchWrapper(input, init) {
|
|
12
|
+
const fetchOpts = {};
|
|
13
|
+
Object.assign(fetchOpts, init, defaultFetchOpts);
|
|
14
|
+
const fetchResult = await fetch(input, fetchOpts);
|
|
15
|
+
return fetchResult;
|
|
16
|
+
}
|
|
17
|
+
export function hostMatches(host, pattern) {
|
|
18
|
+
if (typeof pattern === 'string')
|
|
19
|
+
return pattern === host;
|
|
20
|
+
return pattern.exec(host);
|
|
21
|
+
}
|
|
22
|
+
export function createApiKeyMiddleware({ apiKey, host = /(.*)api(.*)\.stacks\.co$/i, httpHeader = 'x-api-key', }) {
|
|
23
|
+
return {
|
|
24
|
+
pre: context => {
|
|
25
|
+
const reqUrl = new URL(context.url);
|
|
26
|
+
if (!hostMatches(reqUrl.host, host))
|
|
27
|
+
return;
|
|
28
|
+
const headers = new Headers(context.init.headers);
|
|
29
|
+
headers.set(httpHeader, apiKey);
|
|
30
|
+
context.init.headers = headers;
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function argsForCreateFetchFn(args) {
|
|
35
|
+
let fetchLib = fetchWrapper;
|
|
36
|
+
let middlewares = [];
|
|
37
|
+
if (args.length > 0 && typeof args[0] === 'function') {
|
|
38
|
+
fetchLib = args.shift();
|
|
39
|
+
}
|
|
40
|
+
if (args.length > 0) {
|
|
41
|
+
middlewares = args;
|
|
42
|
+
}
|
|
43
|
+
return { fetchLib, middlewares };
|
|
44
|
+
}
|
|
45
|
+
export function createFetchFn(...args) {
|
|
46
|
+
const { fetchLib, middlewares } = argsForCreateFetchFn(args);
|
|
47
|
+
const fetchFn = async (url, init) => {
|
|
48
|
+
var _a;
|
|
49
|
+
let fetchParams = { url, init: init !== null && init !== void 0 ? init : {} };
|
|
50
|
+
for (const middleware of middlewares) {
|
|
51
|
+
if (typeof middleware.pre === 'function') {
|
|
52
|
+
const result = await Promise.resolve(middleware.pre(Object.assign({ fetch: fetchLib }, fetchParams)));
|
|
53
|
+
fetchParams = result !== null && result !== void 0 ? result : fetchParams;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
let response = await fetchLib(fetchParams.url, fetchParams.init);
|
|
57
|
+
for (const middleware of middlewares) {
|
|
58
|
+
if (typeof middleware.post === 'function') {
|
|
59
|
+
const result = await Promise.resolve(middleware.post({
|
|
60
|
+
fetch: fetchLib,
|
|
61
|
+
url: fetchParams.url,
|
|
62
|
+
init: fetchParams.init,
|
|
63
|
+
response: (_a = response === null || response === void 0 ? void 0 : response.clone()) !== null && _a !== void 0 ? _a : response,
|
|
64
|
+
}));
|
|
65
|
+
response = result !== null && result !== void 0 ? result : response;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return response;
|
|
69
|
+
};
|
|
70
|
+
return fetchFn;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=fetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,sBAAsB,CAAC;AAI9B,MAAM,gBAAgB,GAAgB;IAEpC,cAAc,EAAE,QAAQ;CAEzB,CAAC;AAMF,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,EAAE;IAClC,OAAO,gBAAgB,CAAC;AAC1B,CAAC,CAAC;AAiBF,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,GAAgB,EAAe,EAAE;IAC/D,OAAO,MAAM,CAAC,MAAM,CAAC,gBAAgB,EAAE,GAAG,CAAC,CAAC;AAC9C,CAAC,CAAC;AAGF,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,KAAkB,EAAE,IAAkB;IACvE,MAAM,SAAS,GAAG,EAAE,CAAC;IAErB,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,gBAAgB,CAAC,CAAC;IAEjD,MAAM,WAAW,GAAG,MAAM,KAAK,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IAClD,OAAO,WAAW,CAAC;AACrB,CAAC;AAoCD,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,OAAwB;IAChE,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,OAAO,OAAO,KAAK,IAAI,CAAC;IACzD,OAAQ,OAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACxC,CAAC;A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AAC;QAE5C,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE;YACpC,IAAI,OAAO,UAAU,CAAC,GAAG,KAAK,UAAU,EAAE;gBACxC,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,OAAO,CAClC,UAAU,CAAC,GAAG,iBACZ,KAAK,EAAE,QAAQ,IACZ,WAAW,EACd,CACH,CAAC;gBACF,WAAW,GAAG,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,WAAW,CAAC;aACrC;SACF;QAED,IAAI,QAAQ,GAAG,MAAM,QAAQ,CAAC,WAAW,CAAC,GAAG,EAAE,WAAW,CAAC,IAAI,CAAC,CAAC;QAEjE,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE;YACpC,IAAI,OAAO,UAAU,CAAC,IAAI,KAAK,UAAU,EAAE;gBACzC,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,OAAO,CAClC,UAAU,CAAC,IAAI,CAAC;oBACd,KAAK,EAAE,QAAQ;oBACf,GAAG,EAAE,WAAW,CAAC,GAAG;oBACpB,IAAI,EAAE,WAAW,CAAC,IAAI;oBACtB,QAAQ,EAAE,MAAA,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,KAAK,EAAE,mCAAI,QAAQ;iBACxC,CAAC,CACH,CAAC;gBACF,QAAQ,GAAG,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,QAAQ,CAAC;aAC/B;SACF;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;IACF,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,54 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export declare const HIRO_TESTNET_DEFAULT = "https://stacks-node-api.testnet.stacks.co";
|
|
4
|
-
export declare const HIRO_MOCKNET_DEFAULT = "http://localhost:3999";
|
|
5
|
-
export interface NetworkConfig {
|
|
6
|
-
url: string;
|
|
7
|
-
}
|
|
8
|
-
export declare const StacksNetworks: readonly ["mainnet", "testnet"];
|
|
9
|
-
export declare type StacksNetworkName = typeof StacksNetworks[number];
|
|
10
|
-
export declare class StacksNetwork {
|
|
11
|
-
version: TransactionVersion;
|
|
12
|
-
chainId: ChainID;
|
|
13
|
-
bnsLookupUrl: string;
|
|
14
|
-
broadcastEndpoint: string;
|
|
15
|
-
transferFeeEstimateEndpoint: string;
|
|
16
|
-
transactionFeeEstimateEndpoint: string;
|
|
17
|
-
accountEndpoint: string;
|
|
18
|
-
contractAbiEndpoint: string;
|
|
19
|
-
readOnlyFunctionCallEndpoint: string;
|
|
20
|
-
readonly coreApiUrl: string;
|
|
21
|
-
constructor(networkConfig: NetworkConfig);
|
|
22
|
-
static fromName: (networkName: StacksNetworkName) => StacksNetwork;
|
|
23
|
-
static fromNameOrNetwork: (network: StacksNetworkName | StacksNetwork) => StacksNetwork;
|
|
24
|
-
isMainnet: () => boolean;
|
|
25
|
-
getBroadcastApiUrl: () => string;
|
|
26
|
-
getTransferFeeEstimateApiUrl: () => string;
|
|
27
|
-
getTransactionFeeEstimateApiUrl: () => string;
|
|
28
|
-
getAccountApiUrl: (address: string) => string;
|
|
29
|
-
getAbiApiUrl: (address: string, contract: string) => string;
|
|
30
|
-
getReadOnlyFunctionCallApiUrl: (contractAddress: string, contractName: string, functionName: string) => string;
|
|
31
|
-
getInfoUrl: () => string;
|
|
32
|
-
getBlockTimeInfoUrl: () => string;
|
|
33
|
-
getPoxInfoUrl: () => string;
|
|
34
|
-
getRewardsUrl: (address: string, options?: any) => string;
|
|
35
|
-
getRewardsTotalUrl: (address: string) => string;
|
|
36
|
-
getRewardHoldersUrl: (address: string, options?: any) => string;
|
|
37
|
-
getStackerInfoUrl: (contractAddress: string, contractName: string) => string;
|
|
38
|
-
getNameInfo(fullyQualifiedName: string): Promise<any>;
|
|
39
|
-
}
|
|
40
|
-
export declare class StacksMainnet extends StacksNetwork {
|
|
41
|
-
version: TransactionVersion;
|
|
42
|
-
chainId: ChainID;
|
|
43
|
-
constructor(networkUrl?: NetworkConfig);
|
|
44
|
-
}
|
|
45
|
-
export declare class StacksTestnet extends StacksNetwork {
|
|
46
|
-
version: TransactionVersion;
|
|
47
|
-
chainId: ChainID;
|
|
48
|
-
constructor(networkUrl?: NetworkConfig);
|
|
49
|
-
}
|
|
50
|
-
export declare class StacksMocknet extends StacksNetwork {
|
|
51
|
-
version: TransactionVersion;
|
|
52
|
-
chainId: ChainID;
|
|
53
|
-
constructor(networkUrl?: NetworkConfig);
|
|
54
|
-
}
|
|
1
|
+
export * from './fetch';
|
|
2
|
+
export * from './network';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,107 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export const HIRO_TESTNET_DEFAULT = 'https://stacks-node-api.testnet.stacks.co';
|
|
4
|
-
export const HIRO_MOCKNET_DEFAULT = 'http://localhost:3999';
|
|
5
|
-
export const StacksNetworks = ['mainnet', 'testnet'];
|
|
6
|
-
export class StacksNetwork {
|
|
7
|
-
constructor(networkConfig) {
|
|
8
|
-
this.version = TransactionVersion.Mainnet;
|
|
9
|
-
this.chainId = ChainID.Mainnet;
|
|
10
|
-
this.bnsLookupUrl = 'https://stacks-node-api.mainnet.stacks.co';
|
|
11
|
-
this.broadcastEndpoint = '/v2/transactions';
|
|
12
|
-
this.transferFeeEstimateEndpoint = '/v2/fees/transfer';
|
|
13
|
-
this.transactionFeeEstimateEndpoint = '/v2/fees/transaction';
|
|
14
|
-
this.accountEndpoint = '/v2/accounts';
|
|
15
|
-
this.contractAbiEndpoint = '/v2/contracts/interface';
|
|
16
|
-
this.readOnlyFunctionCallEndpoint = '/v2/contracts/call-read';
|
|
17
|
-
this.isMainnet = () => this.version === TransactionVersion.Mainnet;
|
|
18
|
-
this.getBroadcastApiUrl = () => `${this.coreApiUrl}${this.broadcastEndpoint}`;
|
|
19
|
-
this.getTransferFeeEstimateApiUrl = () => `${this.coreApiUrl}${this.transferFeeEstimateEndpoint}`;
|
|
20
|
-
this.getTransactionFeeEstimateApiUrl = () => `${this.coreApiUrl}${this.transactionFeeEstimateEndpoint}`;
|
|
21
|
-
this.getAccountApiUrl = (address) => `${this.coreApiUrl}${this.accountEndpoint}/${address}?proof=0`;
|
|
22
|
-
this.getAbiApiUrl = (address, contract) => `${this.coreApiUrl}${this.contractAbiEndpoint}/${address}/${contract}`;
|
|
23
|
-
this.getReadOnlyFunctionCallApiUrl = (contractAddress, contractName, functionName) => `${this.coreApiUrl}${this.readOnlyFunctionCallEndpoint}/${contractAddress}/${contractName}/${encodeURIComponent(functionName)}`;
|
|
24
|
-
this.getInfoUrl = () => `${this.coreApiUrl}/v2/info`;
|
|
25
|
-
this.getBlockTimeInfoUrl = () => `${this.coreApiUrl}/extended/v1/info/network_block_times`;
|
|
26
|
-
this.getPoxInfoUrl = () => `${this.coreApiUrl}/v2/pox`;
|
|
27
|
-
this.getRewardsUrl = (address, options) => {
|
|
28
|
-
let url = `${this.coreApiUrl}/extended/v1/burnchain/rewards/${address}`;
|
|
29
|
-
if (options) {
|
|
30
|
-
url = `${url}?limit=${options.limit}&offset=${options.offset}`;
|
|
31
|
-
}
|
|
32
|
-
return url;
|
|
33
|
-
};
|
|
34
|
-
this.getRewardsTotalUrl = (address) => `${this.coreApiUrl}/extended/v1/burnchain/rewards/${address}/total`;
|
|
35
|
-
this.getRewardHoldersUrl = (address, options) => {
|
|
36
|
-
let url = `${this.coreApiUrl}/extended/v1/burnchain/reward_slot_holders/${address}`;
|
|
37
|
-
if (options) {
|
|
38
|
-
url = `${url}?limit=${options.limit}&offset=${options.offset}`;
|
|
39
|
-
}
|
|
40
|
-
return url;
|
|
41
|
-
};
|
|
42
|
-
this.getStackerInfoUrl = (contractAddress, contractName) => `${this.coreApiUrl}${this.readOnlyFunctionCallEndpoint}
|
|
43
|
-
${contractAddress}/${contractName}/get-stacker-info`;
|
|
44
|
-
this.coreApiUrl = networkConfig.url;
|
|
45
|
-
}
|
|
46
|
-
getNameInfo(fullyQualifiedName) {
|
|
47
|
-
const nameLookupURL = `${this.bnsLookupUrl}/v1/names/${fullyQualifiedName}`;
|
|
48
|
-
return fetchPrivate(nameLookupURL)
|
|
49
|
-
.then(resp => {
|
|
50
|
-
if (resp.status === 404) {
|
|
51
|
-
throw new Error('Name not found');
|
|
52
|
-
}
|
|
53
|
-
else if (resp.status !== 200) {
|
|
54
|
-
throw new Error(`Bad response status: ${resp.status}`);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
return resp.json();
|
|
58
|
-
}
|
|
59
|
-
})
|
|
60
|
-
.then(nameInfo => {
|
|
61
|
-
if (nameInfo.address) {
|
|
62
|
-
return Object.assign({}, nameInfo, { address: nameInfo.address });
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
return nameInfo;
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
StacksNetwork.fromName = (networkName) => {
|
|
71
|
-
switch (networkName) {
|
|
72
|
-
case 'mainnet':
|
|
73
|
-
return new StacksMainnet();
|
|
74
|
-
case 'testnet':
|
|
75
|
-
return new StacksTestnet();
|
|
76
|
-
default:
|
|
77
|
-
throw new Error(`Invalid network name provided. Must be one of the following: ${StacksNetworks.join(', ')}`);
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
StacksNetwork.fromNameOrNetwork = (network) => {
|
|
81
|
-
if (typeof network !== 'string' && 'version' in network) {
|
|
82
|
-
return network;
|
|
83
|
-
}
|
|
84
|
-
return StacksNetwork.fromName(network);
|
|
85
|
-
};
|
|
86
|
-
export class StacksMainnet extends StacksNetwork {
|
|
87
|
-
constructor(networkUrl = { url: HIRO_MAINNET_DEFAULT }) {
|
|
88
|
-
super(networkUrl);
|
|
89
|
-
this.version = TransactionVersion.Mainnet;
|
|
90
|
-
this.chainId = ChainID.Mainnet;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
export class StacksTestnet extends StacksNetwork {
|
|
94
|
-
constructor(networkUrl = { url: HIRO_TESTNET_DEFAULT }) {
|
|
95
|
-
super(networkUrl);
|
|
96
|
-
this.version = TransactionVersion.Testnet;
|
|
97
|
-
this.chainId = ChainID.Testnet;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
export class StacksMocknet extends StacksNetwork {
|
|
101
|
-
constructor(networkUrl = { url: HIRO_MOCKNET_DEFAULT }) {
|
|
102
|
-
super(networkUrl);
|
|
103
|
-
this.version = TransactionVersion.Testnet;
|
|
104
|
-
this.chainId = ChainID.Testnet;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
1
|
+
export * from './fetch';
|
|
2
|
+
export * from './network';
|
|
107
3
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { TransactionVersion, ChainID } from '@stacks/common';
|
|
2
|
+
import { FetchFn } from './fetch';
|
|
3
|
+
export declare const HIRO_MAINNET_DEFAULT = "https://stacks-node-api.mainnet.stacks.co";
|
|
4
|
+
export declare const HIRO_TESTNET_DEFAULT = "https://stacks-node-api.testnet.stacks.co";
|
|
5
|
+
export declare const HIRO_MOCKNET_DEFAULT = "http://localhost:3999";
|
|
6
|
+
export interface NetworkConfig {
|
|
7
|
+
url: string;
|
|
8
|
+
fetchFn?: FetchFn;
|
|
9
|
+
}
|
|
10
|
+
export declare const StacksNetworks: readonly ["mainnet", "testnet"];
|
|
11
|
+
export declare type StacksNetworkName = typeof StacksNetworks[number];
|
|
12
|
+
export declare class StacksNetwork {
|
|
13
|
+
version: TransactionVersion;
|
|
14
|
+
chainId: ChainID;
|
|
15
|
+
bnsLookupUrl: string;
|
|
16
|
+
broadcastEndpoint: string;
|
|
17
|
+
transferFeeEstimateEndpoint: string;
|
|
18
|
+
transactionFeeEstimateEndpoint: string;
|
|
19
|
+
accountEndpoint: string;
|
|
20
|
+
contractAbiEndpoint: string;
|
|
21
|
+
readOnlyFunctionCallEndpoint: string;
|
|
22
|
+
readonly coreApiUrl: string;
|
|
23
|
+
fetchFn: FetchFn;
|
|
24
|
+
constructor(networkConfig: NetworkConfig);
|
|
25
|
+
static fromName: (networkName: StacksNetworkName) => StacksNetwork;
|
|
26
|
+
static fromNameOrNetwork: (network: StacksNetworkName | StacksNetwork) => StacksNetwork;
|
|
27
|
+
isMainnet: () => boolean;
|
|
28
|
+
getBroadcastApiUrl: () => string;
|
|
29
|
+
getTransferFeeEstimateApiUrl: () => string;
|
|
30
|
+
getTransactionFeeEstimateApiUrl: () => string;
|
|
31
|
+
getAccountApiUrl: (address: string) => string;
|
|
32
|
+
getAbiApiUrl: (address: string, contract: string) => string;
|
|
33
|
+
getReadOnlyFunctionCallApiUrl: (contractAddress: string, contractName: string, functionName: string) => string;
|
|
34
|
+
getInfoUrl: () => string;
|
|
35
|
+
getBlockTimeInfoUrl: () => string;
|
|
36
|
+
getPoxInfoUrl: () => string;
|
|
37
|
+
getRewardsUrl: (address: string, options?: any) => string;
|
|
38
|
+
getRewardsTotalUrl: (address: string) => string;
|
|
39
|
+
getRewardHoldersUrl: (address: string, options?: any) => string;
|
|
40
|
+
getStackerInfoUrl: (contractAddress: string, contractName: string) => string;
|
|
41
|
+
getNameInfo(fullyQualifiedName: string): Promise<any>;
|
|
42
|
+
}
|
|
43
|
+
export declare class StacksMainnet extends StacksNetwork {
|
|
44
|
+
version: TransactionVersion;
|
|
45
|
+
chainId: ChainID;
|
|
46
|
+
constructor(opts?: Partial<NetworkConfig>);
|
|
47
|
+
}
|
|
48
|
+
export declare class StacksTestnet extends StacksNetwork {
|
|
49
|
+
version: TransactionVersion;
|
|
50
|
+
chainId: ChainID;
|
|
51
|
+
constructor(opts?: Partial<NetworkConfig>);
|
|
52
|
+
}
|
|
53
|
+
export declare class StacksMocknet extends StacksNetwork {
|
|
54
|
+
version: TransactionVersion;
|
|
55
|
+
chainId: ChainID;
|
|
56
|
+
constructor(opts?: Partial<NetworkConfig>);
|
|
57
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { TransactionVersion, ChainID } from '@stacks/common';
|
|
2
|
+
import { createFetchFn } from './fetch';
|
|
3
|
+
export const HIRO_MAINNET_DEFAULT = 'https://stacks-node-api.mainnet.stacks.co';
|
|
4
|
+
export const HIRO_TESTNET_DEFAULT = 'https://stacks-node-api.testnet.stacks.co';
|
|
5
|
+
export const HIRO_MOCKNET_DEFAULT = 'http://localhost:3999';
|
|
6
|
+
export const StacksNetworks = ['mainnet', 'testnet'];
|
|
7
|
+
export class StacksNetwork {
|
|
8
|
+
constructor(networkConfig) {
|
|
9
|
+
var _a;
|
|
10
|
+
this.version = TransactionVersion.Mainnet;
|
|
11
|
+
this.chainId = ChainID.Mainnet;
|
|
12
|
+
this.bnsLookupUrl = 'https://stacks-node-api.mainnet.stacks.co';
|
|
13
|
+
this.broadcastEndpoint = '/v2/transactions';
|
|
14
|
+
this.transferFeeEstimateEndpoint = '/v2/fees/transfer';
|
|
15
|
+
this.transactionFeeEstimateEndpoint = '/v2/fees/transaction';
|
|
16
|
+
this.accountEndpoint = '/v2/accounts';
|
|
17
|
+
this.contractAbiEndpoint = '/v2/contracts/interface';
|
|
18
|
+
this.readOnlyFunctionCallEndpoint = '/v2/contracts/call-read';
|
|
19
|
+
this.isMainnet = () => this.version === TransactionVersion.Mainnet;
|
|
20
|
+
this.getBroadcastApiUrl = () => `${this.coreApiUrl}${this.broadcastEndpoint}`;
|
|
21
|
+
this.getTransferFeeEstimateApiUrl = () => `${this.coreApiUrl}${this.transferFeeEstimateEndpoint}`;
|
|
22
|
+
this.getTransactionFeeEstimateApiUrl = () => `${this.coreApiUrl}${this.transactionFeeEstimateEndpoint}`;
|
|
23
|
+
this.getAccountApiUrl = (address) => `${this.coreApiUrl}${this.accountEndpoint}/${address}?proof=0`;
|
|
24
|
+
this.getAbiApiUrl = (address, contract) => `${this.coreApiUrl}${this.contractAbiEndpoint}/${address}/${contract}`;
|
|
25
|
+
this.getReadOnlyFunctionCallApiUrl = (contractAddress, contractName, functionName) => `${this.coreApiUrl}${this.readOnlyFunctionCallEndpoint}/${contractAddress}/${contractName}/${encodeURIComponent(functionName)}`;
|
|
26
|
+
this.getInfoUrl = () => `${this.coreApiUrl}/v2/info`;
|
|
27
|
+
this.getBlockTimeInfoUrl = () => `${this.coreApiUrl}/extended/v1/info/network_block_times`;
|
|
28
|
+
this.getPoxInfoUrl = () => `${this.coreApiUrl}/v2/pox`;
|
|
29
|
+
this.getRewardsUrl = (address, options) => {
|
|
30
|
+
let url = `${this.coreApiUrl}/extended/v1/burnchain/rewards/${address}`;
|
|
31
|
+
if (options) {
|
|
32
|
+
url = `${url}?limit=${options.limit}&offset=${options.offset}`;
|
|
33
|
+
}
|
|
34
|
+
return url;
|
|
35
|
+
};
|
|
36
|
+
this.getRewardsTotalUrl = (address) => `${this.coreApiUrl}/extended/v1/burnchain/rewards/${address}/total`;
|
|
37
|
+
this.getRewardHoldersUrl = (address, options) => {
|
|
38
|
+
let url = `${this.coreApiUrl}/extended/v1/burnchain/reward_slot_holders/${address}`;
|
|
39
|
+
if (options) {
|
|
40
|
+
url = `${url}?limit=${options.limit}&offset=${options.offset}`;
|
|
41
|
+
}
|
|
42
|
+
return url;
|
|
43
|
+
};
|
|
44
|
+
this.getStackerInfoUrl = (contractAddress, contractName) => `${this.coreApiUrl}${this.readOnlyFunctionCallEndpoint}
|
|
45
|
+
${contractAddress}/${contractName}/get-stacker-info`;
|
|
46
|
+
this.coreApiUrl = networkConfig.url;
|
|
47
|
+
this.fetchFn = (_a = networkConfig.fetchFn) !== null && _a !== void 0 ? _a : createFetchFn();
|
|
48
|
+
}
|
|
49
|
+
getNameInfo(fullyQualifiedName) {
|
|
50
|
+
const nameLookupURL = `${this.bnsLookupUrl}/v1/names/${fullyQualifiedName}`;
|
|
51
|
+
return this.fetchFn(nameLookupURL)
|
|
52
|
+
.then(resp => {
|
|
53
|
+
if (resp.status === 404) {
|
|
54
|
+
throw new Error('Name not found');
|
|
55
|
+
}
|
|
56
|
+
else if (resp.status !== 200) {
|
|
57
|
+
throw new Error(`Bad response status: ${resp.status}`);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
return resp.json();
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
.then(nameInfo => {
|
|
64
|
+
if (nameInfo.address) {
|
|
65
|
+
return Object.assign({}, nameInfo, { address: nameInfo.address });
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return nameInfo;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
StacksNetwork.fromName = (networkName) => {
|
|
74
|
+
switch (networkName) {
|
|
75
|
+
case 'mainnet':
|
|
76
|
+
return new StacksMainnet();
|
|
77
|
+
case 'testnet':
|
|
78
|
+
return new StacksTestnet();
|
|
79
|
+
default:
|
|
80
|
+
throw new Error(`Invalid network name provided. Must be one of the following: ${StacksNetworks.join(', ')}`);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
StacksNetwork.fromNameOrNetwork = (network) => {
|
|
84
|
+
if (typeof network !== 'string' && 'version' in network) {
|
|
85
|
+
return network;
|
|
86
|
+
}
|
|
87
|
+
return StacksNetwork.fromName(network);
|
|
88
|
+
};
|
|
89
|
+
export class StacksMainnet extends StacksNetwork {
|
|
90
|
+
constructor(opts) {
|
|
91
|
+
var _a;
|
|
92
|
+
super({
|
|
93
|
+
url: (_a = opts === null || opts === void 0 ? void 0 : opts.url) !== null && _a !== void 0 ? _a : HIRO_MAINNET_DEFAULT,
|
|
94
|
+
fetchFn: opts === null || opts === void 0 ? void 0 : opts.fetchFn,
|
|
95
|
+
});
|
|
96
|
+
this.version = TransactionVersion.Mainnet;
|
|
97
|
+
this.chainId = ChainID.Mainnet;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
export class StacksTestnet extends StacksNetwork {
|
|
101
|
+
constructor(opts) {
|
|
102
|
+
var _a;
|
|
103
|
+
super({
|
|
104
|
+
url: (_a = opts === null || opts === void 0 ? void 0 : opts.url) !== null && _a !== void 0 ? _a : HIRO_TESTNET_DEFAULT,
|
|
105
|
+
fetchFn: opts === null || opts === void 0 ? void 0 : opts.fetchFn,
|
|
106
|
+
});
|
|
107
|
+
this.version = TransactionVersion.Testnet;
|
|
108
|
+
this.chainId = ChainID.Testnet;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
export class StacksMocknet extends StacksNetwork {
|
|
112
|
+
constructor(opts) {
|
|
113
|
+
var _a;
|
|
114
|
+
super({
|
|
115
|
+
url: (_a = opts === null || opts === void 0 ? void 0 : opts.url) !== null && _a !== void 0 ? _a : HIRO_MOCKNET_DEFAULT,
|
|
116
|
+
fetchFn: opts === null || opts === void 0 ? void 0 : opts.fetchFn,
|
|
117
|
+
});
|
|
118
|
+
this.version = TransactionVersion.Testnet;
|
|
119
|
+
this.chainId = ChainID.Testnet;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=network.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network.js","sourceRoot":"","sources":["../../src/network.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAW,MAAM,SAAS,CAAC;AAEjD,MAAM,CAAC,MAAM,oBAAoB,GAAG,2CAA2C,CAAC;AAChF,MAAM,CAAC,MAAM,oBAAoB,GAAG,2CAA2C,CAAC;AAChF,MAAM,CAAC,MAAM,oBAAoB,GAAG,uBAAuB,CAAC;AAO5D,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,S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|
|
3
|
+
export declare const setFetchOptions: (ops: RequestInit) => RequestInit;
|
|
4
|
+
export declare type FetchFn = (url: string, init?: RequestInit) => Promise<Response>;
|
|
5
|
+
export interface RequestContext {
|
|
6
|
+
fetch: FetchFn;
|
|
7
|
+
url: string;
|
|
8
|
+
init: RequestInit;
|
|
9
|
+
}
|
|
10
|
+
export interface ResponseContext {
|
|
11
|
+
fetch: FetchFn;
|
|
12
|
+
url: string;
|
|
13
|
+
init: RequestInit;
|
|
14
|
+
response: Response;
|
|
15
|
+
}
|
|
16
|
+
export interface FetchParams {
|
|
17
|
+
url: string;
|
|
18
|
+
init: RequestInit;
|
|
19
|
+
}
|
|
20
|
+
export interface FetchMiddleware {
|
|
21
|
+
pre?: (context: RequestContext) => PromiseLike<FetchParams | void> | FetchParams | void;
|
|
22
|
+
post?: (context: ResponseContext) => Promise<Response | void> | Response | void;
|
|
23
|
+
}
|
|
24
|
+
export interface ApiKeyMiddlewareOpts {
|
|
25
|
+
host?: RegExp | string;
|
|
26
|
+
httpHeader?: string;
|
|
27
|
+
apiKey: string;
|
|
28
|
+
}
|
|
29
|
+
export declare function createApiKeyMiddleware({ apiKey, host, httpHeader, }: ApiKeyMiddlewareOpts): FetchMiddleware;
|
|
30
|
+
export declare function createFetchFn(fetchLib: FetchFn, ...middleware: FetchMiddleware[]): FetchFn;
|
|
31
|
+
export declare function createFetchFn(...middleware: FetchMiddleware[]): FetchFn;
|