Skip to content

Commit

Permalink
chore: pull monorepo changes (#147)
Browse files Browse the repository at this point in the history
* Project import generated by Copybara.

GitOrigin-RevId: cd4e94f983d8c7d3596f88ac821a5bd49c5bbf54

* manual fix

---------

Co-authored-by: Copybara <copybara@example.com>
  • Loading branch information
gallynaut and Copybara authored Sep 21, 2023
1 parent 7eeab51 commit 1b777a6
Show file tree
Hide file tree
Showing 26 changed files with 716 additions and 184 deletions.
2 changes: 1 addition & 1 deletion examples/functions/01_basic_oracle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@coral-xyz/anchor": "^0.28.0",
"@solana/spl-token": "^0.3.6",
"@solana/web3.js": "^1.78.0",
"@switchboard-xyz/solana.js": "workspace:^"
"@switchboard-xyz/solana.js": "workspace:*"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
Expand Down
63 changes: 55 additions & 8 deletions examples/functions/01_basic_oracle/switchboard-function/Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ futures = "0.3"
serde = "^1"
serde_json = "^1"
switchboard-utils = "0.8.0"
switchboard-solana = { version = "0.28.19" }
# switchboard-solana = { version = "0.28.19", path = "../../../../rust/switchboard-solana" }
# switchboard-solana = { version = "0.28.19" }
switchboard-solana = { version = "0.28.19", path = "../../../../rust/switchboard-solana" }
# switchboard-utils = { version = "0.8.0", path = "../../../../../../rust/switchboard-utils" }
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,41 @@ pub use switchboard_solana::prelude::*;
pub mod binance;
pub use binance::*;

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

pub use basic_oracle::{
self, OracleData, OracleDataWithTradingSymbol, RefreshPrices, RefreshPricesParams,
SwitchboardDecimal, TradingSymbol, ID as PROGRAM_ID,
};

#[tokio::main(worker_threads = 12)]
async fn main() {
// First, initialize the runner instance with a freshly generated Gramine keypair
let runner: FunctionRunner = FunctionRunner::from_env(None).unwrap();
pub async fn perform(runner: &FunctionRunner) -> Result<()> {

msg!("function runner loaded!");

// Then, write your own Rust logic and build a Vec of instructions.
// Should be under 700 bytes after serialization
let binance = Binance::fetch().await.unwrap();
let binance = Binance::fetch().await?;
let ixs: Vec<Instruction> = binance.to_ixns(&runner);

msg!("sending transaction");

// Finally, emit the signed quote and partially signed transaction to the functionRunner oracle
// The functionRunner oracle will use the last outputted word to stdout as the serialized result. This is what gets executed on-chain.
runner.emit(ixs).await.unwrap();
runner.emit(ixs).await?;
Ok(())
}

#[tokio::main(worker_threads = 12)]
async fn main() -> Result<()> {
// First, initialize the runner instance with a freshly generated Gramine keypair
let runner = FunctionRunner::from_env(None)?;
if runner.assert_mr_enclave().is_err() {
runner.emit_error(199).await?;
}

let res = perform(&runner).await;
if let Some(e) = res.err() {
runner.emit_error(1).await?;
}
Ok(())
}
69 changes: 56 additions & 13 deletions javascript/solana.js/src/accounts/functionAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ import type { RawBuffer } from "@switchboard-xyz/common";
import { BN, parseRawMrEnclave, toUtf8 } from "@switchboard-xyz/common";
import assert from "assert";

const addressLookupProgram = new PublicKey(
"AddressLookupTab1e1111111111111111111111111"
);

export type ContainerRegistryType = "dockerhub" | "ipfs";

export type FunctionAccountInitSeeds = {
Expand Down Expand Up @@ -121,9 +125,15 @@ export interface FunctionSetAuthorityParams {
}

/**
* Parameters for an {@linkcode types.functionVerify} instruction.
* Parameters for setting a {@linkcode types.functionClose} authority
*/
export interface FunctionCloseAccountParams {
authority?: Keypair;
}

/**
* Parameters for an {@linkcode types.functionVerify} instruction.
*/
export interface FunctionVerifySyncParams {
observedTime: anchor.BN;
nextAllowedTimestamp: anchor.BN;
Expand All @@ -145,7 +155,6 @@ export interface FunctionVerifySyncParams {
/**
* Parameters for an {@linkcode types.functionVerify} instruction.
*/

export interface FunctionVerifyParams {
observedTime: anchor.BN;
nextAllowedTimestamp: anchor.BN;
Expand All @@ -163,7 +172,6 @@ export interface FunctionVerifyParams {
/**
* Parameters for an {@linkcode types.functionTrigger} instruction.
*/

export interface FunctionTriggerParams {
authority?: Keypair;
}
Expand All @@ -173,8 +181,6 @@ export type CreateFunctionRequestParams = Omit<
"functionAccount"
> & { user?: Keypair };

/**
/**
* Account type representing a Switchboard Function.
*
Expand All @@ -197,10 +203,6 @@ export class FunctionAccount extends Account<types.FunctionAccountData> {
public static getMetadata = (functionState: types.FunctionAccountData) =>
toUtf8(functionState.metadata);

/**
* Load an existing {@linkcode FunctionAccount} with its current on-chain state
*/

/**
* Get the size of an {@linkcode FunctionAccount} on-chain.
*/
Expand Down Expand Up @@ -304,6 +306,9 @@ export class FunctionAccount extends Account<types.FunctionAccountData> {
return [functionAccount, data];
}

/**
* Load an existing {@linkcode FunctionAccount} with its current on-chain state
*/
public static async load(
program: SwitchboardProgram,
address: PublicKey | string
Expand Down Expand Up @@ -349,10 +354,6 @@ export class FunctionAccount extends Account<types.FunctionAccountData> {
recentSlot
);

const addressLookupProgram = new PublicKey(
"AddressLookupTab1e1111111111111111111111111"
);

const [addressLookupTable] = PublicKey.findProgramAddressSync(
[functionAccount.publicKey.toBuffer(), recentSlot.toBuffer("le", 8)],
addressLookupProgram
Expand Down Expand Up @@ -928,6 +929,48 @@ export class FunctionAccount extends Account<types.FunctionAccountData> {
).then((txn) => this.program.signAndSend(txn, options));
}

public async closeAccountInstruction(
payer: PublicKey,
params: FunctionCloseAccountParams,
options?: TransactionObjectOptions
): Promise<TransactionObject> {
const signers: Keypair[] = [];
if (params.authority) {
signers.push(params.authority);
}

const functionState = await this.loadData();
const wallet = await this.wallet;

const functionCloseIxn = types.functionClose(
this.program,
{ params: {} },
{
function: this.publicKey,
authority: functionState.authority,
addressLookupProgram: addressLookupProgram,
addressLookupTable: functionState.addressLookupTable,
escrowWallet: wallet.publicKey,
solDest: payer,
escrowDest: this.program.mint.getAssociatedAddress(payer),
tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
systemProgram: anchor.web3.SystemProgram.programId,
}
);
return new TransactionObject(payer, [functionCloseIxn], signers, options);
}

public async closeAccount(
params: FunctionCloseAccountParams,
options?: SendTransactionObjectOptions
): Promise<TransactionSignature> {
return await this.closeAccountInstruction(
this.program.walletPubkey,
params,
options
).then((txn) => this.program.signAndSend(txn, options));
}

public verifyInstructionSync(
params: FunctionVerifySyncParams
): TransactionInstruction {
Expand Down
Loading

0 comments on commit 1b777a6

Please sign in to comment.