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

How to invoke a specific function within a deployed Phat Contract? #29

Open
a-moreira opened this issue Jun 24, 2023 · 5 comments
Open

Comments

@a-moreira
Copy link

a-moreira commented Jun 24, 2023

I want to invoke a specific function within a deployed Phat Contract from Anchor by calling anchor::push_message. I know this should be encoded as the data field, but would like further clarification on how to do it.
I've been able to receive responses from this Phat Contract into my Anchor pallet via the OnResponse trait, but not yet to send to it.

@FaisalAl-Tameemi
Copy link
Contributor

FaisalAl-Tameemi commented Jun 25, 2023

Perhaps another way to think about this would also be as follows:

Does the phat contract need to implement a general onMessageReceived type of callback handler which receives whatever the pallet passed into the anchor::push_message() call? -- This would be akin to the OnResponse which a pallet would implement but on the phat contract side.

As an example, let's assume we'd like like to call the feed_price() function in the deployed sub_price_feed phat contract which we've successfully initialized rollup for.

https://github.com/Phala-Network/phat-offchain-rollup/blob/main/phat/contracts/sub_price_feed/lib.rs#L187C62-L187C62

@h4x3rotab
Copy link
Contributor

h4x3rotab commented Jun 27, 2023

I want to invoke a specific function within a deployed Phat Contract from Anchor by calling anchor::push_message.

@FaisalAl-Tameemi's answer is correct. If you want to trigger the Phat Contract from the anchor side (pallet or smart contract), you will need to push a message. Once the message is pushed, it stays in the anchor storage. The next time when the offchain rollup Phat Contract reads the storage, it will notice it, and you can get it by accessing the request queue from your PC like mentioned in the readme file (assume you have initialized a rollup client):

// Get a request if available
if let Some(raw_req) = client
    .session()
    .pop()
    .expect("failed to read queue") {
    // let action: Vec<u8> = ... Create your response based on the raw_req ...
    client.action(Action::Reply(action));
}

This is called Req-Resp model. You add requests on chain, and the Phat Contract answer it later on. The request will be removed once the blockchain receives the response. The request and the response are bytes arrays. So you can pass arbitrary message back and forth.

@FaisalAl-Tameemi
Copy link
Contributor

@h4x3rotab out of curiously, is it correct that XCM could also be used to invoke an ink! message in the phat contract?

@h4x3rotab
Copy link
Contributor

Theoretically you can send a Transact message to Phala to call phalaPhatContract.pushContractMessage

@FaisalAl-Tameemi
Copy link
Contributor

but not recommended vs. RPC?

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