Skip to content

Commit

Permalink
chore: improve get route type (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdev3 authored Nov 2, 2023
1 parent ed4fe74 commit 7125671
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 59 deletions.
111 changes: 54 additions & 57 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ export class Squid extends TokensChains {
return { ...data, requestId, integratorId };
}

async getRoute(
params: RouteRequest
): Promise<RouteResponse & { requestId?: string; integratorId?: string }> {
async getRoute(params: RouteRequest): Promise<RouteResponse> {
this.validateInit();

const { data, headers, status } = await this.httpInstance.post(
Expand Down Expand Up @@ -218,7 +216,6 @@ export class Squid extends TokensChains {
}
}

// TODO: IS THIS METHOD GONNA BE EVM ONLY ?
public getRawTxHex(
data: Omit<ExecuteRoute, "signer"> & { nonce: number }
): string {
Expand All @@ -228,58 +225,6 @@ export class Squid extends TokensChains {
return this.handlers.evm.getRawTxHex({ ...data });
}

// INTERNAL PRIVATES METHODS

private validateInit() {
if (!this.initialized) {
throw new Error(
"SquidSdk must be initialized! Please call the SquidSdk.init method"
);
}
}

private populateRouteParams(
params: RouteRequest,
signer?: EvmWallet
): RouteParamsPopulated {
const { fromChain, toChain, fromToken, toToken } = params;

const _fromChain = this.getChainData(fromChain);
const _toChain = this.getChainData(toChain);
const _fromToken = this.getTokenData(fromToken, fromChain);
const _toToken = this.getTokenData(toToken, toChain);

const fromProvider = ethersAdapter.rpcProvider(_fromChain.rpc);

const fromIsNative = _fromToken.address === nativeTokenConstant;
let fromTokenContract;

if (!fromIsNative) {
fromTokenContract = ethersAdapter.contract(
_fromToken.address,
erc20Abi,
signer || fromProvider
);
}

return {
...params,
fromChain: _fromChain,
toChain: _toChain,
fromToken: _fromToken,
toToken: _toToken,
fromTokenContract,
fromProvider,
fromIsNative
};
}

private validateTransactionRequest(route: RouteResponse["route"]) {
if (!route.transactionRequest) {
throw new Error("transactionRequest param not found in route object");
}
}

public getFromAmount({
fromToken,
toAmount,
Expand Down Expand Up @@ -340,7 +285,7 @@ export class Squid extends TokensChains {
}: {
addresses: CosmosAddress[];
chainIds?: (string | number)[];
}) {
}): Promise<CosmosBalance[]> {
const cosmosChains = this.chains.filter(c =>
c.chainType === ChainType.COSMOS &&
// if chainIds is not provided, return all cosmos chains
Expand Down Expand Up @@ -406,4 +351,56 @@ export class Squid extends TokensChains {
cosmosBalances
};
}

// INTERNAL PRIVATES METHODS

private validateInit() {
if (!this.initialized) {
throw new Error(
"SquidSdk must be initialized! Please call the SquidSdk.init method"
);
}
}

private populateRouteParams(
params: RouteRequest,
signer?: EvmWallet
): RouteParamsPopulated {
const { fromChain, toChain, fromToken, toToken } = params;

const _fromChain = this.getChainData(fromChain);
const _toChain = this.getChainData(toChain);
const _fromToken = this.getTokenData(fromToken, fromChain);
const _toToken = this.getTokenData(toToken, toChain);

const fromProvider = ethersAdapter.rpcProvider(_fromChain.rpc);

const fromIsNative = _fromToken.address === nativeTokenConstant;
let fromTokenContract;

if (!fromIsNative) {
fromTokenContract = ethersAdapter.contract(
_fromToken.address,
erc20Abi,
signer || fromProvider
);
}

return {
...params,
fromChain: _fromChain,
toChain: _toChain,
fromToken: _fromToken,
toToken: _toToken,
fromTokenContract,
fromProvider,
fromIsNative
};
}

private validateTransactionRequest(route: RouteResponse["route"]) {
if (!route.transactionRequest) {
throw new Error("transactionRequest param not found in route object");
}
}
}
9 changes: 7 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ChainData,
Token,
RouteRequest,
RouteResponse
RouteResponse as _RouteResponse
} from "@0xsquid/squid-types";
import { SigningStargateClient } from "@cosmjs/stargate";

Expand Down Expand Up @@ -43,12 +43,17 @@ export type CosmosSigner = SigningStargateClient;

export type ExecuteRoute = {
signer: EvmWallet | CosmosSigner;
route: RouteResponse["route"];
route: _RouteResponse["route"];
executionSettings?: ExecutionSettings;
overrides?: OverrideParams;
signerAddress?: string; // cosmos specific
};

export type RouteResponse = _RouteResponse & {
requestId?: string;
integratorId?: string;
};

export type TransactionResponses = TransactionResponse | TxRaw;

export type GetStatus = {
Expand Down

0 comments on commit 7125671

Please sign in to comment.