@orderly.network/default-evm-adapter
Version:
470 lines (465 loc) • 14.2 kB
JavaScript
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/index.ts
var index_exports = {};
__export(index_exports, {
DefaultEVMWalletAdapter: () => DefaultEVMWalletAdapter,
version: () => version_default
});
module.exports = __toCommonJS(index_exports);
// src/version.ts
if (typeof window !== "undefined") {
window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
window.__ORDERLY_VERSION__["@orderly.network/default-evm-adapter"] = "2.8.12";
}
var version_default = "2.8.12";
// src/walletAdapter.ts
var ed = __toESM(require("@noble/ed25519"));
var import_bs58 = require("bs58");
var import_core = require("@orderly.network/core");
var import_types2 = require("@orderly.network/types");
// src/helper.ts
var import_types = require("@orderly.network/types");
function withdrawMessage(inputs) {
return __async(this, null, function* () {
const { chainId, receiver, token, amount, nonce, brokerId, domain } = inputs;
const primaryType = "Withdraw";
const timestamp = Date.now();
const typeDefinition = {
EIP712Domain: import_types.definedTypes.EIP712Domain,
[primaryType]: import_types.definedTypes[primaryType]
};
const message = {
brokerId,
chainId,
receiver,
token,
amount,
timestamp,
withdrawNonce: nonce
};
const toSignatureMessage = {
domain,
message,
primaryType,
types: typeDefinition
};
return [message, toSignatureMessage];
});
}
function internalTransferMessage(inputs) {
return __async(this, null, function* () {
const { chainId, receiver, token, amount, nonce, domain } = inputs;
const primaryType = "InternalTransfer";
const typeDefinition = {
EIP712Domain: import_types.definedTypes.EIP712Domain,
[primaryType]: import_types.definedTypes[primaryType]
};
const message = {
chainId,
receiver,
token,
amount,
transferNonce: nonce
};
const toSignatureMessage = {
domain,
message,
primaryType,
types: typeDefinition
};
return [message, toSignatureMessage];
});
}
function addOrderlyKeyMessage(inputs) {
return __async(this, null, function* () {
const {
publicKey,
chainId,
// primaryType,
brokerId,
expiration = 365,
timestamp = Date.now(),
scope,
tag,
domain,
subAccountId
} = inputs;
const primaryType = "AddOrderlyKey";
const message = __spreadValues(__spreadValues({
brokerId,
orderlyKey: publicKey,
scope: scope || import_types.DEFAUL_ORDERLY_KEY_SCOPE,
chainId,
timestamp,
// chainType,
expiration: timestamp + 1e3 * 60 * 60 * 24 * expiration
}, typeof tag !== "undefined" ? { tag } : {}), typeof subAccountId !== "undefined" ? { subAccountId } : {});
const typeDefinition = {
EIP712Domain: import_types.definedTypes.EIP712Domain,
[primaryType]: import_types.definedTypes[primaryType]
};
const toSignatureMessage = {
domain,
message,
primaryType,
types: typeDefinition
};
return [message, toSignatureMessage];
});
}
function registerAccountMessage(inputs) {
return __async(this, null, function* () {
const { chainId, domain, registrationNonce, brokerId, timestamp } = inputs;
const primaryType = "Registration";
const message = {
brokerId,
chainId,
timestamp,
registrationNonce
};
const typeDefinition = {
EIP712Domain: import_types.definedTypes.EIP712Domain,
[primaryType]: import_types.definedTypes[primaryType]
};
const toSignatureMessage = {
domain,
message,
primaryType,
types: typeDefinition
};
return [message, toSignatureMessage];
});
}
function settleMessage(inputs) {
return __async(this, null, function* () {
const { settlePnlNonce, brokerId, chainId, timestamp, domain } = inputs;
const primaryType = "SettlePnl";
const typeDefinition = {
EIP712Domain: import_types.definedTypes.EIP712Domain,
[primaryType]: import_types.definedTypes[primaryType]
};
const message = {
brokerId,
chainId,
timestamp,
settleNonce: settlePnlNonce
};
const toSignatureMessage = {
domain,
message,
primaryType,
types: typeDefinition
};
return [message, toSignatureMessage];
});
}
function dexRequestMessage(inputs) {
return __async(this, null, function* () {
const {
payloadType,
nonce,
receiver,
amount,
vaultId,
token,
dexBrokerId,
domain
} = inputs;
const primaryType = "DexRequest";
const typeDefinition = {
EIP712Domain: import_types.definedTypes.EIP712Domain,
[primaryType]: import_types.definedTypes[primaryType]
};
const message = {
payloadType,
nonce,
receiver,
amount,
vaultId,
token,
dexBrokerId
};
const toSignatureMessage = {
domain,
message,
primaryType,
types: typeDefinition
};
return [message, toSignatureMessage];
});
}
// src/walletAdapter.ts
var DefaultEVMWalletAdapter = class extends import_core.BaseWalletAdapter {
constructor(web3Provider) {
super();
this.web3Provider = web3Provider;
__publicField(this, "chainNamespace", import_types2.ChainNamespace.evm);
__publicField(this, "_address");
__publicField(this, "_chainId");
__publicField(this, "contractManager");
}
get address() {
return this._address;
}
get chainId() {
return this._chainId;
}
set chainId(chainId) {
this._chainId = chainId;
}
setConfig(config) {
this._address = config.address;
this._chainId = config.chain.id;
if (config.provider) {
this.web3Provider.provider = config.provider;
}
if (config.contractManager) {
this.contractManager = config.contractManager;
} else {
throw new Error("Please provide contract manager class");
}
}
active(config) {
this.setConfig(config);
this.lifecycleName("active", config);
}
deactivate() {
this.lifecycleName("deactivate", {});
}
update(config) {
this.lifecycleName("update", config);
this.setConfig(config);
}
generateSecretKey() {
let privKey, secretKey;
do {
privKey = ed.utils.randomPrivateKey();
secretKey = (0, import_bs58.encode)(privKey);
} while (secretKey.length !== 44);
return secretKey;
}
lifecycleName(name, data) {
console.log("lifecycle", name, data);
}
signTypedData(toSignatureMessage) {
return __async(this, null, function* () {
console.log("toSignatureMessage", this.address);
return yield this.web3Provider.signTypedData(
// address,
this.address,
// toSignatureMessage
JSON.stringify(toSignatureMessage)
);
});
}
generateRegisterAccountMessage(inputs) {
return __async(this, null, function* () {
const [message, toSignatureMessage] = yield registerAccountMessage(__spreadProps(__spreadValues({}, inputs), {
chainId: this.chainId,
domain: this.getDomain()
}));
const signedMessage = yield this.signTypedData(toSignatureMessage);
return {
message: __spreadProps(__spreadValues({}, message), {
chainType: "EVM"
}),
signatured: signedMessage
};
});
}
generateAddOrderlyKeyMessage(inputs) {
return __async(this, null, function* () {
const [message, toSignatureMessage] = yield addOrderlyKeyMessage(__spreadProps(__spreadValues({}, inputs), {
chainId: this.chainId,
domain: this.getDomain()
}));
const signedMessage = yield this.signTypedData(toSignatureMessage);
return {
message: __spreadProps(__spreadValues({}, message), {
chainType: "EVM"
}),
signatured: signedMessage
};
});
}
generateWithdrawMessage(inputs) {
return __async(this, null, function* () {
const domain = this.getDomain(true);
const [message, toSignatureMessage] = yield withdrawMessage(__spreadProps(__spreadValues({}, inputs), {
chainId: this.chainId,
domain
}));
const signedMessage = yield this.signTypedData(toSignatureMessage);
return {
message: __spreadProps(__spreadValues({}, message), {
chainType: "EVM"
}),
signatured: signedMessage,
domain
};
});
}
generateInternalTransferMessage(inputs) {
return __async(this, null, function* () {
const domain = this.getDomain(true);
const [message, toSignatureMessage] = yield internalTransferMessage(__spreadProps(__spreadValues({}, inputs), {
chainId: this.chainId,
domain
}));
const signedMessage = yield this.signTypedData(toSignatureMessage);
return {
message: __spreadProps(__spreadValues({}, message), {
chainType: "EVM"
}),
signatured: signedMessage,
domain
};
});
}
generateSettleMessage(inputs) {
return __async(this, null, function* () {
const domain = this.getDomain(true);
const [message, toSignatureMessage] = yield settleMessage(__spreadProps(__spreadValues({}, inputs), {
chainId: this.chainId,
domain
}));
const signedMessage = yield this.signTypedData(toSignatureMessage);
return {
message: __spreadProps(__spreadValues({}, message), {
chainType: "EVM"
}),
signatured: signedMessage,
domain
};
});
}
generateDexRequestMessage(inputs) {
return __async(this, null, function* () {
const [message, toSignatureMessage] = yield dexRequestMessage(__spreadProps(__spreadValues({}, inputs), {
chainId: this.chainId
}));
const signedMessage = yield this.signTypedData(toSignatureMessage);
return {
message: __spreadProps(__spreadValues({}, message), {
chainType: "EVM"
}),
signatured: signedMessage,
domain: inputs.domain
};
});
}
getBalance() {
return this.web3Provider.getBalance(this.address);
}
call(address, method, params, options) {
return this.web3Provider.call(address, method, params, options);
}
sendTransaction(contractAddress, method, payload, options) {
return this.web3Provider.sendTransaction(
contractAddress,
method,
payload,
options
);
}
callOnChain(chain, address, method, params, options) {
return this.web3Provider.callOnChain(
chain,
address,
method,
params,
options
);
}
getDomain(onChainDomain) {
if (!this.web3Provider) {
throw new Error("web3Provider is undefined");
}
const chainId = this.chainId;
return {
name: "Orderly",
version: "1",
chainId,
verifyingContract: onChainDomain ? this.contractManager.getContractInfoByEnv().verifyContractAddress : "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
};
}
pollTransactionReceiptWithBackoff(txHash, baseInterval, maxInterval, maxRetries) {
return this.web3Provider.pollTransactionReceiptWithBackoff(
txHash,
baseInterval,
maxInterval,
maxRetries
);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
DefaultEVMWalletAdapter,
version
});
//# sourceMappingURL=index.js.map