From ec33256035b2397e8ae0fabe4070885326f3c6d7 Mon Sep 17 00:00:00 2001 From: Ondra Chaloupka Date: Thu, 22 Sep 2022 11:59:33 +0200 Subject: [PATCH 1/2] Call adjustments to cache the referrral state call --- src/marinade.ts | 94 ++++++----------------- src/programs/marinade-referral-program.ts | 26 ++++--- 2 files changed, 42 insertions(+), 78 deletions(-) diff --git a/src/marinade.ts b/src/marinade.ts index e341a5b..1f80ef3 100644 --- a/src/marinade.ts +++ b/src/marinade.ts @@ -35,6 +35,7 @@ export class Marinade { this.config.marinadeReferralProgramId, this.provider, this.config.referralCode, + this, ) private provideReferralOrMainProgram(): MarinadeFinanceProgram | MarinadeReferralProgram { @@ -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) @@ -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) @@ -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) diff --git a/src/programs/marinade-referral-program.ts b/src/programs/marinade-referral-program.ts index e93b66b..3807040 100644 --- a/src/programs/marinade-referral-program.ts +++ b/src/programs/marinade-referral-program.ts @@ -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 { @@ -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 => ({ marinadeFinanceProgram: marinadeState.marinadeFinanceProgramId, state: marinadeState.marinadeStateAddress, @@ -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 }: { @@ -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 => ({ reservePda: await marinadeState.reserveAddress(), marinadeFinanceProgram: marinadeState.marinadeFinanceProgramId, @@ -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 }: { @@ -100,7 +103,6 @@ export class MarinadeReferralProgram { stakeAccountAddress, authorizedWithdrawerAddress, associatedMSolTokenAccountAddress, - msolTokenPartnerAccount, }: { marinadeState: MarinadeState, duplicationFlag: web3.PublicKey, @@ -108,7 +110,6 @@ export class MarinadeReferralProgram { stakeAccountAddress: web3.PublicKey, authorizedWithdrawerAddress: web3.PublicKey, associatedMSolTokenAccountAddress: web3.PublicKey, - msolTokenPartnerAccount: web3.PublicKey, }): Promise => ({ duplicationFlag, stakeAuthority: authorizedWithdrawerAddress, @@ -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 }: { @@ -143,4 +144,11 @@ export class MarinadeReferralProgram { validatorIndex, accounts: await this.depositStakeAccountInstructionAccounts(accountsArgs), }) + + async getReferralStateData(): Promise { + if (!this.referralStateData) { + this.referralStateData = (await this.marinade.getReferralPartnerState()).state + } + return this.referralStateData + } } From b538c57fef479fa9a4fc742c9f5d67ae8dccd4cf Mon Sep 17 00:00:00 2001 From: Ondra Chaloupka Date: Thu, 22 Sep 2022 18:33:32 +0200 Subject: [PATCH 2/2] Integration tests + code fixes in progress --- src/config/marinade-config.ts | 2 +- .../marinade-referral-state.types.ts | 7 ++++-- test/marinade-referral-state.spec.ts | 25 +++++++++++++------ test/test-world.ts | 2 -- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/config/marinade-config.ts b/src/config/marinade-config.ts index cfd1c87..fc79096 100644 --- a/src/config/marinade-config.ts +++ b/src/config/marinade-config.ts @@ -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') diff --git a/src/marinade-referral-state/marinade-referral-state.types.ts b/src/marinade-referral-state/marinade-referral-state.types.ts index 107c667..10f1a95 100644 --- a/src/marinade-referral-state/marinade-referral-state.types.ts +++ b/src/marinade-referral-state/marinade-referral-state.types.ts @@ -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 { diff --git a/test/marinade-referral-state.spec.ts b/test/marinade-referral-state.spec.ts index 7594086..e37c9e4 100644 --- a/test/marinade-referral-state.spec.ts +++ b/test/marinade-referral-state.spec.ts @@ -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, @@ -15,8 +15,11 @@ describe('Marinade Referral State', () => { expect(state).toStrictEqual({ 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), }) }) }) @@ -33,13 +36,14 @@ describe('Marinade Referral State', () => { expect(state).toStrictEqual({ 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), @@ -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), }) }) }) diff --git a/test/test-world.ts b/test/test-world.ts index 24f3163..5fc3990 100644 --- a/test/test-world.ts +++ b/test/test-world.ts @@ -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,