Skip to content

Commit

Permalink
fix(sequenceProvider): feedergatewayUrl and gatewayUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkelawala committed Aug 8, 2022
1 parent 7e1d5b2 commit e236d23
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
32 changes: 31 additions & 1 deletion __tests__/sequencerProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SequencerProvider } from '../src';
import { Contract, Provider, SequencerProvider, stark } from '../src';
import { toBN } from '../src/utils/number';
import {
compiledErc20,
describeIfNotDevnet,
Expand All @@ -8,9 +9,17 @@ import {

describeIfSequencer('SequencerProvider', () => {
let provider: SequencerProvider;
let customSequencerProvider: Provider;

beforeAll(async () => {
provider = getTestProvider() as SequencerProvider;
customSequencerProvider = new Provider({
sequencer: {
baseUrl: 'https://alpha4.starknet.io',
feederGatewayUrl: 'feeder_gateway',
gatewayUrl: 'gateway',
}, // Similar to arguements used in docs
});
});

describe('Gateway specific methods', () => {
Expand Down Expand Up @@ -42,4 +51,25 @@ describeIfSequencer('SequencerProvider', () => {
});
});
});

describe('Test calls with Custom Sequencer Provider', () => {
let erc20: Contract;
const wallet = stark.randomAddress();

beforeAll(async () => {
const { contract_address, transaction_hash } = await customSequencerProvider.deployContract({
contract: compiledErc20,
});

await customSequencerProvider.waitForTransaction(transaction_hash);
erc20 = new Contract(compiledErc20.abi, contract_address, customSequencerProvider);
});

test('Check ERC20 balance using Custom Sequencer Provider', async () => {
const result = await erc20.balance_of(wallet);
const [res] = result;
expect(res).toStrictEqual(toBN(0));
expect(res).toStrictEqual(result.res);
});
});
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export * as uint256 from './utils/uint256';
export * as shortString from './utils/shortString';
export * as typedData from './utils/typedData';
export * from './utils/address';
export * from './utils/url';
21 changes: 18 additions & 3 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from
import { parseContract, wait } from '../utils/provider';
import { SequencerAPIResponseParser } from '../utils/responseParser/sequencer';
import { randomAddress } from '../utils/stark';
import { isUrl } from '../utils/url';
import { GatewayError, HttpError } from './errors';
import { ProviderInterface } from './interface';
import { BlockIdentifier, getFormattedBlockIdentifier } from './utils';
Expand Down Expand Up @@ -74,9 +75,13 @@ export class SequencerProvider implements ProviderInterface {
this.gatewayUrl = urljoin(this.baseUrl, 'gateway');
} else {
this.baseUrl = optionsOrProvider.baseUrl;
this.feederGatewayUrl =
optionsOrProvider.feederGatewayUrl ?? urljoin(this.baseUrl, 'feeder_gateway');
this.gatewayUrl = optionsOrProvider.gatewayUrl ?? urljoin(this.baseUrl, 'gateway');
this.feederGatewayUrl = this.parseUrlOrPath(
this.baseUrl,
'feeder_gateway',
optionsOrProvider.feederGatewayUrl
);
this.gatewayUrl = this.parseUrlOrPath(this.baseUrl, 'gateway', optionsOrProvider.gatewayUrl);

this.chainId =
optionsOrProvider.chainId ??
SequencerProvider.getChainIdFromBaseUrl(optionsOrProvider.baseUrl);
Expand Down Expand Up @@ -106,6 +111,16 @@ export class SequencerProvider implements ProviderInterface {
return StarknetChainId.TESTNET;
}

private parseUrlOrPath(baseUrl: string, defaultPath: string, urlOrPath?: string) {
if (urlOrPath) {
if (isUrl(urlOrPath)) {
return urlOrPath;
}
return urljoin(baseUrl, urlOrPath);
}
return urljoin(baseUrl, defaultPath);
}

private getFetchUrl(endpoint: keyof Sequencer.Endpoints) {
const gatewayUrlEndpoints = ['add_transaction'];

Expand Down
44 changes: 44 additions & 0 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* RegExps.
* A URL must match #1 and then at least one of #2/#3.
* Use two levels of REs to avoid REDOS.
*/

/**
* Inspired from https://github.com/segmentio/is-url
*/

const protocolAndDomainRE = /^(?:\w+:)?\/\/(\S+)$/;

const localhostDomainRE = /^localhost[:?\d]*(?:[^:?\d]\S*)?$/;
const nonLocalhostDomainRE = /^[^\s.]+\.\S{2,}$/;

/**
* Loosely validate a URL `string`.
* @param {String} s
* @return {Boolean}
*/
export function isUrl(s: string): boolean {
if (typeof s !== 'string') {
return false;
}

const match = s.match(protocolAndDomainRE);
if (!match) {
return false;
}

const everythingAfterProtocol = match[1];
if (!everythingAfterProtocol) {
return false;
}

if (
localhostDomainRE.test(everythingAfterProtocol) ||
nonLocalhostDomainRE.test(everythingAfterProtocol)
) {
return true;
}

return false;
}

0 comments on commit e236d23

Please sign in to comment.