Basic AI Oracle contract interaction

import { SimulateCosmWasmClient } from '@terran-one/cw-simulate';
import { AioracleContractClient, AioracleContractTypes, DataSourceState, Service } from '@oraichain/aioracle-contracts-sdk';
import { getContractDir } from '@oraichain/aioracle-contracts-build';

import { assert } from 'console';

const admin = 'admin_aioraclev2';
const client = new SimulateCosmWasmClient({
chainId: 'Oraichain-testnet',
bech32Prefix: 'orai'
});
const SERVICE_DEFAULT = 'price';

export const basicProviderFlow = async () => {
const { contractAddress } = await client.deploy(
admin,
getContractDir(),
{
executors: getExecutors()
} as AioracleContractTypes.InstantiateMsg,
'aioraclev2 label'
);
const aioracleContract = new AioracleContractClient(client, admin, contractAddress);
await addService(aioracleContract);
const result = await aioracleContract.request({ input: undefined, service: SERVICE_DEFAULT, threshold: 1 });
console.log("request result: ", result);
}

const addService = async (aioracle: AioracleContractClient) => {
const serviceData: Service = { oscript_url: "https://raw.githubusercontent.com/oraichain/deno-scripts/bf3fbc3265f9698a1a0a85c5e7724ed91f4e562f/src/pricefeed/emptyOscript.js", tcases: [], dsources: [{ language: "node", parameters: ["BTC", "ETH", "BNB", "XRP", "DOGE", "USDT", "LINK", "UNI", "USDC", "BUSD", "ORAI", "DAI", "SOL", "MATIC", "SUSHI", "DOT", "LUNA", "ICP", "XLM", "ATOM", "AAVE", "THETA", "EOS", "CAKE", "AXS", "ALGO", "MKR", "KSM", "XTZ", "FIL", "AMP", "RUNE", "COMP"], script_url: "https://raw.githubusercontent.com/oraichain/deno-scripts/ea584de4397312b9cc88e518e9e5ae68678e8a8c/src/pricefeed/coinbase.js" } as DataSourceState] };
await aioracle.addService({ serviceName: SERVICE_DEFAULT, service: serviceData });

const service = await aioracle.getService({ serviceName: SERVICE_DEFAULT });
assert(service.service.dsources.length === 1);
assert(service.service.tcases.length === 0);
assert(service.service.oscript_url.length > 0);
}

const getExecutors = (): any[] => {
const executors = [
"orai18hr8jggl3xnrutfujy2jwpeu0l76azprlvgrwt",
"orai14n3tx8s5ftzhlxvq0w5962v60vd82h30rha573"
];
return executors;
};

Basic AI Oracle backend interaction to retrieve service reports

async function collectReports(url: string, contractAddr: string, requestId: number, threshold: number) {
let count = 0;
let reports: any;
do {
try {
reports = await fetch(`${url}/report/reports?contract_addr=${contractAddr}&request_id=${requestId}`).then(data => data.json());
console.log("reports: ", reports)
if (!reports.data || reports.data.data.length < threshold) throw "error";
} catch (error) {
count++;
if (count > 100) break; // break the loop and return the request id.
// sleep for a few seconds then repeat
await new Promise(r => setTimeout(r, 5000));
}

} while (!reports.data || reports.data.data.length < threshold);
return reports.data;
}

Hierarchy

Implements

Constructors

Properties

client: SigningCosmWasmClient
contractAddress: string
sender: string

Methods

  • Parameters

    • __namedParameters: {
          service: Service;
          serviceName: string;
      }
      • service: Service
      • serviceName: string
    • $fee: number | StdFee | "auto" = "auto"
    • Optional $memo: string
    • Optional $funds: Coin[]

    Returns Promise<ExecuteResult>

  • Parameters

    • __namedParameters: {
          serviceName: string;
      }
      • serviceName: string
    • $fee: number | StdFee | "auto" = "auto"
    • Optional $memo: string
    • Optional $funds: Coin[]

    Returns Promise<ExecuteResult>

  • Parameters

    • __namedParameters: {
          executors: string[];
          merkleRoot: string;
          stage: number;
      }
      • executors: string[]
      • merkleRoot: string
      • stage: number
    • $fee: number | StdFee | "auto" = "auto"
    • Optional $memo: string
    • Optional $funds: Coin[]

    Returns Promise<ExecuteResult>

  • Parameters

    • __namedParameters: {
          input?: string;
          service: string;
          threshold: number;
      }
      • Optional input?: string
      • service: string
      • threshold: number
    • $fee: number | StdFee | "auto" = "auto"
    • Optional $memo: string
    • Optional $funds: Coin[]

    Returns Promise<ExecuteResult>

  • Parameters

    • __namedParameters: {
          dsources?: DataSourceState[];
          newOwner?: string;
          oscriptUrl?: string;
          serviceName: string;
          tcases?: TestCaseState[];
      }
      • Optional dsources?: DataSourceState[]
      • Optional newOwner?: string
      • Optional oscriptUrl?: string
      • serviceName: string
      • Optional tcases?: TestCaseState[]
    • $fee: number | StdFee | "auto" = "auto"
    • Optional $memo: string
    • Optional $funds: Coin[]

    Returns Promise<ExecuteResult>

©2020 - 2023 Oraichain Foundation