Skip to content

Add multihop swap support #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crocswap-libs/sdk",
"version": "1.1.2",
"version": "1.2.0",
"description": "🛠🐊🛠 An SDK for building applications on top of CrocSwap",
"author": "Ben Wolski <[email protected]>",
"repository": "https://github.com/CrocSwap/sdk.git",
Expand Down
1 change: 1 addition & 0 deletions src/abis/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./query";
export * from "./impact";
export * from "./multiImpact";
export * from "./erc20";
export * from "./croc";
133 changes: 133 additions & 0 deletions src/abis/multiImpact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

export const MULTI_IMPACT_ABI = [
{
"inputs": [
{
"internalType": "address",
"name": "dex",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [
{
"components": [
{
"internalType": "address",
"name": "token",
"type": "address"
},
{
"internalType": "uint256",
"name": "poolIdx",
"type": "uint256"
}
],
"internalType": "struct CrocMultiImpact.SwapHop[]",
"name": "hops",
"type": "tuple[]"
},
{
"internalType": "uint128",
"name": "qty",
"type": "uint128"
},
{
"internalType": "bool",
"name": "isReverse",
"type": "bool"
}
],
"name": "calcMultiHopImpact",
"outputs": [
{
"internalType": "int128",
"name": "inputFlow",
"type": "int128"
},
{
"internalType": "int128",
"name": "outputFlow",
"type": "int128"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"components": [
{
"components": [
{
"internalType": "address",
"name": "token",
"type": "address"
},
{
"internalType": "uint256",
"name": "poolIdx",
"type": "uint256"
}
],
"internalType": "struct CrocMultiImpact.SwapHop[]",
"name": "hops",
"type": "tuple[]"
},
{
"internalType": "uint128",
"name": "qty",
"type": "uint128"
},
{
"internalType": "bool",
"name": "isFixedOutput",
"type": "bool"
}
],
"internalType": "struct CrocMultiImpact.SwapPath[]",
"name": "paths",
"type": "tuple[]"
}
],
"name": "calcMultiPathImpact",
"outputs": [
{
"components": [
{
"internalType": "int128",
"name": "inputFlow",
"type": "int128"
},
{
"internalType": "int128",
"name": "outputFlow",
"type": "int128"
}
],
"internalType": "struct CrocMultiImpact.SwapPathOutput[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "dex_",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
]
4 changes: 3 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type ChainAddress = string;
type ChainId = string;
export type ChainId = string;

export const MIN_TICK = -665454;
export const MAX_TICK = 831818;
Expand All @@ -17,6 +17,7 @@ export interface ChainSpec {
dex: ChainAddress;
query: ChainAddress;
impact: ChainAddress;
multiImpact?: ChainAddress;
router?: ChainAddress
routerBypass?: ChainAddress
}
Expand Down Expand Up @@ -343,6 +344,7 @@ const SCROLL_CHAIN: ChainSpec = {
dex: "0xaaaaAAAACB71BF2C8CaE522EA5fa455571A74106",
query: "0x62223e90605845Cf5CC6DAE6E0de4CDA130d6DDf",
impact: "0xc2c301759B5e0C385a38e678014868A33E2F3ae3",
multiImpact: "0x0F5Ef3835d0D1Ecf25A395CcB91E061CBde69205",
router: "0xfB5f26851E03449A0403Ca945eBB4201415fd1fc",
routerBypass: "0xED5535C6237f72BD9b4fDEAa3b6D8d9998b4C4e4",
},
Expand Down
3 changes: 3 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ChainSpec, CHAIN_SPECS } from "./constants";
import { CROC_ABI, QUERY_ABI, ERC20_ABI } from "./abis";
import { ZeroAddress } from "ethers";
import { IMPACT_ABI } from "./abis/impact";
import { MULTI_IMPACT_ABI } from "./abis/multiImpact";
import { ERC20_READ_ABI } from "./abis/erc20.read";

export interface CrocContext {
Expand All @@ -14,6 +15,7 @@ export interface CrocContext {
routerBypass?: Contract;
query: Contract;
slipQuery: Contract;
multiImpact?: Contract;
erc20Read: Contract;
erc20Write: Contract;
chain: ChainSpec;
Expand Down Expand Up @@ -115,6 +117,7 @@ function inflateContracts(
routerBypass: context.addrs.routerBypass ? new Contract(context.addrs.routerBypass || ZeroAddress, CROC_ABI, actor) : undefined,
query: new Contract(context.addrs.query, QUERY_ABI, provider),
slipQuery: new Contract(context.addrs.impact, IMPACT_ABI, provider),
multiImpact: context.addrs.multiImpact ? new Contract(context.addrs.multiImpact, MULTI_IMPACT_ABI, provider) : undefined,
erc20Write: new Contract(ZeroAddress, ERC20_ABI, actor),
erc20Read: new Contract(ZeroAddress, ERC20_READ_ABI, provider),
chain: context,
Expand Down
14 changes: 14 additions & 0 deletions src/croc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CrocPositionView } from './position';
import { CrocSlotReader } from './slots';
import { TransactionResponse } from 'ethers';
import { TempestStrategy, TempestVault } from './vaults/tempest';
import { CrocSmartSwapPlan, CrocSmartSwapExecOpts } from './smartSwap';

/* This is the main entry point for the Croc SDK. It provides a high-level interface
* for interacting with CrocSwap smart contracts in an ergonomic way. */
Expand Down Expand Up @@ -56,6 +57,19 @@ export class CrocEnv {
return new SellPrefix(ZeroAddress, qty, this.tokens, this.context)
}

/* Creates a smart swap plan for swapping from one token to another. The plan will
* automatically select the best route to swap through.

* @param fromToken The address of the token to swap from.
* @param toToken The address of the token to swap to.
* @param qty The quantity of the swap, either input or output depending on isFixedOutput.
* @param isFixedOutput Whether the quantity is fixed output or fixed input.
* @param opts Optional parameters for the swap plan. */
smartSwap (fromToken: string, toToken: string, qty: TokenQty, isFixedOutput: boolean, opts?: CrocSmartSwapExecOpts): CrocSmartSwapPlan {
return new CrocSmartSwapPlan(this.tokens.materialize(fromToken),
this.tokens.materialize(toToken), qty, isFixedOutput, this.context, opts)
}

/* Returns a view of the canonical pool for the underlying token pair. For example the
* below would return pool view for WBTC/USDC with WBTC as the quote side token:
* crocEnv.pool(WBTC, USDC)
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./abis";
export * from "./pool";
export * from "./position";
export * from "./swap";
export * from "./smartSwap";
export * from "./croc";

export * from "./encoding/liquidity";
Expand Down
3 changes: 3 additions & 0 deletions src/smartSwap/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./smartSwap"
export * from "./smartSwapRoute"
export * from "./routers"
Loading
Loading