Skip to content

Commit

Permalink
fix: rename gateway to sequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
badurinantun committed Jul 13, 2022
1 parent b2fc530 commit b7291e6
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 41 deletions.
2 changes: 1 addition & 1 deletion __tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const BASE_URL = process.env.TEST_PROVIDER_BASE_URL || DEFAULT_TEST_PROVIDER_BAS
export const IS_DEVNET = !BASE_URL.includes('starknet.io');

export const getTestProvider = () => {
const provider = new Provider({ gateway: { baseUrl: BASE_URL } });
const provider = new Provider({ sequencer: { baseUrl: BASE_URL } });

if (IS_DEVNET) {
// accelerate the tests when running locally
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { DeclareContractResponse, DeployContractResponse, GatewayProvider } from '../src';
import { DeclareContractResponse, DeployContractResponse, SequencerProvider } from '../src';
import { compileCalldata } from '../src/utils/stark';
import { compiledErc20 } from './fixtures';

describe('GatewayProvider', () => {
let provider: GatewayProvider;
describe('SequencerProvider', () => {
let provider: SequencerProvider;

beforeAll(async () => {
provider = new GatewayProvider();
provider = new SequencerProvider();
});

describe('Provider methods', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import {
InvokeFunctionResponse,
} from '../types';
import { BigNumberish } from '../utils/number';
import { GatewayProvider, GatewayProviderOptions } from './gateway';
import { ProviderInterface } from './interface';
import { RPCProvider, RpcProviderOptions } from './rpc';
import { SequencerProvider, SequencerProviderOptions } from './sequencer';
import { BlockIdentifier } from './utils';

export interface ProviderOptions {
gateway?: GatewayProviderOptions;
sequencer?: SequencerProviderOptions;
rpc?: RpcProviderOptions;
}

Expand All @@ -34,7 +34,7 @@ export class Provider implements ProviderInterface {
} else if (providerOrOptions && providerOrOptions.rpc) {
this.provider = new RPCProvider(providerOrOptions.rpc);
} else {
this.provider = new GatewayProvider(providerOrOptions?.gateway);
this.provider = new SequencerProvider(providerOrOptions?.sequencer);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Provider } from './default';

export * from './default';
export * from './errors';
export * from './gateway';
export * from './sequencer';
export * from './interface';
export * from './rpc';

Expand Down
40 changes: 20 additions & 20 deletions src/provider/gateway.ts → src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
DeployContractPayload,
DeployContractResponse,
EstimateFeeResponse,
Gateway,
GetBlockResponse,
GetContractAddressesResponse,
GetTransactionReceiptResponse,
Expand All @@ -20,12 +19,13 @@ import {
Invocation,
InvocationsDetails,
InvokeFunctionResponse,
Sequencer,
} from '../types';
import { getSelectorFromName } from '../utils/hash';
import { parse, parseAlwaysAsBig, stringify } from '../utils/json';
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
import { parseContract, wait } from '../utils/provider';
import { GatewayAPIResponseParser } from '../utils/responseParser/gateway';
import { SequencerAPIResponseParser } from '../utils/responseParser/sequencer';
import { randomAddress } from '../utils/stark';
import { GatewayError, HttpError } from './errors';
import { ProviderInterface } from './interface';
Expand All @@ -42,7 +42,7 @@ function isEmptyQueryObject(obj?: Record<any, any>): obj is undefined {
);
}

export type GatewayProviderOptions =
export type SequencerProviderOptions =
| { network: NetworkName }
| {
baseUrl: string;
Expand All @@ -51,7 +51,7 @@ export type GatewayProviderOptions =
chainId?: StarknetChainId;
};

export class GatewayProvider implements ProviderInterface {
export class SequencerProvider implements ProviderInterface {
public baseUrl: string;

public feederGatewayUrl: string;
Expand All @@ -60,12 +60,12 @@ export class GatewayProvider implements ProviderInterface {

public chainId: StarknetChainId;

private responseParser = new GatewayAPIResponseParser();
private responseParser = new SequencerAPIResponseParser();

constructor(optionsOrProvider: GatewayProviderOptions = { network: 'goerli-alpha' }) {
constructor(optionsOrProvider: SequencerProviderOptions = { network: 'goerli-alpha' }) {
if ('network' in optionsOrProvider) {
this.baseUrl = GatewayProvider.getNetworkFromName(optionsOrProvider.network);
this.chainId = GatewayProvider.getChainIdFromBaseUrl(this.baseUrl);
this.baseUrl = SequencerProvider.getNetworkFromName(optionsOrProvider.network);
this.chainId = SequencerProvider.getChainIdFromBaseUrl(this.baseUrl);
this.feederGatewayUrl = urljoin(this.baseUrl, 'feeder_gateway');
this.gatewayUrl = urljoin(this.baseUrl, 'gateway');
} else {
Expand All @@ -75,7 +75,7 @@ export class GatewayProvider implements ProviderInterface {
this.gatewayUrl = optionsOrProvider.gatewayUrl ?? urljoin(this.baseUrl, 'gateway');
this.chainId =
optionsOrProvider.chainId ??
GatewayProvider.getChainIdFromBaseUrl(optionsOrProvider.baseUrl);
SequencerProvider.getChainIdFromBaseUrl(optionsOrProvider.baseUrl);
}
}

Expand All @@ -102,13 +102,13 @@ export class GatewayProvider implements ProviderInterface {
return StarknetChainId.TESTNET;
}

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

return gatewayUrlEndpoints.includes(endpoint) ? this.gatewayUrl : this.feederGatewayUrl;
}

private getFetchMethod(endpoint: keyof Gateway.Endpoints) {
private getFetchMethod(endpoint: keyof Sequencer.Endpoints) {
const postMethodEndpoints = ['add_transaction', 'call_contract', 'estimate_fee'];

return postMethodEndpoints.includes(endpoint) ? 'POST' : 'GET';
Expand Down Expand Up @@ -140,17 +140,17 @@ export class GatewayProvider implements ProviderInterface {
}

// typesafe fetch
protected async fetchEndpoint<T extends keyof Gateway.Endpoints>(
protected async fetchEndpoint<T extends keyof Sequencer.Endpoints>(
endpoint: T,
// typescript type magiuc to create a nice fitting function interface
...[query, request]: Gateway.Endpoints[T]['QUERY'] extends never
? Gateway.Endpoints[T]['REQUEST'] extends never
...[query, request]: Sequencer.Endpoints[T]['QUERY'] extends never
? Sequencer.Endpoints[T]['REQUEST'] extends never
? [] // when no query and no request is needed, we can omit the query and request parameters
: [undefined, Gateway.Endpoints[T]['REQUEST']]
: Gateway.Endpoints[T]['REQUEST'] extends never
? [Gateway.Endpoints[T]['QUERY']] // when no request is needed, we can omit the request parameter
: [Gateway.Endpoints[T]['QUERY'], Gateway.Endpoints[T]['REQUEST']] // when both query and request are needed, we cant omit anything
): Promise<Gateway.Endpoints[T]['RESPONSE']> {
: [undefined, Sequencer.Endpoints[T]['REQUEST']]
: Sequencer.Endpoints[T]['REQUEST'] extends never
? [Sequencer.Endpoints[T]['QUERY']] // when no request is needed, we can omit the request parameter
: [Sequencer.Endpoints[T]['QUERY'], Sequencer.Endpoints[T]['REQUEST']] // when both query and request are needed, we cant omit anything
): Promise<Sequencer.Endpoints[T]['RESPONSE']> {
const baseUrl = this.getFetchUrl(endpoint);
const method = this.getFetchMethod(endpoint);
const queryString = this.getQueryString(query);
Expand Down Expand Up @@ -186,7 +186,7 @@ export class GatewayProvider implements ProviderInterface {
return v;
});
}
return parse(textResponse) as Gateway.Endpoints[T]['RESPONSE'];
return parse(textResponse) as Sequencer.Endpoints[T]['RESPONSE'];
} catch (err) {
// rethrow custom errors
if (err instanceof GatewayError || err instanceof HttpError) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export type Overrides = {
signature?: Signature;
};

export * from './gateway';
export * from './sequencer';
export * from './rpc';
2 changes: 1 addition & 1 deletion src/types/api/gateway.ts → src/types/api/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export type RawArgs = {
[inputName: string]: string | string[] | { type: 'struct'; [k: string]: BigNumberish };
};

export namespace Gateway {
export namespace Sequencer {
export type DeclareTransaction = {
type: 'DECLARE';
contract_class: ContractClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import {
DeclareContractResponse,
DeployContractResponse,
EstimateFeeResponse,
Gateway,
GetBlockResponse,
GetTransactionReceiptResponse,
GetTransactionResponse,
InvokeFunctionResponse,
Sequencer,
} from '../../types';
import { toBN } from '../number';
import { ResponseParser } from '.';

export class GatewayAPIResponseParser extends ResponseParser {
public parseGetBlockResponse(res: Gateway.GetBlockResponse): GetBlockResponse {
export class SequencerAPIResponseParser extends ResponseParser {
public parseGetBlockResponse(res: Sequencer.GetBlockResponse): GetBlockResponse {
return {
accepted_time: res.timestamp,
block_hash: res.block_hash,
Expand All @@ -30,7 +30,9 @@ export class GatewayAPIResponseParser extends ResponseParser {
};
}

public parseGetTransactionResponse(res: Gateway.GetTransactionResponse): GetTransactionResponse {
public parseGetTransactionResponse(
res: Sequencer.GetTransactionResponse
): GetTransactionResponse {
return {
calldata: 'calldata' in res.transaction ? (res.transaction.calldata as Array<string>) : [],
contract_address:
Expand All @@ -55,7 +57,7 @@ export class GatewayAPIResponseParser extends ResponseParser {
}

public parseGetTransactionReceiptResponse(
res: Gateway.TransactionReceiptResponse
res: Sequencer.TransactionReceiptResponse
): GetTransactionReceiptResponse {
return {
transaction_hash: res.transaction_hash,
Expand All @@ -68,33 +70,37 @@ export class GatewayAPIResponseParser extends ResponseParser {
};
}

public parseFeeEstimateResponse(res: Gateway.EstimateFeeResponse): EstimateFeeResponse {
public parseFeeEstimateResponse(res: Sequencer.EstimateFeeResponse): EstimateFeeResponse {
return {
overall_fee: toBN(res.amount),
};
}

public parseCallContractResponse(res: Gateway.CallContractResponse): CallContractResponse {
public parseCallContractResponse(res: Sequencer.CallContractResponse): CallContractResponse {
return {
result: res.result,
};
}

public parseInvokeFunctionResponse(res: Gateway.AddTransactionResponse): InvokeFunctionResponse {
public parseInvokeFunctionResponse(
res: Sequencer.AddTransactionResponse
): InvokeFunctionResponse {
return {
transaction_hash: res.transaction_hash,
};
}

public parseDeployContractResponse(res: Gateway.AddTransactionResponse): DeployContractResponse {
public parseDeployContractResponse(
res: Sequencer.AddTransactionResponse
): DeployContractResponse {
return {
transaction_hash: res.transaction_hash,
contract_address: res.address as string,
};
}

public parseDeclareContractResponse(
res: Gateway.AddTransactionResponse
res: Sequencer.AddTransactionResponse
): DeclareContractResponse {
return {
transaction_hash: res.transaction_hash,
Expand Down

0 comments on commit b7291e6

Please sign in to comment.