Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oracle update #3248

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/verity_oracle_example/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version = "0.0.1"
MoveStdlib = { git = "https://github.com/rooch-network/rooch.git", subdir = "frameworks/move-stdlib", rev = "main" }
MoveosStdlib = { git = "https://github.com/rooch-network/rooch.git", subdir = "frameworks/moveos-stdlib", rev = "main" }
RoochFramework = { git = "https://github.com/rooch-network/rooch.git", subdir = "frameworks/rooch-framework", rev = "main" }
VerityMoveOracles = { git = "https://github.com/usherlabs/verity-move-oracles.git", subdir = "rooch", rev = "feature/open-ai" } # change feature/open-ai to main once PR#15 is merged
VerityMoveOracles = { git = "https://github.com/usherlabs/verity-move-oracles.git", subdir = "rooch", rev = "main" }


[addresses]
Expand Down
46 changes: 21 additions & 25 deletions examples/verity_oracle_example/sources/example.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ module verity_oracle_example::example_caller {
use moveos_std::object::{ObjectID};
use std::option::{Self, Option};
use std::vector;
use std::string::String;
use std::string::{Self,String};
use verity::oracles::{Self as Oracles};
use rooch_framework::gas_coin::RGas;
use rooch_framework::account_coin_store;
#[test_only]
use verity::oracles;

struct GlobalParams has key {
pending_requests: vector<ObjectID>,
}

#[test_only]
public fun init_for_test(){
oracles::init_for_test();
Oracles::init_for_test();
init();
}

Expand All @@ -39,7 +37,7 @@ module verity_oracle_example::example_caller {
// Initiate the module with an empty vector of pending requests
// Requests are managed in the caller to prevent other modules from impersonating the calling module, and spoofing new data.
fun init(){
// let params = account::borrow_mut_resource<GlobalParams>(@verity_test_foreign_module); // account::borrow_mut_resource in init throws an error on deployment
// let params = account::borrow_mut_resource<GlobalParams>(@verity_oracle_example); // account::borrow_mut_resource in init throws an error on deployment
// params.pending_requests = vector::empty<ObjectID>();
let signer = moveos_std::signer::module_signer<GlobalParams>();
account::move_resource_to(&signer, GlobalParams { pending_requests: vector::empty<ObjectID>() });
Expand All @@ -60,16 +58,16 @@ module verity_oracle_example::example_caller {
// We're passing the address and function identifier of the recipient address. in this from <module_name>::<function_name>
// If you do not want to pay for the Oracle to notify your contract, you can pass in option::none() as the argument.
let payment = account_coin_store::withdraw<RGas>(from, amount);
let request_id = Oracles::new_request_with_payment(http_request, pick, oracle, Oracles::with_notify(@verity_test_foreign_module, b"example_caller::receive_data"),payment);
let request_id = Oracles::new_request_with_payment(http_request, pick, oracle, Oracles::with_notify(@verity_oracle_example, string::utf8(b"example_caller::receive_data")),payment);
// let no_notify_request_id = Oracles::new_request(http_request, pick, oracle, Oracles::without_notify());
let params = account::borrow_mut_resource<GlobalParams>(@verity_test_foreign_module);
let params = account::borrow_mut_resource<GlobalParams>(@verity_oracle_example);
vector::push_back(&mut params.pending_requests, request_id);
}

// This notify function is called by the Oracle.
// ! It must not include parameters, or return arguments.
public entry fun receive_data() {
let params = account::borrow_mut_resource<GlobalParams>(@verity_test_foreign_module);
let params = account::borrow_mut_resource<GlobalParams>(@verity_oracle_example);
let pending_requests = params.pending_requests;

let i = 0;
Expand All @@ -78,22 +76,20 @@ module verity_oracle_example::example_caller {
// Remove the fulfilled request from the pending_requests vector
// This ensures unfulfilled requests are retained in the vector
if (option::is_some(&Oracles::get_response(request_id))) {
vector::remove(&mut params.pending_requests, i);
// Decrement i to account for the removed element
if (i > 0) {
i = i - 1;
let (found, index) = vector::index_of(&mut params.pending_requests, request_id);
if (found){
vector::remove(&mut params.pending_requests, index);
// ? ------ OPTIONAL ------
let request_url = Oracles::get_request_params_url(request_id);
let request_method = Oracles::get_request_params_method(request_id);
let response = Oracles::get_response(request_id);
// For each fulfilment, emit an event
event::emit(RequestFulfilledEvent {
request_url,
request_method,
response,
});
};

// ? ------ OPTIONAL ------
let request_url = Oracles::get_request_params_url(request_id);
let request_method = Oracles::get_request_params_method(request_id);
let response = Oracles::get_response(request_id);
// For each fulfilment, emit an event
event::emit(RequestFulfilledEvent {
request_url,
request_method,
response,
});
// \ ------ OPTIONAL ------
};

Expand All @@ -103,15 +99,15 @@ module verity_oracle_example::example_caller {

#[view]
public fun pending_requests_count(): u64 {
let params = account::borrow_resource<GlobalParams>(@verity_test_foreign_module);
let params = account::borrow_resource<GlobalParams>(@verity_oracle_example);
vector::length(&params.pending_requests)
}
}

#[test_only]
module verity_oracle_example::test_foreign_module {
use moveos_std::signer;
use verity_test_foreign_module::example_caller::{Self, request_data, pending_requests_count};
use verity_oracle_example::example_caller::{Self, request_data, pending_requests_count};
use rooch_framework::gas_coin;
use verity::registry;
use std::vector;
Expand Down
Loading