Skip to content

Commit

Permalink
fix: type issue
Browse files Browse the repository at this point in the history
  • Loading branch information
0x1NotMe committed Jul 20, 2023
1 parent 619fa95 commit 4cee2be
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 78 deletions.
112 changes: 43 additions & 69 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
StatusResponse,
ValidateBalanceAndApproval
} from "./types";

import { RouteResponse as RouteData } from "@0xsquid/squid-types";
import erc20Abi from "./abi/erc20.json";
import { nativeTokenConstant, uint256MaxValue } from "./constants";
import { ErrorType, SquidError } from "./error";
Expand All @@ -28,7 +28,11 @@ import {
SquidData,
Token
} from "@0xsquid/squid-types";
import { parseSdkInfoResponse, parseRouteResponse } from "./0xsquid";
import {
parseSdkInfoResponse,
parseRouteResponse,
parseStatusResponse
} from "./0xsquid";

const baseUrl = "https://testnet.api.0xsquid.com/";

Expand Down Expand Up @@ -149,35 +153,33 @@ export class Squid {

const _fromChain = getChainData(
this.chains as ChainData[],
params.fromChain
fromChain,
this.config
);
if (!_fromChain) {
throw new SquidError({
message: `fromChain not found for ${fromChain}`,
errorType: ErrorType.ValidationError,
logging: this.config.logging,
logLevel: this.config.logLevel
});
}

const _toChain = getChainData(this.chains as ChainData[], toChain);
if (!_toChain) {
throw new SquidError({
message: `toChain not found for ${fromChain}`,
errorType: ErrorType.ValidationError,
logging: this.config.logging,
logLevel: this.config.logLevel
});
}
const _toChain = getChainData(
this.chains as ChainData[],
toChain,
this.config
);

const _fromToken = getTokenData(
this.tokens,
fromToken,
fromChain,
this.config
);

const _toToken = getTokenData(this.tokens, toToken, toChain, this.config);

const fromProvider = new ethers.providers.JsonRpcProvider(_fromChain.rpc);

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

if (!fromIsNative) {
fromTokenContract = new ethers.Contract(
fromToken.address,
_fromToken.address,
erc20Abi,
fromProvider
);
Expand All @@ -186,8 +188,8 @@ export class Squid {
return {
fromChain: _fromChain,
toChain: _toChain,
fromToken,
toToken,
fromToken: _fromToken,
toToken: _toToken,
fromTokenContract,
fromProvider,
fromIsNative
Expand Down Expand Up @@ -431,12 +433,12 @@ export class Squid {

const allowance = await (fromTokenContract as ethers.Contract).allowance(
sender,
targetAddress
target
);

if (amount.gt(allowance)) {
throw new SquidError({
message: `Insufficient allowance for contract: ${targetAddress} on chain ${fromChain.chainId}`,
message: `Insufficient allowance for contract: ${target} on chain ${fromChain.chainId}`,
errorType: ErrorType.ValidationError,
logging: this.config.logging,
logLevel: this.config.logLevel
Expand Down Expand Up @@ -480,7 +482,7 @@ export class Squid {
route.params
);

const { targetAddress } = this.validateTransactionRequest(
const { target } = this.validateTransactionRequest(
route.transactionRequest
);

Expand All @@ -500,7 +502,7 @@ export class Squid {

const approveTx = await (fromTokenContract as ethers.Contract)
.connect(signer)
.approve(targetAddress, amountToApprove, overrides);
.approve(target, amountToApprove, overrides);
await approveTx.wait();

return true;
Expand All @@ -514,25 +516,17 @@ export class Squid {
}: Allowance): Promise<BigNumber> {
this.validateInit();

const token = getTokenData(this.tokens as Token[], tokenAddress, chainId);
if (!token) {
throw new SquidError({
message: `Token not found for ${tokenAddress}`,
errorType: ErrorType.ValidationError,
logging: this.config.logging,
logLevel: this.config.logLevel
});
}

const chain = getChainData(this.chains as ChainData[], token.chainId);
if (!chain) {
throw new SquidError({
message: `Chain not found for ${token.chainId}`,
errorType: ErrorType.ValidationError,
logging: this.config.logging,
logLevel: this.config.logLevel
});
}
const token = getTokenData(
this.tokens as Token[],
tokenAddress,
chainId,
this.config
);
const chain = getChainData(
this.chains as ChainData[],
token.chainId,
this.config
);

const provider = new ethers.providers.JsonRpcProvider(chain.rpc);
const contract = new ethers.Contract(token.address, erc20Abi, provider);
Expand All @@ -552,29 +546,9 @@ export class Squid {
const token = getTokenData(
this.tokens as Token[],
tokenAddress,
chainId as number | string
chainId,
this.config
);
if (!token) {
throw new SquidError({
message: `Token not found for ${tokenAddress}`,
errorType: ErrorType.ValidationError,
logging: this.config.logging,
logLevel: this.config.logLevel
});
}

const chain = getChainData(
this.chains as ChainData[],
token.chainId as number | string
);
if (!chain) {
throw new SquidError({
message: `Chain not found for ${token.chainId}`,
errorType: ErrorType.ValidationError,
logging: this.config.logging,
logLevel: this.config.logLevel
});
}

const contract = new ethers.Contract(token.address, erc20Abi, signer);
return await contract.approve(
Expand Down
9 changes: 5 additions & 4 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
RouteResponse as RouteData,
ChainData,
Token,
SquidData
SquidData,
RouteRequest
} from "@0xsquid/squid-types";

export type MapChainIdName = {
Expand All @@ -25,7 +26,7 @@ export type Config = {
integratorId?: string;
};

export type GetRoute = RouteData;
export type GetRoute = RouteRequest;

export type SdkInfoResponse = {
chains: ChainData[];
Expand Down Expand Up @@ -100,8 +101,8 @@ export type ApproveRoute = {
export type RouteParamsData = {
fromChain: ChainData;
toChain: ChainData;
fromToken: Token | undefined;
toToken: Token | undefined;
fromToken: Token;
toToken: Token;
fromTokenContract: ethers.Contract | undefined;
fromProvider: ethers.providers.JsonRpcProvider;
fromIsNative: boolean;
Expand Down
35 changes: 30 additions & 5 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
import { ChainData, Token } from "@0xsquid/squid-types";
import { AxiosRequestHeaders } from "axios";
import { ErrorType, SquidError } from "error";
import { Config } from "types";

export const getTokenData = (
tokens: Token[],
address: string,
chainId: string
): Token | undefined =>
tokens.find(
chainId: string,
config: Config
): Token => {
const token = tokens.find(
e =>
e.address.toLowerCase() === address?.toLowerCase() && e.chainId == chainId
);
if (!token) {
throw new SquidError({
message: `Could not find token with address ${address} on chain ${chainId}`,
errorType: ErrorType.ValidationError,
logging: config.logging,
logLevel: config.logLevel
});
}
return token;
};

export const getChainData = (
chains: ChainData[],
chainId: string
): ChainData | undefined => chains.find(chain => chain.chainId == chainId);
chainId: string,
config: Config
): ChainData => {
const chain = chains.find(chain => chain.chainId == chainId);
if (!chain) {
throw new SquidError({
message: `Could not find chain with ${chainId}`,
errorType: ErrorType.ValidationError,
logging: config.logging,
logLevel: config.logLevel
});
}
return chain;
};

export const getHeaderTracker = (headers: AxiosRequestHeaders) => {
return {
Expand Down

0 comments on commit 4cee2be

Please sign in to comment.