UNPKG

@orderly.network/default-evm-adapter

Version:

437 lines (433 loc) 12.5 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; 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 __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/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 import * as ed from "@noble/ed25519"; import { encode as bs58encode } from "bs58"; import { BaseWalletAdapter } from "@orderly.network/core"; import { ChainNamespace } from "@orderly.network/types"; // src/helper.ts import { DEFAUL_ORDERLY_KEY_SCOPE, definedTypes } from "@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: definedTypes.EIP712Domain, [primaryType]: 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: definedTypes.EIP712Domain, [primaryType]: 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 || 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: definedTypes.EIP712Domain, [primaryType]: 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: definedTypes.EIP712Domain, [primaryType]: 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: definedTypes.EIP712Domain, [primaryType]: 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: definedTypes.EIP712Domain, [primaryType]: 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 BaseWalletAdapter { constructor(web3Provider) { super(); this.web3Provider = web3Provider; __publicField(this, "chainNamespace", 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 = bs58encode(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 ); } }; export { DefaultEVMWalletAdapter, version_default as version }; //# sourceMappingURL=index.mjs.map