@parifi/synthetix-sdk-ts
Version:
A Typescript SDK for interactions with the Synthetix protocol
257 lines (255 loc) • 8.5 kB
JavaScript
// src/core/index.ts
import { encodeFunctionData, formatEther, parseUnits } from "viem";
// src/constants/common.ts
import { zeroAddress } from "viem";
var ZERO_ADDRESS = 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 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: 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 = parseUnits(amount.toString(), decimals);
const coreProxy = await this.sdk.contracts.getCoreProxyInstance();
const depositTx = {
target: coreProxy.address,
callData: 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 = parseUnits(amount.toString(), decimals);
const coreProxy = await this.sdk.contracts.getCoreProxyInstance();
const withdrawTx = {
target: coreProxy.address,
callData: 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 = parseUnits(amount.toString(), 18);
const leverageInWei = parseUnits(leverage.toString(), 18);
const coreProxy = await this.sdk.contracts.getCoreProxyInstance();
const delegateCollateralTx = {
target: coreProxy.address,
callData: 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 = parseUnits(amount.toString(), 18);
const coreProxy = await this.sdk.contracts.getCoreProxyInstance();
const mintUsdTx = {
target: coreProxy.address,
callData: 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);
}
};
export {
Core
};
//# sourceMappingURL=index.mjs.map