Skip to content

Commit

Permalink
fix: use urljoin
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Nov 30, 2021
1 parent ea96243 commit 4f1a040
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
24 changes: 17 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@types/json-bigint": "^1.0.1",
"@types/minimalistic-assert": "^1.0.1",
"@types/pako": "^1.0.2",
"@types/url-join": "^4.0.1",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^7.32.0",
Expand All @@ -65,7 +66,8 @@
"hash.js": "^1.1.7",
"json-bigint": "^1.0.0",
"minimalistic-assert": "^1.0.1",
"pako": "^2.0.4"
"pako": "^2.0.4",
"url-join": "^4.0.1"
},
"lint-staged": {
"*.ts": "eslint --cache --fix",
Expand Down
39 changes: 24 additions & 15 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios';
import urljoin from 'url-join';

import {
AddTransactionResponse,
Expand Down Expand Up @@ -49,15 +50,15 @@ export class Provider implements ProviderInterface {
? optionsOrProvider.baseUrl
: Provider.getNetworkFromName(optionsOrProvider.network);
this.baseUrl = baseUrl;
this.feederGatewayUrl = `${baseUrl}/feeder_gateway`;
this.gatewayUrl = `${baseUrl}/gateway`;
this.feederGatewayUrl = urljoin(baseUrl, 'feeder_gateway');
this.gatewayUrl = urljoin(baseUrl, 'gateway');
}
}

protected static getNetworkFromName(name: NetworkName) {
switch (name) {
case 'mainnet-alpha':
return 'http://alpha-mainnet.starknet.io/';
return 'https://alpha-mainnet.starknet.io';
case 'georli-alpha':
default:
return 'https://alpha4.starknet.io';
Expand All @@ -72,7 +73,7 @@ export class Provider implements ProviderInterface {
*/
public async getContractAddresses(): Promise<GetContractAddressesResponse> {
const { data } = await axios.get<GetContractAddressesResponse>(
`${this.feederGatewayUrl}/get_contract_addresses`
urljoin(this.feederGatewayUrl, 'get_contract_addresses')
);
return data;
}
Expand All @@ -91,7 +92,7 @@ export class Provider implements ProviderInterface {
blockId?: number
): Promise<CallContractResponse> {
const { data } = await axios.post<CallContractResponse>(
`${this.feederGatewayUrl}/call_contract?blockId=${blockId ?? 'null'}`,
urljoin(this.feederGatewayUrl, 'call_contract', `?blockId=${blockId ?? 'null'}`),
{
signature: [],
calldata: [],
Expand All @@ -111,7 +112,7 @@ export class Provider implements ProviderInterface {
*/
public async getBlock(blockId?: number): Promise<GetBlockResponse> {
const { data } = await axios.get<GetBlockResponse>(
`${this.feederGatewayUrl}/get_block?blockId=${blockId ?? 'null'}`
urljoin(this.feederGatewayUrl, 'get_block', `?blockId=${blockId ?? 'null'}`)
);
return data;
}
Expand All @@ -127,9 +128,11 @@ export class Provider implements ProviderInterface {
*/
public async getCode(contractAddress: string, blockId?: number): Promise<GetCodeResponse> {
const { data } = await axios.get<GetCodeResponse>(
`${this.feederGatewayUrl}/get_code?contractAddress=${contractAddress}&blockId=${
blockId ?? 'null'
}`
urljoin(
this.feederGatewayUrl,
'get_code',
`?contractAddress=${contractAddress}&blockId=${blockId ?? 'null'}`
)
);
return data;
}
Expand All @@ -151,9 +154,11 @@ export class Provider implements ProviderInterface {
blockId?: number
): Promise<object> {
const { data } = await axios.get<object>(
`${
this.feederGatewayUrl
}/get_storage_at?contractAddress=${contractAddress}&key=${key}&blockId=${blockId ?? 'null'}`
urljoin(
this.feederGatewayUrl,
'get_storage_at',
`?contractAddress=${contractAddress}&key=${key}&blockId=${blockId ?? 'null'}`
)
);
return data;
}
Expand All @@ -169,7 +174,11 @@ export class Provider implements ProviderInterface {
public async getTransactionStatus(txHash: BigNumberish): Promise<GetTransactionStatusResponse> {
const txHashBn = toBN(txHash);
const { data } = await axios.get<GetTransactionStatusResponse>(
`${this.feederGatewayUrl}/get_transaction_status?transactionHash=${toHex(txHashBn)}`
urljoin(
this.feederGatewayUrl,
'get_transaction_status',
`?transactionHash=${toHex(txHashBn)}`
)
);
return data;
}
Expand All @@ -185,7 +194,7 @@ export class Provider implements ProviderInterface {
public async getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse> {
const txHashBn = toBN(txHash);
const { data } = await axios.get<GetTransactionResponse>(
`${this.feederGatewayUrl}/get_transaction?transactionHash=${toHex(txHashBn)}`
urljoin(this.feederGatewayUrl, 'get_transaction', `?transactionHash=${toHex(txHashBn)}`)
);
return data;
}
Expand All @@ -203,7 +212,7 @@ export class Provider implements ProviderInterface {
const contract_address_salt = tx.type === 'DEPLOY' && toHex(toBN(tx.contract_address_salt));

const { data } = await axios.post<AddTransactionResponse>(
`${this.gatewayUrl}/add_transaction`,
urljoin(this.gatewayUrl, 'add_transaction'),
stringify({
...tx, // the tx can contain BigInts, so we use our own `stringify`
...(Array.isArray(signature) && { signature }), // not needed on deploy tx
Expand Down

0 comments on commit 4f1a040

Please sign in to comment.