Skip to content

Commit

Permalink
Merge pull request #19 from alephium/vrf-oracle-example
Browse files Browse the repository at this point in the history
VRF oracle example
  • Loading branch information
polarker authored Aug 8, 2024
2 parents 96cf456 + 3281d26 commit ddd1869
Show file tree
Hide file tree
Showing 22 changed files with 389 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: |
pushd docker
docker-compose up -d
docker compose up -d
popd
SECONDS=0
sleep 5
Expand Down
33 changes: 27 additions & 6 deletions call-oracle/.project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"fullNodeVersion": "v3.3.1",
"fullNodeVersion": "v3.3.0",
"compilerOptionsUsed": {
"ignoreUnusedConstantsWarnings": false,
"ignoreUnusedVariablesWarnings": false,
Expand All @@ -9,26 +9,47 @@
"ignoreCheckExternalCallerWarnings": false
},
"infos": {
"IOracle": {
"DIAOracleValue": {
"sourceFile": "price_fetcher.ral",
"sourceCodeHash": "2d69637edba7d73038d6c408b7a2be3f72554193c6d915f532c1822c1e00cea2",
"sourceCodeHash": "0b4cfa86cb15cee0dbfb5d7f31800b82025f6b44aea7439558d6912f2bb84bc3",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
},
"OracleValue": {
"DIARandomValue": {
"sourceFile": "random_value_fetcher.ral",
"sourceCodeHash": "0560deb6df4c1d36d9b323c615a490d20aa63549cb7929901454ac798fd7c3f1",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
},
"IDIAOracle": {
"sourceFile": "price_fetcher.ral",
"sourceCodeHash": "2d69637edba7d73038d6c408b7a2be3f72554193c6d915f532c1822c1e00cea2",
"sourceCodeHash": "0b4cfa86cb15cee0dbfb5d7f31800b82025f6b44aea7439558d6912f2bb84bc3",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
},
"IDIARandomOracle": {
"sourceFile": "random_value_fetcher.ral",
"sourceCodeHash": "0560deb6df4c1d36d9b323c615a490d20aa63549cb7929901454ac798fd7c3f1",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
},
"PriceFetcher": {
"sourceFile": "price_fetcher.ral",
"sourceCodeHash": "2d69637edba7d73038d6c408b7a2be3f72554193c6d915f532c1822c1e00cea2",
"sourceCodeHash": "0b4cfa86cb15cee0dbfb5d7f31800b82025f6b44aea7439558d6912f2bb84bc3",
"bytecodeDebugPatch": "",
"codeHashDebug": "234c356f92e0e441323a14f31234857df46285aba20dc9423bccba90b0f3714e",
"warnings": []
},
"RandomValueFetcher": {
"sourceFile": "random_value_fetcher.ral",
"sourceCodeHash": "0560deb6df4c1d36d9b323c615a490d20aa63549cb7929901454ac798fd7c3f1",
"bytecodeDebugPatch": "",
"codeHashDebug": "9f3715ba3cf130bd8398a3338315876a13c2ecbae485b4a6a439c7e0f5331564",
"warnings": []
}
}
}
8 changes: 6 additions & 2 deletions call-oracle/alephium.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Configuration } from '@alephium/cli'
import { Address } from '@alephium/web3'

export type Settings = {
oracleAddress: Address
assetOracleAddress: Address
vrfOracleAddress: Address
}

const configuration: Configuration<Settings> = {
Expand All @@ -16,7 +17,10 @@ const configuration: Configuration<Settings> = {
testnet: {
nodeUrl: process.env.NODE_URL as string ?? 'https://node.testnet.alephium.org',
privateKeys: process.env.PRIVATE_KEYS === undefined ? [] : process.env.PRIVATE_KEYS.split(','),
settings: { oracleAddress: '216wgM3Xi5uBFYwwiw2T7iZoCy9vozPJ4XjToW74nQjbV' } // DIA oracle address on testnet
settings: {
assetOracleAddress: '216wgM3Xi5uBFYwwiw2T7iZoCy9vozPJ4XjToW74nQjbV',
vrfOracleAddress: 'x5b8UP9m7XuZfiH5eZB3HKuKDAiWaKoZsBX5rXRYm6By'
}
},

mainnet: {
Expand Down
4 changes: 2 additions & 2 deletions call-oracle/artifacts/PriceFetcher.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v3.3.1",
"version": "v3.3.0",
"name": "PriceFetcher",
"bytecode": "060140910100000a00402ed3948fae9514074254432f5553440d0ece00d4a0e6c231170117001600a10014074554482f5553440d0ece00d4a0e6c231170317021602a1011408555344432f5553440d0ece00d4a0e6c231170517041604a1021408414c50482f5553440d0ece00d4a0e6c231170717061606a10314084159494e2f5553440d0ece00d4a0e6c231170917081608a104",
"codeHash": "234c356f92e0e441323a14f31234857df46285aba20dc9423bccba90b0f3714e",
Expand All @@ -13,7 +13,7 @@
"ayinPrice"
],
"types": [
"IOracle",
"IDIAOracle",
"U256",
"U256",
"U256",
Expand Down
35 changes: 35 additions & 0 deletions call-oracle/artifacts/RandomValueFetcher.ral.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "v3.3.0",
"name": "RandomValueFetcher",
"bytecode": "0501402701000000000ed3948fae950c0dce00d46393ea60a100a0000d0fce00d4b121f490a103a102a101",
"codeHash": "9f3715ba3cf130bd8398a3338315876a13c2ecbae485b4a6a439c7e0f5331564",
"fieldsSig": {
"names": [
"oracle",
"lastRound",
"value"
],
"types": [
"IDIARandomOracle",
"U256",
"DIARandomValue"
],
"isMutable": [
false,
true,
true
]
},
"eventsSig": [],
"functions": [
{
"name": "update",
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": []
}
],
"constants": [],
"enums": []
}
24 changes: 21 additions & 3 deletions call-oracle/artifacts/structs.ral.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"name": "OracleValue",
"name": "DIAOracleValue",
"fieldNames": [
"value",
"timestamp"
Expand All @@ -10,8 +10,26 @@
"U256"
],
"isMutable": [
false,
false
true,
true
]
},
{
"name": "DIARandomValue",
"fieldNames": [
"randomness",
"signature",
"previousSignature"
],
"fieldTypes": [
"ByteVec",
"ByteVec",
"ByteVec"
],
"isMutable": [
true,
true,
true
]
}
]
2 changes: 1 addition & 1 deletion call-oracle/artifacts/ts/PriceFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
} from "@alephium/web3";
import { default as PriceFetcherContractJson } from "../PriceFetcher.ral.json";
import { getContractByCodeHash } from "./contracts";
import { OracleValue, AllStructs } from "./types";
import { DIAOracleValue, DIARandomValue, AllStructs } from "./types";

// Custom types for the contract
export namespace PriceFetcherTypes {
Expand Down
152 changes: 152 additions & 0 deletions call-oracle/artifacts/ts/RandomValueFetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */

import {
Address,
Contract,
ContractState,
TestContractResult,
HexString,
ContractFactory,
EventSubscribeOptions,
EventSubscription,
CallContractParams,
CallContractResult,
TestContractParams,
ContractEvent,
subscribeContractEvent,
subscribeContractEvents,
testMethod,
callMethod,
multicallMethods,
fetchContractState,
ContractInstance,
getContractEventsCurrentCount,
TestContractParamsWithoutMaps,
TestContractResultWithoutMaps,
SignExecuteContractMethodParams,
SignExecuteScriptTxResult,
signExecuteMethod,
addStdIdToFields,
encodeContractFields,
} from "@alephium/web3";
import { default as RandomValueFetcherContractJson } from "../RandomValueFetcher.ral.json";
import { getContractByCodeHash } from "./contracts";
import { DIAOracleValue, DIARandomValue, AllStructs } from "./types";

// Custom types for the contract
export namespace RandomValueFetcherTypes {
export type Fields = {
oracle: HexString;
lastRound: bigint;
value: DIARandomValue;
};

export type State = ContractState<Fields>;

export interface CallMethodTable {
update: {
params: Omit<CallContractParams<{}>, "args">;
result: CallContractResult<null>;
};
}
export type CallMethodParams<T extends keyof CallMethodTable> =
CallMethodTable[T]["params"];
export type CallMethodResult<T extends keyof CallMethodTable> =
CallMethodTable[T]["result"];
export type MultiCallParams = Partial<{
[Name in keyof CallMethodTable]: CallMethodTable[Name]["params"];
}>;
export type MultiCallResults<T extends MultiCallParams> = {
[MaybeName in keyof T]: MaybeName extends keyof CallMethodTable
? CallMethodTable[MaybeName]["result"]
: undefined;
};

export interface SignExecuteMethodTable {
update: {
params: Omit<SignExecuteContractMethodParams<{}>, "args">;
result: SignExecuteScriptTxResult;
};
}
export type SignExecuteMethodParams<T extends keyof SignExecuteMethodTable> =
SignExecuteMethodTable[T]["params"];
export type SignExecuteMethodResult<T extends keyof SignExecuteMethodTable> =
SignExecuteMethodTable[T]["result"];
}

class Factory extends ContractFactory<
RandomValueFetcherInstance,
RandomValueFetcherTypes.Fields
> {
encodeFields(fields: RandomValueFetcherTypes.Fields) {
return encodeContractFields(
addStdIdToFields(this.contract, fields),
this.contract.fieldsSig,
AllStructs
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as RandomValueFetcherTypes.Fields;
}

at(address: string): RandomValueFetcherInstance {
return new RandomValueFetcherInstance(address);
}

tests = {
update: async (
params: Omit<
TestContractParamsWithoutMaps<RandomValueFetcherTypes.Fields, never>,
"testArgs"
>
): Promise<TestContractResultWithoutMaps<null>> => {
return testMethod(this, "update", params, getContractByCodeHash);
},
};
}

// Use this object to test and deploy the contract
export const RandomValueFetcher = new Factory(
Contract.fromJson(
RandomValueFetcherContractJson,
"",
"9f3715ba3cf130bd8398a3338315876a13c2ecbae485b4a6a439c7e0f5331564",
AllStructs
)
);

// Use this class to interact with the blockchain
export class RandomValueFetcherInstance extends ContractInstance {
constructor(address: Address) {
super(address);
}

async fetchState(): Promise<RandomValueFetcherTypes.State> {
return fetchContractState(RandomValueFetcher, this);
}

view = {
update: async (
params?: RandomValueFetcherTypes.CallMethodParams<"update">
): Promise<RandomValueFetcherTypes.CallMethodResult<"update">> => {
return callMethod(
RandomValueFetcher,
this,
"update",
params === undefined ? {} : params,
getContractByCodeHash
);
},
};

transact = {
update: async (
params: RandomValueFetcherTypes.SignExecuteMethodParams<"update">
): Promise<RandomValueFetcherTypes.SignExecuteMethodResult<"update">> => {
return signExecuteMethod(RandomValueFetcher, this, "update", params);
},
};
}
4 changes: 2 additions & 2 deletions call-oracle/artifacts/ts/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
/* eslint-disable */

import { Contract, ContractFactory } from "@alephium/web3";
import { PriceFetcher } from ".";
import { PriceFetcher, RandomValueFetcher } from ".";

let contracts: ContractFactory<any>[] | undefined = undefined;
export function getContractByCodeHash(codeHash: string): Contract {
if (contracts === undefined) {
contracts = [PriceFetcher];
contracts = [PriceFetcher, RandomValueFetcher];
}
const c = contracts.find(
(c) =>
Expand Down
14 changes: 13 additions & 1 deletion call-oracle/artifacts/ts/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

import { RunScriptResult, DeployContractExecutionResult } from "@alephium/cli";
import { NetworkId } from "@alephium/web3";
import { PriceFetcher, PriceFetcherInstance } from ".";
import {
PriceFetcher,
PriceFetcherInstance,
RandomValueFetcher,
RandomValueFetcherInstance,
} from ".";
import { default as testnetDeployments } from "../../deployments/.deployments.testnet.json";

export type Deployments = {
deployerAddress: string;
contracts: {
PriceFetcher: DeployContractExecutionResult<PriceFetcherInstance>;
RandomValueFetcher: DeployContractExecutionResult<RandomValueFetcherInstance>;
};
};

Expand All @@ -22,6 +28,12 @@ function toDeployments(json: any): Deployments {
json.contracts["PriceFetcher"].contractInstance.address
),
},
RandomValueFetcher: {
...json.contracts["RandomValueFetcher"],
contractInstance: RandomValueFetcher.at(
json.contracts["RandomValueFetcher"].contractInstance.address
),
},
};
return {
...json,
Expand Down
1 change: 1 addition & 0 deletions call-oracle/artifacts/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/* eslint-disable */

export * from "./PriceFetcher";
export * from "./RandomValueFetcher";
export * from "./scripts";
Loading

0 comments on commit ddd1869

Please sign in to comment.