update hash forming

This commit is contained in:
TheJuze
2024-05-22 17:20:46 +03:00
parent cc928e47eb
commit 7c7f2f1cbf
7 changed files with 94 additions and 24 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@orionprotocol/sdk",
"version": "0.21.0-rc11",
"version": "0.21.0-rc12",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@orionprotocol/sdk",
"version": "0.21.0-rc11",
"version": "0.21.0-rc12",
"hasInstallScript": true,
"license": "ISC",
"dependencies": {

View File

@@ -1,6 +1,6 @@
{
"name": "@orionprotocol/sdk",
"version": "0.21.0-rc11",
"version": "0.21.0-rc12",
"description": "Orion Protocol SDK",
"main": "./lib/index.cjs",
"module": "./lib/index.js",

View File

@@ -1,16 +1,28 @@
import { ethers, keccak256 } from 'ethers';
import type { Order } from '../types.js';
const ORDER_TYPEHASH = "0xb5132db62dfceb466f2f8aee7a039db36a99772e5a9771d28388a5f9baad7c54"
const ORDER_TYPEHASH = '0xb5132db62dfceb466f2f8aee7a039db36a99772e5a9771d28388a5f9baad7c54'
export default function getOrderHash(order: Order) {
const abiCoder = ethers.AbiCoder.defaultAbiCoder()
const abiCoder = ethers.AbiCoder.defaultAbiCoder()
const { senderAddress, matcherAddress, baseAsset, quoteAsset, matcherFeeAsset, amount, price, matcherFee, nonce, expiration, buySide } = order
const orderBytes = abiCoder.encode(
["bytes32", "address", "address", "address", "address", "address", "uint64", "uint64", "uint64", "uint64", "uint64", "uint8"],
[ORDER_TYPEHASH, senderAddress, matcherAddress, baseAsset, quoteAsset, matcherFeeAsset, amount, price, matcherFee, nonce, expiration, buySide]
)
const {
senderAddress,
matcherAddress,
baseAsset,
quoteAsset,
matcherFeeAsset,
amount,
price,
matcherFee,
nonce,
expiration,
buySide
} = order
const orderBytes = abiCoder.encode(
['bytes32', 'address', 'address', 'address', 'address', 'address', 'uint64', 'uint64', 'uint64', 'uint64', 'uint64', 'uint8'],
[ORDER_TYPEHASH, senderAddress, matcherAddress, baseAsset, quoteAsset, matcherFeeAsset, amount, price, matcherFee, nonce, expiration, buySide]
)
return keccak256(orderBytes)
}
return keccak256(orderBytes)
}

View File

@@ -10,8 +10,7 @@ import { LOCK_ORDER_TYPES } from '../constants/lockOrderTypes';
const DEFAULT_EXPIRATION = 29 * 24 * 60 * 60 * 1000; // 29 days
export type SignLockOrderProps = {
userAddress: string // адрес юзера который хочет сделать лок
senderAddress: string // broker
senderAddress: string // user
asset: string
amount: BigNumber.Value
signer: ethers.Signer
@@ -20,7 +19,6 @@ export type SignLockOrderProps = {
}
export const signLockOrder = async ({
userAddress,
senderAddress,
amount,
chainId,
@@ -34,7 +32,6 @@ export const signLockOrder = async ({
const secretHash = ethers.keccak256(secret);
const order: LockOrder = {
user: userAddress,
sender: senderAddress,
expiration,
asset,

View File

@@ -41,8 +41,7 @@ export const signOrder = async ({
}: SignOrderProps) => {
const nonce = Date.now();
const expiration = nonce + DEFAULT_EXPIRATION;
const secret = generateSecret();
const secretHash = ethers.keccak256(secret);
// const secretHash = ethers.keccak256(secret);
const isCrossChain = targetChainId === undefined || targetChainId !== chainId;
@@ -75,8 +74,70 @@ export const signOrder = async ({
}
: {}),
buySide: side === 'BUY' ? 1 : 0,
// chainId,
// secretHash,
// lockOrderExpiration: expiration
};
const limitOrder = {
senderAddress,
matcherAddress,
baseAsset: baseAssetAddress,
quoteAsset: quoteAssetAddress,
matcherFeeAsset: serviceFeeAssetAddress,
amount: normalizeNumber(
amount,
INTERNAL_PROTOCOL_PRECISION,
BigNumber.ROUND_FLOOR,
),
price: normalizeNumber(
price,
INTERNAL_PROTOCOL_PRECISION,
BigNumber.ROUND_FLOOR,
),
matcherFee: normalizeNumber(
matcherFee,
INTERNAL_PROTOCOL_PRECISION,
BigNumber.ROUND_CEIL, // ROUND_CEIL because we don't want get "not enough fee" error
),
nonce: BigInt(nonce),
expiration: BigInt(expiration),
buySide: side === 'BUY' ? 1 : 0,
};
// const limitOrderHash = ethers.keccak256(ethers.toUtf8Bytes(JSON.stringify(limitOrder)));
// Generate the orderParamsHash
const orderParamsHash = ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(
['address', 'address', 'address', 'address', 'address', 'uint64', 'uint64', 'uint64', 'uint64', 'uint64', 'uint8'],
[
limitOrder.senderAddress,
limitOrder.matcherAddress,
limitOrder.baseAsset,
limitOrder.quoteAsset,
limitOrder.matcherFeeAsset,
limitOrder.amount,
limitOrder.price,
limitOrder.matcherFee,
limitOrder.nonce,
limitOrder.expiration,
limitOrder.buySide
]
));
const secret = generateSecret();
const secretHash = ethers.keccak256(secret);
console.log(limitOrder, chainId, secretHash, expiration);
// Type hash from Solidity contract
const CROSS_CHAIN_ORDER_TYPEHASH = ethers.keccak256(ethers.toUtf8Bytes('Order(address senderAddress,address matcherAddress,address baseAsset,address quoteAsset,address matcherFeeAsset,uint64 amount,uint64 price,uint64 matcherFee,uint64 nonce,uint64 expiration,uint8 buySide,uint24 chainId,bytes32 secretHash,uint64 lockOrderExpiration)'))
// Generate the full crossChainOrder hash
const crossChainOrderHash = ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(
['bytes32', 'bytes32', 'uint24', 'bytes32', 'uint64'],
[CROSS_CHAIN_ORDER_TYPEHASH, orderParamsHash, Number(chainId), secretHash, BigInt(expiration)]
));
const signature = await signer.signTypedData(
getDomainData(chainId),
ORDER_TYPES,
@@ -93,7 +154,7 @@ export const signOrder = async ({
...order,
id: hashOrder(order),
signature: fixedSignature,
...(isCrossChain ? { secret, secretHash, targetChainId } : {})
...(isCrossChain ? { secret, secretHash: crossChainOrderHash, targetChainId } : {})
};
return signedOrder;
};

View File

@@ -265,7 +265,6 @@ class Aggregator {
const body = {
secretHash: signedLockOrder.secretHash,
user: signedLockOrder.user,
sender: signedLockOrder.sender,
expiration: signedLockOrder.expiration,
asset: signedLockOrder.asset,
@@ -530,7 +529,7 @@ class Aggregator {
const signatureHeaders = this.generateHeaders(data, method, path, timestamp, apiKey, secretKey);
const compiledHeaders = { ...headers, ...signatureHeaders.headers, };
const body = JSON.stringify(data)
;
;
const res = pmmOrderSchema.parse({});
@@ -550,7 +549,9 @@ class Aggregator {
const errorParseResult = errorSchema.safeParse(json);
if (!errorParseResult.success) { throw Error(`Unrecognized answer from aggregator: ${json}`); }
if (!errorParseResult.success) {
throw Error(`Unrecognized answer from aggregator: ${json}`);
}
throw Error(errorParseResult.data.error.reason);
}

View File

@@ -56,8 +56,7 @@ export type CrossOrder = Order & {
}
export type LockOrder = {
user: string // address // адрес юзера который хочет сделать лок
sender: string // address // broker
sender: string // user address
expiration: number // uint64
asset: string // address(?)
amount: number // uint64