Skip to content

Commit

Permalink
refactoring in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaloup committed Sep 13, 2023
1 parent c0f4eb3 commit cd5e1d5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 60 deletions.
9 changes: 5 additions & 4 deletions src/marinade-state/marinade-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Readonly<MarinadeState>> {
export async function fetchMarinadeState(
program: MarinadeFinanceProgramType,
marinadeStateAddress?: web3.PublicKey = DEFAULT_MARINADE_STATE_ADDRESS
): Promise<Readonly<MarinadeState>> {
const stateResponse = (await program.account.state.fetch(
config.marinadeStateAddress
)) as unknown as MarinadeStateResponse
Expand Down
30 changes: 19 additions & 11 deletions src/programs/marinade-finance-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MarinadeState>
Expand All @@ -92,9 +92,7 @@ export async function getEstimatedUnstakeTicketDueDate(
export class MarinadeFinanceProgram {
readonly program: MarinadeFinanceProgramType

constructor(
program: MarinadeFinanceProgramType,
) {
constructor(program: MarinadeFinanceProgramType) {
this.program = program
}

Expand All @@ -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
Expand All @@ -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(
Expand All @@ -144,7 +149,10 @@ export class MarinadeFinanceProgram {
Date.now()
)

return [ticketAccountPubkey, { ...ticketAccountData, ...ticketDateInfo }]
return [
ticketAccountPubkey,
{ ...ticketAccountData, ...ticketDateInfo },
]
})
)
}
Expand Down Expand Up @@ -360,4 +368,4 @@ export class MarinadeFinanceProgram {
})
.instruction()
}
}
}
105 changes: 60 additions & 45 deletions src/programs/marinade-referral-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MarinadeReferral>

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
Expand All @@ -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<MarinadeReferral>(
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,
Expand All @@ -72,19 +84,19 @@ export class MarinadeReferralProgram {
ownerAddress: web3.PublicKey
associatedMSolTokenAccountAddress: web3.PublicKey
amountLamports: BN
}): Promise<web3.TransactionInstruction> =>
await this.program.methods
}): Promise<web3.TransactionInstruction> {
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,
Expand All @@ -96,8 +108,9 @@ export class MarinadeReferralProgram {
).msolTokenPartnerAccount,
})
.instruction()
}

depositInstructionBuilder = async ({
async depositInstructionBuilder({
marinadeState,
transferFrom,
associatedMSolTokenAccountAddress,
Expand All @@ -107,22 +120,22 @@ export class MarinadeReferralProgram {
transferFrom: web3.PublicKey
associatedMSolTokenAccountAddress: web3.PublicKey
amountLamports: BN
}): Promise<web3.TransactionInstruction> =>
await this.program.methods
}): Promise<web3.TransactionInstruction> {
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,
Expand All @@ -132,8 +145,9 @@ export class MarinadeReferralProgram {
).msolTokenPartnerAccount,
})
.instruction()
}

depositStakeAccountInstructionBuilder = async ({
async depositStakeAccountInstructionBuilder ({
marinadeState,
duplicationFlag,
ownerAddress,
Expand All @@ -149,24 +163,24 @@ export class MarinadeReferralProgram {
authorizedWithdrawerAddress: web3.PublicKey
associatedMSolTokenAccountAddress: web3.PublicKey
validatorIndex: number
}): Promise<web3.TransactionInstruction> =>
await this.program.methods
}): Promise<web3.TransactionInstruction> {
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,
Expand All @@ -179,6 +193,7 @@ export class MarinadeReferralProgram {
).msolTokenPartnerAccount,
})
.instruction()
}

async getReferralStateData(): Promise<MarinadeReferralStateResponse.ReferralState> {
if (!this.referralStateData) {
Expand Down

0 comments on commit cd5e1d5

Please sign in to comment.