diff --git a/.gitignore b/.gitignore index c83985d51..8061f8e77 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/scripts/common.ts b/scripts/common.ts new file mode 100644 index 000000000..100e657f7 --- /dev/null +++ b/scripts/common.ts @@ -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()); diff --git a/scripts/trigger_collect_profits_mercurial.ts b/scripts/trigger_collect_profits_mercurial.ts new file mode 100644 index 000000000..6abf6d1e0 --- /dev/null +++ b/scripts/trigger_collect_profits_mercurial.ts @@ -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(); diff --git a/scripts/trigger_rebalance_credix.js b/scripts/trigger_rebalance_credix.js index ccef49807..538eaa024 100644 --- a/scripts/trigger_rebalance_credix.js +++ b/scripts/trigger_rebalance_credix.js @@ -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(); @@ -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(); @@ -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