Skip to content

Commit

Permalink
Merge pull request #21 from sunrise-stake/redefine-certificate-vault
Browse files Browse the repository at this point in the history
Redefine `certificate_vault` as account owning all certificate ATAs
  • Loading branch information
dankelleher committed Oct 25, 2023
2 parents d1c5df1 + 3e3483d commit 1f37e74
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 172 deletions.
89 changes: 44 additions & 45 deletions packages/fund-sender/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { AnchorProvider, Program } from "@coral-xyz/anchor";
import * as anchor from "@coral-xyz/anchor";
import { PublicKey, SystemProgram, Connection } from "@solana/web3.js";
import TOKEN_PROGRAM_ID from "@solana/spl-token";
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
} from "@solana/spl-token";
import BN from "bn.js";
import { FundSender, IDL } from "../../types/fund_sender";

Expand Down Expand Up @@ -41,31 +44,24 @@ export const confirm = (connection: Connection) => async (txSig: string) => {
};

/**
* Returns output yield account address for given state address.
* Returns input account address for given state address.
*
*
* @param stateAddress - Public key of state
* @returns Public Key of output yield account
* @returns Public Key of input account
*
*/
const getOutputYieldAccountForState = (
stateAddress: PublicKey,
destinationSeed: Buffer
): PublicKey => {
const [outputYieldAccount] = PublicKey.findProgramAddressSync(
[
Buffer.from("output_yield_account"),
destinationSeed,
stateAddress.toBuffer(),
],
const getInputAccountForState = (stateAddress: PublicKey): PublicKey => {
const [inputAccount] = PublicKey.findProgramAddressSync(
[Buffer.from("input_account"), stateAddress.toBuffer()],
PROGRAM_ID
);

return outputYieldAccount;
return inputAccount;
};

export interface FundSenderConfig {
destinationSeed: Buffer;
destinationName: string;
updateAuthority: PublicKey;
destinationAccount: PublicKey;
certificateVault: PublicKey;
Expand Down Expand Up @@ -95,7 +91,7 @@ export class FundSenderClient {
const state = await this.program.account.state.fetch(this.stateAddress);

this.config = {
destinationSeed: state.destinationSeed,
destinationName: state.destinationName,
updateAuthority: state.updateAuthority,
destinationAccount: state.destinationAccount,
certificateVault: state.certificateVault,
Expand All @@ -113,25 +109,29 @@ export class FundSenderClient {
*/
public static getStateAddressFromSunriseAddress(
sunriseState: PublicKey,
destinationSeed: Buffer
destinationName: string
): PublicKey {
const [state] = PublicKey.findProgramAddressSync(
[Buffer.from("state"), destinationSeed, sunriseState.toBuffer()],
[
Buffer.from("state"),
Buffer.from(destinationName),
sunriseState.toBuffer(),
],
PROGRAM_ID
);

return state;
}

/**
* Returns output yield account address.
* Returns input account address.
*
*
* @returns Public Key of output yield account
* @returns Public Key of input account
*
*/
public getOutputYieldAccount(destinationSeed: Buffer): PublicKey {
return getOutputYieldAccountForState(this.stateAddress, destinationSeed);
public getInputAccount(): PublicKey {
return getInputAccountForState(this.stateAddress);
}

/**
Expand Down Expand Up @@ -175,33 +175,30 @@ export class FundSenderClient {
public static async register(
sunriseState: PublicKey,
updateAuthority: PublicKey,
destinationSeed: Buffer,
destinationName: string,
destinationAccount: PublicKey,
certificateVault: PublicKey,
spendThreshold: BN
): Promise<InitialisedClient> {
// find state address
const stateAddress = FundSenderClient.getStateAddressFromSunriseAddress(
sunriseState,
destinationSeed
);
const outputYieldAccount = getOutputYieldAccountForState(
stateAddress,
destinationSeed
destinationName
);
const inputAccount = getInputAccountForState(stateAddress);

const client = new FundSenderClient(setUpAnchor(), stateAddress);

// accounts needed to register state
const accounts = {
payer: client.provider.wallet.publicKey,
state: stateAddress,
outputYieldAccount,
inputAccount,
systemProgram: SystemProgram.programId,
};

const args = {
destinationSeed,
destinationName,
updateAuthority,
destinationAccount,
certificateVault,
Expand Down Expand Up @@ -250,7 +247,7 @@ export class FundSenderClient {
};

const args = {
destinationSeed: this.config.destinationSeed,
destinationName: this.config.destinationName,
updateAuthority: this.config.updateAuthority,
destinationAccount,
certificateVault: this.config.certificateVault,
Expand Down Expand Up @@ -290,7 +287,7 @@ export class FundSenderClient {
};

const args = {
destinationSeed: this.config.destinationSeed,
destinationName: this.config.destinationName,
updateAuthority: this.config.updateAuthority,
destinationAccount: this.config.destinationAccount,
certificateVault,
Expand Down Expand Up @@ -330,7 +327,7 @@ export class FundSenderClient {
};

const args = {
destinationSeed: this.config.destinationSeed,
destinationName: this.config.destinationName,
updateAuthority, // only this argument is new, everything else is inferred from the original state account
destinationAccount: this.config.destinationAccount,
certificateVault: this.config.certificateVault,
Expand All @@ -352,7 +349,7 @@ export class FundSenderClient {
}

/**
* Sends all funds from output yield account to destination account.
* Sends all funds from input account to destination account.
*
*
* @returns Fund sender client
Expand All @@ -362,16 +359,14 @@ export class FundSenderClient {
if (!this.config) {
throw new Error("Client not initialized");
}
const outputYieldAccount = this.getOutputYieldAccount(
this.config.destinationSeed
);
const inputAccount = this.getInputAccount();

await this.program.methods
.sendFund()
.accounts({
payer: this.provider.publicKey,
state: this.stateAddress,
outputYieldAccount,
inputAccount,
destinationAccount: this.config.destinationAccount,
systemProgram: SystemProgram.programId,
})
Expand All @@ -382,31 +377,35 @@ export class FundSenderClient {
}

/**
* Sends specified amount of NFTs from output yield account to hold account.
* Sends specified amount of NFTs from input account to hold account.
*
*
* @returns Fund sender client
*
*/
public async storeCertificates(
outputYieldTokenAccount: PublicKey
inputTokenAccount: PublicKey,
certificateVaultAta: PublicKey,
certificateMint: PublicKey
): Promise<FundSenderClient> {
if (!this.config) {
throw new Error("Client not initialized");
}
const outputYieldAccount = this.getOutputYieldAccount(
this.config.destinationSeed
);
const inputAccount = this.getInputAccount();

await this.program.methods
.storeCertificates()
.accounts({
payer: this.provider.publicKey,
state: this.stateAddress,
outputYieldAccount,
outputYieldTokenAccount,
inputAccount,
certificateMint,
inputTokenAccount,
certificateVault: this.config.certificateVault,
certificateVaultAta,
systemProgram: SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
})
.rpc()
.then(confirm(this.provider.connection));
Expand Down
Loading

0 comments on commit 1f37e74

Please sign in to comment.