Skip to content

Commit

Permalink
fix: avoid use of getters to enable safer serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Aug 23, 2021
1 parent 7b0c8e5 commit 6f626a1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 27 deletions.
25 changes: 15 additions & 10 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,9 @@ function balance(network: CLINetworkAdapter, args: string[]): Promise<string> {
}

// temporary hack to use network config from stacks-transactions lib
const txNetwork = network.isMainnet() ? new StacksMainnet() : new StacksTestnet();
txNetwork.coreApiUrl = network.legacyNetwork.blockstackAPIUrl;
const txNetwork = network.isMainnet()
? new StacksMainnet({ url: network.legacyNetwork.blockstackAPIUrl })
: new StacksTestnet({ url: network.legacyNetwork.blockstackAPIUrl });

return fetch(txNetwork.getAccountApiUrl(address))
.then(response => {
Expand Down Expand Up @@ -570,8 +571,9 @@ async function sendTokens(network: CLINetworkAdapter, args: string[]): Promise<s
}

// temporary hack to use network config from stacks-transactions lib
const txNetwork = network.isMainnet() ? new StacksMainnet() : new StacksTestnet();
txNetwork.coreApiUrl = network.legacyNetwork.blockstackAPIUrl;
const txNetwork = network.isMainnet()
? new StacksMainnet({ url: network.legacyNetwork.blockstackAPIUrl })
: new StacksTestnet({ url: network.legacyNetwork.blockstackAPIUrl });

const options: SignedTokenTransferOptions = {
recipient: recipientAddress,
Expand Down Expand Up @@ -630,8 +632,9 @@ async function contractDeploy(network: CLINetworkAdapter, args: string[]): Promi
const source = fs.readFileSync(sourceFile).toString();

// temporary hack to use network config from stacks-transactions lib
const txNetwork = network.isMainnet() ? new StacksMainnet() : new StacksTestnet();
txNetwork.coreApiUrl = network.legacyNetwork.blockstackAPIUrl;
const txNetwork = network.isMainnet()
? new StacksMainnet({ url: network.legacyNetwork.blockstackAPIUrl })
: new StacksTestnet({ url: network.legacyNetwork.blockstackAPIUrl });

const options: ContractDeployOptions = {
contractName,
Expand Down Expand Up @@ -690,8 +693,9 @@ async function contractFunctionCall(network: CLINetworkAdapter, args: string[]):
const privateKey = args[5];

// temporary hack to use network config from stacks-transactions lib
const txNetwork = network.isMainnet() ? new StacksMainnet() : new StacksTestnet();
txNetwork.coreApiUrl = network.legacyNetwork.blockstackAPIUrl;
const txNetwork = network.isMainnet()
? new StacksMainnet({ url: network.legacyNetwork.blockstackAPIUrl })
: new StacksTestnet({ url: network.legacyNetwork.blockstackAPIUrl });

let abi: ClarityAbi;
let abiArgs: ClarityFunctionArg[];
Expand Down Expand Up @@ -776,8 +780,9 @@ async function readOnlyContractFunctionCall(
const senderAddress = args[3];

// temporary hack to use network config from stacks-transactions lib
const txNetwork = network.isMainnet() ? new StacksMainnet() : new StacksTestnet();
txNetwork.coreApiUrl = network.legacyNetwork.blockstackAPIUrl;
const txNetwork = network.isMainnet()
? new StacksMainnet({ url: network.legacyNetwork.blockstackAPIUrl })
: new StacksTestnet({ url: network.legacyNetwork.blockstackAPIUrl });

let abi: ClarityAbi;
let abiArgs: ClarityFunctionArg[];
Expand Down
4 changes: 1 addition & 3 deletions packages/keychain/src/wallet/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ export class WalletSigner {
}

getNetwork() {
const network = new StacksTestnet();
network.coreApiUrl = 'https://sidecar.staging.blockstack.xyz';
return network;
return new StacksTestnet();
}

async fetchAccount({
Expand Down
11 changes: 3 additions & 8 deletions packages/network/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,13 @@ export class StacksMainnet implements StacksNetwork {
accountEndpoint = '/v2/accounts';
contractAbiEndpoint = '/v2/contracts/interface';
readOnlyFunctionCallEndpoint = '/v2/contracts/call-read';
private _coreApiUrl: string;

get coreApiUrl() {
return this._coreApiUrl;
}
set coreApiUrl(_url: string) {
throw new Error('Cannot modify property `coreApiUrl` after object initialization');
}
readonly coreApiUrl: string;

constructor(networkUrl: NetworkConfig = { url: HIRO_MAINNET_DEFAULT }) {
this._coreApiUrl = networkUrl.url;
this.coreApiUrl = networkUrl.url;
}

isMainnet = () => this.version === TransactionVersion.Mainnet;
getBroadcastApiUrl = () => `${this.coreApiUrl}${this.broadcastEndpoint}`;
getTransferFeeEstimateApiUrl = () => `${this.coreApiUrl}${this.transferFeeEstimateEndpoint}`;
Expand Down
6 changes: 0 additions & 6 deletions packages/network/tests/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,4 @@ describe('Setting coreApiUrl', () => {
const customNET = new StacksMainnet({ url: customURL });
expect(customNET.coreApiUrl).toEqual(customURL);
});
test('it prevents changing url after initialisation', () => {
const network = new StacksMainnet({ url: 'https://legiturl.com' });
// @ts-ignore
expect(() => (network.coreApiUrl = 'https://dodgyurl.com')).toThrowError();
expect(network.coreApiUrl).toEqual('https://legiturl.com');
});
});

1 comment on commit 6f626a1

@vercel
Copy link

@vercel vercel bot commented on 6f626a1 Aug 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.