Skip to content

Instantly share code, notes, and snippets.

@HolyGrease
Created December 22, 2023 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HolyGrease/501b1c7832c2d911cd35ee9011fff41e to your computer and use it in GitHub Desktop.
Save HolyGrease/501b1c7832c2d911cd35ee9011fff41e to your computer and use it in GitHub Desktop.
Fix position
const util = require('util')
const nearApi = require('near-api-js')
const { connect, Contract, keyStores } = nearApi;
async function main() {
const homedir = require("os").homedir();
const CREDENTIALS_DIR = ".near-credentials";
const credentialsPath = require("path").join(homedir, CREDENTIALS_DIR);
const myKeyStore = new keyStores.UnencryptedFileSystemKeyStore(credentialsPath);
const connectionConfig = {
networkId: "mainnet",
keyStore: myKeyStore, // first create a key store
nodeUrl: "https://rpc.mainnet.near.org",
walletUrl: "https://wallet.mainnet.near.org",
helperUrl: "https://helper.mainnet.near.org",
explorerUrl: "https://explorer.mainnet.near.org",
};
const nearConnection = await connect(connectionConfig);
const account = await nearConnection.account("holygrease.near");
const contract = new Contract(
account,
'v1.pembrock.near', {
viewMethods: [
"get_farms",
"get_tokens",
"get_token",
"get_position",
"get_positions",
"check_liquidation",
"get_account",
"get_settings",
"get_owner",
],
changeMethods: [
"withdraw",
"kill_position",
"push_position",
"proxy_claim_reward_by_seed",
"proxy_withdraw_reward",
"proxy_swap",
"proxy_swap_fee",
"proxy_withdraw_reinvest_fee",
"proxy_add_liquidity",
"proxy_add_stable_liquidity",
"proxy_mft_transfer_call",
"proxy_transfer_profit_to_exchange",
"proxy_swap_profit",
"proxy_withdraw_profit_from_exchange",
"proxy_transfer_profit_to_distribution",
],
});
const POSITION_ID = 4526;
const position = await contract.get_position({ position_id: POSITION_ID });
const token = await contract.get_token({ token_id: position.debt_token_id });
const debt = BigInt(position.debt_shares) * BigInt(token.total_borrowed) / BigInt(token.debt_shares);
const positionTokenValue = position.flags.DebtIsToken1 ? position.tokens[0] : position.tokens[1];
const difference = debt - BigInt(positionTokenValue);
const transferTokenCommand = "NEAR_ENV=mainnet near call "
+ position.debt_token_id
+ "ft_transfer '{\"amount\": \""
+ difference.toString()
+ "\"}' --depositYocto 1 --accountId ";
const token1Amount = position.flags.DebtIsToken1 ? positionTokenValue : position.tokens[0];
const token2Amount = position.flags.DebtIsToken1 ? position.tokens[1] : positionTokenValue;
const updatePositionCommand = "NEAR_ENV=mainnet near call "
+ "v1.pembrock.near"
+ "owner_update_position '{\"assets\": {\"token1_amount\": \""
+ token1Amount.toString()
+ "\", \"token2_amount\": \""
+ token2Amount.toString()
+ "\"}, \"position_id\": "
+ POSITION_ID
+ "}' --depositYocto 1 --accountId v1.pembrock.near";
const unlockPositionCommand = "NEAR_ENV=mainnet near call "
+ "v1.pembrock.near "
+ "owner_on_withdraw1 '{\"position_id\": "
+ POSITION_ID
+ "}' --depositYocto 1 --accountId v1.pembrock.near --gas 300000000000000";
const tokenContract = new Contract(
account,
position.debt_token_id, {
viewMethods: ["ft_metadata"],
changeMethods: []
});
const { decimals } = await tokenContract.ft_metadata();
console.log("Assets to transfer: " + position.debt_token_id + " - " + (Number(difference) / 10 ** decimals).toString());
console.log("Transfer tokens: ", transferTokenCommand);
console.log("Update position: ", updatePositionCommand);
console.log("Unlock position: ", unlockPositionCommand);
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment