diff --git a/package.json b/package.json index fa10312..fb57e71 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.14-rc0", + "version": "0.20.14-rc1", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Unit/Exchange/callGenerators/utils.ts b/src/Unit/Exchange/callGenerators/utils.ts index e49217b..ad10f6b 100644 --- a/src/Unit/Exchange/callGenerators/utils.ts +++ b/src/Unit/Exchange/callGenerators/utils.ts @@ -87,6 +87,10 @@ export function generateCalls(calls: BytesLike[]) { } export async function exchangeToNativeDecimals(token: AddressLike, amount: BigNumberish, provider: ethers.JsonRpcProvider) { + return await toNativeDecimals(token, amount, provider) / (BigInt(10) ** 8n) +} + +export async function toNativeDecimals(token: AddressLike, amount: BigNumberish, provider: ethers.JsonRpcProvider) { token = await token if (typeof token !== "string") token = await token.getAddress() diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index e792a27..a55d4a5 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -17,8 +17,7 @@ import type { SingleSwap } from "../../types.js"; import type { AddressLike } from "ethers"; import { addressLikeToString } from "../../utils/addressLikeToString.js"; import { generateUnwrapAndTransferCall, generateWrapAndTransferCall } from "./callGenerators/weth.js"; -import { Exchange__factory } from "@orionprotocol/contracts/lib/ethers-v6/index.js"; -import getBalance from "../../utils/getBalance.js"; +import { getWalletBalance } from "../../utils/getBalance.js"; export type Factory = "UniswapV2" | "UniswapV3" | "Curve" | "OrionV2" | "OrionV3"; @@ -57,17 +56,14 @@ export async function generateSwapCalldataWithUnit({ } const wethAddress = safeGet(unit.contracts, "WETH"); const curveRegistryAddress = safeGet(unit.contracts, "curveRegistry"); - const { assetToAddress, swapExecutorContractAddress, exchangeContractAddress } = await simpleFetch( + const { assetToAddress, swapExecutorContractAddress } = await simpleFetch( unit.blockchainService.getInfo )(); let path = SafeArray.from(arrayLikePath); - const { wallet } = await getBalance( - unit.aggregator, + const walletBalance = await getWalletBalance( path.first().assetIn, - safeGet(assetToAddress, path.first().assetIn), receiverAddress, - Exchange__factory.connect(exchangeContractAddress, unit.provider), unit.provider ); @@ -81,7 +77,7 @@ export async function generateSwapCalldataWithUnit({ amount, minReturnAmount, receiverAddress, - useContractBalance: BigInt(wallet.toString()) < BigInt(amount), + useContractBalance: walletBalance < BigInt(amount), path, wethAddress, curveRegistryAddress, diff --git a/src/utils/getBalance.ts b/src/utils/getBalance.ts index 35738ce..e7c84b2 100644 --- a/src/utils/getBalance.ts +++ b/src/utils/getBalance.ts @@ -41,3 +41,22 @@ export default async function getBalance( wallet: denormalizedAssetInWalletBalance, }; } + +export async function getWalletBalance( + assetAddress: string, + walletAddress: string, + provider: ethers.Provider, +) { + const assetIsNativeCryptocurrency = assetAddress === ethers.ZeroAddress; + + let assetWalletBalance: bigint | undefined; + + if (!assetIsNativeCryptocurrency) { + const assetContract = ERC20__factory.connect(assetAddress, provider); + assetWalletBalance = await assetContract.balanceOf(walletAddress); + } else { + assetWalletBalance = await provider.getBalance(walletAddress); + } + + return assetWalletBalance +}