@dzapio/sdk
Version:
A TypeScript/JavaScript SDK for interacting with the DZap protocol, providing utilities for DeFi operations including Swaps, Bridges, and Zaps.
476 lines (475 loc) • 12.5 kB
TypeScript
import { Signer } from 'ethers';
import { Prettify, TypedDataDomain, WalletClient } from 'viem';
import { DZapAbis, GaslessTxType, OtherAbis, QuoteFilters, Services, STATUS, STATUS_RESPONSE } from './../constants';
import { ApprovalModes } from './../constants/approval';
import { PermitTypes } from './../constants/permit';
import { AppEnv, ContractVersion, StatusCodes, TxnStatus } from './../enums';
import { PsbtInput, PsbtOutput } from './btc';
import { GaslessBridgeParams, GaslessSwapParams } from './permit';
export type HexString = `0x${string}`;
export type ChainData = {
[key in number]: Chain;
};
export type NativeTokenInfo = {
contract: string;
symbol: string;
decimals: number;
name: string;
balance: string;
price?: string;
logo: string;
};
export declare const contractErrorActions: {
readonly TRY_ANOTHER_ROUTE: "TRY_ANOTHER_ROUTE";
readonly INCREASE_SLIPPAGE: "INCREASE_SLIPPAGE";
readonly INCREASE_ALLOWANCE: "INCREASE_ALLOWANCE";
};
export type ContractErrorResponse = {
status: string;
txId: string;
code: number;
message: string;
error: unknown;
action: keyof typeof contractErrorActions;
details?: unknown;
};
export type CalculatePointsRequest = {
srcTokens: {
amount: string;
address: string;
decimals: number;
}[];
destTokens: {
amount: string;
address: string;
decimals: number;
}[];
providers: string[];
chainId: number;
account: string;
txType: 'swap' | 'bridge';
};
export type DisabledPermitTokens = {
eip2612: string[];
};
export type Chain = {
coinKey: string;
chainId: number;
chainType: string;
name: string;
coin: string;
dcaContract: string;
swapBridgeContract: string;
logo: string;
tokenlistUrl?: string;
multicallAddress?: HexString;
blockExplorerUrl: string;
nativeToken: NativeTokenInfo;
rpcProviders: ApiRpcResponse[];
pricingAvailable: boolean;
balanceAvailable: boolean;
supportedAs: {
source: boolean;
destination: boolean;
};
contracts?: Partial<{
router: string;
dca: string;
zap: string;
}>;
coingecko?: {
chainKey: string;
nativeTokenKey: string;
};
defiLlama?: {
chainKey: string;
nativeTokenKey: string;
};
disableMultiTxn: boolean;
isEnabled: boolean;
mainnet: boolean;
tags?: Tag[];
version?: ContractVersion;
};
export type ApiRpcResponse = {
url: string;
keyRequired: boolean;
keyType?: 'ALCHEMY_KEY' | 'BLASTAPI_KEY';
};
export type ProviderDetails = {
id: string;
name: string;
icon: string;
};
export type FeeDetails = {
address: string;
decimals: number;
chainId: number;
symbol: string;
logo?: string;
amount: string;
amountUSD: string;
included: boolean;
};
export type Fee = {
gasFee: FeeDetails[];
protocolFee: FeeDetails[];
providerFee: FeeDetails[];
};
export type QuoteFilter = keyof typeof QuoteFilters;
export type TradeQuotesRequest = {
fromChain: number;
gasless?: boolean;
data: TradeQuotesRequestData[];
disableEstimation?: boolean;
account?: string;
allowedProtocols?: string[];
filter?: QuoteFilter;
};
export type TradeQuotesRequestData = {
amount: string;
srcToken: string;
srcDecimals?: number;
destToken: string;
destDecimals?: number;
toChain: number;
slippage: number;
selectedSource?: string;
};
export type TradeStep = {
type: string;
exchange: {
logo: string;
name: string;
};
};
export type TradePath = {
type: string;
exchange: ProviderDetails;
srcToken: Token;
srcAmount: string;
srcAmountUSD: string;
destToken: Token;
destAmount: string;
destAmountUSD: string;
fee: Fee;
};
export type Tag = {
title: string;
link?: string;
message?: string;
};
export type TradeQuote = {
bridgeDetails?: ProviderDetails;
providerDetails: ProviderDetails;
srcAmount: string;
srcAmountUSD: string;
destAmount: string;
destAmountUSD: string;
minDestAmount: string;
swapPerUnit: string;
srcToken: Token;
destToken: Token;
fee: Fee;
priceImpactPercent: string;
duration: string;
gasless: boolean;
steps: TradeStep[];
path: TradePath[];
tags?: Tag[];
additionalInfo?: AdditionalInfo;
};
export type TradeQuotesByProviderId = {
[providerAndBridge: string]: TradeQuote;
};
export type TradeQuotesResponse = {
[pair: string]: {
status?: string;
message?: string;
recommendedSource: string;
fastestSource?: string;
bestReturnSource: string;
questSource?: string;
quoteRates?: TradeQuotesByProviderId;
tokensWithoutPrice: Record<number, string[]>;
};
};
export type AdditionalInfo = {
[key: string]: unknown;
};
export type Token = {
address: HexString;
decimals: number;
chainId: number;
logo: string;
symbol: string;
price?: string;
};
export type TokenPermitData = {
eip2612: {
supported: boolean;
data?: {
domain?: TypedDataDomain;
};
};
permit2: {
supported: boolean;
};
};
export type TokenInfo = Prettify<NativeTokenInfo & {
chainId: number;
balanceInUsd?: number | null;
isDisabledOnSwapBridge?: {
source: boolean;
destination: boolean;
};
isDisabledOnZap?: {
source: boolean;
destination: boolean;
};
permit?: TokenPermitData;
}>;
export type TokenResponse = {
[key: string]: TokenInfo;
};
export type TradeBuildTxnRequest = {
sender: HexString;
refundee: HexString;
fromChain: number;
gasless: boolean;
disableEstimation?: boolean;
data: TradeBuildTxnRequestData[];
hasPermit2ApprovalForAllTokens?: boolean;
publicKey?: string;
};
export type TradeBuildTxnRequestData = {
amount: string;
srcToken: string;
srcDecimals?: number;
destToken: string;
destDecimals?: number;
toChain: number;
protocol: string;
recipient: string;
slippage: number;
additionalInfo?: AdditionalInfo;
permitData?: string;
permit?: TokenPermitData;
};
export type ParamQuotes = {
additionalInfo?: Record<string, unknown>;
destAmount: string;
provider: string;
minDestAmount: string;
};
export type EvmTxData = {
from: HexString;
data: HexString;
to: HexString;
value: string;
gasLimit: string;
};
export type SvmTxData = {
from: string;
data: string;
blockhash?: string;
lastValidBlockHeight?: number;
};
export type BtcTxData = {
from: string;
data: string;
inputs: PsbtInput[];
outputs: PsbtOutput[];
feeRate: number;
};
export type TxData = EvmTxData | SvmTxData | BtcTxData;
export type TxRequestData<T> = {
status: typeof STATUS.success;
txId: string;
chainId: number;
transaction: T;
};
export type TradeGasBuildTxnResponse<T = TxData> = TxRequestData<T> & {
quotes: Record<string, ParamQuotes>;
gasless: false;
};
export type TradeBuildTxnResponse = TradeGasBuildTxnResponse & {
data: string;
from: string;
to?: string;
value?: string;
gasLimit?: string;
svmTxData?: {
blockhash: string;
lastValidBlockHeight: number;
};
btcTxData?: {
inputs: PsbtInput[];
outputs: PsbtOutput[];
feeRate: number;
};
additionalInfo: Record<string, Record<string, unknown>>;
updatedQuotes: Record<string, string>;
};
export type GaslessTxTypes = keyof typeof GaslessTxType;
type BridgeGaslessTxData = {
executorFeesHash: HexString;
swapDataHash?: HexString;
adapterDataHash: HexString;
txType: typeof GaslessTxType.bridge;
value: string;
};
type SwapGaslessTxData = {
executorFeesHash: HexString;
swapDataHash: HexString;
txType: typeof GaslessTxType.swap;
value: string;
};
export type GaslessTradeBuildTxnResponse = {
status: 'success';
txId: HexString;
transaction: BridgeGaslessTxData | SwapGaslessTxData;
quotes: Record<string, ParamQuotes>;
gasless: true;
onlySwapData: false;
};
export type AvailableDZapServices = (typeof Services)[keyof typeof Services];
export type DZapAvailableAbis = (typeof DZapAbis)[keyof typeof DZapAbis];
export type OtherAvailableAbis = (typeof OtherAbis)[keyof typeof OtherAbis];
export type AppEnvType = `${AppEnv}`;
export type DZapTransactionResponse = {
status: TxnStatus;
errorMsg?: string;
code: StatusCodes | number;
action?: keyof typeof contractErrorActions;
txnHash?: HexString;
error?: unknown;
additionalInfo?: Record<string, unknown>;
updatedQuotes?: Record<string, string>;
};
export type SwapInfo = {
dex: string;
fromToken: string;
fromAmount: bigint;
toToken: string;
returnToAmount: bigint;
};
export type PartialStatusData = {
receiveToken?: string;
receiveAmount?: string;
receiveAmountUSD?: string;
};
export type RefundStatusData = {
refundTxHash?: string;
refundToken?: string;
refundAmount?: string;
refundAmountUSD?: string;
refundTimeStamp?: string;
};
export type TradeStatusResponseData = PartialStatusData & RefundStatusData & {
srcChainId: number;
srcToken: string;
srcAmount: string;
srcAmountUSD: string;
srcTxHash: string;
destChainId: number;
destToken: string;
destAmount: string;
destAmountUSD: string;
destTxHash: string;
account: string;
recipient: string;
provider: string;
allowUserTxOnDestChain: boolean;
status: keyof typeof STATUS_RESPONSE;
outputToken?: string;
refundTxHash?: string;
};
export type TradeStatusResponse = {
[pair: string]: TradeStatusResponseData;
};
export type EIP2612GaslessExecuteTxParams = {
permitData: {
token: HexString;
amount: string;
permit: HexString;
}[];
gaslessIntentSignature: HexString;
gaslessIntentDeadline: string;
gaslessIntentNonce: string;
};
export type BatchGaslessExecuteTxParams = {
batchPermitData: HexString;
};
export type GaslessExecuteTxParams = {
chainId: number;
txId: HexString;
permit: EIP2612GaslessExecuteTxParams | BatchGaslessExecuteTxParams;
};
export type PermitMode = keyof typeof PermitTypes;
export type ApprovalMode = Exclude<keyof typeof ApprovalModes, 'EIP2612Permit'>;
export type SinglePermitCallbackParams = {
permitData: HexString;
srcToken: HexString;
amount: string;
permitType: Exclude<PermitMode, keyof typeof PermitTypes.PermitBatchWitnessTransferFrom>;
};
export type BatchPermitCallbackParams = {
batchPermitData: HexString;
tokens: {
address: HexString;
amount: string;
}[];
permitType: typeof PermitTypes.PermitBatchWitnessTransferFrom;
};
export type SignatureCallbackParams = SinglePermitCallbackParams | BatchPermitCallbackParams;
export type SignatureParamsBase = {
chainId: number;
sender: HexString;
signer: WalletClient | Signer;
tokens: {
address: HexString;
permitData?: HexString;
amount: string;
permit?: TokenPermitData;
}[];
spender: HexString;
rpcUrls: string[];
permitType: PermitMode;
isBatchPermitAllowed?: boolean;
signatureCallback?: (params: SignatureCallbackParams) => Promise<void>;
service: AvailableDZapServices;
contractVersion: ContractVersion;
};
export type GasSignatureParams = SignatureParamsBase & {
gasless: false;
};
export type GaslessSignatureParams = ((SignatureParamsBase & GaslessBridgeParams) | (SignatureParamsBase & GaslessSwapParams)) & {
gasless: true;
};
export type SignatureParams = GasSignatureParams | GaslessSignatureParams;
export type SignPermitResponse = {
status: TxnStatus.success;
code: StatusCodes;
tokens: {
address: HexString;
permitData?: HexString;
amount: string;
}[];
permitType: PermitMode;
} | {
status: TxnStatus.success;
code: StatusCodes;
batchPermitData: HexString;
permitType: typeof PermitTypes.PermitBatchWitnessTransferFrom;
} | {
status: Exclude<TxnStatus, typeof TxnStatus.success>;
code: StatusCodes;
permitType: PermitMode;
};
export type BroadcastTxParams = {
txId: string;
chainId: number;
signedTxData: HexString;
};
export {};