Skip to content

Commit

Permalink
Project import generated by Copybara.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: cd4e94f983d8c7d3596f88ac821a5bd49c5bbf54
  • Loading branch information
Copybara authored and actions-user committed Sep 21, 2023
1 parent 7eeab51 commit 7deabad
Show file tree
Hide file tree
Showing 25 changed files with 667 additions and 135 deletions.
4 changes: 2 additions & 2 deletions 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": "^"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
Expand All @@ -26,4 +26,4 @@
"mocha": "^9.0.3",
"ts-mocha": "^10.0.0"
}
}
}
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
4 changes: 2 additions & 2 deletions rust/switchboard-solana/Cargo.anchor27.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4078,7 +4078,7 @@ checksum = "ab16ced94dbd8a46c82fd81e3ed9a8727dac2977ea869d217bcc4ea1f122e81f"

[[package]]
name = "switchboard-common"
version = "0.8.18"
version = "0.8.20"
dependencies = [
"envy",
"getrandom 0.2.10",
Expand All @@ -4091,7 +4091,7 @@ dependencies = [

[[package]]
name = "switchboard-solana"
version = "0.27.18"
version = "0.27.25"
dependencies = [
"anchor-client",
"anchor-lang",
Expand Down
6 changes: 3 additions & 3 deletions rust/switchboard-solana/Cargo.anchor27.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "switchboard-solana"
version = "0.27.18"
version = "0.27.26"
edition = "2021"
description = "A Rust library to interact with Switchboard accounts."
readme = "README.md"
Expand Down Expand Up @@ -29,11 +29,11 @@ bytemuck = "^1"
superslice = "1"

[target.'cfg(target_os = "solana")'.dependencies]
switchboard-common = { version = "0.8.15", path = "../switchboard-common" }
switchboard-common = { version = "0.8.20", path = "../switchboard-common" }
anchor-lang = { version = "0.27.0" }

[target.'cfg(not(target_os = "solana"))'.dependencies]
switchboard-common = { version = "0.8.15", path = "../switchboard-common", features = [
switchboard-common = { version = "0.8.20", path = "../switchboard-common", features = [
"client",
"solana",
] }
Expand Down
4 changes: 2 additions & 2 deletions rust/switchboard-solana/Cargo.lock

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

Loading

0 comments on commit 7deabad

Please sign in to comment.