Skip to content

Commit 723c345

Browse files
authored
feat(rpc-server): Custom method to emulate transaction (#408)
* feat(rpc-server): Custom method to emulate transaction * fmt * function call Code storage update for transaction emulation * collect cross call results * make clippy happy * tx emulator improvement * add docstring * add docs for new custom method * improvement
1 parent b98c1f8 commit 723c345

File tree

12 files changed

+871
-140
lines changed

12 files changed

+871
-140
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ near-chain-configs = { git = "https://github.com/kobayurii/nearcore", branch = "
5757
near-crypto = { git = "https://github.com/kobayurii/nearcore", branch = "fork/2.6.5" }
5858
near-jsonrpc = { git = "https://github.com/kobayurii/nearcore", branch = "fork/2.6.5" }
5959
near-parameters = { git = "https://github.com/kobayurii/nearcore", branch = "fork/2.6.5" }
60+
node-runtime = { git = "https://github.com/kobayurii/nearcore", branch = "fork/2.6.5" }
6061
near-vm-runner = { git = "https://github.com/kobayurii/nearcore", branch = "fork/2.6.5" , features = [
6162
"wasmer0_vm",
6263
"wasmer2_vm",

docs/CUSTOM_RPC_METHODS.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,51 @@ Response:
116116
}
117117
}
118118
```
119+
120+
# emulate_tx
121+
122+
The `emulate_tx` method is a custom method that allows you to estimate transaction costs and simulate its execution without actually sending it to the network. For now simulation supports only `FunctionCall` action, for other actions it will return only estimate fee.
123+
124+
## How to use it
125+
### Example
126+
127+
Request:
128+
```json
129+
{
130+
"jsonrpc": "2.0",
131+
"id": "dontcare",
132+
"method": "emulate_tx",
133+
"params": {
134+
"signed_tx_base64": "DgAAAHNlbmRlci50ZXN0bmV0AOrmAai64SZOv9e/naX4W15pJx0GAap35wTT1T/DwcbbDwAAAAAAAAAQAAAAcmVjZWl2ZXIudGVzdG5ldNMnL7URB1cxPOu3G8jTqlEwlcasagIbKlAJlF5ywVFLAQAAAAMAAACh7czOG8LTAAAAAAAAAGQcOG03xVSFQFjoagOb4NBBqWhERnnz45LY4+52JgZhm1iQKz7qAdPByrGFDQhQ2Mfga8RlbysuQ8D8LlA6bQE=",
135+
"wait_until": "INCLUDED_FINAL"
136+
}
137+
}
138+
```
139+
or
140+
```json
141+
{
142+
"jsonrpc": "2.0",
143+
"id": "dontcare",
144+
"method": "emulate_tx",
145+
"params": [ "DgAAAHNlbmRlci50ZXN0bmV0AOrmAai64SZOv9e/naX4W15pJx0GAap35wTT1T/DwcbbDwAAAAAAAAAQAAAAcmVjZWl2ZXIudGVzdG5ldNMnL7URB1cxPOu3G8jTqlEwlcasagIbKlAJlF5ywVFLAQAAAAMAAACh7czOG8LTAAAAAAAAAGQcOG03xVSFQFjoagOb4NBBqWhERnnz45LY4+52JgZhm1iQKz7qAdPByrGFDQhQ2Mfga8RlbysuQ8D8LlA6bQE="
146+
]
147+
}
148+
```
149+
Response:
150+
```json
151+
{
152+
"jsonrpc": "2.0",
153+
"result": {
154+
"block_height": 156743532,
155+
"gas_price": 100000000,
156+
"results": [
157+
{
158+
"Transfer": {
159+
"fee": 115123062500
160+
}
161+
}
162+
]
163+
},
164+
"id": "dontcare"
165+
}
166+
```

docs/RPC_METHODS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
| **Method** | **status** | **Note** |
22
|-----------------------------------|---------------|-----------------------------------------------------------------------------|
33
| view_state_paginated | Included | Custom method. See details [here](../docs/CUSTOM_RPC_METHODS.md) |
4-
| view_receipt_record | Included | Custom method. See details [here](../docs/CUSTOM_RPC_METHODS.md) |
4+
| view_state_paginated | Included | Custom method. See details [here](../docs/CUSTOM_RPC_METHODS.md) |
5+
| emulate_tx | Included | Custom method. See details [here](../docs/CUSTOM_RPC_METHODS.md) |
56
| query.view_account | Included | |
67
| query.view_code | Included | |
78
| query.view_state | Included | |

rpc-server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ near-jsonrpc.workspace = true
4646
near-jsonrpc-client.workspace = true
4747
near-indexer-primitives.workspace = true
4848
near-lake-framework.workspace = true
49+
node-runtime.workspace = true
4950
near-primitives.workspace = true
5051
near-parameters.workspace = true
5152
near-vm-runner.workspace = true

rpc-server/src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ async fn rpc_handler(
7878
})
7979
.await
8080
}
81+
82+
"emulate_tx" => {
83+
process_method_call(request, |params| {
84+
modules::transactions::methods::emulate_tx(data, params)
85+
})
86+
.await
87+
}
8188
// request methods
8289
"query" => {
8390
process_method_call(request, |params| {

0 commit comments

Comments
 (0)