Skip to content

Commit

Permalink
Upgrade to ethers v6
Browse files Browse the repository at this point in the history
  • Loading branch information
busimus committed Jun 6, 2024
1 parent e3b2aee commit 84e2281
Show file tree
Hide file tree
Showing 25 changed files with 1,906 additions and 2,327 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ parse a transaction receipt from ethers.js and web3.js
# yarn ts-node ./examples/01-retrieve-spot-price.ts

# project dependencies
ethers
ethers v6
```
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crocswap-libs/sdk",
"version": "0.3.9-3",
"version": "1.0.0-1",
"description": "🛠🐊🛠 An SDK for building applications on top of CrocSwap",
"author": "Ben Wolski <[email protected]>",
"repository": "https://github.com/CrocSwap/sdk.git",
Expand All @@ -27,19 +27,19 @@
"build-local": "yarn build-all && yarn copy-local"
},
"devDependencies": {
"@types/jest": "^27.4.0",
"@types/node": "^17.0.8",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"esbuild": "^0.14.11",
"eslint": "^8.6.0",
"jest": "^27.4.7",
"ts-jest": "^27.1.2",
"ts-node": "^10.4.0",
"typedoc": "^0.25.4",
"typescript": "^5.3.3"
"@types/jest": "^29.5.12",
"@types/node": "^20.12.12",
"@typescript-eslint/eslint-plugin": "^7.11.0",
"@typescript-eslint/parser": "^7.11.0",
"esbuild": "^0.21.4",
"eslint": "^9.3.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.4",
"ts-node": "^10.9.2",
"typedoc": "^0.25.13",
"typescript": "^5.4.5"
},
"dependencies": {
"ethers": "^5.5.3"
"ethers": "^6.13.0"
}
}
17 changes: 8 additions & 9 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { BigNumber } from "ethers";

type ChainAddress = string;
type ChainId = string;

export const MIN_TICK = -665454;
export const MAX_TICK = 831818;
export const MAX_SQRT_PRICE: BigNumber = BigNumber.from(
"21267430153580247136652501917186561138").sub(1);
export const MIN_SQRT_PRICE: BigNumber = BigNumber.from("65538").sub(1);
export const MAX_LIQ = BigNumber.from(2).pow(128).sub(1);
export const MAX_SQRT_PRICE: bigint = BigInt(
"21267430153580247136652501917186561138") - BigInt(1);
export const MIN_SQRT_PRICE: bigint = BigInt("65538") - BigInt(1);
export const MAX_LIQ = BigInt(2) ** BigInt(128) - BigInt(1);


export interface ChainSpec {
nodeUrl: string;
Expand Down Expand Up @@ -39,14 +38,14 @@ const ETHEREUM_LOGO =
"https://d33wubrfki0l68.cloudfront.net/fcd4ecd90386aeb50a235ddc4f0063cfbb8a7b66/4295e/static/bfc04ac72981166c740b189463e1f74c/40129/eth-diamond-black-white.jpg";
const SCROLL_LOGO =
"https://develop--ambient-finance.netlify.app/scroll_logo.png";
const BLAST_LOGO =
const BLAST_LOGO =
"https://assets-global.website-files.com/65a6baa1a3f8ed336f415cb4/65a6cc95aae1066cf96d497d_Logo%20Black%20on%20Yellow%20Background%402x-p-500.png"

const DFLT_SDK_INFURA_KEY = '4741d1713bff4013bc3075ed6e7ce091'

const GOERLI_CHAIN: ChainSpec = {
nodeUrl: "https://goerli.infura.io/v3/" + DFLT_SDK_INFURA_KEY,
wsUrl: "wss://goerli.infura.io/ws/v3/" + DFLT_SDK_INFURA_KEY,
nodeUrl: "https://goerli.infura.io/v3/" + DFLT_SDK_INFURA_KEY,
wsUrl: "wss://goerli.infura.io/ws/v3/" + DFLT_SDK_INFURA_KEY,
addrs: {
dex: "0xfafcd1f5530827e7398b6d3c509f450b1b24a209",
query: "0xc9900777baa5EE94Cd2C6509fb09278A1A46b7e8",
Expand Down
4 changes: 2 additions & 2 deletions src/constants/mainnetTokens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from "ethers";
import { ZeroAddress } from "ethers";

export const ETH = ethers.constants.AddressZero
export const ETH = ZeroAddress

export const USDC = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
export const DAI = "0x6b175474e89094c44da98b954eedeac495271d0f"
34 changes: 18 additions & 16 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Provider, JsonRpcProvider } from "@ethersproject/providers";
import { Provider, JsonRpcProvider } from "ethers";
import { Contract, ethers, Signer } from "ethers";
import { ChainSpec, CHAIN_SPECS } from "./constants";
import { CROC_ABI, QUERY_ABI, ERC20_ABI } from "./abis";
import { AddressZero } from "@ethersproject/constants";
import { ZeroAddress } from "ethers";
import { IMPACT_ABI } from "./abis/impact";
import { ERC20_READ_ABI } from "./abis/erc20.read";

Expand Down Expand Up @@ -40,7 +40,7 @@ async function buildProvider(
} else if ("getNetwork" in arg) {
return [arg, signer];
} else {
const chainId = await arg.getChainId();
const chainId = Number((await arg.provider?.getNetwork())?.chainId);
return buildProvider(chainId, signer);
}
}
Expand All @@ -49,26 +49,28 @@ async function setupProvider(
provider: Provider,
signer?: Signer
): Promise<CrocContext> {
const actor = determineActor(provider, signer);
const actor = await determineActor(provider, signer);
const chainId = await getChain(provider);
let cntx = inflateContracts(chainId, provider, actor);
return await attachSenderAddr(cntx, actor)
}

async function attachSenderAddr (cntx: CrocContext,
async function attachSenderAddr (cntx: CrocContext,
actor: Provider | Signer): Promise<CrocContext> {
if ('getAddress' in actor) {
try {
cntx.senderAddr = await actor.getAddress()
} catch (e) { }
} catch (e) {
console.warn("Failed to get signer address:", e)
}
}
return cntx
}

function determineActor(
async function determineActor(
provider: Provider,
signer?: Signer
): Signer | Provider {
): Promise<Signer | Provider> {
if (signer) {
try {
return signer.connect(provider)
Expand All @@ -77,10 +79,10 @@ function determineActor(
}
} else if ("getSigner" in provider) {
try {
let signer = (provider as ethers.providers.Web3Provider).getSigner();
let signer = await ((provider as ethers.JsonRpcProvider).getSigner());
return signer
} catch {
return provider
} catch {
return provider
}
} else {
return provider;
Expand All @@ -91,7 +93,7 @@ async function getChain(provider: Provider): Promise<number> {
if ("chainId" in provider) {
return (provider as any).chainId as number;
} else if ("getNetwork" in provider) {
return provider.getNetwork().then((n) => n.chainId);
return provider.getNetwork().then((n) => Number(n.chainId));
} else {
throw new Error("Invalid provider");
}
Expand All @@ -107,12 +109,12 @@ function inflateContracts(
return {
provider: provider,
dex: new Contract(context.addrs.dex, CROC_ABI, actor),
router: context.addrs.router ? new Contract(context.addrs.router || AddressZero, CROC_ABI, actor) : undefined,
routerBypass: context.addrs.routerBypass ? new Contract(context.addrs.routerBypass || AddressZero, CROC_ABI, actor) : undefined,
router: context.addrs.router ? new Contract(context.addrs.router || ZeroAddress, CROC_ABI, actor) : undefined,
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),
erc20Write: new Contract(AddressZero, ERC20_ABI, actor),
erc20Read: new Contract(AddressZero, ERC20_READ_ABI, provider),
erc20Write: new Contract(ZeroAddress, ERC20_ABI, actor),
erc20Read: new Contract(ZeroAddress, ERC20_READ_ABI, provider),
chain: context,
senderAddr: addr
};
Expand Down
52 changes: 26 additions & 26 deletions src/croc.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
import { ConnectArg, CrocContext, connectCroc } from './context';
import { CrocPoolView } from './pool';
import { AddressZero } from '@ethersproject/constants';
import { ZeroAddress } from 'ethers';
import { TokenQty, CrocTokenView } from './tokens';
import { CrocSwapPlan, CrocSwapPlanOpts } from './swap';
import { Signer } from 'ethers';
import { CrocKnockoutHandle } from './knockout';
import { CrocPositionView } from './position';
import { CrocSlotReader } from './slots';
import { TransactionResponse } from "@ethersproject/providers";
import { TransactionResponse } from 'ethers';

/* 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. */
export class CrocEnv {
constructor (conn: ConnectArg, signer?: Signer) {
this.context = connectCroc(conn, signer)
this.tokens = new TokenRepo(this.context)
this.context = connectCroc(conn, signer)
this.tokens = new TokenRepo(this.context)
}

/* Generates a prefix object for a swap with a fixed buy quantity.
/* Generates a prefix object for a swap with a fixed buy quantity.
* Example of generating a swap plan for buying 100 USDC by swapping DAI:
* crocEnv.buy(USDC, 100).with(DAI)
*
*
* @param token The address of the token to buy.
* @param qty The fixed quantity of the token to buy. */
buy (token: string, qty: TokenQty): BuyPrefix {
return new BuyPrefix(token, qty, this.tokens, this.context)
}

/* Generates a prefix object for a swap with a fixed buy quantity of native ETH.
/* Generates a prefix object for a swap with a fixed buy quantity of native ETH.
* Example of generating a swap plan for buying 100 USDC by swapping DAI:
* crocEnv.buyEth(100).with(DAI)
*
*
* @param qty The fixed quantity of native ETH to buy. */
buyEth (qty: TokenQty): BuyPrefix {
return new BuyPrefix(AddressZero, qty, this.tokens, this.context)
return new BuyPrefix(ZeroAddress, qty, this.tokens, this.context)
}

/* Generates a prefix object for a swap with a fixed sell quantity.
/* Generates a prefix object for a swap with a fixed sell quantity.
* Example of generating a swap plan for selling 100 USDC to swap into DAI:
* crocEnv.sell(USDC, 100).for(DAI)
*
*
* @param token The address of the token to sell.
* @param qty The fixed quantity of the token to sell. */
sell (token: string, qty: TokenQty): SellPrefix {
return new SellPrefix(token, qty, this.tokens, this.context)
}

/* Generates a prefix object for a swap with a fixed sell quantity of native ETH.
/* Generates a prefix object for a swap with a fixed sell quantity of native ETH.
* Example of generating a swap plan for selling 100 native ETH to swap into DAI:
* crocEnv.sellEth(100).for(DAI)
*
*
* @param qty The fixed quantity of native ETH to sell. */
sellEth (qty: TokenQty): SellPrefix {
return new SellPrefix(AddressZero, qty, this.tokens, this.context)
return new SellPrefix(ZeroAddress, qty, this.tokens, this.context)
}

/* Returns a view of the canonical pool for the underlying token pair. For example the
Expand All @@ -69,19 +69,19 @@ export class CrocEnv {
return new CrocPoolView(viewA, viewB, this.context)
}

/* Returns a view of the canonical pool for the token pair against native ETH. For example
/* Returns a view of the canonical pool for the token pair against native ETH. For example
* the below woudl return a pool view for MKR/ETH with MKR priced in ETH for display purposes
* crocEnv.poolEth(MKR) */
poolEth (token: string): CrocPoolView {
return this.pool(token, AddressZero)
return this.pool(token, ZeroAddress)
}

/* Returns a view of the canonical pool for the token pair against native ETH, but ETH is
* priced in terms of the token. Usually the convention when ETH is paired against stablecoins
* or paired against Bitcoin. For example the below would return a pool view for ETH/USDC
* crocEnv.poolEthQuote(USDC) */
poolEthQuote (token: string): CrocPoolView {
return this.pool(AddressZero, token)
return this.pool(ZeroAddress, token)
}

/* Returns a position view for a single user on the canonical pool for a single pair. */
Expand All @@ -91,14 +91,14 @@ export class CrocEnv {
}

/* Returns a tokenView for a single token
* @param token The address of the specifc token. */
* @param token The address of the specifc token. */
token (token: string): CrocTokenView {
return this.tokens.materialize(token)
}

/* Returns a tokenView for native ETH. */
tokenEth(): CrocTokenView {
return this.tokens.materialize(AddressZero)
return this.tokens.materialize(ZeroAddress)
}

async approveBypassRouter(): Promise<TransactionResponse | undefined> {
Expand All @@ -122,16 +122,16 @@ class BuyPrefix {
}

with (token: string, args?: CrocSwapPlanOpts): CrocSwapPlan {
return new CrocSwapPlan(this.repo.materialize(token),
return new CrocSwapPlan(this.repo.materialize(token),
this.repo.materialize(this.token), this.qty, true, this.context, args)
}

withEth (args?: CrocSwapPlanOpts): CrocSwapPlan {
return this.with(AddressZero, args)
return this.with(ZeroAddress, args)
}

atLimit (token: string, tick: number): CrocKnockoutHandle {
return new CrocKnockoutHandle(this.repo.materialize(token),
return new CrocKnockoutHandle(this.repo.materialize(token),
this.repo.materialize(this.token), this.qty, false, tick, this.context)
}

Expand All @@ -150,16 +150,16 @@ class SellPrefix {
}

for (token: string, args?: CrocSwapPlanOpts): CrocSwapPlan {
return new CrocSwapPlan(this.repo.materialize(this.token),
return new CrocSwapPlan(this.repo.materialize(this.token),
this.repo.materialize(token), this.qty, false, this.context, args)

}
forEth (args?: CrocSwapPlanOpts): CrocSwapPlan {
return this.for(AddressZero, args)
return this.for(ZeroAddress, args)
}

atLimit (token: string, tick: number): CrocKnockoutHandle {
return new CrocKnockoutHandle(this.repo.materialize(this.token),
return new CrocKnockoutHandle(this.repo.materialize(this.token),
this.repo.materialize(token), this.qty, true, tick, this.context)
}

Expand Down Expand Up @@ -193,4 +193,4 @@ class TokenRepo {

tokenViews: Map<string, CrocTokenView>
context: Promise<CrocContext>
}
}
6 changes: 3 additions & 3 deletions src/encoding/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class PoolInitEncoder {
this.baseToken = baseToken
this.quoteToken = quoteToken
this.poolIdx = poolIdx
this.abiCoder = new ethers.utils.AbiCoder();
this.abiCoder = new ethers.AbiCoder();
}

encodeInitialize (initPrice: number): string {
Expand All @@ -23,5 +23,5 @@ export class PoolInitEncoder {
private baseToken: Address
private quoteToken: Address
private poolIdx: PoolType
private abiCoder: ethers.utils.AbiCoder;
}
private abiCoder: ethers.AbiCoder;
}
Loading

0 comments on commit 84e2281

Please sign in to comment.