Skip to content

Commit

Permalink
feat: add provider utils
Browse files Browse the repository at this point in the history
  • Loading branch information
badurinantun committed Jul 7, 2022
1 parent e942a9d commit 4df4ae7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 84 deletions.
6 changes: 3 additions & 3 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import { RPCProvider, RpcProviderOptions } from './rpc';
import { BlockIdentifier } from './utils';

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

export class Provider implements ProviderInterface {
private provider!: ProviderInterface;

constructor(options?: ProviderOptions) {
if (options && 'rpc' in options) {
if (options && options.rpc) {
this.provider = new RPCProvider(options.rpc);
}

Expand Down
40 changes: 7 additions & 33 deletions src/provider/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ONE, StarknetChainId, ZERO } from '../constants';
import {
Call,
CallContractResponse,
CompiledContract,
DeclareContractPayload,
DeclareContractResponse,
DeployContractPayload,
Expand All @@ -25,20 +24,15 @@ import {
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 { compressProgram, randomAddress } from '../utils/stark';
import { randomAddress } from '../utils/stark';
import { GatewayError, HttpError } from './errors';
import { ProviderInterface } from './interface';
import { BlockIdentifier, getFormattedBlockIdentifier } from './utils';

type NetworkName = 'mainnet-alpha' | 'goerli-alpha';

function wait(delay: number) {
return new Promise((res) => {
setTimeout(res, delay);
});
}

function isEmptyQueryObject(obj?: Record<any, any>): obj is undefined {
return (
obj === undefined ||
Expand Down Expand Up @@ -254,13 +248,7 @@ export class GatewayProvider implements ProviderInterface {
blockIdentifier: BlockIdentifier = 'pending'
): Promise<any> {
return this.fetchEndpoint('get_full_contract', { blockIdentifier, contractAddress }).then(
(res) => {
const parsedContract = typeof res === 'string' ? (parse(res) as CompiledContract) : res;
return {
...parsedContract,
program: compressProgram(parsedContract.program),
};
}
parseContract
);
}

Expand All @@ -280,18 +268,11 @@ export class GatewayProvider implements ProviderInterface {
}

public async deployContract({
contract: compiledContract,
contract,
constructorCalldata,
addressSalt,
}: DeployContractPayload): Promise<DeployContractResponse> {
const parsedContract =
typeof compiledContract === 'string'
? (parse(compiledContract) as CompiledContract)
: compiledContract;
const contractDefinition = {
...parsedContract,
program: compressProgram(parsedContract.program),
};
const contractDefinition = parseContract(contract);

return this.fetchEndpoint('add_transaction', undefined, {
type: 'DEPLOY',
Expand All @@ -302,16 +283,9 @@ export class GatewayProvider implements ProviderInterface {
}

public async declareContract({
contract: compiledContract,
contract,
}: DeclareContractPayload): Promise<DeclareContractResponse> {
const parsedContract =
typeof compiledContract === 'string'
? (parse(compiledContract) as CompiledContract)
: compiledContract;
const contractDefinition = {
...parsedContract,
program: compressProgram(parsedContract.program),
};
const contractDefinition = parseContract(contract);

return this.fetchEndpoint('add_transaction', undefined, {
type: 'DECLARE',
Expand Down
57 changes: 9 additions & 48 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { StarknetChainId } from '../constants';
import {
Call,
CallContractResponse,
CompiledContract,
DeclareContractPayload,
DeclareContractResponse,
DeployContractPayload,
Expand All @@ -20,25 +19,20 @@ import {
Signature,
} from '../types';
import { getSelectorFromName } from '../utils/hash';
import { parse, stringify } from '../utils/json';
import { stringify } from '../utils/json';
import {
BigNumberish,
bigNumberishArrayToDecimalStringArray,
isHex,
toBN,
toHex,
} from '../utils/number';
import { parseCalldata, parseContract, wait } from '../utils/provider';
import { RPCResponseParser } from '../utils/responseParser/rpc';
import { compressProgram, randomAddress } from '../utils/stark';
import { randomAddress } from '../utils/stark';
import { ProviderInterface } from './interface';
import { BlockIdentifier } from './utils';

function wait(delay: number) {
return new Promise((res) => {
setTimeout(res, delay);
});
}

export type RpcProviderOptions = { nodeUrl: string };

export class RPCProvider implements ProviderInterface {
Expand Down Expand Up @@ -140,35 +134,21 @@ export class RPCProvider implements ProviderInterface {
blockIdentifier: BlockIdentifier = 'pending',
_signature: Signature = []
): Promise<EstimateFeeResponse> {
const parsedCalldata = call.calldata?.map((data) => {
if (typeof data === 'string' && isHex(data as string)) {
return data;
}
return toHex(toBN(data));
});

return this.fetchEndpoint('starknet_estimateFee', [
{
contract_address: call.contractAddress,
entry_point_selector: getSelectorFromName(call.entrypoint),
calldata: parsedCalldata,
calldata: parseCalldata(call.calldata),
},
blockIdentifier,
]).then(this.responseParser.parseFeeEstimateResponse);
}

public async declareContract({
contract: compiledContract,
contract,
version,
}: DeclareContractPayload): Promise<DeclareContractResponse> {
const parsedContract =
typeof compiledContract === 'string'
? (parse(compiledContract) as CompiledContract)
: compiledContract;
const contractDefinition = {
...parsedContract,
program: compressProgram(parsedContract.program),
};
const contractDefinition = parseContract(contract);

return this.fetchEndpoint('starknet_addDeclareTransaction', [
{
Expand All @@ -184,12 +164,7 @@ export class RPCProvider implements ProviderInterface {
constructorCalldata,
addressSalt,
}: DeployContractPayload): Promise<DeployContractResponse> {
const parsedContract =
typeof contract === 'string' ? (parse(contract) as CompiledContract) : contract;
const contractDefinition = {
...parsedContract,
program: compressProgram(parsedContract.program),
};
const contractDefinition = parseContract(contract);

return this.fetchEndpoint('starknet_addDeployTransaction', [
addressSalt ?? randomAddress(),
Expand All @@ -205,18 +180,11 @@ export class RPCProvider implements ProviderInterface {
functionInvocation: Invocation,
details: InvocationsDetails
): Promise<InvokeFunctionResponse> {
const parsedCalldata = functionInvocation.calldata?.map((data) => {
if (typeof data === 'string' && isHex(data as string)) {
return data;
}
return toHex(toBN(data));
});

return this.fetchEndpoint('starknet_addInvokeTransaction', [
{
contract_address: functionInvocation.contractAddress,
entry_point_selector: getSelectorFromName(functionInvocation.entrypoint),
calldata: parsedCalldata,
calldata: parseCalldata(functionInvocation.calldata),
},
functionInvocation.signature,
details.maxFee,
Expand All @@ -228,18 +196,11 @@ export class RPCProvider implements ProviderInterface {
call: Call,
blockIdentifier: BlockIdentifier = 'pending'
): Promise<CallContractResponse> {
const parsedCalldata = call.calldata?.map((data) => {
if (typeof data === 'string' && isHex(data as string)) {
return data;
}
return toHex(toBN(data));
});

const result = await this.fetchEndpoint('starknet_call', [
{
contract_address: call.contractAddress,
entry_point_selector: getSelectorFromName(call.entrypoint),
calldata: parsedCalldata,
calldata: parseCalldata(call.calldata),
},
blockIdentifier,
]);
Expand Down
28 changes: 28 additions & 0 deletions src/utils/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { CompiledContract, RawCalldata } from '../types';
import { parse } from './json';
import { isHex, toBN, toHex } from './number';
import { compressProgram } from './stark';

export function wait(delay: number) {
return new Promise((res) => {
setTimeout(res, delay);
});
}

export function parseCalldata(calldata: RawCalldata = []) {
return calldata.map((data) => {
if (typeof data === 'string' && isHex(data as string)) {
return data;
}
return toHex(toBN(data));
});
}

export function parseContract(contract: CompiledContract | string) {
const parsedContract =
typeof contract === 'string' ? (parse(contract) as CompiledContract) : contract;
return {
...parsedContract,
program: compressProgram(parsedContract.program),
};
}

0 comments on commit 4df4ae7

Please sign in to comment.