@renegade-fi/node
Version:
Node.js library for Renegade
299 lines • 10.5 kB
JavaScript
import { createConfig, createExternalKeyConfig, getSDKConfig, getTaskQueue, getWalletId, updateOrder, } from "@renegade-fi/core";
import { cancelOrder, createOrder, createWallet, deposit, getBackOfQueueWallet, getTaskQueuePaused, getWalletFromRelayer, lookupWallet, payFees, refreshWallet, withdraw, } from "@renegade-fi/core/actions";
import { CHAIN_IDS, ROOT_KEY_MESSAGE_PREFIX } from "@renegade-fi/core/constants";
import * as rustUtils from "../../../renegade-utils/index.js";
import { executeDeposit } from "../../actions/executeDeposit.js";
import { executeWithdrawal, } from "../../actions/executeWithdrawal.js";
import { generateWalletSecrets, } from "../../actions/generateWalletSecrets.js";
/**
* The client for interacting with the Renegade relayer with a keychain.
*/
export class RenegadeClient {
/**
* @internal
*/
constructor(params) {
Object.defineProperty(this, "config", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "configv2", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "seed", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "walletSecrets", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
const defaultConfig = getSDKConfig(params.chainId);
const configv2 = params.overrides
? { ...defaultConfig, ...params.overrides }
: defaultConfig;
this.configv2 = configv2;
if (params.mode === "seed") {
this.seed = params.seed;
this.config = createConfig({
darkPoolAddress: configv2.darkpoolAddress,
priceReporterUrl: configv2.priceReporterUrl,
relayerUrl: configv2.relayerUrl,
chainId: configv2.id,
utils: rustUtils,
});
this.config.setState((s) => ({ ...s, seed: params.seed }));
}
else {
this.walletSecrets = params.walletSecrets;
this.config = createExternalKeyConfig({
chainId: configv2.id,
darkPoolAddress: configv2.darkpoolAddress,
relayerUrl: `https://${configv2.relayerUrl}:3000`,
utils: rustUtils,
websocketUrl: configv2.websocketUrl,
signMessage: params.signMessage,
symmetricKey: params.walletSecrets.symmetric_key,
walletId: params.walletSecrets.wallet_id,
publicKey: params.publicKey,
});
}
}
/**
* Create a client for any chain by seed.
*
* @param params.chainId the chain ID (e.g. CHAIN_IDS.ArbitrumMainnet)
* @param params.seed your 0x… seed
* @param params.overrides any SDKConfig field can be passed directly as an override
*/
static new({ chainId, seed, overrides, }) {
return new RenegadeClient({ chainId, mode: "seed", seed, overrides });
}
/**
* Create a client for any chain with an external keychain.
*
* @param params.chainId the chain ID
* @param params.walletSecrets symmetric key + wallet ID
* @param params.signMessage callback to sign auth messages
* @param params.publicKey your public key
*/
static newWithExternalKeychain({ chainId, walletSecrets, signMessage, publicKey, }) {
return new RenegadeClient({
chainId,
mode: "keychain",
walletSecrets,
signMessage,
publicKey,
});
}
/**
* Arbitrum One client via seed.
*
* @param params.seed your 0x… seed
*/
static newArbitrumOneClient({ seed, }) {
return RenegadeClient.new({
chainId: CHAIN_IDS.ArbitrumOne,
seed,
});
}
/**
* Arbitrum Mainnet client with external keychain.
*
* @param params.walletSecrets symmetric key + wallet ID
* @param params.signMessage callback to sign auth messages
* @param params.publicKey your public key
*/
static newArbitrumOneClientWithKeychain({ walletSecrets, signMessage, publicKey, }) {
return RenegadeClient.newWithExternalKeychain({
chainId: CHAIN_IDS.ArbitrumOne,
walletSecrets,
signMessage,
publicKey,
});
}
/**
* Arbitrum Sepolia client via seed.
*
* @param params.seed your 0x… seed
*/
static newArbitrumSepoliaClient({ seed, }) {
return RenegadeClient.new({
chainId: CHAIN_IDS.ArbitrumSepolia,
seed,
});
}
/**
* Arbitrum Sepolia client with external keychain.
*
* @param params.walletSecrets symmetric key + wallet ID
* @param params.signMessage callback to sign auth messages
* @param params.publicKey your public key
*/
static newArbitrumSepoliaClientWithKeychain({ walletSecrets, signMessage, publicKey, }) {
return RenegadeClient.newWithExternalKeychain({
chainId: CHAIN_IDS.ArbitrumSepolia,
walletSecrets,
signMessage,
publicKey,
});
}
/**
* Base Sepolia client via seed.
*
* @param params.seed your 0x… seed
*/
static newBaseSepoliaClient({ seed }) {
return RenegadeClient.new({ chainId: CHAIN_IDS.BaseSepolia, seed });
}
/**
* Base Sepolia client with external keychain.
*
* @param params.walletSecrets symmetric key + wallet ID
* @param params.signMessage callback to sign auth messages
* @param params.publicKey your public key
*/
static newBaseSepoliaClientWithKeychain({ walletSecrets, signMessage, publicKey, }) {
return RenegadeClient.newWithExternalKeychain({
chainId: CHAIN_IDS.BaseSepolia,
walletSecrets,
signMessage,
publicKey,
});
}
// -- Wallet Operations -- //
async getWallet(params = {
filterDefaults: true,
}) {
return getWalletFromRelayer(this.getConfig(), params);
}
async getBackOfQueueWallet(params = {
filterDefaults: true,
}) {
return getBackOfQueueWallet(this.getConfig(), params);
}
async lookupWallet() {
if (this.walletSecrets) {
return lookupWallet(this.getConfig(), {
blinderSeed: this.walletSecrets.blinder_seed,
shareSeed: this.walletSecrets.share_seed,
skMatch: this.walletSecrets.sk_match,
});
}
return lookupWallet(this.getConfig());
}
async refreshWallet() {
return refreshWallet(this.getConfig());
}
async createWallet() {
if (this.walletSecrets) {
return createWallet(this.getConfig(), {
blinderSeed: this.walletSecrets.blinder_seed,
shareSeed: this.walletSecrets.share_seed,
skMatch: this.walletSecrets.sk_match,
});
}
return createWallet(this.getConfig());
}
getWalletId() {
return getWalletId(this.getConfig());
}
// -- Balance Operations -- //
async deposit(parameters) {
return deposit(this.getConfig(), parameters);
}
async executeDeposit(parameters) {
let config;
if (this.config.renegadeKeyType === "internal") {
config = createConfig({
darkPoolAddress: this.configv2.darkpoolAddress,
priceReporterUrl: this.configv2.priceReporterUrl,
relayerUrl: this.configv2.relayerUrl,
chainId: this.configv2.id,
utils: rustUtils,
viemClient: parameters.publicClient,
});
config.setState((s) => ({ ...s, seed: this.seed }));
}
else {
config = createExternalKeyConfig({
chainId: this.configv2.id,
darkPoolAddress: this.configv2.darkpoolAddress,
relayerUrl: `https://${this.configv2.relayerUrl}:3000`,
utils: rustUtils,
websocketUrl: this.configv2.websocketUrl,
signMessage: this.config.signMessage,
symmetricKey: this.config.symmetricKey,
walletId: this.config.walletId,
publicKey: this.config.publicKey,
viemClient: parameters.publicClient,
});
}
return executeDeposit(config, {
...parameters,
permit2Address: this.configv2.permit2Address,
});
}
async withdraw(parameters) {
return withdraw(this.getConfig(), parameters);
}
async executeWithdraw(parameters) {
return executeWithdrawal(this.getConfig(), parameters);
}
async payFees() {
return payFees(this.getConfig());
}
// -- Order Operations -- //
async placeOrder(parameters) {
return createOrder(this.getConfig(), parameters);
}
async updateOrder(parameters) {
return updateOrder(this.getConfig(), parameters);
}
async cancelOrder(parameters) {
return cancelOrder(this.getConfig(), parameters);
}
// -- Task Operations -- //
async getTaskQueue() {
return getTaskQueue(this.getConfig());
}
async getTaskQueuePaused() {
return getTaskQueuePaused(this.getConfig());
}
// --- Keychain Generation --- //
/**
* Generate the message from which the seed can be derived.
*
* @param chainId - the chain ID
* @returns the message to sign
*/
static generateSeedMessage(chainId) {
return `${ROOT_KEY_MESSAGE_PREFIX} ${chainId}`;
}
/**
* Generate an externally managed keychain for a Renegade wallet.
*
* @param sign - the callback to sign messages
* @returns the keychain
*/
static async generateKeychain({ sign, }) {
return generateWalletSecrets(sign);
}
// -- Private -- //
/**
* @internal
*/
getConfig() {
return this.config;
}
}
//# sourceMappingURL=base.js.map