@quant-finance/order-utils
v1.6.3
Published
This module is used for signing quant protocol orders via ethers
Downloads
2
Readme
Order Utils
@quant-finance/order-utils
is Rolla's public Node.js package for interacting
with and handling orders in the Rolla system.
Use cases
Signing Orders
The order-utils pacakge can be used to sign orders in the Rolla system as demonstrated below.
import {
signOrder,
Order,
defaultOrderTracking,
validateOrderSignature,
} from '@quant-finance/order-utils';
import { getContractAddressesForDeploymentOrThrow } from '@quant-finance/contract-addresses';
import { Wallet } from 'ethers';
(async () => {
const makerPrivKey =
'0x052db11f36081c1cf664786856878045b8b0adc68153059406d4e7440600d169';
const makerWallet = new Wallet(makerPrivKey);
const rollaOrderProtocol =
getContractAddressesForDeploymentOrThrow('bsc-testnet').RollaOrderProtocol;
const chainId = 97;
const order: Order = {
orderCreationTimestamp: '1642084292',
orderExpirationTimestamp: '4797310065',
makerAsset: '0x87a26B26BA07eBEADfeFE8d75Ba39bd8B658Bf0C',
optionAttributes: {
underlyingAsset: '0x628eBdE2f88A85BbAfEC4B0844F68998c014e199',
oracle: '0x2331179fdE29E3f4b792eF2F19907e2087CeC7e6',
strikePrice: '3000000000',
expiryTime: '1644676289',
isCall: true,
},
maker: '0x21d6a2cfd24941cba2d7a8c902f68f5790ed7a2c',
taker: '0x1103ddf9398151e22c22c25866c1e24a71a1fae4',
allowedSender: '0x0000000000000000000000000000000000000000',
makingAmount: '9000000',
takingAmount: '6000000000000000000',
whitelist: '0x0000000000000000000000000000000000000000',
orderTracking: defaultOrderTracking,
takerIsSigner: false,
};
const signedOrder = await signOrder(
makerWallet,
order,
rollaOrderProtocol,
chainId,
);
const isSignatureValid = await validateOrderSignature(
order,
rollaOrderProtocol,
chainId,
signedOrder.signature,
);
console.log(`The signature is ${isSignatureValid ? 'valid' : 'invalid'}`);
})()
.then()
.catch(console.error);
Signing Meta Transactions
You can also sign meta transactions for the Rolla Order Protocol:
import {
signOrderMetaTxn,
MetaOrder,
defaultOrderTracking,
SignatureType,
validateMetaOrderSignature,
} from '@quant-finance/order-utils';
import { getContractAddressesForDeploymentOrThrow } from '@quant-finance/contract-addresses';
import { ethers, Wallet } from 'ethers';
(async () => {
const makerPrivKey =
'0x052db11f36081c1cf664786856878045b8b0adc68153059406d4e7440600d169';
const makerWallet = new Wallet(makerPrivKey);
const rollaOrderProtocol =
getContractAddressesForDeploymentOrThrow('bsc-testnet').RollaOrderProtocol;
const chainId = 97;
const metaOrder: MetaOrder = {
functionType: 0,
nonce: 0,
deadline: '1642505010',
from: '0x21d6a2cfd24941cba2d7a8c902f68f5790ed7a2c',
order: {
orderCreationTimestamp: '1642501414',
orderExpirationTimestamp: '4797310065',
makerAsset: '0x87a26B26BA07eBEADfeFE8d75Ba39bd8B658Bf0C',
optionAttributes: {
underlyingAsset: '0x628eBdE2f88A85BbAfEC4B0844F68998c014e199',
oracle: '0xB8D0D97c13e3658c3Ecbf3980b00B5521090C13f',
strikePrice: '3000000000',
expiryTime: '1645093410',
isCall: true,
},
maker: '0x21d6a2cfd24941cba2d7a8c902f68f5790ed7a2c',
taker: '0x1103ddf9398151e22c22c25866c1e24a71a1fae4',
allowedSender: '0x0000000000000000000000000000000000000000',
makingAmount: '3000000',
takingAmount: '6000000000000000000',
whitelist: '0x0000000000000000000000000000000000000000',
orderTracking: defaultOrderTracking,
takerIsSigner: false,
},
signature: {
signatureType: SignatureType.EIP712,
signatureData:
'0x78d3c47281775d27f0c1b16538893e111192c5fb5158e09ff60e2d28656967087c24e045b09aa0887ba7952df7c2b8bb6598eca0937c5d5f5ccdf43850d0d3431c',
},
permit: '0x00',
};
const signedMetaOrder = await signOrderMetaTxn(
makerWallet,
rollaOrderProtocol,
metaOrder.order,
chainId,
metaOrder.functionType,
metaOrder.signature,
metaOrder.permit,
metaOrder.deadline,
metaOrder.nonce,
);
const isMetaTxnSignatureValid = await validateMetaOrderSignature(
makerWallet.address,
rollaOrderProtocol,
signedMetaOrder.metaOrder,
{
signatureData: ethers.utils.joinSignature({
r: signedMetaOrder.r,
s: signedMetaOrder.s,
v: signedMetaOrder.v,
}),
signatureType: SignatureType.EIP712,
},
chainId,
);
console.log(
`The signature is ${isMetaTxnSignatureValid ? 'valid' : 'invalid'}`,
);
})().then();