UNPKG

@parifi/synthetix-sdk-ts

Version:

A Typescript SDK for interactions with the Synthetix protocol

282 lines (279 loc) 9.67 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/core/index.ts var core_exports = {}; __export(core_exports, { Core: () => Core }); module.exports = __toCommonJS(core_exports); var import_viem2 = require("viem"); // src/constants/common.ts var import_viem = require("viem"); var ZERO_ADDRESS = import_viem.zeroAddress; var CUSTOM_DECIMALS = { [421614 /* ARBITUM_SEPOLIA */]: { 2: 6, USDC: 6 }, [42161 /* ARBITRUM */]: { 2: 6, USDC: 6 }, [84532 /* BASE_SEPOLIA */]: { 1: 6, USDC: 6 }, [8453 /* BASE */]: { 1: 6, USDC: 6 } }; // src/core/index.ts var Core = class { constructor(synthetixSdk) { this.sdk = synthetixSdk; this.accountIds = []; } async initCore() { await this.getAccountIds(); } /** * Returns the Owner wallet address for an account ID * @param accountId - Account ID * @returns string - Address of the account owning the accountId */ async getAccountOwner(accountId) { const coreProxy = await this.sdk.contracts.getCoreProxyInstance(); const response = await this.sdk.utils.callErc7412({ contractAddress: coreProxy.address, abi: coreProxy.abi, functionName: "getAccountOwner", args: [accountId] }); this.sdk.logger.info(`Core account Owner for id ${accountId} is ${response}`); return response; } /** * Get the address of the USD stablecoin token * * @returns Address of the USD stablecoin token */ async getUsdToken() { const coreProxy = await this.sdk.contracts.getCoreProxyInstance(); const response = await this.sdk.utils.callErc7412({ contractAddress: coreProxy.address, abi: coreProxy.abi, functionName: "getUsdToken", args: [] }); this.sdk.logger.info("USD Token address: ", response); return response; } /** * Get the core account IDs owned by an address. * Fetches the account IDs for the given address by checking the balance of * the AccountProxy contract, which is an NFT owned by the address. * If no address is provided, uses the connected wallet address. * @param address The address to get accounts for. Uses connected address if not provided. * @param defaultAccountId The default account ID to set after fetching. * @returns A list of account IDs owned by the address */ async getAccountIds({ address: accountAddress = this.sdk.accountAddress || ZERO_ADDRESS, accountId: defaultAccountId = void 0 } = {}) { if (accountAddress == ZERO_ADDRESS) { throw new Error("Invalid address"); } const accountProxy = await this.sdk.contracts.getAccountProxyInstance(); const balance = await accountProxy.read.balanceOf([accountAddress]); this.sdk.logger.info("balance", balance); const argsList = []; for (let index = 0; index < Number(balance); index++) { argsList.push([accountAddress, index]); } const accountIds = await this.sdk.utils.multicallErc7412({ contractAddress: accountProxy.address, abi: accountProxy.abi, functionName: "tokenOfOwnerByIndex", args: argsList }); this.accountIds = accountIds; this.sdk.logger.info("accountIds", accountIds); if (defaultAccountId) { this.defaultAccountId = defaultAccountId; } else if (this.accountIds.length > 0) { this.defaultAccountId = this.accountIds[0]; this.sdk.logger.info("Using default account id as ", this.defaultAccountId); } return accountIds; } /** * Get the available collateral for an account for a specified collateral type * of ``token_address`` * Fetches the amount of undelegated collateral available for withdrawal * for a given token and account. * @param tokenAddress The address of the collateral token * @param accountId The ID of the account to check. Uses default if not provided. * @returns The available collateral as an ether value. */ async getAvailableCollateral({ tokenAddress, accountId = this.defaultAccountId }) { if (!accountId) throw new Error("Invalid account ID"); const coreProxy = await this.sdk.contracts.getCoreProxyInstance(); const availableCollateral = await this.sdk.utils.callErc7412({ contractAddress: coreProxy.address, abi: coreProxy.abi, functionName: "getAccountAvailableCollateral", args: [accountId, tokenAddress] }); return (0, import_viem2.formatEther)(availableCollateral); } /** * Retrieves the unique system preferred pool * @returns poolId The id of the pool that is currently set as preferred in the system. */ async getPreferredPool() { const coreProxy = await this.sdk.contracts.getCoreProxyInstance(); const preferredPool = await this.sdk.utils.callErc7412({ contractAddress: coreProxy.address, abi: coreProxy.abi, functionName: "getPreferredPool", args: [] }); this.sdk.logger.info(preferredPool); return preferredPool; } async createAccount(accountId, override = { shouldRevertOnTxFailure: false }) { const txArgs = []; if (accountId != void 0) { txArgs.push(accountId); } const coreProxy = await this.sdk.contracts.getCoreProxyInstance(); const createAccountTx = { target: coreProxy.address, callData: (0, import_viem2.encodeFunctionData)({ abi: coreProxy.abi, functionName: "createAccount", args: txArgs }), value: 0n, requireSuccess: true }; const txs = [createAccountTx]; return this.sdk.utils.processTransactions(txs, { ...override, useOracleCalls: false }); } async deposit({ tokenAddress, amount, decimals = 18, accountId = this.defaultAccountId }, override = { shouldRevertOnTxFailure: false }) { if (!accountId) throw new Error("Invalid account ID"); const amountInWei = (0, import_viem2.parseUnits)(amount.toString(), decimals); const coreProxy = await this.sdk.contracts.getCoreProxyInstance(); const depositTx = { target: coreProxy.address, callData: (0, import_viem2.encodeFunctionData)({ abi: coreProxy.abi, functionName: "deposit", args: [accountId, tokenAddress, amountInWei] }), value: 0n, requireSuccess: true }; const txs = [depositTx]; return this.sdk.utils.processTransactions(txs, override); } async withdraw({ tokenAddress, amount, decimals = 18, accountId = this.defaultAccountId }, override = { shouldRevertOnTxFailure: false }) { if (!accountId) throw new Error("Account ID is required for withdrawal"); const amountInWei = (0, import_viem2.parseUnits)(amount.toString(), decimals); const coreProxy = await this.sdk.contracts.getCoreProxyInstance(); const withdrawTx = { target: coreProxy.address, callData: (0, import_viem2.encodeFunctionData)({ abi: coreProxy.abi, functionName: "withdraw", args: [accountId, tokenAddress, amountInWei] }), value: 0n, requireSuccess: true }; const txs = [withdrawTx]; return this.sdk.utils.processTransactions(txs, override); } async delegateCollateral({ tokenAddress, amount, poolId, leverage, accountId = this.defaultAccountId }, override = { shouldRevertOnTxFailure: false }) { if (!accountId) throw new Error("Invalid account ID"); const amountInWei = (0, import_viem2.parseUnits)(amount.toString(), 18); const leverageInWei = (0, import_viem2.parseUnits)(leverage.toString(), 18); const coreProxy = await this.sdk.contracts.getCoreProxyInstance(); const delegateCollateralTx = { target: coreProxy.address, callData: (0, import_viem2.encodeFunctionData)({ abi: coreProxy.abi, functionName: "delegateCollateral", args: [accountId, poolId, tokenAddress, amountInWei, leverageInWei] }), value: 0n, requireSuccess: true }; const txs = [delegateCollateralTx]; return this.sdk.utils.processTransactions(txs, override); } async mintUsd({ tokenAddress, amount, poolId, accountId = this.defaultAccountId }, override = { shouldRevertOnTxFailure: false }) { if (!accountId) throw new Error("Invalid account ID"); const amountInWei = (0, import_viem2.parseUnits)(amount.toString(), 18); const coreProxy = await this.sdk.contracts.getCoreProxyInstance(); const mintUsdTx = { target: coreProxy.address, callData: (0, import_viem2.encodeFunctionData)({ abi: coreProxy.abi, functionName: "mintUsd", args: [accountId, poolId, tokenAddress, amountInWei] }), value: 0n, requireSuccess: true }; const txs = [mintUsdTx]; return this.sdk.utils.processTransactions(txs, override); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Core }); //# sourceMappingURL=index.js.map