Skip to content

Commit

Permalink
sdk updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gallynaut committed Jul 3, 2023
1 parent d27ca34 commit 5149b1d
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 14 deletions.
55 changes: 55 additions & 0 deletions javascript/solana.js/src/accounts/functionAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,61 @@ export class FunctionAccount extends Account<types.FunctionAccountData> {
).then((txn) => this.program.signAndSend(txn, options));
}

public async resetEscrowInstruction(
payer: PublicKey,
authority?: Keypair,
options?: TransactionObjectOptions
): Promise<TransactionObject> {
this.program.verifyAttestation();

const functionState = await this.loadData();

const defaultWallet = SwitchboardWallet.fromSeed(
this.program,
functionState.attestationQueue,
functionState.authority,
this.publicKey.toBytes()
);

const ixn = types.functionResetEscrow(
this.program,
{ params: {} },
{
function: this.publicKey,
authority: functionState.authority,
attestationQueue: functionState.attestationQueue,
mint: this.program.mint.address,
escrowWallet: functionState.escrowWallet,
defaultWallet: defaultWallet.publicKey,
tokenWallet: defaultWallet.tokenWallet,
payer: payer,
state: this.program.attestationProgramState.publicKey,
tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
associatedTokenProgram: anchor.utils.token.ASSOCIATED_PROGRAM_ID,
systemProgram: anchor.web3.SystemProgram.programId,
}
);

const txn = new TransactionObject(
payer,
[ixn],
authority ? [authority] : [],
options
);
return txn;
}

public async resetEscrow(
authority?: Keypair,
options?: SendTransactionObjectOptions
): Promise<TransactionSignature> {
return await this.resetEscrowInstruction(
this.program.walletPubkey,
authority,
options
).then((txn) => this.program.signAndSend(txn, options));
}

public async fundInstruction(
payer: PublicKey,
params: SwitchboardWalletFundParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export interface FunctionAccountDataFields {
version: Array<number>;
/** The authority of the function which is authorized to make account changes. */
authority: PublicKey;
/** The address_lookup_table of the function used to increase the number of accounts we can fit into a function result. */
addressLookupTable: PublicKey;
/** The address of the AttestationQueueAccountData that will be processing function requests and verifying the function measurements. */
attestationQueue: PublicKey;
/** An incrementer used to rotate through an AttestationQueue's verifiers. */
queueIdx: number;
/** The address_lookup_table of the function used to increase the number of accounts we can fit into a function result. */
addressLookupTable: PublicKey;
/** The cron schedule to run the function on. */
schedule: Array<number>;
/** The unix timestamp when the function was last run. */
Expand Down Expand Up @@ -106,12 +106,12 @@ export interface FunctionAccountDataJSON {
version: Array<number>;
/** The authority of the function which is authorized to make account changes. */
authority: string;
/** The address_lookup_table of the function used to increase the number of accounts we can fit into a function result. */
addressLookupTable: string;
/** The address of the AttestationQueueAccountData that will be processing function requests and verifying the function measurements. */
attestationQueue: string;
/** An incrementer used to rotate through an AttestationQueue's verifiers. */
queueIdx: number;
/** The address_lookup_table of the function used to increase the number of accounts we can fit into a function result. */
addressLookupTable: string;
/** The cron schedule to run the function on. */
schedule: Array<number>;
/** The unix timestamp when the function was last run. */
Expand Down Expand Up @@ -183,12 +183,12 @@ export class FunctionAccountData {
readonly version: Array<number>;
/** The authority of the function which is authorized to make account changes. */
readonly authority: PublicKey;
/** The address_lookup_table of the function used to increase the number of accounts we can fit into a function result. */
readonly addressLookupTable: PublicKey;
/** The address of the AttestationQueueAccountData that will be processing function requests and verifying the function measurements. */
readonly attestationQueue: PublicKey;
/** An incrementer used to rotate through an AttestationQueue's verifiers. */
readonly queueIdx: number;
/** The address_lookup_table of the function used to increase the number of accounts we can fit into a function result. */
readonly addressLookupTable: PublicKey;
/** The cron schedule to run the function on. */
readonly schedule: Array<number>;
/** The unix timestamp when the function was last run. */
Expand Down Expand Up @@ -252,9 +252,9 @@ export class FunctionAccountData {
borsh.array(borsh.u8(), 64, "container"),
borsh.array(borsh.u8(), 32, "version"),
borsh.publicKey("authority"),
borsh.publicKey("addressLookupTable"),
borsh.publicKey("attestationQueue"),
borsh.u32("queueIdx"),
borsh.publicKey("addressLookupTable"),
borsh.array(borsh.u8(), 64, "schedule"),
borsh.i64("lastExecutionTimestamp"),
borsh.i64("nextAllowedTimestamp"),
Expand All @@ -272,7 +272,7 @@ export class FunctionAccountData {
borsh.publicKey("escrowTokenWallet"),
borsh.publicKey("rewardEscrowWallet"),
borsh.publicKey("rewardEscrowTokenWallet"),
borsh.array(borsh.u8(), 959, "ebuf"),
borsh.array(borsh.u8(), 879, "ebuf"),
]);

constructor(fields: FunctionAccountDataFields) {
Expand All @@ -288,9 +288,9 @@ export class FunctionAccountData {
this.container = fields.container;
this.version = fields.version;
this.authority = fields.authority;
this.addressLookupTable = fields.addressLookupTable;
this.attestationQueue = fields.attestationQueue;
this.queueIdx = fields.queueIdx;
this.addressLookupTable = fields.addressLookupTable;
this.schedule = fields.schedule;
this.lastExecutionTimestamp = fields.lastExecutionTimestamp;
this.nextAllowedTimestamp = fields.nextAllowedTimestamp;
Expand Down Expand Up @@ -366,9 +366,9 @@ export class FunctionAccountData {
container: dec.container,
version: dec.version,
authority: dec.authority,
addressLookupTable: dec.addressLookupTable,
attestationQueue: dec.attestationQueue,
queueIdx: dec.queueIdx,
addressLookupTable: dec.addressLookupTable,
schedule: dec.schedule,
lastExecutionTimestamp: dec.lastExecutionTimestamp,
nextAllowedTimestamp: dec.nextAllowedTimestamp,
Expand Down Expand Up @@ -405,9 +405,9 @@ export class FunctionAccountData {
container: this.container,
version: this.version,
authority: this.authority.toString(),
addressLookupTable: this.addressLookupTable.toString(),
attestationQueue: this.attestationQueue.toString(),
queueIdx: this.queueIdx,
addressLookupTable: this.addressLookupTable.toString(),
schedule: this.schedule,
lastExecutionTimestamp: this.lastExecutionTimestamp.toString(),
nextAllowedTimestamp: this.nextAllowedTimestamp.toString(),
Expand Down Expand Up @@ -444,9 +444,9 @@ export class FunctionAccountData {
container: obj.container,
version: obj.version,
authority: new PublicKey(obj.authority),
addressLookupTable: new PublicKey(obj.addressLookupTable),
attestationQueue: new PublicKey(obj.attestationQueue),
queueIdx: obj.queueIdx,
addressLookupTable: new PublicKey(obj.addressLookupTable),
schedule: obj.schedule,
lastExecutionTimestamp: new BN(obj.lastExecutionTimestamp),
nextAllowedTimestamp: new BN(obj.nextAllowedTimestamp),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { SwitchboardProgram } from "../../../SwitchboardProgram.js";
import * as types from "../types/index.js"; // eslint-disable-line @typescript-eslint/no-unused-vars

import * as borsh from "@coral-xyz/borsh"; // eslint-disable-line @typescript-eslint/no-unused-vars
import {
AccountMeta,
PublicKey,
TransactionInstruction,
} from "@solana/web3.js"; // eslint-disable-line @typescript-eslint/no-unused-vars
import { BN } from "@switchboard-xyz/common"; // eslint-disable-line @typescript-eslint/no-unused-vars

export interface FunctionResetEscrowArgs {
params: types.FunctionResetEscrowParamsFields;
}

export interface FunctionResetEscrowAccounts {
function: PublicKey;
authority: PublicKey;
attestationQueue: PublicKey;
mint: PublicKey;
escrowWallet: PublicKey;
defaultWallet: PublicKey;
tokenWallet: PublicKey;
payer: PublicKey;
state: PublicKey;
tokenProgram: PublicKey;
associatedTokenProgram: PublicKey;
systemProgram: PublicKey;
}

export const layout = borsh.struct([
types.FunctionResetEscrowParams.layout("params"),
]);

export function functionResetEscrow(
program: SwitchboardProgram,
args: FunctionResetEscrowArgs,
accounts: FunctionResetEscrowAccounts
) {
const keys: Array<AccountMeta> = [
{ pubkey: accounts.function, isSigner: false, isWritable: true },
{ pubkey: accounts.authority, isSigner: true, isWritable: false },
{ pubkey: accounts.attestationQueue, isSigner: false, isWritable: false },
{ pubkey: accounts.mint, isSigner: false, isWritable: false },
{ pubkey: accounts.escrowWallet, isSigner: false, isWritable: true },
{ pubkey: accounts.defaultWallet, isSigner: false, isWritable: true },
{ pubkey: accounts.tokenWallet, isSigner: false, isWritable: true },
{ pubkey: accounts.payer, isSigner: true, isWritable: true },
{ pubkey: accounts.state, isSigner: false, isWritable: false },
{ pubkey: accounts.tokenProgram, isSigner: false, isWritable: false },
{
pubkey: accounts.associatedTokenProgram,
isSigner: false,
isWritable: false,
},
{ pubkey: accounts.systemProgram, isSigner: false, isWritable: false },
];
const identifier = Buffer.from([18, 242, 138, 38, 112, 252, 39, 228]);
const buffer = Buffer.alloc(1000);
const len = layout.encode(
{
params: types.FunctionResetEscrowParams.toEncodable(args.params),
},
buffer
);
const data = Buffer.concat([identifier, buffer]).slice(0, 8 + len);
const ix = new TransactionInstruction({
keys,
programId: program.attestationProgramId,
data,
});
return ix;
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export type {
FunctionRequestVerifyArgs,
} from "./functionRequestVerify.js";
export { functionRequestVerify } from "./functionRequestVerify.js";
export type {
FunctionResetEscrowAccounts,
FunctionResetEscrowArgs,
} from "./functionResetEscrow.js";
export { functionResetEscrow } from "./functionResetEscrow.js";
export type {
FunctionSetAuthorityAccounts,
FunctionSetAuthorityArgs,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { SwitchboardProgram } from "../../../SwitchboardProgram.js";
import * as types from "../types/index.js"; // eslint-disable-line @typescript-eslint/no-unused-vars

import * as borsh from "@coral-xyz/borsh";
import { PublicKey } from "@solana/web3.js"; // eslint-disable-line @typescript-eslint/no-unused-vars
import { BN } from "@switchboard-xyz/common"; // eslint-disable-line @typescript-eslint/no-unused-vars

export interface FunctionResetEscrowParamsFields {}

export interface FunctionResetEscrowParamsJSON {}

export class FunctionResetEscrowParams {
constructor(fields: FunctionResetEscrowParamsFields) {}

static layout(property?: string) {
return borsh.struct([], property);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
static fromDecoded(obj: any) {
return new FunctionResetEscrowParams({});
}

static toEncodable(fields: FunctionResetEscrowParamsFields) {
return {};
}

toJSON(): FunctionResetEscrowParamsJSON {
return {};
}

static fromJSON(
obj: FunctionResetEscrowParamsJSON
): FunctionResetEscrowParams {
return new FunctionResetEscrowParams({});
}

toEncodable() {
return FunctionResetEscrowParams.toEncodable(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export type {
FunctionRequestVerifyParamsJSON,
} from "./FunctionRequestVerifyParams.js";
export { FunctionRequestVerifyParams } from "./FunctionRequestVerifyParams.js";
export type {
FunctionResetEscrowParamsFields,
FunctionResetEscrowParamsJSON,
} from "./FunctionResetEscrowParams.js";
export { FunctionResetEscrowParams } from "./FunctionResetEscrowParams.js";
export type {
FunctionSetAuthorityParamsFields,
FunctionSetAuthorityParamsJSON,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub struct FunctionAccountData {
pub reward_escrow_token_wallet: Pubkey,

/// Reserved.
pub _ebuf: [u8; 895],
pub _ebuf: [u8; 879],
}

unsafe impl Pod for FunctionAccountData {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl From<u8> for RequestStatus {
}
}
}

#[derive(Copy, Clone, AnchorSerialize, AnchorDeserialize)]
pub struct FunctionRequestTriggerRound {
/// The status of the request.
Expand Down Expand Up @@ -147,7 +148,7 @@ impl anchor_lang::AccountDeserialize for FunctionRequestAccountData {
return Err(anchor_lang::error::ErrorCode::AccountDiscriminatorNotFound.into());
}
let given_disc = &buf[..8];
if &FunctionRequestAccountData::discriminator() != given_disc {
if FunctionRequestAccountData::discriminator() != given_disc {
return Err(
anchor_lang::error::Error::from(anchor_lang::error::AnchorError {
error_name: anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch.name(),
Expand Down

0 comments on commit 5149b1d

Please sign in to comment.