Skip to content

Commit

Permalink
fix: network fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aulneau committed Dec 14, 2021
1 parent 6e84dff commit 4483653
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
21 changes: 12 additions & 9 deletions src/api/read-only/call-read-only-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { ReadOnlyFunctionOptions, ReadOnlyFunctionResponse } from './types'
*/
export async function callReadOnlyFunction<T extends ClarityValue>(
options: ReadOnlyFunctionOptions
): Promise<ClarityValue> {
): Promise<T> {
const {
contractName,
contractAddress,
Expand All @@ -28,16 +28,19 @@ export async function callReadOnlyFunction<T extends ClarityValue>(
senderAddress = contractAddress,
} = options;

let isMainnet = true;
try {
isMainnet = isMainnetAddress(contractAddress);
} catch (e) {
throw new Error(
'[micro-stacks] callReadOnlyFunction -> Incorrect Stacks addressed passed to contractAddress'
);
let network = options.network;
if (!options.network) {
try {
network = isMainnetAddress(contractAddress) ? new StacksMainnet() : new StacksTestnet();
} catch (e) {
console.error(e);
throw new Error(
'[micro-stacks] callReadOnlyFunction -> Incorrect Stacks addressed passed to contractAddress'
);
}
}

const network = options.network || isMainnet ? new StacksMainnet() : new StacksTestnet();
if (!network) throw Error('[micro-stacks] callReadOnlyFunction -> no network defined');

const url = network.getReadOnlyFunctionCallApiUrl(contractAddress, contractName, functionName);

Expand Down
26 changes: 13 additions & 13 deletions src/api/smartcontracts/fetchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as TRANSACTION_EVENT_SMART_CONTRACT_LOG_200_RESPONSE from '../../../tes
//import * as TRANSACTION_EVENT_FUNGIBLE_ASSET_200_RESPONSE from '../../../../tests/mocks/api/smartcontracts/TransactionEventFungibleAsset200.json';
//import * as TRANSACTION_EVENT_NON_FUNGIBLE_ASSET_200_RESPONSE from '../../../../tests/mocks/api/smartcontracts/TransactionEventNonFungibleAsset200.json';
import * as CONTRACT_SOURCE_200_RESPONSE from '../../../tests/mocks/api/smartcontracts/ContractSourceResponse200.json';

const CONTRACT_DATA_MAP_ENTRY = {
data: '0x0a0c000000010a6d6f6e737465722d69640100000000000000000000000000000001',
proof: '0x123',
Expand All @@ -19,7 +20,6 @@ import {
fetchContractEventsById,
fetchContractInterface,
fetchContractSource,
fetchReadOnlyFunction,
} from './fetchers';
import { contractEndpoint, contractsEndpoint, v2Endpoint } from '../utils';
import { HIRO_TESTNET_DEFAULT } from 'micro-stacks/network';
Expand Down Expand Up @@ -147,16 +147,16 @@ describe('smartcontracts fetchers', () => {
});

// TODO: the spec returns 'null'
test(fetchReadOnlyFunction.name, async () => {
const args = {
url: HIRO_TESTNET_DEFAULT,
contractName: contract_name,
contractAddress: contract_address,
functionName: functionName,
functionArgs: functionArgs,
senderAddress: senderAddress,
};
const data = await fetchReadOnlyFunction(args);
expect(data).toEqual({ body: null });
});
// test(callReadOnlyFunction.name, async () => {
// const args = {
// url: HIRO_TESTNET_DEFAULT,
// contractName: contract_name,
// contractAddress: contract_address,
// functionName: functionName,
// functionArgs: functionArgs,
// senderAddress: senderAddress,
// };
// const data = await callReadOnlyFunction(args);
// expect(data).toEqual({ body: null });
// });
});
38 changes: 23 additions & 15 deletions src/network/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export const HIRO_TESTNET_DEFAULT = 'https://stacks-node-api.testnet.stacks.co';
export const HIRO_MOCKNET_DEFAULT = 'http://localhost:3999';

export interface NetworkConfig {
url: string;
// TODO: deprecate
url?: string;
coreApiUrl?: string;
bnsLookupUrl?: string;
fetcher?: Fetcher;
}

Expand Down Expand Up @@ -55,12 +58,12 @@ export interface StacksNetwork {
export class StacksMainnet implements StacksNetwork {
version = TransactionVersion.Mainnet;
chainId = ChainID.Mainnet;
bnsLookupUrl = 'https://stacks-node-api.mainnet.stacks.co';
broadcastEndpoint = '/v2/transactions';
transferFeeEstimateEndpoint = '/v2/fees/transfer';
accountEndpoint = '/v2/accounts';
contractAbiEndpoint = '/v2/contracts/interface';
readOnlyFunctionCallEndpoint = '/v2/contracts/call-read';
bnsLookupUrl: string;
private _coreApiUrl: string;
private fetcher: Fetcher;

Expand All @@ -73,47 +76,52 @@ export class StacksMainnet implements StacksNetwork {
}

constructor(networkConfig: NetworkConfig = { url: HIRO_MAINNET_DEFAULT }) {
this._coreApiUrl = networkConfig.url;
if (!networkConfig.url && !networkConfig.coreApiUrl)
throw Error('[miro-stacks] Network initialized with no api url');
this._coreApiUrl = (networkConfig.url || networkConfig.coreApiUrl) as string;
this.bnsLookupUrl = (networkConfig.bnsLookupUrl ||
networkConfig.url ||
networkConfig.coreApiUrl) as string;
this.fetcher = networkConfig.fetcher || fetchPrivate;
}

getCoreApiUrl = () => this._coreApiUrl;
isMainnet = () => this.version === TransactionVersion.Mainnet;
getBroadcastApiUrl = () => `${this.coreApiUrl}${this.broadcastEndpoint}`;
getTransferFeeEstimateApiUrl = () => `${this.coreApiUrl}${this.transferFeeEstimateEndpoint}`;
getBroadcastApiUrl = () => `${this.getCoreApiUrl()}${this.broadcastEndpoint}`;
getTransferFeeEstimateApiUrl = () => `${this.getCoreApiUrl()}${this.transferFeeEstimateEndpoint}`;
getAccountApiUrl = (address: string) =>
`${this.coreApiUrl}${this.accountEndpoint}/${address}?proof=0`;
`${this.getCoreApiUrl()}${this.accountEndpoint}/${address}?proof=0`;
getAbiApiUrl = (address: string, contract: string) =>
`${this.coreApiUrl}${this.contractAbiEndpoint}/${address}/${contract}`;
`${this.getCoreApiUrl()}${this.contractAbiEndpoint}/${address}/${contract}`;
getReadOnlyFunctionCallApiUrl = (
contractAddress: string,
contractName: string,
functionName: string
) =>
`${this.coreApiUrl}${
`${this.getCoreApiUrl()}${
this.readOnlyFunctionCallEndpoint
}/${contractAddress}/${contractName}/${encodeURIComponent(functionName)}`;
getInfoUrl = () => `${this.coreApiUrl}/v2/info`;
getBlockTimeInfoUrl = () => `${this.coreApiUrl}/extended/v1/info/network_block_times`;
getPoxInfoUrl = () => `${this.coreApiUrl}/v2/pox`;
getInfoUrl = () => `${this.getCoreApiUrl()}/v2/info`;
getBlockTimeInfoUrl = () => `${this.getCoreApiUrl()}/extended/v1/info/network_block_times`;
getPoxInfoUrl = () => `${this.getCoreApiUrl()}/v2/pox`;
getRewardsUrl = (address: string, options?: any) => {
let url = `${this.coreApiUrl}/extended/v1/burnchain/rewards/${address}`;
let url = `${this.getCoreApiUrl()}/extended/v1/burnchain/rewards/${address}`;
if (options) {
url = `${url}?limit=${options.limit}&offset=${options.offset}`;
}
return url;
};
getRewardsTotalUrl = (address: string) =>
`${this.coreApiUrl}/extended/v1/burnchain/rewards/${address}/total`;
`${this.getCoreApiUrl()}/extended/v1/burnchain/rewards/${address}/total`;
getRewardHoldersUrl = (address: string, options?: any) => {
let url = `${this.coreApiUrl}/extended/v1/burnchain/reward_slot_holders/${address}`;
let url = `${this.getCoreApiUrl()}/extended/v1/burnchain/reward_slot_holders/${address}`;
if (options) {
url = `${url}?limit=${options.limit}&offset=${options.offset}`;
}
return url;
};
getStackerInfoUrl = (contractAddress: string, contractName: string) =>
`${this.coreApiUrl}${this.readOnlyFunctionCallEndpoint}
`${this.getCoreApiUrl()}${this.readOnlyFunctionCallEndpoint}
${contractAddress}/${contractName}/get-stacker-info`;

getNameInfo(fullyQualifiedName: string) {
Expand Down
4 changes: 3 additions & 1 deletion src/transactions/builders/builders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,12 +1537,14 @@ describe('tx builders', function () {
network,
senderAddress,
};
fetchMock.mockOnce(`{"okay": true, "result": "0x${mockResult}"}`);
const apiUrl = network.getReadOnlyFunctionCallApiUrl(
contractAddress,
contractName,
functionName
);

fetchMock.mockOnce(`{"okay": true, "result": "0x${mockResult}"}`, { url: apiUrl });

const result = await callReadOnlyFunction(options);
expect(fetchMock.mock.calls.length).toEqual(1);
expect(fetchMock.mock.calls[0][0]).toEqual(apiUrl);
Expand Down

0 comments on commit 4483653

Please sign in to comment.