diff --git a/package-lock.json b/package-lock.json index c15e66e..1665d3a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,18 @@ { "name": "@marinade.finance/marinade-ts-sdk", - "version": "2.0.9", + "version": "2.0.10", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@marinade.finance/marinade-ts-sdk", - "version": "2.0.8", + "version": "2.0.9", "license": "ISC", "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", @@ -1046,6 +1047,22 @@ "node": ">=11" } }, + "node_modules/@project-serum/anchor/node_modules/base-x": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.9.tgz", + "integrity": "sha512-H7JU6iBHTal1gp56aKoaa//YUxEaAOUiydvrV/pILqIHXTtqxSkATOnDA2u+jZ/61sD+L/412+7kzXRtWukhpQ==", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, + "node_modules/@project-serum/anchor/node_modules/bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha1-vhYedsNU9veIrkBx9j806MTwpCo=", + "dependencies": { + "base-x": "^3.0.2" + } + }, "node_modules/@project-serum/borsh": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/@project-serum/borsh/-/borsh-0.2.2.tgz", @@ -1176,6 +1193,14 @@ "node": ">=12.20.0" } }, + "node_modules/@solana/web3.js/node_modules/base-x": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.9.tgz", + "integrity": "sha512-H7JU6iBHTal1gp56aKoaa//YUxEaAOUiydvrV/pILqIHXTtqxSkATOnDA2u+jZ/61sD+L/412+7kzXRtWukhpQ==", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, "node_modules/@solana/web3.js/node_modules/borsh": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.4.0.tgz", @@ -1187,6 +1212,14 @@ "text-encoding-utf-8": "^1.0.2" } }, + "node_modules/@solana/web3.js/node_modules/bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha1-vhYedsNU9veIrkBx9j806MTwpCo=", + "dependencies": { + "base-x": "^3.0.2" + } + }, "node_modules/@tootallnate/once": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz", @@ -2141,12 +2174,9 @@ "dev": true }, "node_modules/base-x": { - "version": "3.0.8", - "resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.8.tgz", - "integrity": "sha512-Rl/1AWP4J/zRrk54hhlxH4drNxPJXYUaKffODVI53/dAsV4t9fBxyxYKAVPU1XBHxYwOWP9h9H0hM2MVw4YfJA==", - "dependencies": { - "safe-buffer": "^5.0.1" - } + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-4.0.0.tgz", + "integrity": "sha512-FuwxlW4H5kh37X/oW59pwTzzTKRzfrrQwhmyspRM7swOEZcHtDZSCt45U6oKgtuFE+WYPblePMVIPR4RZrh/hw==" }, "node_modules/base64-js": { "version": "1.5.1", @@ -2182,6 +2212,22 @@ "text-encoding-utf-8": "^1.0.2" } }, + "node_modules/borsh/node_modules/base-x": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.9.tgz", + "integrity": "sha512-H7JU6iBHTal1gp56aKoaa//YUxEaAOUiydvrV/pILqIHXTtqxSkATOnDA2u+jZ/61sD+L/412+7kzXRtWukhpQ==", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, + "node_modules/borsh/node_modules/bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha1-vhYedsNU9veIrkBx9j806MTwpCo=", + "dependencies": { + "base-x": "^3.0.2" + } + }, "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -2330,11 +2376,11 @@ } }, "node_modules/bs58": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", - "integrity": "sha1-vhYedsNU9veIrkBx9j806MTwpCo=", + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-5.0.0.tgz", + "integrity": "sha512-r+ihvQJvahgYT50JD05dyJNKlmmSlMoOGwn1lCcEzanPglg7TxYjioQUYehQ9mAR/+hOSd2jRc/Z2y5UxBymvQ==", "dependencies": { - "base-x": "^3.0.2" + "base-x": "^4.0.0" } }, "node_modules/bser": { @@ -8497,6 +8543,24 @@ "pako": "^2.0.3", "snake-case": "^3.0.4", "toml": "^3.0.0" + }, + "dependencies": { + "base-x": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.9.tgz", + "integrity": "sha512-H7JU6iBHTal1gp56aKoaa//YUxEaAOUiydvrV/pILqIHXTtqxSkATOnDA2u+jZ/61sD+L/412+7kzXRtWukhpQ==", + "requires": { + "safe-buffer": "^5.0.1" + } + }, + "bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha1-vhYedsNU9veIrkBx9j806MTwpCo=", + "requires": { + "base-x": "^3.0.2" + } + } } }, "@project-serum/borsh": { @@ -8590,6 +8654,14 @@ "tweetnacl": "^1.0.0" }, "dependencies": { + "base-x": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.9.tgz", + "integrity": "sha512-H7JU6iBHTal1gp56aKoaa//YUxEaAOUiydvrV/pILqIHXTtqxSkATOnDA2u+jZ/61sD+L/412+7kzXRtWukhpQ==", + "requires": { + "safe-buffer": "^5.0.1" + } + }, "borsh": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.4.0.tgz", @@ -8600,6 +8672,14 @@ "bs58": "^4.0.0", "text-encoding-utf-8": "^1.0.2" } + }, + "bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha1-vhYedsNU9veIrkBx9j806MTwpCo=", + "requires": { + "base-x": "^3.0.2" + } } } }, @@ -9391,12 +9471,9 @@ "dev": true }, "base-x": { - "version": "3.0.8", - "resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.8.tgz", - "integrity": "sha512-Rl/1AWP4J/zRrk54hhlxH4drNxPJXYUaKffODVI53/dAsV4t9fBxyxYKAVPU1XBHxYwOWP9h9H0hM2MVw4YfJA==", - "requires": { - "safe-buffer": "^5.0.1" - } + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-4.0.0.tgz", + "integrity": "sha512-FuwxlW4H5kh37X/oW59pwTzzTKRzfrrQwhmyspRM7swOEZcHtDZSCt45U6oKgtuFE+WYPblePMVIPR4RZrh/hw==" }, "base64-js": { "version": "1.5.1", @@ -9416,6 +9493,24 @@ "bn.js": "^5.2.0", "bs58": "^4.0.0", "text-encoding-utf-8": "^1.0.2" + }, + "dependencies": { + "base-x": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.9.tgz", + "integrity": "sha512-H7JU6iBHTal1gp56aKoaa//YUxEaAOUiydvrV/pILqIHXTtqxSkATOnDA2u+jZ/61sD+L/412+7kzXRtWukhpQ==", + "requires": { + "safe-buffer": "^5.0.1" + } + }, + "bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha1-vhYedsNU9veIrkBx9j806MTwpCo=", + "requires": { + "base-x": "^3.0.2" + } + } } }, "brace-expansion": { @@ -9552,11 +9647,11 @@ } }, "bs58": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", - "integrity": "sha1-vhYedsNU9veIrkBx9j806MTwpCo=", + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-5.0.0.tgz", + "integrity": "sha512-r+ihvQJvahgYT50JD05dyJNKlmmSlMoOGwn1lCcEzanPglg7TxYjioQUYehQ9mAR/+hOSd2jRc/Z2y5UxBymvQ==", "requires": { - "base-x": "^3.0.2" + "base-x": "^4.0.0" } }, "bser": { diff --git a/package.json b/package.json index 369b1c2..c4637ab 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/src/marinade-state/borsh/marinade-borsh.ts b/src/marinade-state/borsh/marinade-borsh.ts index 87595ea..6edb173 100644 --- a/src/marinade-state/borsh/marinade-borsh.ts +++ b/src/marinade-state/borsh/marinade-borsh.ts @@ -2,6 +2,7 @@ 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([ @@ -9,4 +10,5 @@ export const MARINADE_BORSH_SCHEMA = new Map([ ...validatorRecordBorshSchema, ...stakeRecordBorshSchema, ...stakeStateBorshSchema, + ...ticketAccountBorshSchema, ]) diff --git a/src/marinade-state/borsh/ticket-account.ts b/src/marinade-state/borsh/ticket-account.ts new file mode 100644 index 0000000..2388544 --- /dev/null +++ b/src/marinade-state/borsh/ticket-account.ts @@ -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 diff --git a/src/marinade-state/marinade-state.ts b/src/marinade-state/marinade-state.ts index 6252c99..af6169f 100644 --- a/src/marinade-state/marinade-state.ts +++ b/src/marinade-state/marinade-state.ts @@ -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 diff --git a/src/marinade.ts b/src/marinade.ts index 19f129e..f5a9d16 100644 --- a/src/marinade.ts +++ b/src/marinade.ts @@ -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()) { } @@ -280,4 +281,11 @@ export class Marinade { transaction, } } + + /** + * @todo + */ + async getDelayedUnstakeTickets(beneficiary?: web3.PublicKey): Promise { + return this.marinadeFinanceProgram.getDelayedUnstakeTickets(beneficiary) + } } diff --git a/src/programs/marinade-finance-program.ts b/src/programs/marinade-finance-program.ts index 2d093be..2973b31 100644 --- a/src/programs/marinade-finance-program.ts +++ b/src/programs/marinade-finance-program.ts @@ -1,3 +1,4 @@ +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' @@ -5,6 +6,9 @@ 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( @@ -20,6 +24,45 @@ export class MarinadeFinanceProgram { ) } + async getDelayedUnstakeTickets(beneficiary?: web3.PublicKey): Promise { + 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,