Skip to content

Commit

Permalink
Change GetSmartContractCode to return EVM code (#1235)
Browse files Browse the repository at this point in the history
* Change GetSmartContractCode to return EVM code

* Change GetSmartContractCode to return hex of EVM code

* Change `GetSmartContractCode` to return hex of EVM coder

* Add extra field to result of `GetSmartContractCode` to return the `language` in `code`

* Update documentation to respect changes to GetSmartContractCode

* Update language to type

* Update docs/api/zilliqa/getsmartcontractcode.doc.md

Co-authored-by: James Hinshelwood <[email protected]>

* Update zilliqa/src/api/zil.rs

Co-authored-by: James Hinshelwood <[email protected]>

---------

Co-authored-by: James Hinshelwood <[email protected]>
  • Loading branch information
joseph-zilliqa and JamesHinshelwood authored Jul 31, 2024
1 parent 3709071 commit 5b6cfa9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions docs/api/zilliqa/getsmartcontractcode.doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ contract,smart contract,code,get,address

# Description

Returns the Scilla code associated with a smart contract address. This is represented as a `String`.
Returns the Scilla or EVM code associated with a smart contract address. Scilla code is represented as a `String` for the code, EVM code is returned as a hex representation of the compiled bytes.

# Curl

Expand Down Expand Up @@ -68,7 +68,8 @@ func GetSmartContractCode() {
"id": "1",
"jsonrpc": "2.0",
"result": {
"code": "scilla_version 0\n\n (* HelloWorld contract *)\n \n import ListUtils\n \n (***************************************************)\n (* Associated library *)\n (***************************************************)\n library HelloWorld\n \n let one_msg = \n fun (msg : Message) => \n let nil_msg = Nil {Message} in\n Cons {Message} msg nil_msg\n \n let not_owner_code = Int32 1\n let set_hello_code = Int32 2\n \n (***************************************************)\n (* The contract definition *)\n (***************************************************)\n \n contract HelloWorld\n (owner: ByStr20)\n \n field welcome_msg : `String` = \"\"\n \n transition setHello (msg : `String`)\n is_owner = builtin eq owner _sender;\n match is_owner with\n | False =>\n msg = {_tag : \"Main\"; _recipient : _sender; _amount : Uint128 0; code : not_owner_code};\n msgs = one_msg msg;\n send msgs\n | True =>\n welcome_msg := msg;\n msg = {_tag : \"Main\"; _recipient : _sender; _amount : Uint128 0; code : set_hello_code};\n msgs = one_msg msg;\n send msgs\n end\n end\n \n \n transition getHello ()\n r <- welcome_msg;\n e = {_eventname: \"getHello()\"; msg: r};\n event e\n end"
"code": "scilla_version 0\n\n (* HelloWorld contract *)\n \n import ListUtils\n \n (***************************************************)\n (* Associated library *)\n (***************************************************)\n library HelloWorld\n \n let one_msg = \n fun (msg : Message) => \n let nil_msg = Nil {Message} in\n Cons {Message} msg nil_msg\n \n let not_owner_code = Int32 1\n let set_hello_code = Int32 2\n \n (***************************************************)\n (* The contract definition *)\n (***************************************************)\n \n contract HelloWorld\n (owner: ByStr20)\n \n field welcome_msg : `String` = \"\"\n \n transition setHello (msg : `String`)\n is_owner = builtin eq owner _sender;\n match is_owner with\n | False =>\n msg = {_tag : \"Main\"; _recipient : _sender; _amount : Uint128 0; code : not_owner_code};\n msgs = one_msg msg;\n send msgs\n | True =>\n welcome_msg := msg;\n msg = {_tag : \"Main\"; _recipient : _sender; _amount : Uint128 0; code : set_hello_code};\n msgs = one_msg msg;\n send msgs\n end\n end\n \n \n transition getHello ()\n r <- welcome_msg;\n e = {_eventname: \"getHello()\"; msg: r};\n event e\n end",
"type": "scilla"
}
}
```
Expand Down
8 changes: 5 additions & 3 deletions zilliqa/src/api/zil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{
node::Node,
schnorr,
scilla::split_storage_key,
state::Code,
transaction::{ScillaGas, SignedTransaction, TxZilliqa, ZilAmount, EVM_GAS_PER_SCILLA_GAS},
};

Expand Down Expand Up @@ -343,11 +344,12 @@ fn get_smart_contract_code(params: Params, node: &Arc<Mutex<Node>>) -> Result<Va
.get_state(&block)?
.get_account(smart_contract_address)?;

let Some((code, _)) = account.code.scilla_code_and_init_data() else {
return Err(anyhow!("Address not contract address"));
let (code, type_) = match account.code {
Code::Evm(ref bytes) => (hex::encode(bytes), "evm"),
Code::Scilla { code, .. } => (code, "scilla"),
};

Ok(json!({ "code": code }))
Ok(json!({ "code": code, "type": type_ }))
}

fn get_smart_contract_init(params: Params, node: &Arc<Mutex<Node>>) -> Result<Value> {
Expand Down

0 comments on commit 5b6cfa9

Please sign in to comment.