Skip to main content

Introduction

Oracles are devices or entities that connect a blockchain network to the outside world. They enable smart contracts to access all kinds of off-chain information by entering every data input through an external transaction.

Umbrella

Available Price feeds

https://umbrella-network.readme.io/docs/price-feeds

Deployed Smart contracts

https://umbrella-network.readme.io/docs/umb-token-contracts

Example: fetching a price feed

import {Client, Args, IReadData} from "@massalabs/massa-web3";
import keccak256 from "@indeliblelabs/keccak256";
import {getClient} from "./utils";

import {
Args,
IDeserializedResult,
ISerializable,
} from '@massalabs/massa-web3';

export class PriceData implements ISerializable<PriceData> {
private data: number = 0; // u8
private heartbeat: number = 0; // u32
private timestamp: number = 0; // u32
private price: bigint = BigInt(0); // u128

constructor(data: number = 0, heartbeat: number = 0, timestamp: number = 0, price: bigint = BigInt(0)) {
this.data = data;
this.heartbeat = heartbeat;
this.timestamp = timestamp;
this.price = price;
}

serialize(): Uint8Array {
let args = new Args()
.addU8(this.data)
.addU32(this.heartbeat)
.addU32(this.timestamp)
.addU128(BigInt(this.price));
return new Uint8Array(args.serialize());
}
deserialize(data: Uint8Array, offset: number): IDeserializedResult<PriceData> {
const args = new Args(data, offset);
this.data = parseInt(args.nextU8().toString());
this.heartbeat = args.nextU32();
this.timestamp = args.nextU32();
this.price = BigInt(parseInt(args.nextU128().toString()));
return { instance: this, offset: args.getOffset() };
}
}

let getPriceDataArgs = new Args();
let pair_ = keccak256("MS-USD");
getPriceDataArgs.addUint8Array(new Uint8Array(pair_));
const scAddr: string = "AS12bVikBj3xFP3ouP71hpWbzLRDTxq2RaY2ssQWzoSZJUdK3aRm";

const {client, account} = await getClient();

let readData: IReadData = {
maxGas: BigInt(40_000_000),
targetAddress: scAddr,
targetFunction: "getPriceData",
parameter: getPriceDataArgs,
}
const resp = await client.smartContracts().readSmartContract(readData);
const priceData = new Args(resp.returnValue).nextSerializable(PriceData);
console.log("priceData:", priceData);