Web3.js
Use generated Doppler SDKs with @solana/web3.js.
The web3.js client is generated for your payload schema. It is for applications built around @solana/web3.js.
Generate the SDK
Doppler does not publish this client to npm. Generate it with the generate command after initializing your payload schema:
npx @blueshift-gg/doppler-cli generate ./payload.ts \
--web3js-sdk ./generated/sdk/web3jsThe generator writes a sibling common/ package and a web3js/ package. The web3js package depends on common through file:../common.
Link both generated directories into your application workspace, then import from the generated package name. For a config named price-feed, the package is price-feed-web3js.
Your application must provide @solana/web3.js as a dependency. The generated SDK lists it as a peer dependency.
Note
Requires v3 of @solana/web3.js.
Create a Client
import { ADMIN, Doppler, PROGRAM_ID, priceFeedSerializer } from "price-feed-web3js";
import { Address, Connection, Keypair } from "@solana/web3.js";
const connection = new Connection("https://api.mainnet-beta.solana.com");
const signer = await Keypair.fromSecretKey(secretKeyBytes);
const client = new Doppler(connection, signer, {
programId: new Address(PROGRAM_ID),
admin: new Address(ADMIN),
});Fetch an Oracle
const oracleAddress = new PublicKey("...");
const oracle = await client.fetchOracle(oracleAddress, priceFeedSerializer);
if (!oracle) {
throw new Error("Oracle account not found");
}
console.log(oracle.sequence);
console.log(oracle.payload.price);Update an Oracle
const signature = await client.updateOracle(
oracleAddress,
{
sequence: BigInt(Date.now()),
payload: {
price: 42_000_000n,
},
},
priceFeedSerializer,
1_000n,
);
console.log(signature);For custom transaction composition, use the builder:
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
const transaction = await client
.createTransactionBuilder()
.addOracleUpdate(
oracleAddress,
{
sequence: 123n,
payload: { price: 42_000_000n },
},
priceFeedSerializer,
)
.withUnitPrice(1_000n)
.build(blockhash, lastValidBlockHeight);