From cd5e1d54eb9847948aa02094354fd3138cd9f529 Mon Sep 17 00:00:00 2001 From: Ondra Chaloupka Date: Wed, 13 Sep 2023 18:44:09 +0200 Subject: [PATCH] refactoring in progress --- src/marinade-state/marinade-state.ts | 9 +- src/programs/marinade-finance-program.ts | 30 ++++--- src/programs/marinade-referral-program.ts | 105 ++++++++++++---------- 3 files changed, 84 insertions(+), 60 deletions(-) diff --git a/src/marinade-state/marinade-state.ts b/src/marinade-state/marinade-state.ts index 8fddf35..5c6c398 100644 --- a/src/marinade-state/marinade-state.ts +++ b/src/marinade-state/marinade-state.ts @@ -19,11 +19,12 @@ import { MarinadeFinanceProgramTypeWithConfig, } from '../programs/marinade-finance-program' import { assert } from 'console' +import { DEFAULT_MARINADE_STATE_ADDRESS } from '../config/marinade-config' -export async function fetchMarinadeState({ - program, - config, -}: MarinadeFinanceProgramTypeWithConfig): Promise> { +export async function fetchMarinadeState( + program: MarinadeFinanceProgramType, + marinadeStateAddress?: web3.PublicKey = DEFAULT_MARINADE_STATE_ADDRESS +): Promise> { const stateResponse = (await program.account.state.fetch( config.marinadeStateAddress )) as unknown as MarinadeStateResponse diff --git a/src/programs/marinade-finance-program.ts b/src/programs/marinade-finance-program.ts index 9fb2fa5..97ab05d 100644 --- a/src/programs/marinade-finance-program.ts +++ b/src/programs/marinade-finance-program.ts @@ -75,8 +75,8 @@ export function marinadeFinanceProgram({ } /** - * Estimate due date if a ticket would be created right now - */ + * Estimate due date if a ticket would be created right now + */ export async function getEstimatedUnstakeTicketDueDate( connection: web3.Connection, state: Readonly @@ -92,9 +92,7 @@ export async function getEstimatedUnstakeTicketDueDate( export class MarinadeFinanceProgram { readonly program: MarinadeFinanceProgramType - constructor( - program: MarinadeFinanceProgramType, - ) { + constructor(program: MarinadeFinanceProgramType) { this.program = program } @@ -109,9 +107,14 @@ export class MarinadeFinanceProgram { wallet?: WalletInterface | web3.Keypair opts?: web3.ConfirmOptions }): MarinadeFinanceProgram { - const program = marinadeFinanceProgram({config, provider, wallet, opts}).program - return new MarinadeFinanceProgram(program) - } + const program = marinadeFinanceProgram({ + config, + provider, + wallet, + opts, + }).program + return new MarinadeFinanceProgram(program) + } async getDelayedUnstakeTickets( beneficiary?: web3.PublicKey @@ -131,7 +134,9 @@ export class MarinadeFinanceProgram { }) } - const ticketAccounts = await this.program.account.ticketAccountData.all(filters) + const ticketAccounts = await this.program.account.ticketAccountData.all( + filters + ) const epochInfo = await getEpochInfo(this.program.provider.connection) return new Map( @@ -144,7 +149,10 @@ export class MarinadeFinanceProgram { Date.now() ) - return [ticketAccountPubkey, { ...ticketAccountData, ...ticketDateInfo }] + return [ + ticketAccountPubkey, + { ...ticketAccountData, ...ticketDateInfo }, + ] }) ) } @@ -360,4 +368,4 @@ export class MarinadeFinanceProgram { }) .instruction() } -} \ No newline at end of file +} diff --git a/src/programs/marinade-referral-program.ts b/src/programs/marinade-referral-program.ts index 24548dc..5fbb4a9 100644 --- a/src/programs/marinade-referral-program.ts +++ b/src/programs/marinade-referral-program.ts @@ -4,23 +4,25 @@ import { MarinadeReferralStateResponse } from '../marinade-referral-state/marina import { STAKE_PROGRAM_ID, SYSTEM_PROGRAM_ID } from '../util' import { assertNotNullAndReturn } from '../util/assert' import * as marinadeReferral from './idl/types/marinade_referral' -import { DEFAULT_MARINADE_REFERRAL_PROGRAM_ID } from '../config/marinade-config' +import { DEFAULT_MARINADE_REFERRAL_GLOBAL_STATE_ADDRESS, DEFAULT_MARINADE_REFERRAL_PROGRAM_ID } from '../config/marinade-config' import { AnchorProvider, Wallet as WalletInterface, } from '@coral-xyz/anchor/dist/cjs/provider' +import { MarinadeState } from '../marinade-state/marinade-state.types' +import { mSolLegAuthority, mSolMintAuthority, reserveAddress, solLeg } from '../marinade-state/marinade-state' const MarinadeReferralIDL = marinadeReferral.IDL type MarinadeReferral = marinadeReferral.MarinadeReferral export type MarinadeReferralProgramType = Program export function marinadeReferralProgram({ - programAddress = DEFAULT_MARINADE_REFERRAL_PROGRAM_ID, + programAddress = DEFAULT_MARINADE_REFERRAL_PROGRAM_ID, provider, wallet, opts = {}, }: { - programAddress?: web3.PublicKey, + programAddress?: web3.PublicKey provider: web3.Connection | Provider wallet?: WalletInterface | web3.Keypair opts?: web3.ConfirmOptions @@ -43,26 +45,36 @@ export function marinadeReferralProgram({ ) } - export class MarinadeReferralProgram { - referralStateData: MarinadeReferralStateResponse.ReferralState | null = null + readonly program: MarinadeReferralProgramType + readonly referralState: web3.PublicKey constructor( - public readonly programAddress: web3.PublicKey, - public readonly anchorProvider: Provider, - public readonly referralState: web3.PublicKey | null, - readonly marinade: Marinade - ) {} - - get program(): MarinadeReferralProgramType { - return new Program( - MarinadeReferralIDL, - this.programAddress, - this.anchorProvider - ) + referralState: web3.PublicKey, + program: MarinadeReferralProgramType + ) { + this.referralState = referralState + this.program = program } - liquidUnstakeInstructionBuilder = async ({ + static init({ + referralState = DEFAULT_MARINADE_REFERRAL_GLOBAL_STATE_ADDRESS, + programAddress, + provider, + wallet, + opts = {}, + }: { + referralState?: web3.PublicKey + programAddress?: web3.PublicKey, + provider: web3.Connection | Provider + wallet?: WalletInterface | web3.Keypair + opts?: web3.ConfirmOptions + }): MarinadeReferralProgram { + const program = marinadeReferralProgram({programAddress, provider, wallet, opts}) + return new MarinadeReferralProgram(referralState, program) + } + + async liquidUnstakeInstructionBuilder({ marinadeState, ownerAddress, associatedMSolTokenAccountAddress, @@ -72,19 +84,19 @@ export class MarinadeReferralProgram { ownerAddress: web3.PublicKey associatedMSolTokenAccountAddress: web3.PublicKey amountLamports: BN - }): Promise => - await this.program.methods + }): Promise { + return await this.program.methods .liquidUnstake(amountLamports) .accountsStrict({ - marinadeFinanceProgram: marinadeState.marinadeFinanceProgramId, - state: marinadeState.marinadeStateAddress, + marinadeFinanceProgram: marinadeState.programId, + state: marinadeState.address, referralState: assertNotNullAndReturn( this.referralState, 'The referral code must be provided!' ), - msolMint: marinadeState.mSolMintAddress, - liqPoolMsolLeg: marinadeState.mSolLeg, - liqPoolSolLegPda: await marinadeState.solLeg(), + msolMint: marinadeState.msolMint, + liqPoolMsolLeg: marinadeState.liqPool.msolLeg, + liqPoolSolLegPda: solLeg(marinadeState), getMsolFrom: associatedMSolTokenAccountAddress, getMsolFromAuthority: ownerAddress, transferSolTo: ownerAddress, @@ -96,8 +108,9 @@ export class MarinadeReferralProgram { ).msolTokenPartnerAccount, }) .instruction() + } - depositInstructionBuilder = async ({ + async depositInstructionBuilder({ marinadeState, transferFrom, associatedMSolTokenAccountAddress, @@ -107,22 +120,22 @@ export class MarinadeReferralProgram { transferFrom: web3.PublicKey associatedMSolTokenAccountAddress: web3.PublicKey amountLamports: BN - }): Promise => - await this.program.methods + }): Promise { + return await this.program.methods .deposit(amountLamports) .accountsStrict({ - reservePda: await marinadeState.reserveAddress(), - marinadeFinanceProgram: marinadeState.marinadeFinanceProgramId, + reservePda: reserveAddress(marinadeState), + marinadeFinanceProgram: marinadeState.programId, referralState: assertNotNullAndReturn( this.referralState, 'The referral code must be provided!' ), - state: marinadeState.marinadeStateAddress, - msolMint: marinadeState.mSolMintAddress, - msolMintAuthority: await marinadeState.mSolMintAuthority(), - liqPoolMsolLegAuthority: await marinadeState.mSolLegAuthority(), - liqPoolMsolLeg: marinadeState.mSolLeg, - liqPoolSolLegPda: await marinadeState.solLeg(), + state: marinadeState.address, + msolMint: marinadeState.msolMint, + msolMintAuthority: mSolMintAuthority(marinadeState), + liqPoolMsolLegAuthority: mSolLegAuthority(marinadeState), + liqPoolMsolLeg: marinadeState.liqPool.msolLeg, + liqPoolSolLegPda: solLeg(marinadeState), mintTo: associatedMSolTokenAccountAddress, transferFrom, systemProgram: SYSTEM_PROGRAM_ID, @@ -132,8 +145,9 @@ export class MarinadeReferralProgram { ).msolTokenPartnerAccount, }) .instruction() + } - depositStakeAccountInstructionBuilder = async ({ + async depositStakeAccountInstructionBuilder ({ marinadeState, duplicationFlag, ownerAddress, @@ -149,24 +163,24 @@ export class MarinadeReferralProgram { authorizedWithdrawerAddress: web3.PublicKey associatedMSolTokenAccountAddress: web3.PublicKey validatorIndex: number - }): Promise => - await this.program.methods + }): Promise { + return await this.program.methods .depositStakeAccount(validatorIndex) .accountsStrict({ duplicationFlag, stakeAuthority: authorizedWithdrawerAddress, - state: marinadeState.marinadeStateAddress, - marinadeFinanceProgram: marinadeState.marinadeFinanceProgramId, + state: marinadeState.address, + marinadeFinanceProgram: marinadeState.programId, referralState: assertNotNullAndReturn( this.referralState, 'The referral code must be provided!' ), - stakeList: marinadeState.state.stakeSystem.stakeList.account, + stakeList: marinadeState.stakeSystem.stakeList.account, stakeAccount: stakeAccountAddress, validatorList: - marinadeState.state.validatorSystem.validatorList.account, - msolMint: marinadeState.mSolMintAddress, - msolMintAuthority: await marinadeState.mSolMintAuthority(), + marinadeState.validatorSystem.validatorList.account, + msolMint: marinadeState.msolMint, + msolMintAuthority: mSolMintAuthority(marinadeState), mintTo: associatedMSolTokenAccountAddress, rentPayer: ownerAddress, clock: web3.SYSVAR_CLOCK_PUBKEY, @@ -179,6 +193,7 @@ export class MarinadeReferralProgram { ).msolTokenPartnerAccount, }) .instruction() + } async getReferralStateData(): Promise { if (!this.referralStateData) {