Skip to content

Commit

Permalink
[WIP] feat: vault instructions (#50)
Browse files Browse the repository at this point in the history
* feat: vault instructions

* feat: vault tests

Co-authored-by: Valentine Panchin <[email protected]>
  • Loading branch information
b2kdaman and Valentine Panchin authored Nov 6, 2021
1 parent 0e41ddc commit bf75e8d
Show file tree
Hide file tree
Showing 16 changed files with 681 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/programs/vault/VaultProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ export enum VaultKey {
ExternalPriceAccountV1 = 2,
}

export enum VaultInstructions {
InitVault,
AddTokenToInactiveVault,
ActivateVault,
CombineVault,
RedeemShares,
WithdrawTokenFromSafetyDepositBox,
MintFractionalShares,
WithdrawSharesFromTreasury,
AddSharesToTreasury,
UpdateExternalPriceAccount,
SetVaultAuthority,
}

export class VaultProgram extends Program {
static readonly PREFIX = 'vault';
static readonly PUBKEY = new PublicKey(config.programs.vault);
Expand Down
6 changes: 5 additions & 1 deletion src/programs/vault/transactions/ActivateVault.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { PublicKey, TransactionCtorFields, TransactionInstruction } from '@solana/web3.js';
import BN from 'bn.js';
import { VaultInstructions } from '..';
import { Transaction } from '../../../Transaction';
import { NumberOfShareArgs } from '../accounts/Vault';
import { VaultProgram } from '../VaultProgram';
Expand All @@ -26,7 +27,10 @@ export class ActivateVault extends Transaction {
numberOfShares,
} = params;

const data = NumberOfShareArgs.serialize({ instruction: 2, numberOfShares });
const data = NumberOfShareArgs.serialize({
instruction: VaultInstructions.ActivateVault,
numberOfShares,
});

this.add(
new TransactionInstruction({
Expand Down
79 changes: 79 additions & 0 deletions src/programs/vault/transactions/AddSharesToTreasury.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { ParamsWithStore } from '@metaplex/types';
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
import {
PublicKey,
SYSVAR_RENT_PUBKEY,
TransactionCtorFields,
TransactionInstruction,
} from '@solana/web3.js';
import BN from 'bn.js';
import { VaultInstructions } from '..';
import { Transaction } from '../../../Transaction';
import { NumberOfShareArgs } from '../accounts/Vault';
import { VaultProgram } from '../VaultProgram';

type AddSharesToTreasuryParams = {
vault: PublicKey;
source: PublicKey;
fractionTreasury: PublicKey;
vaultAuthority: PublicKey;
transferAuthority: PublicKey;
numberOfShares: BN;
};

export class AddSharesToTreasury extends Transaction {
constructor(options: TransactionCtorFields, params: ParamsWithStore<AddSharesToTreasuryParams>) {
super(options);
const { vault, vaultAuthority, source, transferAuthority, fractionTreasury, numberOfShares } =
params;

const data = NumberOfShareArgs.serialize({
instruction: VaultInstructions.AddSharesToTreasury,
numberOfShares,
});

this.add(
new TransactionInstruction({
keys: [
{
pubkey: source,
isSigner: false,
isWritable: true,
},
{
pubkey: fractionTreasury,
isSigner: false,
isWritable: true,
},
{
pubkey: vault,
isSigner: false,
isWritable: false,
},
{
pubkey: transferAuthority,
isSigner: false,
isWritable: false,
},
{
pubkey: vaultAuthority,
isSigner: false,
isWritable: true,
},
{
pubkey: TOKEN_PROGRAM_ID,
isSigner: false,
isWritable: false,
},
{
pubkey: SYSVAR_RENT_PUBKEY,
isSigner: false,
isWritable: false,
},
],
programId: VaultProgram.PUBKEY,
data,
}),
);
}
}
6 changes: 5 additions & 1 deletion src/programs/vault/transactions/AddTokenToInactiveVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import BN from 'bn.js';
import { AmountArgs } from '../accounts/Vault';
import { Transaction } from '../../../Transaction';
import { VaultProgram } from '../VaultProgram';
import { VaultInstructions } from '..';

type AddTokenToInactiveVaultParams = {
vault: PublicKey;
Expand All @@ -36,7 +37,10 @@ export class AddTokenToInactiveVault extends Transaction {
amount,
} = params;

const data = AmountArgs.serialize({ instruction: 1, amount });
const data = AmountArgs.serialize({
instruction: VaultInstructions.AddTokenToInactiveVault,
amount,
});

this.add(
new TransactionInstruction({
Expand Down
3 changes: 2 additions & 1 deletion src/programs/vault/transactions/CombineVault.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Borsh } from '@metaplex/utils';
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { PublicKey, TransactionCtorFields, TransactionInstruction } from '@solana/web3.js';
import { VaultInstructions } from '..';
import { Transaction } from '../../../Transaction';
import { VaultProgram } from '../VaultProgram';

export class CombineVaultArgs extends Borsh.Data {
static readonly SCHEMA = this.struct([['instruction', 'u8']]);

instruction = 3;
instruction = VaultInstructions.CombineVault;
}

type CombineVaultParams = {
Expand Down
3 changes: 2 additions & 1 deletion src/programs/vault/transactions/InitVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
TransactionCtorFields,
TransactionInstruction,
} from '@solana/web3.js';
import { VaultInstructions } from '..';
import { Transaction } from '../../../Transaction';
import { VaultProgram } from '../VaultProgram';

Expand All @@ -15,7 +16,7 @@ export class InitVaultArgs extends Borsh.Data<{ allowFurtherShareCreation: boole
['allowFurtherShareCreation', 'u8'],
]);

instruction = 0;
instruction = VaultInstructions.InitVault;
allowFurtherShareCreation = false;
}

Expand Down
75 changes: 75 additions & 0 deletions src/programs/vault/transactions/MintFractionalShares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { ParamsWithStore } from '@metaplex/types';
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { PublicKey, TransactionCtorFields, TransactionInstruction } from '@solana/web3.js';
import BN from 'bn.js';
import { VaultInstructions } from '..';
import { Transaction } from '../../../Transaction';
import { NumberOfShareArgs } from '../accounts/Vault';
import { VaultProgram } from '../VaultProgram';

type MintFractionalSharesParams = {
vault: PublicKey;
fractionMint: PublicKey;
fractionMintAuthority: PublicKey;
fractionTreasury: PublicKey;
vaultAuthority: PublicKey;
numberOfShares: BN;
};

export class MintFractionalShares extends Transaction {
constructor(options: TransactionCtorFields, params: ParamsWithStore<MintFractionalSharesParams>) {
super(options);
const {
vault,
vaultAuthority,
fractionMint,
fractionTreasury,
fractionMintAuthority,
numberOfShares,
} = params;

const data = NumberOfShareArgs.serialize({
instruction: VaultInstructions.MintFractionalShares,
numberOfShares,
});

this.add(
new TransactionInstruction({
keys: [
{
pubkey: fractionTreasury,
isSigner: false,
isWritable: true,
},
{
pubkey: fractionMint,
isSigner: false,
isWritable: true,
},
{
pubkey: vault,
isSigner: false,
isWritable: false,
},
{
pubkey: fractionMintAuthority,
isSigner: false,
isWritable: false,
},
{
pubkey: vaultAuthority,
isSigner: false,
isWritable: false,
},
{
pubkey: TOKEN_PROGRAM_ID,
isSigner: false,
isWritable: false,
},
],
programId: VaultProgram.PUBKEY,
data,
}),
);
}
}
99 changes: 99 additions & 0 deletions src/programs/vault/transactions/RedeemShares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Borsh } from '@metaplex/utils';
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
import {
PublicKey,
SYSVAR_RENT_PUBKEY,
TransactionCtorFields,
TransactionInstruction,
} from '@solana/web3.js';
import { VaultInstructions } from '..';
import { Transaction } from '../../../Transaction';
import { VaultProgram } from '../VaultProgram';

export class RedeemSharesArgs extends Borsh.Data {
static readonly SCHEMA = this.struct([['instruction', 'u8']]);

instruction = VaultInstructions.RedeemShares;
}

type RedeemSharsParams = {
burnAuthority: PublicKey;
fractionMint: PublicKey;
outstandingSharesAccount: PublicKey;
proceedsAccount: PublicKey;
redeemTreasury: PublicKey;
transferAuthority: PublicKey;
vault: PublicKey;
};

export class RedeemShares extends Transaction {
constructor(options: TransactionCtorFields, params: RedeemSharsParams) {
super(options);
const {
vault,
burnAuthority,
fractionMint,
outstandingSharesAccount,
proceedsAccount,
redeemTreasury,
transferAuthority,
} = params;

const data = RedeemSharesArgs.serialize();

this.add(
new TransactionInstruction({
keys: [
{
pubkey: outstandingSharesAccount,
isSigner: false,
isWritable: true,
},
{
pubkey: proceedsAccount,
isSigner: false,
isWritable: true,
},
{
pubkey: fractionMint,
isSigner: false,
isWritable: true,
},
{
pubkey: redeemTreasury,
isSigner: false,
isWritable: true,
},
{
pubkey: transferAuthority,
isSigner: false,
isWritable: false,
},

{
pubkey: burnAuthority,
isSigner: true,
isWritable: false,
},
{
pubkey: vault,
isSigner: false,
isWritable: false,
},
{
pubkey: TOKEN_PROGRAM_ID,
isSigner: false,
isWritable: false,
},
{
pubkey: SYSVAR_RENT_PUBKEY,
isSigner: false,
isWritable: false,
},
],
programId: VaultProgram.PUBKEY,
data,
}),
);
}
}
3 changes: 2 additions & 1 deletion src/programs/vault/transactions/SetVaultAuthority.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Borsh } from '@metaplex/utils';
import { PublicKey, TransactionCtorFields, TransactionInstruction } from '@solana/web3.js';
import { VaultInstructions } from '..';
import { Transaction } from '../../../Transaction';
import { VaultProgram } from '../VaultProgram';

export class SetVaultAuthorityArgs extends Borsh.Data {
static readonly SCHEMA = this.struct([['instruction', 'u8']]);

instruction = 10;
instruction = VaultInstructions.SetVaultAuthority;
}

type SetVaultAuthorityParams = {
Expand Down
8 changes: 3 additions & 5 deletions src/programs/vault/transactions/UpdateExternalPriceAccount.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ParamsWithStore } from '@metaplex/types';
import { Borsh } from '@metaplex/utils';
import { PublicKey, TransactionCtorFields, TransactionInstruction } from '@solana/web3.js';
import { VaultInstructions } from '..';
import { Transaction } from '../../../Transaction';
import { ExternalPriceAccountData } from '../accounts/ExternalPriceAccount';
import { VaultProgram } from '../VaultProgram';
Expand All @@ -10,13 +11,10 @@ export class UpdateExternalPriceAccountArgs extends Borsh.Data<{
}> {
static readonly SCHEMA = new Map([
...ExternalPriceAccountData.SCHEMA,
...this.struct([
['instruction', 'u8'],
['externalPriceAccount', ExternalPriceAccountData],
]),
...this.struct([['instruction', 'u8']]),
]);

instruction = 9;
instruction = VaultInstructions.UpdateExternalPriceAccount;
externalPriceAccount: ExternalPriceAccountData;
}

Expand Down
Loading

0 comments on commit bf75e8d

Please sign in to comment.