-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cherry-pick] add back rebalancing/mercurial profits collection scrip…
…t to main (#316) * add scripts and refactor * moved payer out * rm hardcoded keypair * added payer gitignore
- Loading branch information
Showing
4 changed files
with
181 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters