Skip to content

Commit

Permalink
Project import generated by Copybara.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 8e1fcb690780885a2aa28fb132c00ed984c24f14
  • Loading branch information
Copybara authored and actions-user committed Aug 23, 2023
1 parent 5e4cde7 commit f5bb7bc
Show file tree
Hide file tree
Showing 28 changed files with 287 additions and 134 deletions.
4 changes: 2 additions & 2 deletions examples/feeds/01_feed_client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"@project-serum/borsh": "^0.2.5",
"@solana/spl-token": "^0.3.8",
"@solana/web3.js": "^1.78.3",
"@switchboard-xyz/common": "^2.3.2",
"@switchboard-xyz/common": "^2.3.3",
"@switchboard-xyz/oracle": "latest",
"@switchboard-xyz/solana.js": "^2.5.4",
"@switchboard-xyz/solana.js": "^2.5.0",
"chalk": "^4.1.2",
"dotenv": "^16.0.1",
"yargs": "^17.5.1"
Expand Down
4 changes: 2 additions & 2 deletions examples/feeds/02_spl_native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"dependencies": {
"@coral-xyz/anchor": "^0.28.0",
"@solana/web3.js": "^1.77.3",
"@switchboard-xyz/common": "^2.3.2",
"@switchboard-xyz/common": "^2.3.3",
"@switchboard-xyz/oracle": "latest",
"@switchboard-xyz/solana.js": "^2.5.4"
"@switchboard-xyz/solana.js": "^2.5.0"
},
"devDependencies": {
"@types/chai": "^4.3.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/functions/01_basic_oracle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ cpi = ["no-entrypoint"]
default = []

[dependencies]
switchboard-solana = "0.28.4"
switchboard-solana = "=0.28.6"
# switchboard-solana = { version = "0.28.4", path = "../../../rust/switchboard-solana" }
bytemuck = "^1"
bytemuck = "^1"
8 changes: 4 additions & 4 deletions examples/functions/01_basic_oracle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
},
"dependencies": {
"@coral-xyz/anchor": "^0.28.0",
"@solana/spl-token": "^0.3.8",
"@solana/web3.js": "^1.78.4",
"@switchboard-xyz/solana.js": "^2.5.4"
"@solana/spl-token": "^0.3.6",
"@solana/web3.js": "^1.78.0",
"@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"
}
}
}
4 changes: 2 additions & 2 deletions examples/functions/01_basic_oracle/sgx-function/Cargo.toml
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 = "0.28.4"
switchboard-solana = "=0.28.6"
# switchboard-solana = { version = "0.28.4", path = "../../../../rust/switchboard-solana" }
# switchboard-utils = { version = "0.8.0", path = "../../../../../../rust/switchboard-utils" }
# switchboard-utils = { version = "0.8.0", path = "../../../../../../rust/switchboard-utils" }
17 changes: 9 additions & 8 deletions examples/functions/01_basic_oracle/sgx-function/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ DOCKER_BUILD_COMMAND=DOCKER_BUILDKIT=1 docker buildx build --platform linux/amd6
# Default make task
all: build

docker_build:
docker_build:
${DOCKER_BUILD_COMMAND} --pull -f Dockerfile -t ${DOCKER_IMAGE_NAME}:latest --load ../

docker_publish:
docker_publish:
${DOCKER_BUILD_COMMAND} --pull -f Dockerfile -t ${DOCKER_IMAGE_NAME}:latest --push ../

dev_docker_build:
dev_docker_build:
${DOCKER_BUILD_COMMAND} --pull -f Dockerfile.dev -t ${DOCKER_IMAGE_NAME}:latest --load ../../../../../../

dev_docker_publish:
dev_docker_publish:
${DOCKER_BUILD_COMMAND} --pull -f Dockerfile.dev -t ${DOCKER_IMAGE_NAME}:latest --push ../../../../../../

build: docker_build measurement
Expand All @@ -30,15 +30,16 @@ publish: docker_publish measurement
dev_publish: dev_docker_publish measurement

measurement:
@docker run -d --platform=linux/amd64 --pull always -q --name=my-switchboard-function ${DOCKER_IMAGE_NAME}:latest > /dev/null
@docker run -d --platform=linux/amd64 -q --name=my-switchboard-function ${DOCKER_IMAGE_NAME}:latest > /dev/null
@docker cp my-switchboard-function:/measurement.txt measurement.txt
@echo -n 'MrEnclve: '
@cat measurement.txt
@docker stop my-switchboard-function > /dev/null
@docker rm my-switchboard-function > /dev/null
@echo MrEnclave: $(shell cat ./measurement.txt)

simulate: docker_build
docker run -it --platform=linux/amd64 --entrypoint=/bin/bash ${DOCKER_IMAGE_NAME}:latest /boot.sh --test

# Task to clean up the compiled rust application
clean:
cargo clean
Expand All @@ -48,4 +49,4 @@ clean:
# @docker cp latest-my-switchboard-function:/measurement.txt latest-measurement.txt
# @docker stop latest-my-switchboard-function > /dev/null
# @docker rm latest-my-switchboard-function > /dev/null
# @echo latest MrEnclave: $(shell rm ./latest-measurement.txt)
# @echo latest MrEnclave: $(shell rm ./latest-measurement.txt)
4 changes: 2 additions & 2 deletions examples/functions/02_liquidity_oracle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"@coral-xyz/anchor": "^0.28.0",
"@solana/spl-token": "^0.3.8",
"@solana/web3.js": "^1.78.3",
"@switchboard-xyz/common": "^2.3.2",
"@switchboard-xyz/solana.js": "^2.5.4"
"@switchboard-xyz/common": "^2.3.3",
"@switchboard-xyz/solana.js": "^2.5.0"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/functions/03_candles_oracle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"@coral-xyz/anchor": "^0.28.0",
"@solana/spl-token": "^0.3.8",
"@solana/web3.js": "^1.78.3",
"@switchboard-xyz/common": "^2.3.2",
"@switchboard-xyz/solana.js": "^2.5.4"
"@switchboard-xyz/common": "^2.3.3",
"@switchboard-xyz/solana.js": "^2.5.0"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/functions/04_randomness_callback/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"@coral-xyz/anchor": "^0.27.0",
"@solana/spl-token": "^0.3.8",
"@solana/web3.js": "^1.78.3",
"@switchboard-xyz/common": "^2.3.2",
"@switchboard-xyz/solana.js": "^2.5.4",
"@switchboard-xyz/common": "^2.3.3",
"@switchboard-xyz/solana.js": "^2.5.0",
"dotenv": "^16.3.1"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions examples/functions/05_raffle_program/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
},
"dependencies": {
"@coral-xyz/anchor": "^0.28.0",
"@switchboard-xyz/common": "^2.3.2",
"@switchboard-xyz/solana.js": "^2.5.4"
"@switchboard-xyz/common": "^2.3.3",
"@switchboard-xyz/solana.js": "^2.5.0"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/vrf/01_vrf_client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"@project-serum/borsh": "^0.2.5",
"@solana/spl-token": "^0.3.6",
"@solana/web3.js": "^1.77.3",
"@switchboard-xyz/common": "^2.3.2",
"@switchboard-xyz/common": "^2.3.3",
"@switchboard-xyz/oracle": "latest",
"@switchboard-xyz/solana.js": "^2.5.4",
"@switchboard-xyz/solana.js": "^2.5.0",
"chalk": "^4.1.2",
"dotenv": "^16.0.1",
"yargs": "^17.5.1"
Expand Down
5 changes: 5 additions & 0 deletions javascript/solana.js/idl/attestation-devnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"version": "0.1.0",
"name": "switchboard_attestation_program",
"instructions": [
{
"name": "viewVersion",
"accounts": [],
"args": []
},
{
"name": "stateInit",
"accounts": [
Expand Down
5 changes: 5 additions & 0 deletions javascript/solana.js/idl/devnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"version": "0.1.0",
"name": "switchboard_v2",
"instructions": [
{
"name": "viewVersion",
"accounts": [],
"args": []
},
{
"name": "aggregatorClose",
"accounts": [
Expand Down
4 changes: 2 additions & 2 deletions javascript/solana.js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@switchboard-xyz/solana.js",
"version": "2.5.4",
"version": "2.5.6",
"author": "",
"license": "MIT",
"description": "A Typescript client to interact with Switchboard on Solana.",
Expand Down Expand Up @@ -29,7 +29,7 @@
"@coral-xyz/borsh": "^0.28.0",
"@solana/spl-token": "^0.3.8",
"@solana/web3.js": "^1.78.3",
"@switchboard-xyz/common": "^2.3.2",
"@switchboard-xyz/common": "^2.3.3",
"cron-validator": "^1.3.1",
"dotenv": "^16.3.1",
"lodash": "^4.17.21"
Expand Down
3 changes: 3 additions & 0 deletions javascript/solana.js/scripts/generate-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ const ignoreFiles = [
`${v2GeneratedPath}/types/Lanes.ts`, // anchor-client-gen struggles with dual exports
`${v2GeneratedPath}/types/index.ts`, // TODO: Need a better way to handle this. anchor-client-gen adds multiple, broken exports (for VRF builder)
`${v2GeneratedPath}/errors/index.ts`, // need to revert the program ID check,
`${v2GeneratedPath}/instructions/viewVersion.ts`,
`${attestationGeneratedPath}/types/VerificationStatus.ts`,
`${attestationGeneratedPath}/errors/index.ts`,
`${attestationGeneratedPath}/types/SwitchboardAttestationPermission.ts`,
`${attestationGeneratedPath}/instructions/functionDeactivateLookup.ts`,
`${attestationGeneratedPath}/instructions/accountCloseOverride.ts`,
`${attestationGeneratedPath}/instructions/viewVersion.ts`,
`${attestationGeneratedPath}/instructions/index.ts`, // make sure to disable this if adding more attestation program instructions. used to avoid name conflict on viewVersion ixn
// `${v2GeneratedPath}/types/VerificationStatus.ts`,
];

Expand Down
62 changes: 61 additions & 1 deletion javascript/solana.js/src/SwitchboardProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ProgramStateAccount,
QueueAccount,
} from "./accounts/index.js";
import { viewVersion as viewAttestationVersion } from "./generated/attestation-program/instructions/viewVersion.js";
import {
AggregatorAccountData,
BufferRelayerAccountData,
Expand All @@ -24,6 +25,7 @@ import {
SlidingResultAccountData,
VrfAccountData,
} from "./generated/index.js";
import { viewVersion as viewSbVersion } from "./generated/oracle-program/instructions/viewVersion.js";
import {
DEVNET_GENESIS_HASH,
MAINNET_GENESIS_HASH,
Expand All @@ -46,6 +48,7 @@ import type { LoadedJobDefinition } from "./types.js";
import type { AccountNamespace, Idl, Wallet } from "@coral-xyz/anchor";
import {
ACCOUNT_DISCRIMINATOR_SIZE,
AnchorError,
AnchorProvider,
BorshAccountsCoder,
Program,
Expand All @@ -60,9 +63,13 @@ import type {
SendOptions,
Transaction,
TransactionSignature,
} from "@solana/web3.js";
import {
Keypair,
PublicKey,
TransactionMessage,
VersionedTransaction,
} from "@solana/web3.js";
import { Keypair, PublicKey } from "@solana/web3.js";
import { OracleJob } from "@switchboard-xyz/common";

export type SendTransactionOptions = (ConfirmOptions | SendOptions) & {
Expand Down Expand Up @@ -429,6 +436,53 @@ export class SwitchboardProgram {
return program;
};

public async getGitVersion(): Promise<string> {
const messageV0 = new TransactionMessage({
payerKey: this.walletPubkey,
instructions: [
await this._program.methods.viewVersion().accounts({}).instruction(),
],
recentBlockhash: (await this.connection.getLatestBlockhash()).blockhash,
}).compileToLegacyMessage();
const simulationResult = await this.connection.simulateTransaction(
new VersionedTransaction(messageV0),
{ sigVerify: false }
);
const logs = (simulationResult.value?.logs ?? []).join("\n");
const version = extractVersion(logs);
if (version) {
return version;
}
throw new Error(
`Failed to yield the git version in the view_version simulation result`
);
}

public async getAttestationGitVersion(): Promise<string> {
const messageV0 = new TransactionMessage({
payerKey: this.walletPubkey,
instructions: [
await this._attestationProgram.methods
.viewVersion()
.accounts({})
.instruction(),
],
recentBlockhash: (await this.connection.getLatestBlockhash()).blockhash,
}).compileToLegacyMessage();
const simulationResult = await this.connection.simulateTransaction(
new VersionedTransaction(messageV0),
{ sigVerify: false }
);
const logs = (simulationResult.value?.logs ?? []).join("\n");
const version = extractVersion(logs);
if (version) {
return version;
}
throw new Error(
`Failed to yield the git version in the view_version simulation result`
);
}

/**
* Retrieves the Switchboard V2 Program ID for the currently connected cluster.
* @return The PublicKey of the Switchboard V2 Program ID.
Expand Down Expand Up @@ -1040,3 +1094,9 @@ interface AccountInfoResponse {
pubkey: PublicKey;
account: AccountInfo<Buffer>;
}

function extractVersion(input: string): string | null {
const regex = /VERSION: (\S+)/;
const match = input.match(regex);
return match ? match[1] : null;
}
16 changes: 13 additions & 3 deletions javascript/solana.js/src/TransactionObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export interface ITransactionObject extends Required<TransactionObjectOptions> {

export interface TransactionObjectOptions {
enableDurableNonce?: boolean;
/** The number of lamports to affix to the transaction. */
computeUnitPrice?: number;
/** The number of compute units for the transaction. */
computeUnitLimit?: number;
}

Expand Down Expand Up @@ -127,7 +129,8 @@ export class TransactionObject implements ITransactionObject {
}

const priorityTxn = TransactionObject.getComputeUnitPriceIxn(
options?.computeUnitPrice
options?.computeUnitPrice,
options?.computeUnitLimit
);
if (
priorityTxn !== undefined &&
Expand Down Expand Up @@ -178,11 +181,18 @@ export class TransactionObject implements ITransactionObject {
}

static getComputeUnitPriceIxn(
computeUnitPrice?: number
computeUnitPrice?: number, // lamports
_computeUnitLimit?: number
): TransactionInstruction | undefined {
// calculatePriorityFee = 100 Lamports
// computeUnitLimit = 250,000 CUs
// lamports / CU = 100 / 250,000 = 0.0004 Lamports / CUs
// microLamports = 0.0004 * 10e6 = 4000 microLamports / CU
if (computeUnitPrice && computeUnitPrice > 0) {
return ComputeBudgetProgram.setComputeUnitPrice({
microLamports: computeUnitPrice,
microLamports: Math.round(
(computeUnitPrice / (_computeUnitLimit ?? 250000)) * 1000000
),
});
}

Expand Down
Loading

0 comments on commit f5bb7bc

Please sign in to comment.