|
| 1 | +import "dotenv/config"; |
| 2 | +import { createPublicClient, createWalletClient, http } from "viem"; |
| 3 | +import { privateKeyToAccount } from "viem/accounts"; |
| 4 | +import { baseSepolia } from "viem/chains"; |
| 5 | +import { Oracle } from "dria-oracle-sdk"; |
| 6 | + |
| 7 | +async function main() { |
| 8 | + const SECRET_KEY = process.env.SECRET_KEY; |
| 9 | + const RPC_URL = process.env.RPC_URL ?? "https://base-sepolia-rpc.publicnode.com"; |
| 10 | + const COORDINATOR_ADDRESS = process.env.COORDINATOR_ADDRESS ?? "0x1deaca041f094ec67baa4fb36d333cb652e6b7a7"; |
| 11 | + |
| 12 | + // create oracle instance |
| 13 | + const oracle = new Oracle({ |
| 14 | + public: createPublicClient({ |
| 15 | + chain: baseSepolia, |
| 16 | + transport: http(RPC_URL), |
| 17 | + }), |
| 18 | + wallet: createWalletClient({ |
| 19 | + account: privateKeyToAccount(SECRET_KEY), |
| 20 | + chain: baseSepolia, |
| 21 | + transport: http(RPC_URL), |
| 22 | + }), |
| 23 | + }); |
| 24 | + |
| 25 | + await oracle.init(COORDINATOR_ADDRESS); |
| 26 | + |
| 27 | + // check approval (you only need to do this once) |
| 28 | + // you can ignore if you have gave allowance already |
| 29 | + const allowance = await oracle.allowance(); |
| 30 | + if (allowance === 0n) { |
| 31 | + console.log("Making allowance"); |
| 32 | + const txHash = await oracle.approve(); |
| 33 | + console.log({ txHash }); |
| 34 | + } |
| 35 | + |
| 36 | + // make a request |
| 37 | + const input = "What is the result of 2+2?"; |
| 38 | + const model = "*"; |
| 39 | + const requestTxHash = await oracle.request(input, model, { |
| 40 | + taskParameters: { |
| 41 | + numValidations: 0, |
| 42 | + }, |
| 43 | + }); |
| 44 | + const taskId = await oracle.waitRequest(requestTxHash); |
| 45 | + |
| 46 | + // wait for the result |
| 47 | + await oracle.wait(taskId); |
| 48 | + |
| 49 | + // read best result |
| 50 | + const response = await oracle.read(taskId); |
| 51 | + console.log({ response }); |
| 52 | + |
| 53 | + // read validations |
| 54 | + const validations = await oracle.getValidations(taskId); |
| 55 | + console.log({ validations }); |
| 56 | +} |
| 57 | + |
| 58 | +main().catch(console.error); |
0 commit comments