Skip to content

Commit cd20930

Browse files
committed
fix: add block reference support to view calls in NEAR client
1 parent 1fcd320 commit cd20930

File tree

5 files changed

+76
-43
lines changed

5 files changed

+76
-43
lines changed

README.md

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,28 @@
1414

1515
## What is Engine?
1616

17-
[Aurora](https://doc.aurora.dev/getting-started/aurora-engine/) is an Ethereum Virtual Machine (EVM)
18-
project built on the NEAR Protocol, that provides a solution for developers to deploy their apps
19-
on an Ethereum-compatible, high-throughput, scalable and future-safe platform, with low transaction costs
17+
[Aurora](https://doc.aurora.dev/getting-started/aurora-engine/) is an Ethereum
18+
Virtual Machine (EVM) project built on the NEAR Protocol, that provides a
19+
solution for developers to deploy their apps on an Ethereum-compatible,
20+
high-throughput, scalable and future-safe platform, with low transaction costs
2021
for their users. Engine is the Aurora's implementation for it.
2122

2223
## What is Aurora CLI?
2324

24-
Aurora CLI is a command line interface to bootstrap Aurora Engine with rapid speed built with rust.
25+
Aurora CLI is a command line interface to bootstrap Aurora Engine with rapid
26+
speed built with rust.
2527

26-
Aurora CLI comes pre-configuration with opinionated, sensible defaults for standard testing environments.
27-
If other projects mention testing on Aurora, they are referring to the settings defined in this repo.
28+
Aurora CLI comes pre-configuration with opinionated, sensible defaults for
29+
standard testing environments. If other projects mention testing on Aurora, they
30+
are referring to the settings defined in this repo.
2831

2932
**Aurora CLI has the following advantages over api:**
3033

3134
- :pencil: **Easily modifiable EVM states through terminal**
3235
- :handshake: **Quick to interact for rapid iterations**
3336

34-
See also prior version [aurora-cli](https://github.com/aurora-is-near/aurora-cli).
37+
See also prior version
38+
[aurora-cli](https://github.com/aurora-is-near/aurora-cli).
3539

3640
## Prerequisites
3741

@@ -40,14 +44,17 @@ See also prior version [aurora-cli](https://github.com/aurora-is-near/aurora-cli
4044
## Quickstart
4145

4246
- 📦 Install `aurora-cli-rs` and start interacting with it:
43-
*`cargo install --git https://github.com/aurora-is-near/aurora-cli-rs.git`*
44-
- 🔍 Check out what each command is for in the [Commands Reference](#commands-reference) section
45-
- ✋ Have questions? Ask them at the official Aurora [forum](https://forum.aurora.dev/)
47+
_`cargo install --git https://github.com/aurora-is-near/aurora-cli-rs.git`_
48+
- 🔍 Check out what each command is for in the
49+
[Commands Reference](#commands-reference) section
50+
- ✋ Have questions? Ask them at the official Aurora
51+
[forum](https://forum.aurora.dev/)
4652

4753
## Usage
4854

49-
In the following example, we will see how to deploy Aurora EVM on the `localnet`. Also, we will deploy a simple EVM
50-
smart contract and will be interacting with it.
55+
In the following example, we will see how to deploy Aurora EVM on the
56+
`localnet`. Also, we will deploy a simple EVM smart contract and will be
57+
interacting with it.
5158

5259
### **Requirements**
5360

@@ -60,10 +67,11 @@ First what we need to do is to install `aurora-cli`:
6067

6168
```shell
6269
git clone https://github.com/aurora-engine/aurora-cli-rs
63-
cd aurora-cli-rs/cli && cargo install --path .
70+
cd aurora-cli-rs/cli && cargo install --path .
6471
```
6572

66-
Next we need to start a NEAR node locally. We can use the NEAR utility, `nearup`.
73+
Next we need to start a NEAR node locally. We can use the NEAR utility,
74+
`nearup`.
6775

6876
### **Start a NEAR node locally**
6977

@@ -79,16 +87,18 @@ Start NEAR node:
7987
nearup run localnet --home /tmp/localnet
8088
```
8189

82-
When running the `nearup run localnet` command on Apple’s M-based hardware, a local build of `neard` is required due to
83-
compatibility issues with the architecture.
90+
When running the `nearup run localnet` command on Apple’s M-based hardware, a
91+
local build of `neard` is required due to compatibility issues with the
92+
architecture.
8493

8594
Start NEAR node (Apple's M-based hardware):
8695

8796
```shell
8897
nearup run localnet --home /tmp/localnet --binary-path /path/to/nearcore/target/release
8998
```
9099

91-
Replace `/path/to/nearcore/target/release` with the actual path to the locally built `neard` binary.
100+
Replace `/path/to/nearcore/target/release` with the actual path to the locally
101+
built `neard` binary.
92102

93103
### **Prepare an account and create a private key file for Aurora EVM**
94104

@@ -125,8 +135,9 @@ aurora-cli --engine aurora.node0 --near-key-path /tmp/localnet/aurora_key.json i
125135

126136
### **Deploy the EVM smart contract**
127137

128-
And now we can deploy the EVM smart contract. In our example, it will be a simple counter that can return its current
129-
value and increment and decrement its value.
138+
And now we can deploy the EVM smart contract. In our example, it will be a
139+
simple counter that can return its current value and increment and decrement its
140+
value.
130141

131142
But before that we need to generate a private key for signing transactions:
132143

@@ -161,13 +172,14 @@ If everything went well, the response should be like this:
161172
Contract has been deployed to address: 0x53a9fed853e02a39bf8d298f751374de8b5a6ddf successfully
162173
```
163174

164-
So. Now we have deployed the smart contract at address: `0x53a9fed853e02a39bf8d298f751374de8b5a6ddf`.
175+
So. Now we have deployed the smart contract at address:
176+
`0x53a9fed853e02a39bf8d298f751374de8b5a6ddf`.
165177

166178
### **Interact with the smart contract**
167179

168180
First, let's check that the current value is the same as we set in the
169-
initialization stage. For that, we will use the `view-call` operation, which doesn't demand a private key
170-
because it is a read-only operation:
181+
initialization stage. For that, we will use the `view-call` operation, which
182+
doesn't demand a private key because it is a read-only operation:
171183

172184
```shell
173185
aurora-cli --engine aurora.node0 view-call -a 0x53a9fed853e02a39bf8d298f751374de8b5a6ddf -f value \
@@ -186,8 +198,8 @@ aurora-cli --engine aurora.node0 --near-key-path /tmp/localnet/aurora_key.json s
186198
--aurora-secret-key 3fac6dca1c6fc056b971a4e9090afbbfbdf3bc443e9cda595facb653cb1c01e1
187199
```
188200

189-
In the response, we can see if the transaction was successful and the amount of gas used for the execution of this
190-
transaction.
201+
In the response, we can see if the transaction was successful and the amount of
202+
gas used for the execution of this transaction.
191203

192204
Let's make sure that our value was incremented:
193205

@@ -200,13 +212,15 @@ So, if we can see `6` in the output then the demo was successful. That's it!
200212

201213
### **Build aurora-cli with the advanced command line interface (Advanced CLI)**
202214

203-
Advanced CLI provides more options andadvanced features. You can try it by building with the following command:
215+
Advanced CLI provides more options andadvanced features. You can try it by
216+
building with the following command:
204217

205218
```shell
206219
cargo install --path . --no-default-features -F advanced
207220
```
208221

209-
Documentation on how to work with the advanced version of `aurora-cli` can be found [here](docs/localnet.md).
222+
Documentation on how to work with the advanced version of `aurora-cli` can be
223+
found [here](docs/localnet.md).
210224

211225
### **Browse Deployed EVM Metadata**
212226

@@ -269,17 +283,19 @@ Disable whitelist status
269283
aurora-cli --engine aurora.node0 set-whitelist-status --kind <whitelist-kind> --status 0
270284
```
271285

272-
Replace `<whitelist-kind>` with the desired whitelist type (admin, evm-admin, account, or address), and `<entry-value>`
273-
with the address or account to be whitelisted or removed.
286+
Replace `<whitelist-kind>` with the desired whitelist type (admin, evm-admin,
287+
account, or address), and `<entry-value>` with the address or account to be
288+
whitelisted or removed.
274289

275290
Add whitelist batch
276291

277292
```shell
278293
aurora-cli --engine aurora.node0 add-entry-to-whitelist-batch path/to/batch_list.json
279294
```
280295

281-
The batch should be provided in a JSON format. Each entry in the JSON array should have two properties: `kind` and
282-
either `account_id` or `address`, depending on the type of whitelist being updated.
296+
The batch should be provided in a JSON format. Each entry in the JSON array
297+
should have two properties: `kind` and either `account_id` or `address`,
298+
depending on the type of whitelist being updated.
283299

284300
Example JSON batch file (`batch_list.json`):
285301

@@ -439,6 +455,7 @@ Options:
439455
--network <NETWORK> NEAR network ID [default: localnet]
440456
--engine <ACCOUNT_ID> Aurora EVM account [default: aurora]
441457
--near-key-path <NEAR_KEY_PATH> Path to file with NEAR account id and secret key in JSON format
458+
--block-number Block number to get data from
442459
-h, --help Print help
443460
-V, --version Print version
444461
```
@@ -537,7 +554,6 @@ Arguments:
537554

538555
Options:
539556
-h, --help Print help
540-
541557
```
542558

543559
### `aurora-cli get-block-hash`
@@ -568,7 +584,6 @@ Arguments:
568584

569585
Options:
570586
-h, --help Print help
571-
572587
```
573588

574589
### `aurora-cli get-balance`
@@ -867,7 +882,7 @@ Options:
867882
--value <VALUE> Attached value in EVM transaction
868883
--from <FROM> From account_id
869884
-h, --help Print help
870-
```
885+
```
871886

872887
### `aurora-cli submit`
873888

@@ -1202,7 +1217,7 @@ Usage: aurora-cli set-eth-connector-contract-account [OPTIONS] --account-id <ACC
12021217
Options:
12031218
--account-id <ACCOUNT_ID> Account id of eth connector
12041219
--withdraw-ser <WITHDRAW_SER> Serialization type in withdraw method
1205-
-h, --help
1220+
-h, --help
12061221
```
12071222

12081223
### `aurora-cli get-eth-connector-contract-account`
@@ -1290,4 +1305,4 @@ Options:
12901305
--full-access-pub-key <FULL_ACCESS_PUB_KEY>
12911306
--function-call-pub-key <FUNCTION_CALL_PUB_KEY>
12921307
-h, --help Print help
1293-
```
1308+
```

cli/src/cli/simple/command/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ pub async fn get_erc20_metadata(context: Context, identifier: String) -> anyhow:
763763
let result = context
764764
.client
765765
.near()
766-
.view_call("get_erc20_metadata", args)
766+
.view_call("get_erc20_metadata", args, context.block_ref)
767767
.await?;
768768
let output = serde_json::from_slice::<Erc20Metadata>(&result.result)
769769
.and_then(|metadata| serde_json::to_string_pretty(&metadata))?;
@@ -891,7 +891,7 @@ async fn get_value<T: FromCallResult + Display>(
891891
let result = context
892892
.client
893893
.near()
894-
.view_call(method_name, args.unwrap_or_default())
894+
.view_call(method_name, args.unwrap_or_default(), context.block_ref)
895895
.await?;
896896
let output = T::from_result(result)?;
897897
println!("{output}");

cli/src/cli/simple/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use aurora_engine_types::account_id::AccountId;
22
use aurora_engine_types::public_key::{KeyType, PublicKey};
33
use clap::{Parser, Subcommand, ValueEnum};
44
use near_primitives::hash::CryptoHash;
5+
use near_primitives::types::{BlockId, BlockReference};
56
use shadow_rs::shadow;
67
use std::str::FromStr;
78
use std::sync::LazyLock;
@@ -36,6 +37,9 @@ pub struct Cli {
3637
/// Path to file with NEAR account id and secret key in JSON format
3738
#[arg(long)]
3839
pub near_key_path: Option<String>,
40+
/// Block number to get data from
41+
#[arg(long)]
42+
pub block_number: Option<u64>,
3943
#[clap(subcommand)]
4044
pub command: Command,
4145
}
@@ -465,7 +469,12 @@ pub async fn run(args: Cli) -> anyhow::Result<()> {
465469
Network::Localnet => super::NEAR_LOCAL_ENDPOINT,
466470
};
467471
let client = crate::client::Client::new(near_rpc, &args.engine, args.near_key_path);
468-
let context = crate::client::Context::new(client, args.output_format);
472+
let context = crate::client::Context::new(
473+
client,
474+
args.output_format,
475+
args.block_number
476+
.map(|n| BlockReference::BlockId(BlockId::Height(n))),
477+
);
469478

470479
match args.command {
471480
Command::GetChainId => command::get_chain_id(context).await?,

cli/src/client/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use aurora_engine_types::H256;
44
use aurora_engine_types::account_id::AccountId;
55
#[cfg(feature = "advanced")]
66
use aurora_engine_types::parameters::engine::SubmitResult;
7+
#[cfg(feature = "simple")]
8+
use near_primitives::types::BlockReference;
79
#[cfg(feature = "advanced")]
810
use thiserror::Error;
911

@@ -30,14 +32,20 @@ type NearCallError = near_jsonrpc_client::errors::JsonRpcError<
3032
pub struct Context {
3133
pub client: Client,
3234
pub output_format: OutputFormat,
35+
pub block_ref: Option<BlockReference>,
3336
}
3437

3538
#[cfg(feature = "simple")]
3639
impl Context {
37-
pub const fn new(client: Client, output_format: OutputFormat) -> Self {
40+
pub const fn new(
41+
client: Client,
42+
output_format: OutputFormat,
43+
block_ref: Option<BlockReference>,
44+
) -> Self {
3845
Self {
3946
client,
4047
output_format,
48+
block_ref,
4149
}
4250
}
4351
}

cli/src/client/near.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl NearClient {
160160
amount: amount.to_bytes(),
161161
input,
162162
};
163-
let result = self.view_call("view", borsh::to_vec(&args)?).await?;
163+
let result = self.view_call("view", borsh::to_vec(&args)?, None).await?;
164164
let status = TransactionStatus::try_from_slice(&result.result)?;
165165
Ok(status)
166166
}
@@ -169,9 +169,10 @@ impl NearClient {
169169
&self,
170170
method_name: &str,
171171
args: Vec<u8>,
172+
block_ref: Option<BlockReference>,
172173
) -> anyhow::Result<views::CallResult> {
173174
let request = methods::query::RpcQueryRequest {
174-
block_reference: Finality::Final.into(),
175+
block_reference: block_ref.unwrap_or_else(|| Finality::Final.into()),
175176
request: views::QueryRequest::CallFunction {
176177
account_id: self.engine_account_id.clone(),
177178
method_name: method_name.to_string(),
@@ -450,7 +451,7 @@ impl NearClient {
450451
let sender_address = utils::address_from_secret_key(sk)?;
451452
let nonce = {
452453
let result = self
453-
.view_call("get_nonce", sender_address.as_bytes().to_vec())
454+
.view_call("get_nonce", sender_address.as_bytes().to_vec(), None)
454455
.await?;
455456
U256::from_big_endian(&result.result)
456457
};
@@ -464,7 +465,7 @@ impl NearClient {
464465
};
465466
let chain_id = {
466467
let result = self
467-
.view_call("get_chain_id", sender_address.as_bytes().to_vec())
468+
.view_call("get_chain_id", sender_address.as_bytes().to_vec(), None)
468469
.await?;
469470
U256::from_big_endian(&result.result).low_u64()
470471
};

0 commit comments

Comments
 (0)