swaps nativeToken => anyToken use exchangeBalance as a priority; generateSwapCalldata now also returns eth value for transaction

This commit is contained in:
lomonoshka
2023-11-30 14:15:49 +04:00
parent 982ec94d40
commit e0acb78d4f
3 changed files with 204 additions and 80 deletions

View File

@@ -1,73 +1,70 @@
import type { LibValidator } from '@orionprotocol/contracts/lib/ethers-v6/Exchange.js';
import { ethers, ZeroAddress } from 'ethers';
import type { AddressLike, JsonRpcProvider, BigNumberish, BytesLike } from 'ethers';
import cloneDeep from 'lodash.clonedeep';
import { safeGet, SafeArray } from '../../utils/safeGetters.js';
import { simpleFetch } from 'simple-typed-fetch';
import type Unit from '../index.js';
import { generateUni2Calls, generateUni2Call } from './callGenerators/uniswapV2.js';
import type { LibValidator } from "@orionprotocol/contracts/lib/ethers-v6/Exchange.js";
import { ethers, ZeroAddress } from "ethers";
import type { AddressLike, JsonRpcProvider, BigNumberish, BytesLike } from "ethers";
import cloneDeep from "lodash.clonedeep";
import { safeGet, SafeArray } from "../../utils/safeGetters.js";
import { simpleFetch } from "simple-typed-fetch";
import type Unit from "../index.js";
import { generateUni2Calls, generateUni2Call } from "./callGenerators/uniswapV2.js";
import {
generateUni3Calls,
generateOrion3Calls,
generateUni3Call,
generateOrion3Call,
} from './callGenerators/uniswapV3.js';
import { exchangeToNativeDecimals, generateCalls, pathCallWithBalance } from './callGenerators/utils.js';
import { generateTransferCall } from './callGenerators/erc20.js';
import { generateCurveStableSwapCall } from './callGenerators/curve.js';
import type { SingleSwap } from '../../types.js';
import { addressLikeToString } from '../../utils/addressLikeToString.js';
import { generateUnwrapAndTransferCall, generateWrapAndTransferCall } from './callGenerators/weth.js';
import { getWalletBalance } from '../../utils/getBalance.js';
} from "./callGenerators/uniswapV3.js";
import { exchangeToNativeDecimals, generateCalls, pathCallWithBalance } from "./callGenerators/utils.js";
import { generateTransferCall } from "./callGenerators/erc20.js";
import { generateCurveStableSwapCall } from "./callGenerators/curve.js";
import type { SingleSwap } from "../../types.js";
import { addressLikeToString } from "../../utils/addressLikeToString.js";
import { generateUnwrapAndTransferCall, generateWrapAndTransferCall } from "./callGenerators/weth.js";
import { getExchangeBalance, getWalletBalance } from "../../utils/getBalance.js";
export type Factory = 'UniswapV2' | 'UniswapV3' | 'Curve' | 'OrionV2' | 'OrionV3';
export type Factory = "UniswapV2" | "UniswapV3" | "Curve" | "OrionV2" | "OrionV3";
export type GenerateSwapCalldataWithUnitParams = {
amount: BigNumberish
minReturnAmount: BigNumberish
receiverAddress: string
path: ArrayLike<SingleSwap>
unit: Unit
amount: BigNumberish;
minReturnAmount: BigNumberish;
initiatorAddress: string;
receiverAddress: string;
path: ArrayLike<SingleSwap>;
unit: Unit;
};
export type GenerateSwapCalldataParams = {
amount: BigNumberish
minReturnAmount: BigNumberish
receiverAddress: string
useContractBalance: boolean
path: ArrayLike<SingleSwap>
wethAddress: AddressLike
curveRegistryAddress: AddressLike
swapExecutorContractAddress: AddressLike
provider: JsonRpcProvider
amount: BigNumberish;
minReturnAmount: BigNumberish;
initiatorAddress: string;
receiverAddress: string;
path: ArrayLike<SingleSwap>;
exchangeContractAddress: AddressLike;
wethAddress: AddressLike;
curveRegistryAddress: AddressLike;
swapExecutorContractAddress: AddressLike;
provider: JsonRpcProvider;
};
export async function generateSwapCalldataWithUnit({
amount,
minReturnAmount,
initiatorAddress,
receiverAddress,
path: arrayLikePath,
unit,
}: GenerateSwapCalldataWithUnitParams): Promise<{
calldata: string
swapDescription: LibValidator.SwapDescriptionStruct
calldata: string;
swapDescription: LibValidator.SwapDescriptionStruct;
value: BigInt;
}> {
if (arrayLikePath == undefined || arrayLikePath.length == 0) {
throw new Error('Empty path');
throw new Error("Empty path");
}
const wethAddress = safeGet(unit.contracts, 'WETH');
const curveRegistryAddress = safeGet(unit.contracts, 'curveRegistry');
const { assetToAddress, swapExecutorContractAddress } = await simpleFetch(
unit.blockchainService.getInfo
)();
const wethAddress = safeGet(unit.contracts, "WETH");
const curveRegistryAddress = safeGet(unit.contracts, "curveRegistry");
const { assetToAddress, swapExecutorContractAddress, exchangeContractAddress } = await simpleFetch(unit.blockchainService.getInfo)();
const arrayLikePathCopy = cloneDeep(arrayLikePath);
let path = SafeArray.from(arrayLikePathCopy);
const walletBalance = await getWalletBalance(
assetToAddress[path.first().assetIn] ?? path.first().assetIn.toLowerCase(),
receiverAddress,
unit.provider
);
path = SafeArray.from(arrayLikePathCopy).map((swapInfo) => {
swapInfo.assetIn = assetToAddress[swapInfo.assetIn] ?? swapInfo.assetIn.toLowerCase();
@@ -79,8 +76,9 @@ export async function generateSwapCalldataWithUnit({
amount,
minReturnAmount,
receiverAddress,
useContractBalance: walletBalance < await exchangeToNativeDecimals(path.first().assetIn, amount, unit.provider),
initiatorAddress,
path,
exchangeContractAddress,
wethAddress,
curveRegistryAddress,
swapExecutorContractAddress,
@@ -91,21 +89,26 @@ export async function generateSwapCalldataWithUnit({
export async function generateSwapCalldata({
amount,
minReturnAmount,
initiatorAddress,
receiverAddress,
useContractBalance,
path: arrayLikePath,
exchangeContractAddress,
wethAddress: wethAddressLike,
curveRegistryAddress: curveRegistryAddressLike,
swapExecutorContractAddress: swapExecutorContractAddressLike,
provider,
}: GenerateSwapCalldataParams) {
}: GenerateSwapCalldataParams): Promise<{
calldata: string;
swapDescription: LibValidator.SwapDescriptionStruct;
value: BigInt;
}> {
const wethAddress = await addressLikeToString(wethAddressLike);
const curveRegistryAddress = await addressLikeToString(curveRegistryAddressLike);
const swapExecutorContractAddress = await addressLikeToString(swapExecutorContractAddressLike);
let path = SafeArray.from(arrayLikePath);
const { factory, assetIn: srcToken } = path.first();
const dstToken = path.last().assetOut;
const { assetOut: dstToken } = path.last();
let swapDescription: LibValidator.SwapDescriptionStruct = {
srcToken,
@@ -156,11 +159,18 @@ export async function generateSwapCalldata({
));
const calldata = generateCalls(calls);
const initiatorWalletBalance = await getWalletBalance(srcToken, initiatorAddress, provider)
const initiatorExchangeBalance = await getExchangeBalance(srcToken, initiatorAddress, exchangeContractAddress, provider, true)
const useContractBalance = srcToken === ZeroAddress || amountNativeDecimals < initiatorWalletBalance
if (useContractBalance) {
swapDescription.flags = 1n << 255n;
}
let value = 0n;
if (srcToken === ZeroAddress && initiatorExchangeBalance < amountNativeDecimals) {
value = amountNativeDecimals - initiatorExchangeBalance;
}
return { swapDescription, calldata };
return { swapDescription, calldata , value};
}
async function processSingleFactorySwaps(
@@ -174,27 +184,27 @@ async function processSingleFactorySwaps(
) {
let calls: BytesLike[] = [];
switch (factory) {
case 'OrionV2': {
case "OrionV2": {
swapDescription.srcReceiver = path.first().pool;
calls = await generateUni2Calls(path, swapExecutorContractAddress);
break;
}
case 'UniswapV2': {
case "UniswapV2": {
swapDescription.srcReceiver = path.first().pool;
calls = await generateUni2Calls(path, swapExecutorContractAddress);
break;
}
case 'UniswapV3': {
case "UniswapV3": {
calls = await generateUni3Calls(path, amount, swapExecutorContractAddress, provider);
break;
}
case 'OrionV3': {
case "OrionV3": {
calls = await generateOrion3Calls(path, amount, swapExecutorContractAddress, provider);
break;
}
case 'Curve': {
case "Curve": {
if (path.length > 1) {
throw new Error('Supporting only single stable swap on curve');
throw new Error("Supporting only single stable swap on curve");
}
calls = await generateCurveStableSwapCall(
amount,
@@ -224,33 +234,33 @@ async function processMultiFactorySwaps(
let calls: BytesLike[] = [];
for (const swap of path) {
switch (swap.factory) {
case 'OrionV2': {
case "OrionV2": {
let transferCall = await generateTransferCall(swap.assetIn, swap.pool, 0);
transferCall = pathCallWithBalance(transferCall, swap.assetIn);
const uni2Call = await generateUni2Call(swap.pool, swap.assetIn, swap.assetOut, swapExecutorContractAddress);
calls.push(transferCall, uni2Call);
break;
}
case 'UniswapV2': {
case "UniswapV2": {
let transferCall = await generateTransferCall(swap.assetIn, swap.pool, 0);
transferCall = pathCallWithBalance(transferCall, swap.assetIn);
const uni2Call = await generateUni2Call(swap.pool, swap.assetIn, swap.assetOut, swapExecutorContractAddress);
calls.push(transferCall, uni2Call);
break;
}
case 'UniswapV3': {
case "UniswapV3": {
let uni3Call = await generateUni3Call(swap, 0, swapExecutorContractAddress, provider);
uni3Call = pathCallWithBalance(uni3Call, swap.assetIn);
calls.push(uni3Call);
break;
}
case 'OrionV3': {
case "OrionV3": {
let orion3Call = await generateOrion3Call(swap, 0, swapExecutorContractAddress, provider);
orion3Call = pathCallWithBalance(orion3Call, swap.assetIn);
calls.push(orion3Call);
break;
}
case 'Curve': {
case "Curve": {
let curveCalls = await generateCurveStableSwapCall(
amount,
swapExecutorContractAddress,