@sx-bet/sportx-js
Version:
Provides an easy to use API to interact with the SportX relayer.
221 lines • 8.14 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { arrayify, hexlify, hexZeroPad } from "@ethersproject/bytes";
import { keccak256 as solidityKeccak256 } from "@ethersproject/solidity";
import { convertToContractOrder } from "./convert";
export function getOrderSignature(order, wallet) {
return __awaiter(this, void 0, void 0, function* () {
const contractOrder = convertToContractOrder(order);
return getContractOrderSignature(contractOrder, wallet);
});
}
export function getOrderHash(order) {
return solidityKeccak256([
"bytes32",
"address",
"uint256",
"uint256",
"uint256",
"uint256",
"address",
"address",
"bool",
], [
order.marketHash,
order.baseToken,
order.totalBetSize,
order.percentageOdds,
order.expiry,
order.salt,
order.maker,
order.executor,
order.isMakerBettingOutcomeOne,
]);
}
function getContractOrderSignature(order, signer) {
return __awaiter(this, void 0, void 0, function* () {
const hash = arrayify(getOrderHash(order));
return signer.signMessage(hash);
});
}
export function getFillOrderEIP712Payload(fillDetails, chainId, version, verifyingContract) {
const payload = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
Details: [
{ name: "action", type: "string" },
{ name: "market", type: "string" },
{ name: "betting", type: "string" },
{ name: "stake", type: "string" },
{ name: "odds", type: "string" },
{ name: "returning", type: "string" },
{ name: "fills", type: "FillObject" },
],
FillObject: [
{ name: "orders", type: "Order[]" },
{ name: "makerSigs", type: "bytes[]" },
{ name: "takerAmounts", type: "uint256[]" },
{ name: "fillSalt", type: "uint256" },
{ name: "beneficiary", type: "address" },
],
Order: [
{ name: "marketHash", type: "bytes32" },
{ name: "baseToken", type: "address" },
{ name: "totalBetSize", type: "uint256" },
{ name: "percentageOdds", type: "uint256" },
{ name: "expiry", type: "uint256" },
{ name: "salt", type: "uint256" },
{ name: "maker", type: "address" },
{ name: "executor", type: "address" },
{ name: "isMakerBettingOutcomeOne", type: "bool" },
],
},
primaryType: "Details",
domain: {
name: "SportX",
version,
chainId,
verifyingContract,
},
message: {
action: fillDetails.action,
market: fillDetails.market,
betting: fillDetails.betting,
stake: fillDetails.stake,
odds: fillDetails.odds,
returning: fillDetails.returning,
fills: {
makerSigs: fillDetails.fills.makerSigs,
orders: fillDetails.fills.orders.map((order) => ({
marketHash: order.marketHash,
baseToken: order.baseToken,
totalBetSize: order.totalBetSize.toString(),
percentageOdds: order.percentageOdds.toString(),
expiry: order.expiry.toString(),
salt: order.salt.toString(),
maker: order.maker,
executor: order.executor,
isMakerBettingOutcomeOne: order.isMakerBettingOutcomeOne,
})),
takerAmounts: fillDetails.fills.takerAmounts.map((takerAmount) => takerAmount.toString()),
fillSalt: fillDetails.fills.fillSalt.toString(),
beneficiary: fillDetails.fills.beneficiary,
},
},
};
return payload;
}
export function getMaticEip712Payload(abiEncodedFunctionSig, nonce, from, chainId, verifyingContract, domainName) {
return {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "verifyingContract", type: "address" },
{ name: "salt", type: "bytes32" },
],
MetaTransaction: [
{ name: "nonce", type: "uint256" },
{ name: "from", type: "address" },
{ name: "functionSignature", type: "bytes" },
],
},
domain: {
name: domainName,
version: "1",
salt: hexZeroPad(hexlify(chainId), 32),
verifyingContract,
},
message: {
nonce,
from,
functionSignature: abiEncodedFunctionSig,
},
primaryType: "MetaTransaction",
};
}
export function getCancelOrderEventsEIP712Payload(sportXeventId, salt, timestamp, chainId) {
const payload = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "salt", type: "bytes32" },
],
Details: [
{ name: "sportXeventId", type: "string" },
{ name: "timestamp", type: "uint256" },
],
},
primaryType: "Details",
domain: {
name: "CancelOrderEventsSportX",
version: "1.0",
chainId,
salt,
},
message: { sportXeventId, timestamp },
};
return payload;
}
export function getCancelAllOrdersEIP712Payload(salt, timestamp, chainId) {
const payload = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "salt", type: "bytes32" },
],
Details: [{ name: "timestamp", type: "uint256" }],
},
primaryType: "Details",
domain: {
name: "CancelAllOrdersSportX",
version: "1.0",
chainId,
salt,
},
message: { timestamp },
};
return payload;
}
export function getCancelOrderEIP712Payload(orderHashes, salt, timestamp, chainId) {
const payload = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "salt", type: "bytes32" },
],
Details: [
{ name: "orderHashes", type: "string[]" },
{ name: "timestamp", type: "uint256" },
],
},
primaryType: "Details",
domain: {
name: "CancelOrderV2SportX",
version: "1.0",
chainId,
salt,
},
message: { orderHashes, timestamp },
};
return payload;
}
//# sourceMappingURL=signing.js.map