Skip to content

Commit

Permalink
Merge pull request #25 from ochaloup/v4.0.0-uses-referral-v2-proposal
Browse files Browse the repository at this point in the history
TS SDK adjustment and fixes
  • Loading branch information
luciotato authored Sep 23, 2022
2 parents b511bb4 + b538c57 commit 832023e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/config/marinade-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class MarinadeConfig {
marinadeReferralProgramId = new web3.PublicKey('MR2LqxoSbw831bNy68utpu5n4YqBH3AzDmddkgk9LQv')

marinadeStateAddress = new web3.PublicKey('8szGkuLTAux9XMgZ2vtY39jVSowEcpBfFfD8hXSEqdGC')
marinadeReferralGlobalStateAddress = new web3.PublicKey('mRg6bDsAd5uwERAdNTynoUeRbqQsLa7yzuK2kkCUPGW')
marinadeReferralGlobalStateAddress = new web3.PublicKey('MRSh4rUNrpn7mjAq9ENHV4rvwwPKMij113ScZq3twp2')

stakeWithdrawAuthPDA = new web3.PublicKey('9eG63CdHjsfhHmobHgLtESGC8GabbmRcaSpHAZrtmhco')

Expand Down
7 changes: 5 additions & 2 deletions src/marinade-referral-state/marinade-referral-state.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ export const enum ProgramDerivedAddressSeed {
export namespace MarinadeReferralStateResponse {
export interface GlobalState {
adminAccount: web3.PublicKey
treasuryMsolAccount: web3.PublicKey
treasuryMsolAuthBump: number
msolMintAccount: web3.PublicKey
foreman1: web3.PublicKey
foreman2: web3.PublicKey
minKeepPct: number
maxKeepPct: number
}

export interface ReferralState {
Expand Down
94 changes: 25 additions & 69 deletions src/marinade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class Marinade {
this.config.marinadeReferralProgramId,
this.provider,
this.config.referralCode,
this,
)

private provideReferralOrMainProgram(): MarinadeFinanceProgram | MarinadeReferralProgram {
Expand Down Expand Up @@ -162,27 +163,13 @@ export class Marinade {
transaction.add(createAssociateTokenInstruction)
}

let depositInstruction
if (this.config.referralCode) {
const program = this.marinadeReferralProgram
const referralState = await this.getReferralPartnerState()
depositInstruction = await program.depositInstructionBuilder({
amountLamports,
marinadeState,
transferFrom: feePayer,
associatedMSolTokenAccountAddress,
msolTokenPartnerAccount: referralState.state.msolTokenPartnerAccount,
})
}
else {
const program = this.marinadeFinanceProgram
depositInstruction = await program.depositInstructionBuilder({
amountLamports,
marinadeState,
transferFrom: feePayer,
associatedMSolTokenAccountAddress,
})
}
const program = this.provideReferralOrMainProgram()
const depositInstruction = await program.depositInstructionBuilder({
amountLamports,
marinadeState,
transferFrom: feePayer,
associatedMSolTokenAccountAddress,
})

transaction.add(depositInstruction)

Expand Down Expand Up @@ -213,27 +200,13 @@ export class Marinade {
}
}

let liquidUnstakeInstruction
if (this.config.referralCode) {
const program = this.marinadeReferralProgram
const referralState = await this.getReferralPartnerState()
liquidUnstakeInstruction = await program.liquidUnstakeInstructionBuilder({
amountLamports,
marinadeState,
ownerAddress,
associatedMSolTokenAccountAddress,
msolTokenPartnerAccount: referralState.state.msolTokenPartnerAccount,
})
}
else {
const program = this.marinadeFinanceProgram
liquidUnstakeInstruction = await program.liquidUnstakeInstructionBuilder({
amountLamports,
marinadeState,
ownerAddress,
associatedMSolTokenAccountAddress,
})
}
const program = this.provideReferralOrMainProgram()
const liquidUnstakeInstruction = await program.liquidUnstakeInstructionBuilder({
amountLamports,
marinadeState,
ownerAddress,
associatedMSolTokenAccountAddress,
})

transaction.add(liquidUnstakeInstruction)

Expand Down Expand Up @@ -292,33 +265,16 @@ export class Marinade {
transaction.add(createAssociateTokenInstruction)
}

let depositStakeAccountInstruction
if (this.config.referralCode) {
const program = this.marinadeReferralProgram
const referralState = await this.getReferralPartnerState()
depositStakeAccountInstruction = await program.depositStakeAccountInstructionBuilder({
validatorIndex,
marinadeState,
duplicationFlag,
authorizedWithdrawerAddress,
associatedMSolTokenAccountAddress,
ownerAddress,
stakeAccountAddress,
msolTokenPartnerAccount: referralState.state.msolTokenPartnerAccount,
})
}
else {
const program = this.marinadeFinanceProgram
depositStakeAccountInstruction = await program.depositStakeAccountInstructionBuilder({
validatorIndex,
marinadeState,
duplicationFlag,
authorizedWithdrawerAddress,
associatedMSolTokenAccountAddress,
ownerAddress,
stakeAccountAddress,
})
}
const program = this.provideReferralOrMainProgram()
const depositStakeAccountInstruction = await program.depositStakeAccountInstructionBuilder({
validatorIndex,
marinadeState,
duplicationFlag,
authorizedWithdrawerAddress,
associatedMSolTokenAccountAddress,
ownerAddress,
stakeAccountAddress,
})

transaction.add(depositStakeAccountInstruction)

Expand Down
26 changes: 17 additions & 9 deletions src/programs/marinade-referral-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import { BN, Idl, Program, Provider, web3 } from '@project-serum/anchor'
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
import { SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY } from '@solana/web3.js'
import { MarinadeState } from '../marinade-state/marinade-state'
import { MarinadeReferralStateResponse } from '../marinade-referral-state/marinade-referral-state.types'
import { Marinade } from '../marinade'
import { STAKE_PROGRAM_ID, SYSTEM_PROGRAM_ID } from '../util'
import { assertNotNullAndReturn } from '../util/assert'
import { MarinadeReferralIdl } from './idl/marinade-referral-idl'
import * as marinadeReferralIdlSchema from './idl/marinade-referral-idl.json'

export class MarinadeReferralProgram {
referralStateData: MarinadeReferralStateResponse.ReferralState | null = null

constructor(
public readonly programAddress: web3.PublicKey,
public readonly anchorProvider: Provider,
public readonly referralState: web3.PublicKey | null,
readonly marinade: Marinade,
) { }

get program(): Program {
Expand All @@ -22,11 +27,10 @@ export class MarinadeReferralProgram {
)
}

liquidUnstakeInstructionAccounts = async({ marinadeState, ownerAddress, associatedMSolTokenAccountAddress, msolTokenPartnerAccount }: {
liquidUnstakeInstructionAccounts = async({ marinadeState, ownerAddress, associatedMSolTokenAccountAddress }: {
marinadeState: MarinadeState,
ownerAddress: web3.PublicKey,
associatedMSolTokenAccountAddress: web3.PublicKey,
msolTokenPartnerAccount: web3.PublicKey,
}): Promise<MarinadeReferralIdl.Instruction.LiquidUnstake.Accounts> => ({
marinadeFinanceProgram: marinadeState.marinadeFinanceProgramId,
state: marinadeState.marinadeStateAddress,
Expand All @@ -40,7 +44,7 @@ export class MarinadeReferralProgram {
treasuryMsolAccount: marinadeState.treasuryMsolAccount,
systemProgram: SYSTEM_PROGRAM_ID,
tokenProgram: TOKEN_PROGRAM_ID,
msolTokenPartnerAccount: msolTokenPartnerAccount,
msolTokenPartnerAccount: (await this.getReferralStateData()).msolTokenPartnerAccount,
})

liquidUnstakeInstruction = ({ accounts, amountLamports }: {
Expand All @@ -57,11 +61,10 @@ export class MarinadeReferralProgram {
accounts: await this.liquidUnstakeInstructionAccounts(accountsArgs),
})

depositInstructionAccounts = async({ marinadeState, transferFrom, associatedMSolTokenAccountAddress, msolTokenPartnerAccount }: {
depositInstructionAccounts = async({ marinadeState, transferFrom, associatedMSolTokenAccountAddress }: {
marinadeState: MarinadeState,
transferFrom: web3.PublicKey,
associatedMSolTokenAccountAddress: web3.PublicKey,
msolTokenPartnerAccount: web3.PublicKey
}): Promise<MarinadeReferralIdl.Instruction.Deposit.Accounts> => ({
reservePda: await marinadeState.reserveAddress(),
marinadeFinanceProgram: marinadeState.marinadeFinanceProgramId,
Expand All @@ -76,7 +79,7 @@ export class MarinadeReferralProgram {
transferFrom,
systemProgram: SYSTEM_PROGRAM_ID,
tokenProgram: TOKEN_PROGRAM_ID,
msolTokenPartnerAccount: msolTokenPartnerAccount,
msolTokenPartnerAccount: (await this.getReferralStateData()).msolTokenPartnerAccount,
})

depositInstruction = ({ accounts, amountLamports }: {
Expand All @@ -100,15 +103,13 @@ export class MarinadeReferralProgram {
stakeAccountAddress,
authorizedWithdrawerAddress,
associatedMSolTokenAccountAddress,
msolTokenPartnerAccount,
}: {
marinadeState: MarinadeState,
duplicationFlag: web3.PublicKey,
ownerAddress: web3.PublicKey,
stakeAccountAddress: web3.PublicKey,
authorizedWithdrawerAddress: web3.PublicKey,
associatedMSolTokenAccountAddress: web3.PublicKey,
msolTokenPartnerAccount: web3.PublicKey,
}): Promise<MarinadeReferralIdl.Instruction.DepositStakeAccount.Accounts> => ({
duplicationFlag,
stakeAuthority: authorizedWithdrawerAddress,
Expand All @@ -127,7 +128,7 @@ export class MarinadeReferralProgram {
systemProgram: SYSTEM_PROGRAM_ID,
tokenProgram: TOKEN_PROGRAM_ID,
stakeProgram: STAKE_PROGRAM_ID,
msolTokenPartnerAccount,
msolTokenPartnerAccount: (await this.getReferralStateData()).msolTokenPartnerAccount,
})

depositStakeAccountInstruction = ({ accounts, validatorIndex }: {
Expand All @@ -143,4 +144,11 @@ export class MarinadeReferralProgram {
validatorIndex,
accounts: await this.depositStakeAccountInstructionAccounts(accountsArgs),
})

async getReferralStateData(): Promise<MarinadeReferralStateResponse.ReferralState> {
if (!this.referralStateData) {
this.referralStateData = (await this.marinade.getReferralPartnerState()).state
}
return this.referralStateData
}
}
25 changes: 18 additions & 7 deletions test/marinade-referral-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Marinade, MarinadeConfig, web3, BN } from '../src'
import { MarinadeReferralStateResponse } from '../src/marinade-referral-state/marinade-referral-state.types'
import * as TestWorld from './test-world'

describe('Marinade Referral State', () => {
describe('getReferralPartnerState', () => {
describe('Marinade Referral Program', () => {
describe('getReferralGlobalState', () => {
it('fetches the referral program\'s global state which matches the expected type', async() => {
const config = new MarinadeConfig({
connection: TestWorld.CONNECTION,
Expand All @@ -15,8 +15,11 @@ describe('Marinade Referral State', () => {

expect(state).toStrictEqual<MarinadeReferralStateResponse.GlobalState>({
adminAccount: expect.any(web3.PublicKey),
treasuryMsolAccount: expect.any(web3.PublicKey),
treasuryMsolAuthBump: expect.any(Number),
msolMintAccount: expect.any(web3.PublicKey),
foreman1: expect.any(web3.PublicKey),
foreman2: expect.any(web3.PublicKey),
minKeepPct: expect.any(Number),
maxKeepPct: expect.any(Number),
})
})
})
Expand All @@ -33,13 +36,14 @@ describe('Marinade Referral State', () => {

expect(state).toStrictEqual<MarinadeReferralStateResponse.ReferralState>({
baseFee: expect.any(Number),
validatorVoteKey: null,
keepSelfStakePct: expect.any(Number),
delayedUnstakeAmount: expect.any(BN),
delayedUnstakeOperations: expect.any(BN),
depositSolAmount: expect.any(BN),
depositSolOperations: expect.any(BN),
depositStakeAccountAmount: expect.any(BN),
depositStakeAccountOperations: expect.any(BN),
lastTransferTime: expect.any(BN),
liqUnstakeSolAmount: expect.any(BN),
liqUnstakeMsolAmount: expect.any(BN),
liqUnstakeMsolFees: expect.any(BN),
Expand All @@ -49,8 +53,15 @@ describe('Marinade Referral State', () => {
partnerAccount: expect.any(web3.PublicKey),
partnerName: TestWorld.PARTNER_NAME,
pause: expect.any(Boolean),
tokenPartnerAccount: expect.any(web3.PublicKey),
transferDuration: expect.any(Number),
msolTokenPartnerAccount: expect.any(web3.PublicKey),
operationDepositSolFee: expect.any(Number),
operationDepositStakeAccountFee: expect.any(Number),
operationLiquidUnstakeFee: expect.any(Number),
operationDelayedUnstakeFee: expect.any(Number),
accumDepositSolFee: expect.any(BN),
accumDepositStakeAccountFee: expect.any(BN),
accumLiquidUnstakeFee: expect.any(BN),
accumDelayedUnstakeFee: expect.any(BN),
})
})
})
Expand Down
2 changes: 0 additions & 2 deletions test/test-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { MarinadeUtils } from '../src'

export const LAMPORTS_AIRDROP_CAP = MarinadeUtils.solToLamports(2)

export const MARINADE_PROGRAM_REFERRAL_ID = new web3.PublicKey('FqYPYHc3man91xYDCugbGuDdWgkNLp5TvbXPascHW6MR')

export const SDK_USER = web3.Keypair.fromSecretKey(new Uint8Array([
120, 45, 242, 38, 63, 135, 84, 226, 66, 56, 76,
216, 125, 144, 38, 182, 53, 47, 169, 251, 128, 65,
Expand Down

0 comments on commit 832023e

Please sign in to comment.