mirror of
https://github.com/orionprotocol/sdk.git
synced 2026-04-03 03:28:07 +03:00
removed cfd
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
import { ethers } from 'ethers';
|
||||
import type { CFDOrder } from '../types.js';
|
||||
|
||||
const hashCFDOrder = (order: CFDOrder) => ethers.utils.solidityKeccak256(
|
||||
[
|
||||
'uint8',
|
||||
'address',
|
||||
'address',
|
||||
'address',
|
||||
'uint64',
|
||||
'uint64',
|
||||
'uint64',
|
||||
'uint64',
|
||||
'uint64',
|
||||
'uint8',
|
||||
],
|
||||
[
|
||||
'0x03',
|
||||
order.senderAddress,
|
||||
order.matcherAddress,
|
||||
order.instrumentAddress,
|
||||
order.amount,
|
||||
order.price,
|
||||
order.matcherFee,
|
||||
order.nonce,
|
||||
order.expiration,
|
||||
order.buySide === 1 ? '0x01' : '0x00',
|
||||
],
|
||||
);
|
||||
|
||||
export default hashCFDOrder;
|
||||
@@ -1,5 +1,4 @@
|
||||
export { default as signCancelOrder } from './signCancelOrder.js';
|
||||
export { default as signCancelOrderPersonal } from './signCancelOrderPersonal.js';
|
||||
export { default as signOrder } from './signOrder.js';
|
||||
export { default as signCFDOrder } from './signCFDOrder.js';
|
||||
export { default as signOrderPersonal } from './signOrderPersonal.js';
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
import type { TypedDataSigner } from '@ethersproject/abstract-signer';
|
||||
import { BigNumber } from 'bignumber.js';
|
||||
import type { ethers } from 'ethers';
|
||||
import { joinSignature, splitSignature } from 'ethers/lib/utils.js';
|
||||
import { INTERNAL_PROTOCOL_PRECISION } from '../constants/index.js';
|
||||
import type { CFDOrder, SignedCFDOrder, SupportedChainId } from '../types.js';
|
||||
import normalizeNumber from '../utils/normalizeNumber.js';
|
||||
import getDomainData from './getDomainData.js';
|
||||
import signCFDOrderPersonal from './signCFDOrderPersonal.js';
|
||||
import hashCFDOrder from './hashCFDOrder.js';
|
||||
import CFD_ORDER_TYPES from '../constants/cfdOrderTypes.js';
|
||||
|
||||
const DEFAULT_EXPIRATION = 29 * 24 * 60 * 60 * 1000; // 29 days
|
||||
|
||||
type SignerWithTypedDataSign = ethers.Signer & TypedDataSigner;
|
||||
|
||||
export const signCFDOrder = async (
|
||||
instrumentAddress: string,
|
||||
side: 'BUY' | 'SELL',
|
||||
price: BigNumber.Value,
|
||||
amount: BigNumber.Value,
|
||||
matcherFee: BigNumber.Value,
|
||||
senderAddress: string,
|
||||
matcherAddress: string,
|
||||
usePersonalSign: boolean,
|
||||
signer: ethers.Signer,
|
||||
chainId: SupportedChainId,
|
||||
stopPrice: BigNumber.Value | undefined,
|
||||
isFromDelegate?: boolean,
|
||||
) => {
|
||||
const nonce = Date.now();
|
||||
const expiration = nonce + DEFAULT_EXPIRATION;
|
||||
|
||||
const order: CFDOrder = {
|
||||
senderAddress,
|
||||
matcherAddress,
|
||||
instrumentAddress,
|
||||
amount: normalizeNumber(
|
||||
amount,
|
||||
INTERNAL_PROTOCOL_PRECISION,
|
||||
BigNumber.ROUND_FLOOR,
|
||||
).toNumber(),
|
||||
price: normalizeNumber(
|
||||
price,
|
||||
INTERNAL_PROTOCOL_PRECISION,
|
||||
BigNumber.ROUND_FLOOR,
|
||||
).toNumber(),
|
||||
matcherFee: normalizeNumber(
|
||||
matcherFee,
|
||||
INTERNAL_PROTOCOL_PRECISION,
|
||||
BigNumber.ROUND_CEIL, // ROUND_CEIL because we don't want get "not enough fee" error
|
||||
).toNumber(),
|
||||
nonce,
|
||||
expiration,
|
||||
buySide: side === 'BUY' ? 1 : 0,
|
||||
stopPrice: stopPrice !== undefined
|
||||
? new BigNumber(stopPrice).toNumber()
|
||||
: undefined,
|
||||
isPersonalSign: usePersonalSign,
|
||||
isFromDelegate,
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||
const typedDataSigner = signer as SignerWithTypedDataSign;
|
||||
const signature = usePersonalSign
|
||||
? await signCFDOrderPersonal(order, signer)
|
||||
: await typedDataSigner._signTypedData(
|
||||
getDomainData(chainId),
|
||||
CFD_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"
|
||||
const fixedSignature = joinSignature(splitSignature(signature));
|
||||
|
||||
// if (!fixedSignature) throw new Error("Can't sign order");
|
||||
|
||||
const signedOrder: SignedCFDOrder = {
|
||||
...order,
|
||||
id: hashCFDOrder(order),
|
||||
signature: fixedSignature,
|
||||
};
|
||||
|
||||
return signedOrder;
|
||||
};
|
||||
|
||||
export default signCFDOrder;
|
||||
@@ -1,30 +0,0 @@
|
||||
import { ethers } from 'ethers';
|
||||
import type { CFDOrder } from '../types.js';
|
||||
|
||||
const { arrayify, joinSignature, splitSignature } = ethers.utils;
|
||||
|
||||
const signCFDOrderPersonal = async (order: CFDOrder, signer: ethers.Signer) => {
|
||||
const message = ethers.utils.solidityKeccak256(
|
||||
[
|
||||
'string', 'address', 'address', 'address', 'uint64', 'uint64', 'uint64', 'uint64', 'uint64', 'uint8',
|
||||
],
|
||||
[
|
||||
'order',
|
||||
order.senderAddress,
|
||||
order.matcherAddress,
|
||||
order.instrumentAddress,
|
||||
order.amount,
|
||||
order.price,
|
||||
order.matcherFee,
|
||||
order.nonce,
|
||||
order.expiration,
|
||||
order.buySide,
|
||||
],
|
||||
);
|
||||
const signature = await signer.signMessage(arrayify(message));
|
||||
|
||||
// NOTE: metamask broke sig.v value and we fix it in next line
|
||||
return joinSignature(splitSignature(signature));
|
||||
};
|
||||
|
||||
export default signCFDOrderPersonal;
|
||||
Reference in New Issue
Block a user