Skip to content

Commit

Permalink
Ticket accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
janlegner committed May 12, 2022
1 parent dc0f1ab commit 94e5264
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 27 deletions.
141 changes: 118 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@marinade.finance/marinade-ts-sdk",
"version": "2.0.9",
"version": "2.0.10",
"description": "Marinade SDK for Typescript",
"main": "dist/src/index.js",
"repository": {
Expand Down Expand Up @@ -37,7 +37,8 @@
"dependencies": {
"@project-serum/anchor": "^0.18.2",
"@solana/spl-token": "^0.1.8",
"borsh": "^0.6.0"
"borsh": "^0.6.0",
"bs58": "^5.0.0"
},
"devDependencies": {
"@types/bn.js": "^4.11.6",
Expand Down
2 changes: 2 additions & 0 deletions src/marinade-state/borsh/marinade-borsh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { commonBorshSchema } from './common'
import { validatorRecordBorshSchema } from './validator-record'
import { stakeRecordBorshSchema } from './stake-record'
import { stakeStateBorshSchema } from './stake-state'
import { ticketAccountBorshSchema } from './ticket-account'

// eslint-disable-next-line @typescript-eslint/ban-types
export const MARINADE_BORSH_SCHEMA = new Map<Function, unknown>([
...commonBorshSchema,
...validatorRecordBorshSchema,
...stakeRecordBorshSchema,
...stakeStateBorshSchema,
...ticketAccountBorshSchema,
])
26 changes: 26 additions & 0 deletions src/marinade-state/borsh/ticket-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { web3 } from '@project-serum/anchor'
import { deserializePublicKey } from './common'
import BN from 'bn.js'

export class TicketAccount {
stateAddress!: web3.PublicKey
beneficiary!: web3.PublicKey
lamportsAmount!: BN
createdEpoch!: BN

constructor(args: TicketAccount) {
Object.assign(this, args)
}
}

export const ticketAccountBorshSchema = [
[TicketAccount, {
kind: 'struct',
fields: [
['stateAddress', deserializePublicKey],
['beneficiary', deserializePublicKey],
['lamportsAmount', 'u64'],
['createdEpoch', 'u64'],
],
}],
] as const
4 changes: 2 additions & 2 deletions src/marinade-state/marinade-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { StakeRecord } from './borsh/stake-record'
import { StakeState } from './borsh/stake-state'
import { ValidatorRecord } from './borsh/validator-record'
import { ProgramDerivedAddressSeed, MarinadeStateResponse } from './marinade-state.types'
import { StakeInfo } from "./borsh/stake-info"
import { AccountInfo } from "@solana/web3.js"
import { StakeInfo } from './borsh/stake-info'
import { AccountInfo } from '@solana/web3.js'

export class MarinadeState {
// @todo rework args
Expand Down
8 changes: 8 additions & 0 deletions src/marinade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { MarinadeReferralProgram } from './programs/marinade-referral-program'
import { MarinadeReferralPartnerState } from './marinade-referral-state/marinade-referral-partner-state'
import { MarinadeReferralGlobalState } from './marinade-referral-state/marinade-referral-global-state'
import { assertNotNullAndReturn } from './util/assert'
import { TicketAccount } from './marinade-state/borsh/ticket-account'

export class Marinade {
constructor(public readonly config: MarinadeConfig = new MarinadeConfig()) { }
Expand Down Expand Up @@ -280,4 +281,11 @@ export class Marinade {
transaction,
}
}

/**
* @todo
*/
async getDelayedUnstakeTickets(beneficiary?: web3.PublicKey): Promise<TicketAccount[]> {
return this.marinadeFinanceProgram.getDelayedUnstakeTickets(beneficiary)
}
}
43 changes: 43 additions & 0 deletions src/programs/marinade-finance-program.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import bs58 from 'bs58'
import { BN, Program, web3, Provider, Idl } from '@project-serum/anchor'
import { MarinadeFinanceIdl } from './idl/marinade-finance-idl'
import * as marinadeFinanceIdlSchema from './idl/marinade-finance-idl.json'
import { MarinadeState } from '../marinade-state/marinade-state'
import { STAKE_PROGRAM_ID, SYSTEM_PROGRAM_ID } from '../util'
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
import { SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY } from '@solana/web3.js'
import { MARINADE_BORSH_SCHEMA } from '../marinade-state/borsh'
import { deserializeUnchecked } from 'borsh'
import { TicketAccount } from '../marinade-state/borsh/ticket-account'

export class MarinadeFinanceProgram {
constructor(
Expand All @@ -20,6 +24,45 @@ export class MarinadeFinanceProgram {
)
}

async getDelayedUnstakeTickets(beneficiary?: web3.PublicKey): Promise<TicketAccount[]> {
const discriminator = bs58.encode(Uint8Array.from([0x85, 0x4d, 0x12, 0x62]))

const filters = [
{
dataSize: 88,
},
{
memcmp: {
offset: 0,
bytes: discriminator,
},
},
]

if (beneficiary) {
filters.push({
memcmp: {
offset: 8 + 32,
bytes: beneficiary.toBase58(),
},
})
}

const ticketAccountInfos = await this.anchorProvider.connection.getProgramAccounts(this.programAddress, { filters })

return ticketAccountInfos.map((ticketAccountInfo) => {
const { data } = ticketAccountInfo.account

console.log(bs58.encode(data))

return deserializeUnchecked(
MARINADE_BORSH_SCHEMA,
TicketAccount,
data.slice(8, data.length),
)
})
}

addLiquidityInstructionAccounts = async({ marinadeState, ownerAddress, associatedLPTokenAccountAddress }: {
marinadeState: MarinadeState,
ownerAddress: web3.PublicKey,
Expand Down

0 comments on commit 94e5264

Please sign in to comment.