Skip to content
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

Feature/scaled sell amount #115

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
73 changes: 67 additions & 6 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,78 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"organizeImports": {
"enabled": true
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": [
"src/typechain/**/*"
]
},
"formatter": {
"lineWidth": 100,
"enabled": true,
"indentStyle": "space",
"indentSize": 2
"lineWidth": 120
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
"recommended": true,
"complexity": {
"noExcessiveCognitiveComplexity": "warn",
"noUselessStringConcat": "warn",
"noUselessUndefinedInitialization": "warn",
"useSimplifiedLogicExpression": "warn",
"noForEach": "off"
},
"correctness": {
"useExhaustiveDependencies": "off",
"noConstantMathMinMaxClamp": "warn",
"noInvalidNewBuiltin": "warn",
"noNewSymbol": "warn",
"noUnusedFunctionParameters": "warn",
"noUnusedImports": "warn",
"noUnusedVariables": "warn",
"useArrayLiterals": "warn",
"useHookAtTopLevel": "warn"
},
"nursery": {
"noDuplicateElseIf": "warn",
"noNestedTernary": "warn",
"noStaticElementInteractions": "warn",
"useAriaPropsSupportedByRole": "warn",
"useAtIndex": "warn",
"useCollapsedIf": "warn",
"useValidAutocomplete": "warn"
},
"performance": {
"useTopLevelRegex": "warn"
},
"style": {
"noDefaultExport": "warn",
"noImplicitBoolean": "warn",
"noNegationElse": "warn",
"noShoutyConstants": "warn",
"noYodaExpression": "warn",
"useConsistentArrayType": "warn",
"useExplicitLengthCheck": "warn",
"useFragmentSyntax": "warn",
"useThrowNewError": "warn"
},
"suspicious": {
"noExplicitAny": "off",
"noConsoleLog": "warn",
"noEmptyBlockStatements": "warn",
"noEvolvingTypes": "warn",
"useAwait": "warn",
"useErrorMessage": "warn",
"useNumberToFixedDigitsArgument": "warn"
}
}
}
}
7 changes: 4 additions & 3 deletions examples/example-buy-0x.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { JsonRpcProvider, type JsonRpcSigner } from "@ethersproject/providers";
import { Wallet } from "ethers";
import { isAddress } from "viem";
import { AutoRouter, IndexRouter, ZeroExAggregator2 } from "../src";

/// ENVIRONMENT VARIABLES
Expand All @@ -21,16 +22,16 @@ if (!ZERO_EX_API_URL || !ZERO_EX_API_KEY)

/// 0x48f88A3fE843ccb0b5003e70B4192c1d7448bEf0 on Production
const INDEX_ADDRESS = process.env.INDEX_ADDRESS!;
if (!INDEX_ADDRESS) throw new Error("Missing INDEX_ADDRESS");
if (!INDEX_ADDRESS || !isAddress(INDEX_ADDRESS)) throw new Error("Missing INDEX_ADDRESS");

/// 0xD6dd95610fC3A3579a2C32fe06158d8bfB8F4eE9 on Production
/// new 0x6A74b8C452f36ad3a9a162D2710BA012C3E5eB82
const INDEX_ROUTER_ADDRESS = process.env.INDEX_ROUTER_ADDRESS!;
if (!INDEX_ROUTER_ADDRESS) throw new Error("Missing INDEX_ROUTER_ADDRESS");
if (!INDEX_ROUTER_ADDRESS || !isAddress(INDEX_ROUTER_ADDRESS)) throw new Error("Missing INDEX_ROUTER_ADDRESS");

/// Address of the input token, Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for Native
const INPUT_TOKEN = process.env.INPUT_TOKEN!;
if (!INPUT_TOKEN) throw new Error("Missing INPUT_TOKEN");
if (!INPUT_TOKEN || !isAddress(INPUT_TOKEN)) throw new Error("Missing INPUT_TOKEN");

/// Amount of input token to sell
const SELL_AMOUNT = process.env.SELL_AMOUNT!;
Expand Down
30 changes: 16 additions & 14 deletions examples/example-buy-auto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { JsonRpcProvider, type JsonRpcSigner } from "@ethersproject/providers";
import { Wallet } from "ethers";
import { isAddress } from "viem";

import { AutoRouter, IndexRouter, ZeroExAggregator2 } from "../src";
import { yesNo } from "./utils";

Expand All @@ -20,22 +22,10 @@ const ZERO_EX_API_KEY = process.env.ZERO_EX_API_KEY!;
if (!ZERO_EX_API_URL || !ZERO_EX_API_KEY)
throw new Error("Missing ZERO_EX_API_URL or ZERO_EX_API_KEY");

/// 0x48f88A3fE843ccb0b5003e70B4192c1d7448bEf0 on Production
const INDEX_ADDRESS = process.env.INDEX_ADDRESS!;
if (!INDEX_ADDRESS) throw new Error("Missing INDEX_ADDRESS");

/// 0xD6dd95610fC3A3579a2C32fe06158d8bfB8F4eE9 on Production
/// new 0x6A74b8C452f36ad3a9a162D2710BA012C3E5eB82
const INDEX_ROUTER_ADDRESS = process.env.INDEX_ROUTER_ADDRESS!;
if (!INDEX_ROUTER_ADDRESS) throw new Error("Missing INDEX_ROUTER_ADDRESS");

/// Address of the input token, Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for Native
const INPUT_TOKEN = process.env.INPUT_TOKEN!;
if (!INPUT_TOKEN) throw new Error("Missing INPUT_TOKEN");

/// Amount of input token to sell
const SELL_AMOUNT = process.env.SELL_AMOUNT!;
if (!SELL_AMOUNT) throw new Error("Missing SELL_AMOUNT");
if (!INDEX_ROUTER_ADDRESS || !isAddress(INDEX_ROUTER_ADDRESS)) throw new Error("Missing INDEX_ROUTER_ADDRESS");

/// PREPARE ENTITIES
const provider = new Wallet(PRIVATE_KEY, new JsonRpcProvider(RPC_URL));
Expand All @@ -50,8 +40,20 @@ const autoRouter = new AutoRouter(indexRouter, zeroExAggregator);
/// MAIN FUNCTION

async function main() {
/// 0x48f88A3fE843ccb0b5003e70B4192c1d7448bEf0 on Production
const INDEX_ADDRESS = process.env.INDEX_ADDRESS!;
if (!INDEX_ADDRESS || !isAddress(INDEX_ADDRESS)) throw new Error("Missing INDEX_ADDRESS");

/// Address of the input token, Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for Native
const INPUT_TOKEN = process.env.INPUT_TOKEN!;
if (!INPUT_TOKEN || !isAddress(INPUT_TOKEN)) throw new Error("Missing INPUT_TOKEN");

/// Amount of input token to sell
const SELL_AMOUNT = process.env.SELL_AMOUNT!;
if (!SELL_AMOUNT) throw new Error("Missing SELL_AMOUNT");

const select = await autoRouter.selectBuy(INDEX_ADDRESS, SELL_AMOUNT, INPUT_TOKEN);
console.dir({ select }, { depth: null });
console.dir({ ...select, sellAmount: select.sellAmount.toString(), buyAmount: select.buyAmount.toString() }, { depth: null });
if (select.expectedAllowance && select.expectedAllowance !== "0") {
return "need allowance";
}
Expand Down
7 changes: 4 additions & 3 deletions examples/example-buy-primary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { JsonRpcProvider, type JsonRpcSigner } from "@ethersproject/providers";
import { Wallet } from "ethers";
import { isAddress } from "viem";
import { AutoRouter, IndexRouter, ZeroExAggregator2 } from "../src";

/// ENVIRONMENT VARIABLES
Expand All @@ -21,16 +22,16 @@ if (!ZERO_EX_API_URL || !ZERO_EX_API_KEY)

/// 0x48f88A3fE843ccb0b5003e70B4192c1d7448bEf0 on Production
const INDEX_ADDRESS = process.env.INDEX_ADDRESS!;
if (!INDEX_ADDRESS) throw new Error("Missing INDEX_ADDRESS");
if (!INDEX_ADDRESS || !isAddress(INDEX_ADDRESS)) throw new Error("Missing INDEX_ADDRESS");

/// 0xD6dd95610fC3A3579a2C32fe06158d8bfB8F4eE9 on Production
/// new 0x6A74b8C452f36ad3a9a162D2710BA012C3E5eB82
const INDEX_ROUTER_ADDRESS = process.env.INDEX_ROUTER_ADDRESS!;
if (!INDEX_ROUTER_ADDRESS) throw new Error("Missing INDEX_ROUTER_ADDRESS");
if (!INDEX_ROUTER_ADDRESS || !isAddress(INDEX_ROUTER_ADDRESS)) throw new Error("Missing INDEX_ROUTER_ADDRESS");

/// Address of the input token, Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for Native
const INPUT_TOKEN = process.env.INPUT_TOKEN!;
if (!INPUT_TOKEN) throw new Error("Missing INPUT_TOKEN");
if (!INPUT_TOKEN || !isAddress(INPUT_TOKEN)) throw new Error("Missing INPUT_TOKEN");

/// Amount of input token to sell
const SELL_AMOUNT = process.env.SELL_AMOUNT!;
Expand Down
7 changes: 4 additions & 3 deletions examples/example-sell-0x.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { JsonRpcProvider, type JsonRpcSigner } from "@ethersproject/providers";
import { Wallet } from "ethers";
import { isAddress } from "viem";
import { AutoRouter, IndexRouter, ZeroExAggregator2 } from "../src";

/// ENVIRONMENT VARIABLES
Expand All @@ -21,16 +22,16 @@ if (!ZERO_EX_API_URL || !ZERO_EX_API_KEY)

/// 0x48f88A3fE843ccb0b5003e70B4192c1d7448bEf0 on Production
const INDEX_ADDRESS = process.env.INDEX_ADDRESS!;
if (!INDEX_ADDRESS) throw new Error("Missing INDEX_ADDRESS");
if (!INDEX_ADDRESS || !isAddress(INDEX_ADDRESS)) throw new Error("Missing INDEX_ADDRESS");

/// 0xD6dd95610fC3A3579a2C32fe06158d8bfB8F4eE9 on Production
/// new 0x6A74b8C452f36ad3a9a162D2710BA012C3E5eB82
const INDEX_ROUTER_ADDRESS = process.env.INDEX_ROUTER_ADDRESS!;
if (!INDEX_ROUTER_ADDRESS) throw new Error("Missing INDEX_ROUTER_ADDRESS");
if (!INDEX_ROUTER_ADDRESS || !isAddress(INDEX_ROUTER_ADDRESS)) throw new Error("Missing INDEX_ROUTER_ADDRESS");

/// Address of the input token, Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for Native
const OUTPUT_TOKEN = process.env.OUTPUT_TOKEN!;
if (!OUTPUT_TOKEN) throw new Error("Missing OUTPUT_TOKEN");
if (!OUTPUT_TOKEN || !isAddress(OUTPUT_TOKEN)) throw new Error("Missing OUTPUT_TOKEN");

/// Amount of input token to sell
const SELL_AMOUNT = process.env.SELL_AMOUNT!;
Expand Down
32 changes: 17 additions & 15 deletions examples/example-sell-auto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { JsonRpcProvider, type JsonRpcSigner } from "@ethersproject/providers";
import { Wallet } from "ethers";
import { isAddress } from "viem";

import { AutoRouter, IndexRouter, ZeroExAggregator2 } from "../src";
import { yesNo } from "./utils";

Expand All @@ -20,22 +22,10 @@ const ZERO_EX_API_KEY = process.env.ZERO_EX_API_KEY!;
if (!ZERO_EX_API_URL || !ZERO_EX_API_KEY)
throw new Error("Missing ZERO_EX_API_URL or ZERO_EX_API_KEY");

/// 0x48f88A3fE843ccb0b5003e70B4192c1d7448bEf0 on Production
/// new 0x6A74b8C452f36ad3a9a162D2710BA012C3E5eB82
const INDEX_ADDRESS = process.env.INDEX_ADDRESS!;
if (!INDEX_ADDRESS) throw new Error("Missing INDEX_ADDRESS");

/// 0xD6dd95610fC3A3579a2C32fe06158d8bfB8F4eE9 on Production
/// new 0x6A74b8C452f36ad3a9a162D2710BA012C3E5eB82
const INDEX_ROUTER_ADDRESS = process.env.INDEX_ROUTER_ADDRESS!;
if (!INDEX_ROUTER_ADDRESS) throw new Error("Missing INDEX_ROUTER_ADDRESS");

/// Address of the input token, Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for Native
const OUTPUT_TOKEN = process.env.OUTPUT_TOKEN!;
if (!OUTPUT_TOKEN) throw new Error("Missing OUTPUT_TOKEN");

/// Amount of input token to sell
const SELL_AMOUNT = process.env.SELL_AMOUNT!;
if (!SELL_AMOUNT) throw new Error("Missing SELL_AMOUNT");
if (!INDEX_ROUTER_ADDRESS || !isAddress(INDEX_ROUTER_ADDRESS)) throw new Error("Missing INDEX_ROUTER_ADDRESS");

/// PREPARE ENTITIES
const provider = new Wallet(PRIVATE_KEY, new JsonRpcProvider(RPC_URL));
Expand All @@ -50,8 +40,20 @@ const autoRouter = new AutoRouter(indexRouter, zeroExAggregator);
/// MAIN FUNCTION

async function main() {
/// 0x48f88A3fE843ccb0b5003e70B4192c1d7448bEf0 on Production
const INDEX_ADDRESS = process.env.INDEX_ADDRESS!;
if (!INDEX_ADDRESS || !isAddress(INDEX_ADDRESS)) throw new Error("Missing INDEX_ADDRESS");

/// Address of the input token, Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for Native
const OUTPUT_TOKEN = process.env.OUTPUT_TOKEN!;
if (!OUTPUT_TOKEN || !isAddress(OUTPUT_TOKEN)) throw new Error("Missing OUTPUT_TOKEN");

/// Amount of input token to sell
const SELL_AMOUNT = process.env.SELL_AMOUNT!;
if (!SELL_AMOUNT) throw new Error("Missing SELL_AMOUNT");

const select = await autoRouter.selectSell(INDEX_ADDRESS, SELL_AMOUNT, OUTPUT_TOKEN);
console.dir({ select }, { depth: null });
console.dir({ ...select, buyAmount: select.buyAmount.toString() }, { depth: null });
if (select.expectedAllowance && select.expectedAllowance !== "0") {
return "need allowance";
}
Expand Down
7 changes: 4 additions & 3 deletions examples/example-sell-primary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { JsonRpcProvider, type JsonRpcSigner } from "@ethersproject/providers";
import { Wallet } from "ethers";
import { isAddress } from "viem";
import { AutoRouter, IndexRouter, ZeroExAggregator2 } from "../src";

/// ENVIRONMENT VARIABLES
Expand All @@ -21,16 +22,16 @@ if (!ZERO_EX_API_URL || !ZERO_EX_API_KEY)

/// 0x48f88A3fE843ccb0b5003e70B4192c1d7448bEf0 on Production
const INDEX_ADDRESS = process.env.INDEX_ADDRESS!;
if (!INDEX_ADDRESS) throw new Error("Missing INDEX_ADDRESS");
if (!INDEX_ADDRESS || !isAddress(INDEX_ADDRESS)) throw new Error("Missing INDEX_ADDRESS");

/// 0xD6dd95610fC3A3579a2C32fe06158d8bfB8F4eE9 on Production
/// new 0x6A74b8C452f36ad3a9a162D2710BA012C3E5eB82
const INDEX_ROUTER_ADDRESS = process.env.INDEX_ROUTER_ADDRESS!;
if (!INDEX_ROUTER_ADDRESS) throw new Error("Missing INDEX_ROUTER_ADDRESS");
if (!INDEX_ROUTER_ADDRESS || !isAddress(INDEX_ROUTER_ADDRESS)) throw new Error("Missing INDEX_ROUTER_ADDRESS");

/// Address of the input token, Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for Native
const OUTPUT_TOKEN = process.env.OUTPUT_TOKEN!;
if (!OUTPUT_TOKEN) throw new Error("Missing OUTPUT_TOKEN");
if (!OUTPUT_TOKEN || !isAddress(OUTPUT_TOKEN)) throw new Error("Missing OUTPUT_TOKEN");

/// Amount of input token to sell
const SELL_AMOUNT = process.env.SELL_AMOUNT!;
Expand Down
Loading