Skip to content

Conversation

RogerKSI
Copy link
Member

@RogerKSI RogerKSI commented Nov 7, 2023

Implementation details

  • Add macro to help decode/encode ABI on input and output of prepare and execution function.

This is an example of an Oracle script that uses ABI macro.

use alloy_sol_types::{sol, SolValue};
use owasm_kit::{execute_abi_entry_point, ext, oei, prepare_abi_entry_point};

sol! {
    struct Input {
        string word;
    }
}

sol! {
    struct Output {
        string result;
    }
}

fn prepare_impl(input: Input) {
    oei::ask_external_data(1, 1, input.word.as_bytes());
}

fn execute_impl(_: Input) -> Output {
    Output {
        result: ext::load_majority::<String>(1).unwrap(),
    }
}

prepare_abi_entry_point!(prepare_impl);
execute_abi_entry_point!(execute_impl);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_schema() {
        let input = Input {
            word: String::from("hello"),
        };

        let encoded_calldata: [u8; 128] = [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 5, 104, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        ];
        let result: Input = SolValue::abi_decode(&encoded_calldata.clone(), true).unwrap();
        assert_eq!(input.word, result.word);

        let result = result.abi_encode();
        assert_eq!(result, encoded_calldata);
    }
}

Please ensure the following requirements are met before submitting a pull request:

  • The pull request is targeted against the correct target branch
  • The pull request is linked to an issue with appropriate discussion and an accepted design OR is linked to a spec that describes the work.
  • The pull request includes a description of the implementation/work done in detail.
  • The pull request includes any and all appropriate unit/integration tests
  • You have added a relevant changelog entry to CHANGELOG_UNRELEASED.md
  • You have re-reviewed the files affected by the pull request (e.g. using the Files changed tab in the Github PR explorer)

@warittornc
Copy link
Contributor

warittornc commented Dec 4, 2023

  • Maybe we should implement a similar strategy for obi as well rather than using just traits for obi while using macros for abi for consistency
  • Maybe also reimport sol as abi for clarity

@RogerKSI RogerKSI marked this pull request as draft February 15, 2024 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants