Skip to content

Commit

Permalink
[Cherry-pick] add back rebalancing/mercurial profits collection scrip…
Browse files Browse the repository at this point in the history
…t to main (#316)

* add scripts and refactor

* moved payer out

* rm hardcoded keypair

* added payer gitignore
  • Loading branch information
cnek authored Nov 18, 2023
1 parent 1e1e22a commit 59cb6d5
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 83 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ scripts/swap_ci_resident_program_id.sh.bak
.github/workflows/ci-anchor-test.yml.bak
docker-target/
/**/.DS_Store
payer.json
82 changes: 82 additions & 0 deletions scripts/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {
PublicKey,
Connection,
ConnectionConfig,
ConfirmOptions,
Keypair,
} from '@solana/web3.js';
import {
IdentityDepository,
MercurialVaultDepository,
CredixLpDepository,
} from '@uxd-protocol/uxd-client';

const TXN_COMMIT = 'confirmed';
const connectionConfig = {
commitment: TXN_COMMIT,
disableRetryOnRateLimit: false,
confirmTransactionInitialTimeout: 10000,
} as ConnectionConfig;
export const TXN_OPTS = {
commitment: TXN_COMMIT,
preflightCommitment: TXN_COMMIT,
skipPreflight: true,
} as ConfirmOptions;

export const uxdProgramId = new PublicKey(
'UXD8m9cvwk4RcSxnX2HZ9VudQCEeDH6fRnB4CAP57Dr'
);
const usdcMint = new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v');

export function getConnection() {
const connection = new Connection(
'https://api.mainnet-beta.solana.com',
connectionConfig
);
return connection;
}

export function createIdentityDepository() {
return new IdentityDepository(usdcMint, 'USDC', 6, uxdProgramId);
}

export async function createMercurialVaultDepository() {
try {
return await MercurialVaultDepository.initialize({
connection: getConnection(),
collateralMint: {
mint: usdcMint,
name: 'USDC',
symbol: 'USDC',
decimals: 6,
},
uxdProgramId,
});
} catch (error) {
console.error('Failed to initialize mercurial depository');
throw error;
}
}

export async function createCredixLpDepository() {
try {
return await CredixLpDepository.initialize({
connection: getConnection(),
uxdProgramId: uxdProgramId,
collateralMint: usdcMint,
collateralSymbol: 'USDC',
credixProgramId: new PublicKey(
'CRDx2YkdtYtGZXGHZ59wNv1EwKHQndnRc1gT4p8i2vPX'
),
});
} catch (error) {
console.error('Failed to initialize credix depository');
throw error;
}
}

// CI script payer on mainnet: 4NtUyktW5evy1Ez4BgfnRBdU7PLsmDRiZiH5HfBPGRSs
export const payer = Keypair.fromSecretKey(
Buffer.from(JSON.parse(require('fs').readFileSync('./payer.json', 'utf-8')))
);
console.log('payer', payer.publicKey.toBase58());
88 changes: 88 additions & 0 deletions scripts/trigger_collect_profits_mercurial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { web3 } from '@project-serum/anchor';
import { amountToUiAmount } from '@solana/spl-token';
import { Transaction } from '@solana/web3.js';
import { Controller, UXDClient } from '@uxd-protocol/uxd-client';
import {
createMercurialVaultDepository,
getConnection,
payer,
TXN_OPTS,
uxdProgramId,
} from './common';

async function main() {
const controller = new Controller('UXD', 6, uxdProgramId);
const depository = await createMercurialVaultDepository();

controller.info();
depository.info();

const profitsBeneficiaryCollateral = (
await depository.getOnchainAccount(getConnection(), TXN_OPTS)
).profitsBeneficiaryCollateral;
const profitsBeneficiaryCollateralAmountBefore = (
await getConnection().getTokenAccountBalance(profitsBeneficiaryCollateral)
).value.uiAmount;
console.log(
'profitsBeneficiaryCollateral',
profitsBeneficiaryCollateral.toBase58()
);
console.log(
'profitsBeneficiaryCollateral amount before',
profitsBeneficiaryCollateralAmountBefore
);

const estimatedProfitsCollectedAmount = await amountToUiAmount(
getConnection(),
payer,
depository.collateralMint.mint,
(
await depository.calculateProfitsValue(getConnection(), TXN_OPTS)
).toNumber()
);
console.log(
'estimatedProfitsCollectedAmount',
estimatedProfitsCollectedAmount
);

const uxdClient = new UXDClient(uxdProgramId);

const collectProfitsOfMercurialVaultDepositoryIx =
uxdClient.createCollectProfitsOfMercurialVaultDepositoryInstruction(
controller,
depository,
profitsBeneficiaryCollateral,
TXN_OPTS,
payer.publicKey
);

const tx = new Transaction();
tx.add(collectProfitsOfMercurialVaultDepositoryIx);

try {
const txId = await web3.sendAndConfirmTransaction(
getConnection(),
tx,
[payer],
TXN_OPTS
);
console.log(`🔗 'https://explorer.solana.com/tx/${txId}'`);

const profitsBeneficiaryCollateralAmountAfter = (
await getConnection().getTokenAccountBalance(profitsBeneficiaryCollateral)
).value.uiAmount;
console.log(
'profitsBeneficiaryCollateral amount after',
profitsBeneficiaryCollateralAmountAfter
);
console.log(
'actualProfitsCollectedAmount',
profitsBeneficiaryCollateralAmountAfter! -
profitsBeneficiaryCollateralAmountBefore!
);
} catch (error) {
console.log('collectProfits', error);
}
}

main();
93 changes: 10 additions & 83 deletions scripts/trigger_rebalance_credix.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,21 @@
const {
Controller,
CredixLpDepository,
IdentityDepository,
MercurialVaultDepository,
UXDClient,
nativeToUi,
} = require('@uxd-protocol/uxd-client');
const {
Connection,
Keypair,
PublicKey,
Transaction,
ComputeBudgetProgram,
} = require('@solana/web3.js');
const { Transaction, ComputeBudgetProgram } = require('@solana/web3.js');
const { web3 } = require('@project-serum/anchor');
const { BN } = require('bn.js');

const TXN_COMMIT = 'confirmed';
const connectionConfig = {
commitment: TXN_COMMIT,
disableRetryOnRateLimit: false,
confirmTransactionInitialTimeout: 10000,
};
const TXN_OPTS = {
commitment: TXN_COMMIT,
preflightCommitment: TXN_COMMIT,
skipPreflight: true,
};

const uxdProgramId = new PublicKey(
'UXD8m9cvwk4RcSxnX2HZ9VudQCEeDH6fRnB4CAP57Dr'
);
const usdcMint = new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v');

function getConnection() {
const connection = new Connection(
'https://api.mainnet-beta.solana.com',
connectionConfig
);
return connection;
}

function createIdentityDepository() {
return new IdentityDepository(usdcMint, 'USDC', 6, uxdProgramId);
}

async function createMercurialVaultDepository() {
try {
return await MercurialVaultDepository.initialize({
connection: getConnection(),
collateralMint: {
mint: usdcMint,
name: 'USDC',
symbol: 'USDC',
decimals: 6,
},
uxdProgramId,
});
} catch (error) {
console.error('Failed to initialize mercurial depository');
throw error;
}
}

async function createCredixLpDepository() {
try {
return await CredixLpDepository.initialize({
connection: getConnection(),
uxdProgramId: uxdProgramId,
collateralMint: usdcMint,
collateralSymbol: 'USDC',
credixProgramId: new PublicKey(
'CRDx2YkdtYtGZXGHZ59wNv1EwKHQndnRc1gT4p8i2vPX'
),
});
} catch (error) {
console.error('Failed to initialize credix depository');
throw error;
}
}
const {
createCredixLpDepository,
createIdentityDepository,
createMercurialVaultDepository,
getConnection,
payer,
TXN_OPTS,
uxdProgramId,
} = require('./common');

async function main() {
console.log();
Expand All @@ -87,15 +24,6 @@ async function main() {
console.log('------------------------------ ------------------------------');
console.log();

// Dummy payer for mainnet tooling E7N44oZ3APNFjzv95xL6kSxSLgw3wVP3ixM7dgsMApzZ
const payer = Keypair.fromSeed(
Uint8Array.from([
1, 56, 76, 89, 32, 55, 1, 128, 98, 23, 56, 22, 30, 12, 76, 23, 2, 9, 3, 5,
1, 22, 120, 109, 0, 8, 5, 3, 2, 7, 6, 8,
])
);
console.log('payer', payer.publicKey.toBase58());

const controller = new Controller('UXD', 6, uxdProgramId);
const identityDepository = createIdentityDepository();
const mercurialVaultDepository = await createMercurialVaultDepository();
Expand Down Expand Up @@ -209,7 +137,6 @@ async function main() {
const credixGlobalMarketState = credixLpDepository.credixGlobalMarketState;
const credixPass = credixLpDepository.credixPass;
const credixWithdrawEpoch = credixLpDepository.credixWithdrawEpoch;
const credixWithdrawRequest = credixLpDepository.credixWithdrawRequest;
const credixProgram = CredixLpDepository.getCredixProgram(
getConnection(),
credixProgramId
Expand Down

0 comments on commit 59cb6d5

Please sign in to comment.