Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct EthConnectorContract {
contract: EthConnector,
ft: FungibleToken,
paused_mask: PausedMask,
metadata: FungibleTokenMetadata,
}

/// eth-connector specific data
Expand Down Expand Up @@ -58,6 +59,7 @@ impl EthConnectorContract {
contract: Self::get_contract_data(&EthConnectorStorageId::Contract),
ft: Self::get_contract_data(&EthConnectorStorageId::FungibleToken),
paused_mask: Self::get_contract_data(&EthConnectorStorageId::PausedMask),
metadata: Self::get_contract_data(&EthConnectorStorageId::FungibleTokenMetadata),
Comment thread
mrLSD marked this conversation as resolved.
Outdated
}
}

Expand All @@ -82,6 +84,7 @@ impl EthConnectorContract {
let contract_data = Self::set_contract_data(SetContractDataCallArgs {
prover_account: args.prover_account,
eth_custodian_address: args.eth_custodian_address,
metadata: args.metadata.clone(),
});

let current_account_id = sdk::current_account_id();
Expand All @@ -90,6 +93,8 @@ impl EthConnectorContract {
// Register FT account for current contract
ft.internal_register_account(&owner_id);

let metadata = args.metadata;

let paused_mask = UNPAUSE_ALL;
sdk::save_contract(
&Self::get_contract_key(&EthConnectorStorageId::PausedMask),
Expand All @@ -100,6 +105,7 @@ impl EthConnectorContract {
contract: contract_data,
ft,
paused_mask,
metadata,
}
.save_ft_contract();
}
Expand Down Expand Up @@ -583,6 +589,10 @@ impl EthConnectorContract {
&Self::get_contract_key(&EthConnectorStorageId::FungibleToken),
&self.ft,
);
sdk::save_contract(
&Self::get_contract_key(&EthConnectorStorageId::FungibleTokenMetadata),
&self.metadata,
);
}

/// Generate key for used events from Prood
Expand Down Expand Up @@ -616,6 +626,37 @@ impl EthConnectorContract {
pub fn set_paused_flags(&mut self, args: PauseEthConnectorCallArgs) {
self.set_paused(args.paused_mask);
}

/// Return metdata
pub fn get_metadata(&self) {
let icon = if let Some(ref icon) = self.metadata.icon {
format!(r#""{}""#, icon)
} else {
"null".to_string()
};
Comment thread
birchmd marked this conversation as resolved.
Outdated
let reference = if let Some(ref reference) = self.metadata.reference {
format!(r#""{}""#, reference)
} else {
"null".to_string()
};
let reference_hash = if let Some(ref reference_hash) = self.metadata.reference_hash {
format!("{:?}", reference_hash)
Comment thread
birchmd marked this conversation as resolved.
Outdated
} else {
"null".to_string()
};
let json_data = format!(
r#"{{"spec": "{}", "name": "{}", "symbol": "{}", "icon": {}, "reference": {}, "reference_hash": {}, "decimals": {:?}}}"#,
self.metadata.spec,
self.metadata.name,
self.metadata.symbol,
icon,
reference,
reference_hash,
self.metadata.decimals,
);
// Return JSON
sdk::return_output(&json_data.as_bytes());
}
}

impl AdminControlled for EthConnectorContract {
Expand Down
11 changes: 11 additions & 0 deletions src/fungible_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ pub struct FungibleToken {
pub account_storage_usage: StorageUsage,
}

#[derive(BorshDeserialize, BorshSerialize, Clone)]
pub struct FungibleTokenMetadata {
pub spec: String,
pub name: String,
pub symbol: String,
pub icon: Option<String>,
pub reference: Option<String>,
pub reference_hash: Option<[u8; 32]>,
pub decimals: u8,
}

impl FungibleToken {
pub fn new() -> Self {
Self::default()
Expand Down
33 changes: 30 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod contract {
use crate::json::parse_json;
use crate::prelude::{Address, ToString, TryInto, H160, H256, U256};
use crate::sdk;
use crate::storage::{bytes_to_key, KeyPrefix};
use crate::storage::{bytes_to_key, EthConnectorStorageId, KeyPrefix};
use crate::types::{
near_account_to_evm_address, u256_to_arr, SdkExpect, SdkProcess, SdkUnwrap,
ERR_FAILED_PARSE,
Expand Down Expand Up @@ -180,8 +180,30 @@ mod contract {
/// code.
#[no_mangle]
pub extern "C" fn state_migration() {
// This function is purposely left empty because we do not have any state migration
// to do.
// Only owner can call migration
sdk::assert_private_call();
Comment thread
birchmd marked this conversation as resolved.
Outdated

let metadata_key = bytes_to_key(
KeyPrefix::EthConnector,
&[EthConnectorStorageId::FungibleTokenMetadata as u8],
);

//=========================================================
// Migrate Metadata
if !sdk::storage_has_key(&metadata_key[..]) {
use crate::fungible_token::FungibleTokenMetadata;

let metadata = FungibleTokenMetadata {
spec: "ft-1.0.0".to_string(),
symbol: "ETH".to_string(),
name: "Ether".to_string(),
icon: None,
Comment thread
birchmd marked this conversation as resolved.
Outdated
reference: None,
reference_hash: None,
decimals: 18,
};
sdk::save_contract(&metadata_key, &metadata);
}
}

///
Expand Down Expand Up @@ -591,6 +613,11 @@ mod contract {
);
}

#[no_mangle]
pub extern "C" fn ft_metadata() {
EthConnectorContract::get_instance().get_metadata();
}

#[cfg(feature = "integration-test")]
#[no_mangle]
pub extern "C" fn verify_log_entry() {
Expand Down
4 changes: 3 additions & 1 deletion src/parameters.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use borsh::{BorshDeserialize, BorshSerialize};

use crate::fungible_token::FungibleTokenMetadata;
use crate::prelude::{String, Vec};
use crate::types::{AccountId, Balance, RawAddress, RawH256, RawU256};
use crate::{
Expand Down Expand Up @@ -222,7 +223,7 @@ pub struct StorageBalance {
impl StorageBalance {
pub fn to_json_bytes(&self) -> Vec<u8> {
crate::prelude::format!(
"{{\"total\": \"{}\", \"available\": \"{}\",}}",
"{{\"total\": \"{}\", \"available\": \"{}\"}}",
self.total.to_string(),
self.available.to_string()
)
Expand Down Expand Up @@ -272,6 +273,7 @@ pub struct FinishDepositEthCallArgs {
pub struct InitCallArgs {
pub prover_account: AccountId,
pub eth_custodian_address: AccountId,
pub metadata: FungibleTokenMetadata,
Comment thread
mrLSD marked this conversation as resolved.
}

/// Eth-connector Set contract data call args
Expand Down
1 change: 1 addition & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum EthConnectorStorageId {
UsedEvent = 0x2,
PausedMask = 0x3,
StatisticsAuroraAccountsCounter = 0x4,
FungibleTokenMetadata = 0x5,
}

/// We can't use const generic over Enum, but we can do it over integral type
Expand Down
3 changes: 2 additions & 1 deletion src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use primitive_types::U256;
use rlp::RlpStream;
use secp256k1::{self, Message, PublicKey, SecretKey};

use crate::fungible_token::FungibleToken;
use crate::fungible_token::{FungibleToken, FungibleTokenMetadata};
use crate::parameters::{InitCallArgs, NewCallArgs, SubmitResult};
use crate::prelude::Address;
use crate::storage;
Expand Down Expand Up @@ -368,6 +368,7 @@ pub(crate) fn deploy_evm() -> AuroraRunner {
let args = InitCallArgs {
prover_account: "prover.near".to_string(),
eth_custodian_address: "d045f7e19B2488924B97F9c145b5E51D0D895A65".to_string(),
metadata: FungibleTokenMetadata::default(),
};
let (_, maybe_error) = runner.call(
"new_eth_connector",
Expand Down
16 changes: 16 additions & 0 deletions src/tests/eth_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::admin_controlled::{PausedMask, ERR_PAUSED};
use crate::connector::{
ERR_NOT_ENOUGH_BALANCE_FOR_FEE, PAUSE_DEPOSIT, PAUSE_WITHDRAW, UNPAUSE_ALL,
};
use crate::fungible_token::FungibleTokenMetadata;
use crate::parameters::{
InitCallArgs, NewCallArgs, RegisterRelayerCallArgs, WithdrawCallArgs, WithdrawResult,
};
Expand All @@ -27,6 +28,20 @@ const EVM_CUSTODIAN_ADDRESS: &'static str = "096DE9C2B8A5B8c22cEe3289B101f6960d6
const DEPOSITED_EVM_AMOUNT: u128 = 10200;
const DEPOSITED_EVM_FEE: u128 = 200;

impl Default for FungibleTokenMetadata {
fn default() -> Self {
Self {
spec: "".to_string(),
name: "".to_string(),
symbol: "".to_string(),
icon: None,
reference: None,
reference_hash: None,
decimals: 0,
}
}
}

#[derive(BorshDeserialize, Debug)]
pub struct IsUsedProofResult {
pub is_used_proof: bool,
Expand Down Expand Up @@ -71,6 +86,7 @@ fn init_contract(
&InitCallArgs {
prover_account: PROVER_ACCOUNT.into(),
eth_custodian_address: custodian_address.into(),
metadata: FungibleTokenMetadata::default(),
}
.try_to_vec()
.unwrap(),
Expand Down