Skip to content

Commit

Permalink
feat: added a possibility to manage erc20 metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksuss committed Nov 9, 2023
1 parent 572b0fa commit 8cec0d1
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 14 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ simple = []
advanced = []

[dependencies]
aurora-engine-precompiles = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.3.0", features = ["std"] }
aurora-engine-sdk = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.3.0", features = ["std"] }
aurora-engine-transactions = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.3.0", features = ["std"] }
aurora-engine-types = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.3.0", features = ["std", "impl-serde"] }
aurora-engine-precompiles = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.3.1", features = ["std"] }
aurora-engine-sdk = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.3.1", features = ["std"] }
aurora-engine-transactions = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.3.1", features = ["std"] }
aurora-engine-types = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.3.1", features = ["std", "impl-serde"] }

anyhow = "1"
bs58 = "0.5"
Expand Down
150 changes: 149 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,15 @@ Example JSON batch file (`batch_list.json`):
- [`aurora-cli add-entry-to-whitelist`](#aurora-cli-add-entry-to-whitelist)
- [`aurora-cli add-entry-to-whitelist-batch`](#aurora-cli-add-entry-to-whitelist-batch)
- [`aurora-cli remove-entry-from-whitelist`](#aurora-cli-remove-entry-from-whitelist)

- [`aurora-cli set-key-manager`](#aurora-cli-set-key-manager)
- [`aurora-cli add-relayer-key`](#aurora-cli-add-relayer-key)
- [`aurora-cli remove-relayer-key`](#aurora-cli-remove-relayer-key)
- [`aurora-cli get-upgrade-delay-blocks`](#aurora-cli-get-upgrade-delay-blocks)
- [`aurora-cli set-upgrade-delay-blocks`](#aurora-cli-set-upgrade-delay-blocks)
- [`aurora-cli get-erc20-from-nep141`](#aurora-cli-get-erc20-from-nep141)
- [`aurora-cli get-nep141-from-erc20`](#aurora-cli-get-nep141-from-erc20)
- [`aurora-cli get-erc20-metadata`](#aurora-cli-get-erc20-metadata)
- [`aurora-cli set-erc20-metadata`](#aurora-cli-set-erc20-metadata)

### `aurora-cli help`

Expand Down Expand Up @@ -331,6 +339,15 @@ Commands:
add-entry-to-whitelist Add entry into the whitelist
add-entry-to-whitelist-batch Add entries into the whitelist
remove-entry-from-whitelist Remove the entry from the whitelist
set-key-manager Set relayer key manager
add-relayer-key Add relayer public key
remove-relayer-key Remove relayer public key
get-upgrade-delay-blocks Get delay for upgrade in blocks
set-upgrade-delay-blocks Set delay for upgrade in blocks
get-erc20-from-nep141 Get ERC-20 from NEP-141
get-nep141-from-erc20 Get NEP-141 from ERC-20
get-erc20-metadata Get ERC-20 metadata
set-erc20-metadata Set ERC-20 metadata
help Print this message or the help of the given subcommand(s)

Options:
Expand Down Expand Up @@ -849,3 +866,134 @@ Options:
-h, --help Print help
```

### `aurora-cli set-key-manager`

```console
$ aurora-cli help set-key-manager
Set relayer key manager

Usage: aurora-cli set-key-manager [ACCOUNT_ID]

Arguments:
[ACCOUNT_ID] AccountId of the key manager

Options:
-h, --help Print help
```

### `aurora-cli add-relayer-key`

```console
$ aurora-cli help add-relayer-key
Add relayer public key

Usage: aurora-cli add-relayer-key --public-key <PUBLIC_KEY> --allowance <ALLOWANCE>

Options:
--public-key <PUBLIC_KEY> Public key
--allowance <ALLOWANCE> Allowance
-h, --help Print help
```

### `aurora-cli remove-relayer-key`

```console
$ aurora-cli help remove-relayer-key
Remove relayer public key

Usage: aurora-cli remove-relayer-key <PUBLIC_KEY>

Arguments:
<PUBLIC_KEY> Public key

Options:
-h, --help Print help
```

### `aurora-cli get-upgrade-delay-blocks`

```console
$ aurora-cli help get-upgrade-delay-blocks
Get delay for upgrade in blocks

Usage: aurora-cli get-upgrade-delay-blocks

Options:
-h, --help Print help
```

### `aurora-cli set-upgrade-delay-blocks`

```console
$ aurora-cli help set-upgrade-delay-blocks
Set delay for upgrade in blocks

Usage: aurora-cli set-upgrade-delay-blocks <BLOCKS>

Arguments:
<BLOCKS> Number blocks

Options:
-h, --help Print help
```

### `aurora-cli get-erc20-from-nep141`

```console
$ aurora-cli help get-erc20-from-nep141
Get ERC-20 from NEP-141

Usage: aurora-cli get-erc20-from-nep141 <ACCOUNT_ID>

Arguments:
<ACCOUNT_ID> Account id of NEP-141

Options:
-h, --help Print help
```

### `aurora-cli get-nep141-from-erc20`

```console
$ aurora-cli help get-nep141-from-erc20
Get NEP-141 from ERC-20

Usage: aurora-cli get-nep141-from-erc20 <ADDRESS>

Arguments:
<ADDRESS> Address for ERC-20

Options:
-h, --help Print help
```

### `aurora-cli get-erc20-metadata`

```console
$ aurora-cli help get-erc20-metadata
Get ERC-20 metadata

Usage: aurora-cli get-erc20-metadata <ERC20_ID>

Arguments:
<ERC20_ID> Address or account id of the ERC-20 contract

Options:
-h, --help Print help
```

### `aurora-cli set-erc20-metadata`

```console
$ aurora-cli help set-erc20-metadata
Set ERC-20 metadata

Usage: aurora-cli set-erc20-metadata --erc20-id <ERC20_ID> --name <NAME> --symbol <SYMBOL> --decimals <DECIMALS>

Options:
--erc20-id <ERC20_ID> Address or account id of the ERC-20 contract
--name <NAME> Name of the token
--symbol <SYMBOL> Symbol of the token
--decimals <DECIMALS> Decimals of the token
-h, --help Print help
```
75 changes: 72 additions & 3 deletions src/cli/simple/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::fmt::{Display, Formatter};
use std::{path::Path, str::FromStr};

use aurora_engine_sdk::types::near_account_to_evm_address;
use aurora_engine_types::account_id::AccountId;
use aurora_engine_types::borsh::{self, BorshDeserialize, BorshSerialize};
use aurora_engine_types::parameters::connector::InitCallArgs;
use aurora_engine_types::parameters::connector::{
Erc20Identifier, Erc20Metadata, InitCallArgs, SetErc20MetadataArgs,
};
use aurora_engine_types::parameters::engine::{
GetStorageAtArgs, NewCallArgs, NewCallArgsV2, PausePrecompilesCallArgs, RelayerKeyArgs,
RelayerKeyManagerArgs, SetOwnerArgs, SetUpgradeDelayBlocksArgs, SubmitResult,
Expand All @@ -12,8 +17,6 @@ use aurora_engine_types::types::Address;
use aurora_engine_types::{types::Wei, H256, U256};
use near_primitives::views::{CallResult, FinalExecutionStatus};
use serde_json::Value;
use std::fmt::{Display, Formatter};
use std::{path::Path, str::FromStr};

use crate::{
client::Client,
Expand Down Expand Up @@ -578,6 +581,58 @@ pub async fn set_upgrade_delay_blocks(client: Client, blocks: u64) -> anyhow::Re
.await
}

/// Get ERC-20 address from account id of NEP-141.
pub async fn get_erc20_from_nep141(client: Client, account_id: String) -> anyhow::Result<()> {
let args = account_id.try_to_vec()?;
get_value::<HexString>(client, "get_erc20_from_nep141", Some(args)).await
}

/// Get NEP-141 account id from address of ERC-20.
pub async fn get_nep141_from_erc20(client: Client, address: String) -> anyhow::Result<()> {
let args = hex_to_address(&address)?.as_bytes().to_vec();
get_value::<AccountId>(client, "get_nep141_from_erc20", Some(args)).await
}

/// Get a metadata of ERC-20 contract.
pub async fn get_erc20_metadata(client: Client, identifier: String) -> anyhow::Result<()> {
let args = str_to_identifier(&identifier)
.and_then(|id| serde_json::to_vec(&id).map_err(Into::into))?;
let result = client.near().view_call("get_erc20_metadata", args).await?;
let output = serde_json::from_slice::<Erc20Metadata>(&result.result)
.and_then(|metadata| serde_json::to_string_pretty(&metadata))?;

println!("{output}");

Ok(())
}

/// Set a metadata of ERC-20 contract.
pub async fn set_erc20_metadata(
client: Client,
identifier: String,
name: String,
symbol: String,
decimals: u8,
) -> anyhow::Result<()> {
let erc20_identifier = str_to_identifier(&identifier)?;
let args = serde_json::to_vec(&SetErc20MetadataArgs {
erc20_identifier,
metadata: Erc20Metadata {
name,
symbol,
decimals,
},
})?;

contract_call!(
"set_erc20_metadata",
"ERC-20 metadata has been set successfully",
"Error while setting ERC-20 metadata"
)
.proceed(client, args)
.await
}

async fn get_value<T: FromCallResult + Display>(
client: Client,
method_name: &str,
Expand Down Expand Up @@ -607,6 +662,14 @@ fn to_account_id(id: Option<String>, client: &Client) -> anyhow::Result<AccountI
)
}

fn str_to_identifier(id: &str) -> anyhow::Result<Erc20Identifier> {
hex_to_address(&id).map(Into::into).or_else(|_| {

Check failure on line 666 in src/cli/simple/command/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> src/cli/simple/command/mod.rs:666:20 | 666 | hex_to_address(&id).map(Into::into).or_else(|_| { | ^^^ help: change this to: `id` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-D clippy::needless-borrow` implied by `-D warnings`

Check failure on line 666 in src/cli/simple/command/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> src/cli/simple/command/mod.rs:666:20 | 666 | hex_to_address(&id).map(Into::into).or_else(|_| { | ^^^ help: change this to: `id` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-D clippy::needless-borrow` implied by `-D warnings`
id.parse::<AccountId>()
.map(Into::into)
.map_err(|e| anyhow::anyhow!("{e}"))
})
}

struct HexString(String);

trait FromCallResult {
Expand Down Expand Up @@ -656,6 +719,12 @@ impl FromCallResult for HexString {
}
}

impl FromCallResult for AccountId {
fn from_result(result: CallResult) -> anyhow::Result<Self> {
Self::try_from(result.result).map_err(|e| anyhow::anyhow!("{e}"))
}
}

impl Display for HexString {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{}", self.0)
Expand Down
Loading

0 comments on commit 8cec0d1

Please sign in to comment.