diff --git a/package.json b/package.json index b256294..65738eb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.13", + "version": "0.20.15", "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..b61dc4b 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() @@ -95,5 +99,5 @@ export async function exchangeToNativeDecimals(token: AddressLike, amount: BigNu const contract = ERC20__factory.connect(token, provider) decimals = BigInt(await contract.decimals()) } - return BigInt(amount) * (BigInt(10) ** decimals) / (BigInt(10) ** 8n) + return BigInt(amount) * (BigInt(10) ** decimals) } \ No newline at end of file diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index e792a27..506c79b 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, - path.first().assetIn, + const walletBalance = await getWalletBalance( 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 < await exchangeToNativeDecimals(path.first().assetIn, amount, unit.provider), path, wethAddress, curveRegistryAddress, diff --git a/src/config/chains.json b/src/config/chains.json index 0cba1b0..68b940c 100644 --- a/src/config/chains.json +++ b/src/config/chains.json @@ -35,7 +35,7 @@ "baseCurrencyName": "BNB", "contracts": { "WETH": "0x23eE96bEaAB62abE126AA192e677c52bB7d274F0", - "curveRegistry": "0x8845b36C3DE93379F468590FFa102D4aF8C49A6c" + "curveRegistry": "0x93461072c00b2740c474a3d52c70be6249c802d8" } }, "3": { diff --git a/src/crypt/index.ts b/src/crypt/index.ts index 8dbb278..d09530a 100644 --- a/src/crypt/index.ts +++ b/src/crypt/index.ts @@ -1,4 +1,2 @@ export { default as signCancelOrder } from './signCancelOrder.js'; -export { default as signCancelOrderPersonal } from './signCancelOrderPersonal.js'; export { default as signOrder } from './signOrder.js'; -export { default as signOrderPersonal } from './signOrderPersonal.js'; diff --git a/src/crypt/signCancelOrder.ts b/src/crypt/signCancelOrder.ts index ff49d22..f9bd329 100644 --- a/src/crypt/signCancelOrder.ts +++ b/src/crypt/signCancelOrder.ts @@ -3,33 +3,27 @@ import { ethers } from 'ethers'; import CANCEL_ORDER_TYPES from '../constants/cancelOrderTypes.js'; import type { CancelOrderRequest, SignedCancelOrderRequest, SupportedChainId } from '../types.js'; import getDomainData from './getDomainData.js'; -import signCancelOrderPersonal from './signCancelOrderPersonal.js'; type SignerWithTypedDataSign = ethers.Signer & TypedDataSigner; const signCancelOrder = async ( senderAddress: string, id: string, - usePersonalSign: boolean, signer: ethers.Signer, chainId: SupportedChainId, ) => { const cancelOrderRequest: CancelOrderRequest = { id, senderAddress, - isPersonalSign: usePersonalSign, }; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const typedDataSigner = signer as SignerWithTypedDataSign; - const signature = usePersonalSign - ? await signCancelOrderPersonal(cancelOrderRequest, signer) - // https://docs.ethers.io/v5/api/signer/#Signer-signTypedData - : await typedDataSigner.signTypedData( - getDomainData(chainId), - CANCEL_ORDER_TYPES, - cancelOrderRequest, - ); + const signature = await typedDataSigner.signTypedData( + getDomainData(chainId), + CANCEL_ORDER_TYPES, + cancelOrderRequest, + ); // https://github.com/poap-xyz/poap-fun/pull/62#issue-928290265 // "Signature's v was always send as 27 or 28, but from Ledger was 0 or 1" diff --git a/src/crypt/signCancelOrderPersonal.ts b/src/crypt/signCancelOrderPersonal.ts deleted file mode 100644 index 2837c3b..0000000 --- a/src/crypt/signCancelOrderPersonal.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { ethers } from 'ethers'; -import type { CancelOrderRequest } from '../types.js'; - -const signCancelOrderPersonal = async ( - cancelOrderRequest: CancelOrderRequest, - signer: ethers.Signer, -) => { - const types = ['string', 'string', 'address']; - const message = ethers.solidityPackedKeccak256( - types, - ['cancelOrder', cancelOrderRequest.id, cancelOrderRequest.senderAddress], - ); - const signature = await signer.signMessage(ethers.getBytes(message)); - - // NOTE: metamask broke sig.v value and we fix it in next line - return ethers.Signature.from(signature).serialized; -}; - -export default signCancelOrderPersonal; diff --git a/src/crypt/signOrder.ts b/src/crypt/signOrder.ts index 90e59df..fca41a7 100644 --- a/src/crypt/signOrder.ts +++ b/src/crypt/signOrder.ts @@ -7,7 +7,6 @@ import type { Order, SignedOrder, SupportedChainId } from '../types.js'; import normalizeNumber from '../utils/normalizeNumber.js'; import getDomainData from './getDomainData.js'; import hashOrder from './hashOrder.js'; -import signOrderPersonal from './signOrderPersonal.js'; const DEFAULT_EXPIRATION = 29 * 24 * 60 * 60 * 1000; // 29 days @@ -23,7 +22,6 @@ export const signOrder = async ( senderAddress: string, matcherAddress: string, serviceFeeAssetAddr: string, - usePersonalSign: boolean, signer: ethers.Signer, chainId: SupportedChainId, ) => { @@ -54,19 +52,16 @@ export const signOrder = async ( nonce, expiration, buySide: side === 'BUY' ? 1 : 0, - isPersonalSign: usePersonalSign, }; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const typedDataSigner = signer as SignerWithTypedDataSign; - const signature = usePersonalSign - ? await signOrderPersonal(order, signer) - : await typedDataSigner.signTypedData( - getDomainData(chainId), - ORDER_TYPES, - order, - ); + const signature = await typedDataSigner.signTypedData( + getDomainData(chainId), + ORDER_TYPES, + order, + ); // https://github.com/poap-xyz/poap-fun/pull/62#issue-928290265 // "Signature's v was always send as 27 or 28, but from Ledger was 0 or 1" diff --git a/src/crypt/signOrderPersonal.ts b/src/crypt/signOrderPersonal.ts deleted file mode 100644 index 4573e96..0000000 --- a/src/crypt/signOrderPersonal.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { ethers } from 'ethers'; -import type { Order } from '../types.js'; - -const signOrderPersonal = async (order: Order, signer: ethers.Signer) => { - const message = ethers.solidityPackedKeccak256( - [ - 'string', 'address', 'address', 'address', 'address', - 'address', 'uint64', 'uint64', 'uint64', 'uint64', 'uint64', 'uint8', - ], - [ - 'order', - order.senderAddress, - order.matcherAddress, - order.baseAsset, - order.quoteAsset, - order.matcherFeeAsset, - order.amount, - order.price, - order.matcherFee, - order.nonce, - order.expiration, - order.buySide, - ], - ); - const signature = await signer.signMessage(ethers.getBytes(message)); - - // NOTE: metamask broke sig.v value and we fix it in next line - return ethers.Signature.from(signature).serialized; -}; - -export default signOrderPersonal; diff --git a/src/types.ts b/src/types.ts index be08dd2..a528108 100644 --- a/src/types.ts +++ b/src/types.ts @@ -48,7 +48,6 @@ export type Order = { nonce: number // uint64 expiration: number // uint64 buySide: 0 | 1 // uint8, 1=buy, 0=sell - isPersonalSign: boolean // bool } export type SignedOrder = { @@ -60,7 +59,6 @@ export type SignedOrder = { export type CancelOrderRequest = { id: number | string senderAddress: string - isPersonalSign: boolean } export type SignedCancelOrderRequest = { 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 +}