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

ABI for low-level contracts #28

Open
itegulov opened this issue Aug 18, 2022 · 3 comments
Open

ABI for low-level contracts #28

itegulov opened this issue Aug 18, 2022 · 3 comments

Comments

@itegulov
Copy link
Contributor

itegulov commented Aug 18, 2022

There are some rough edges when generating ABI for a low-level contract. For example it is unclear that the following function returns a promise and not a ():

pub fn factorial(&self, n: u32) {
    if n <= 1 {
        env::value_return(&serde_json::to_vec(&1u32).unwrap());
        return;
    }
    let account_id = env::current_account_id();
    let prepaid_gas = env::prepaid_gas() - FACTORIAL_CALL_GAS;
    let promise0 = env::promise_create(
        account_id.clone(),
        "factorial",
        &serde_json::to_vec(&(n - 1,)).unwrap(),
        0,
        prepaid_gas - FACTORIAL_MULT_CALL_GAS,
    );
    let promise1 = env::promise_then(
        promise0,
        account_id,
        "factorial_mult",
        &serde_json::to_vec(&(n,)).unwrap(),
        0,
        FACTORIAL_MULT_CALL_GAS,
    );
    env::promise_return(promise1);
}

Same thing with resolving callbacks: they are not a part of the signature so they will not show up in ABI. Not sure how big of a deal this is, but can be a potential foot gun as an ABI for the code will be successfully generated, but won't have the correct type. We could add some custom attributes (e.g. #[near_abi_returns(<type>)]) and/or warn user who is using cargo-near on a contract that contains env::promise_result(i) (and other similar calls).

@miraclx
Copy link
Contributor

miraclx commented Aug 26, 2022

contract that contains env::promise_result(i)

Hm, that's tricky, what if the return is inside another function that's called from this one. Shouldn't happen, but there's nothing stopping it from happening.

@itegulov
Copy link
Contributor Author

Yeah, it's going to be impossible to traverse the entire call tree outside of rustc, so I don't think we can do much here tbh.

@austinabell
Copy link
Contributor

I wonder if the direction described in near/abi#1 could be useful

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

No branches or pull requests

3 participants