-
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.
[SCRIPT] add script for mercurial profit collection (#312)
* add scripts and refactor * moved payer out * updating-binaries-to-pass-test --------- Co-authored-by: Vincent Brunet <[email protected]>
- Loading branch information
1 parent
a6c9e5c
commit b4dfc6b
Showing
7 changed files
with
183 additions
and
83 deletions.
There are no files selected for viewing
Binary file modified
BIN
+0 Bytes
(100%)
programs/uxd/tests/integration_tests/api/program_alloyx/binaries/executable-devnet.so
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
programs/uxd/tests/integration_tests/api/program_credix/binaries/executable-devnet.so
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
programs/uxd/tests/integration_tests/api/program_credix/binaries/executable-mainnet-beta.so
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
programs/uxd/tests/integration_tests/api/program_credix/binaries/executable-pre-mainnet.so
Binary file not shown.
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,85 @@ | ||
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; | ||
} | ||
} | ||
|
||
// Dummy payer for mainnet tooling E7N44oZ3APNFjzv95xL6kSxSLgw3wVP3ixM7dgsMApzZ | ||
export 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()); |
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