Phala-Network / phat-offchain-rollup

Phat Contract Offchain Rollup implementation
Apache License 2.0
18 stars 13 forks source link

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

Open a-moreira opened 1 year ago

a-moreira commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

h4x3rotab commented 1 year ago

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

FaisalAl-Tameemi commented 1 year ago

but not recommended vs. RPC?