Merge pull request #204 from orionprotocol/OP-4656-remove-signOrderPersonal

OP-4656 Remove signOrderPersonal
This commit is contained in:
lomonoshka
2023-11-06 16:06:47 +04:00
committed by GitHub
11 changed files with 40 additions and 86 deletions

View File

@@ -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",

View File

@@ -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)
}

View File

@@ -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,

View File

@@ -35,7 +35,7 @@
"baseCurrencyName": "BNB",
"contracts": {
"WETH": "0x23eE96bEaAB62abE126AA192e677c52bB7d274F0",
"curveRegistry": "0x8845b36C3DE93379F468590FFa102D4aF8C49A6c"
"curveRegistry": "0x93461072c00b2740c474a3d52c70be6249c802d8"
}
},
"3": {

View File

@@ -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';

View File

@@ -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"

View File

@@ -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;

View File

@@ -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"

View File

@@ -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;

View File

@@ -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 = {

View File

@@ -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
}