Skip to content

Commit

Permalink
feat: add missing self call methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vimpunk committed Mar 7, 2023
1 parent f507a1c commit 460e0a4
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 48 deletions.
4 changes: 4 additions & 0 deletions res/mock_evm/src/ft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ impl MockEvmContract {
pub fn ft_on_transfer(&mut self, sender_id: AccountId, amount: u128, msg: String) -> String {
serde_json::to_string(&0).expect("Failed to serialize message")
}

pub fn ft_total_eth_supply_on_aurora(&self) -> String {
"0".into()
}
}
22 changes: 7 additions & 15 deletions res/mock_evm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::out::SubmitResult;
use aurora_workspace_types::input::{CallInput, DeployErc20Input, SetEthConnectorInput};
use aurora_workspace_types::input::{CallInput, DeployErc20Input, NewInput, SetEthConnectorInput};
use aurora_workspace_types::output::{Log, TransactionStatus};
use aurora_workspace_types::{AccountId, Address, Raw, H256};
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
Expand All @@ -8,6 +8,7 @@ use near_sdk::{near_bindgen, PanicOnDefault};
pub mod ft;
mod metadata;
mod out;
mod selfcall;
mod storage;

fn dummy_submit_result() -> SubmitResult {
Expand All @@ -32,25 +33,16 @@ pub struct MockEvmContract {
#[near_bindgen]
impl MockEvmContract {
#[init]
pub fn new(
chain_id: [u8; 32],
owner_id: AccountId,
bridge_prover_id: AccountId,
upgrade_delay_blocks: u64,
) -> MockEvmContract {
pub fn new(#[serializer(borsh)] input: NewInput) -> MockEvmContract {
MockEvmContract {
chain_id,
owner_id,
bridge_prover_id,
upgrade_delay_blocks,
chain_id: input.chain_id,
owner_id: input.owner_id,
bridge_prover_id: input.bridge_prover_id,
upgrade_delay_blocks: input.upgrade_delay_blocks,
eth_connector: None,
}
}

pub fn new_eth_connector(&mut self, #[serializer(borsh)] input: SetEthConnectorInput) {
self.eth_connector = Some(input);
}

#[result_serializer(borsh)]
pub fn deploy_code(&mut self, #[serializer(borsh)] _input: Raw) -> SubmitResult {
dummy_submit_result()
Expand Down
6 changes: 0 additions & 6 deletions res/mock_evm/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ascii;

use aurora_workspace_types::{output::TransactionStatus, AccountId, Raw};
use near_sdk::{borsh, near_bindgen};

Expand All @@ -11,10 +9,6 @@ impl MockEvmContract {
"v1".to_string()
}

pub fn ft_total_eth_supply_on_aurora(&self) -> String {
"0".into()
}

#[result_serializer(borsh)]
pub fn get_view(&self, #[serializer(borsh)] _input: Raw) -> TransactionStatus {
TransactionStatus::Succeed(vec![])
Expand Down
38 changes: 38 additions & 0 deletions res/mock_evm/src/selfcall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use aurora_workspace_types::input::SetEthConnectorInput;
use aurora_workspace_types::Raw;
use near_sdk::{borsh, near_bindgen};
use near_sdk::{json_types::U128, serde_json};

use crate::{MockEvmContract, MockEvmContractExt};

#[near_bindgen]
impl MockEvmContract {
pub fn new_eth_connector(&mut self, #[serializer(borsh)] input: SetEthConnectorInput) {
self.eth_connector = Some(input);
}

pub fn ft_resolve_transfer(&mut self, #[serializer(borsh)] _input: Raw) -> String {
serde_json::to_string(&U128::from(0)).expect("Failed to serialize message")
}

pub fn set_eth_connector_contract_data(&mut self, #[serializer(borsh)] _input: Raw) {}

pub fn set_paused_flags(&mut self, #[serializer(borsh)] _input: Raw) {}

// Callbacks:

#[result_serializer(borsh)]
pub fn finish_deposit(&self, #[serializer(borsh)] _input: Raw) -> u8 {
0
}

#[result_serializer(borsh)]
pub fn factory_update_address_version(&mut self, #[serializer(borsh)] _input: Raw) -> u8 {
0
}

#[result_serializer(borsh)]
pub fn refund_on_error(&mut self, #[serializer(borsh)] _input: Raw) -> u8 {
0
}
}
162 changes: 142 additions & 20 deletions workspace/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use crate::operation::{
Call, CallDeployCode, CallDeployErc20, CallEvm, CallFtOnTransfer, CallFtTransfer,
CallFtTransferCall, CallRegisterRelayer, CallStorageDeposit, CallStorageUnregister,
CallStorageWithdraw, CallSubmit, View, ViewResultDetails,
Call, CallDeployCode, CallDeployErc20, CallEvm, CallFactoryUpdateAddressVersion,
CallFinishDeposit, CallFtOnTransfer, CallFtResolveTransfer, CallFtTransfer, CallFtTransferCall,
CallRefundOnError, CallRegisterRelayer, CallSetEthConnectorContractData, CallSetPausedFlags,
CallStorageDeposit, CallStorageUnregister, CallStorageWithdraw, CallSubmit, SelfCall, View,
ViewResultDetails,
};
#[cfg(feature = "deposit-withdraw")]
use crate::operation::{CallDeposit, CallWithdraw};
use crate::{EvmCallTransaction, Result};
use aurora_engine::parameters::{
GetStorageAtArgs, InitCallArgs, IsUsedProofCallArgs, StorageBalance, StorageDepositCallArgs,
StorageWithdrawCallArgs, TransactionStatus, TransferCallArgs, TransferCallCallArgs,
};
use aurora_engine::proof::Proof;
use aurora_engine::{fungible_token::FungibleTokenMetadata, parameters::ViewCallArgs};
use aurora_engine::{parameters::PauseEthConnectorCallArgs, proof::Proof};
use aurora_engine::{
parameters::{
FinishDepositCallArgs, GetStorageAtArgs, InitCallArgs, IsUsedProofCallArgs,
ResolveTransferCallArgs, SetContractDataCallArgs, StorageBalance, StorageDepositCallArgs,
StorageWithdrawCallArgs, TransactionStatus, TransferCallArgs, TransferCallCallArgs,
},
xcc::AddressVersionUpdateArgs,
};
use aurora_workspace_types::input::{CallInput, DeployErc20Input, FtOnTransferInput};
#[cfg(feature = "deposit-withdraw")]
use aurora_workspace_types::input::{ProofInput, WithdrawInput};
Expand Down Expand Up @@ -183,6 +189,110 @@ impl<U> EvmAccount<U> {
self.account.id()
}

pub async fn new_eth_connector(
&self,
prover_account: impl AsRef<str>,
eth_custodian_address: impl Into<String>,
metadata: FungibleTokenMetadata,
) -> Result<()> {
let args = InitCallArgs {
prover_account: aurora_engine_types::account_id::AccountId::from_str(
prover_account.as_ref(),
)
.unwrap(),
eth_custodian_address: eth_custodian_address.into(),
metadata,
};
self.near_call(&SelfCall::NewEthConnector)
.args_borsh(args)
.transact()
.await?
.into_result()?;
Ok(())
}

pub fn set_eth_connector_contract_data(
&self,
prover_account: impl AsRef<str>,
eth_custodian_address: impl Into<String>,
metadata: FungibleTokenMetadata,
) -> CallSetEthConnectorContractData<'_> {
let args = SetContractDataCallArgs {
prover_account: aurora_engine_types::account_id::AccountId::new(
prover_account.as_ref(),
)
.unwrap(),
eth_custodian_address: eth_custodian_address.into(),
metadata,
};
CallSetEthConnectorContractData(
self.near_call(&SelfCall::SetEthConnectorContractData)
.args_borsh(args),
)
}

pub fn set_paused_flags(&self, paused_mask: u8) -> CallSetPausedFlags<'_> {
let args = PauseEthConnectorCallArgs { paused_mask };
CallSetPausedFlags(self.near_call(&SelfCall::SetPausedFlags).args_borsh(args))
}

pub fn finish_deposit(
&self,
new_owner_id: impl AsRef<str>,
amount: u128,
proof_key: impl Into<String>,
relayer_id: impl AsRef<str>,
fee: u128,
msg: Option<Vec<u8>>,
) -> CallFinishDeposit<'_> {
let args = FinishDepositCallArgs {
// TODO: handle errors. maybe we should just accept AccountId and let the user deal with
// error handling? seems to make more sense
new_owner_id: aurora_engine_types::account_id::AccountId::new(new_owner_id.as_ref())
.unwrap(),
amount: aurora_engine_types::types::NEP141Wei::new(amount),
proof_key: proof_key.into(),
relayer_id: aurora_engine_types::account_id::AccountId::new(relayer_id.as_ref())
.unwrap(),
fee: fee.into(),
msg,
};
CallFinishDeposit(self.near_call(&SelfCall::FinishDeposit).args_borsh(args))
}

pub fn factory_update_address_version(
&self,
address: impl Into<Address>,
version: u32,
) -> CallFactoryUpdateAddressVersion<'_> {
let args = AddressVersionUpdateArgs {
address: aurora_engine_types::types::Address::new(address.into()),
version: aurora_engine::xcc::CodeVersion(version),
};
CallFactoryUpdateAddressVersion(
self.near_call(&SelfCall::FactoryUpdateAddressVersion)
.args_borsh(args),
)
}

pub fn refund_on_error<A: Into<Address>>(
&self,
recipient_address: A,
erc20_address: Option<A>,
amount: U256,
) -> CallRefundOnError<'_> {
let mut raw_amount: aurora_engine_types::types::RawU256 = Default::default();
amount.to_big_endian(&mut raw_amount);
let args = aurora_engine_types::parameters::RefundCallArgs {
recipient_address: aurora_engine_types::types::Address::new(recipient_address.into()),
erc20_address: erc20_address
.map(Into::into)
.map(aurora_engine_types::types::Address::new),
amount: raw_amount,
};
CallRefundOnError(self.near_call(&SelfCall::RefundOnError).args_borsh(args))
}

/// Deploys contract code using the caller's NEAR account ID as an Ethereum address.
///
/// The logic which creates the ETH address is as follows:
Expand Down Expand Up @@ -292,6 +402,25 @@ impl<U> EvmAccount<U> {
CallFtTransferCall(self.near_call(&Call::FtTransferCall).args_json(args))
}

pub fn ft_resolve_transfer(
&self,
sender_id: impl AsRef<str>,
amount: u128,
receiver_id: impl AsRef<str>,
) -> CallFtResolveTransfer<'_> {
let args = ResolveTransferCallArgs {
// TODO: impl error
sender_id: aurora_engine_types::account_id::AccountId::new(sender_id.as_ref()).unwrap(),
amount: aurora_engine_types::types::NEP141Wei::new(amount),
receiver_id: aurora_engine_types::account_id::AccountId::new(receiver_id.as_ref())
.unwrap(),
};
CallFtResolveTransfer(
self.near_call(&SelfCall::FtResolveTransfer)
.args_borsh(args),
)
}

// TODO we are not NEP-145 compliant
pub fn storage_deposit<A: AsRef<str>>(
&self,
Expand Down Expand Up @@ -684,20 +813,13 @@ impl EvmContract {
.into_result()?;

if let Some(eth_prover_config) = init_config.eth_prover_config {
let new_eth_connector_args = InitCallArgs {
prover_account: aurora_engine_types::account_id::AccountId::from_str(
self.contract
.new_eth_connector(
eth_prover_config.account_id.as_str(),
eth_prover_config.evm_custodian_address,
FungibleTokenMetadata::default(),
)
.unwrap(),
eth_custodian_address: eth_prover_config.evm_custodian_address,
metadata: FungibleTokenMetadata::default(),
};
self.contract
.near_call("new_eth_connector")
.args_borsh(new_eth_connector_args)
.transact()
.await?
.into_result()?;
.await?;
}

Ok(())
Expand Down
32 changes: 25 additions & 7 deletions workspace/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use workspaces::operations::CallTransaction;
use workspaces::result::ExecutionFinalResult;

macro_rules! impl_call_return {
($(($name:ident, $return:ty, $fun:ident)),*) => {
( $( ($name:ident, $return:ty, $deser_fn:ident)),* $(,)? ) => {
$(pub struct $name<'a>(pub(crate) EvmCallTransaction<'a>);

impl<'a> $name<'a> {
Expand All @@ -31,7 +31,7 @@ macro_rules! impl_call_return {
}

pub async fn transact(self) -> Result<$return> {
ExecutionSuccess::$fun(self.0.transact().await?)
ExecutionSuccess::$deser_fn(self.0.transact().await?)
}
})*
}
Expand All @@ -49,10 +49,28 @@ impl_call_return![
(CallRegisterRelayer, ExecutionSuccess<()>, try_from),
(CallFtOnTransfer, ExecutionSuccess<String>, try_from_json),
(CallFtTransfer, ExecutionSuccess<()>, try_from),
(
CallFtResolveTransfer,
ExecutionSuccess<String>,
try_from_json
),
(CallFtTransferCall, ExecutionSuccess<PromiseId>, try_from),
(CallStorageDeposit, ExecutionSuccess<()>, try_from),
(CallStorageUnregister, ExecutionSuccess<()>, try_from),
(CallStorageWithdraw, ExecutionSuccess<()>, try_from)
(CallStorageWithdraw, ExecutionSuccess<()>, try_from),
(
CallSetEthConnectorContractData,
ExecutionSuccess<()>,
try_from_borsh
),
(CallSetPausedFlags, ExecutionSuccess<()>, try_from_borsh),
(CallFinishDeposit, ExecutionSuccess<u8>, try_from_borsh),
(
CallFactoryUpdateAddressVersion,
ExecutionSuccess<u8>,
try_from_borsh
),
(CallRefundOnError, ExecutionSuccess<u8>, try_from_borsh),
];

#[cfg(feature = "deposit-withdraw")]
Expand Down Expand Up @@ -308,10 +326,10 @@ pub enum View {
EthTotalSupply,
FtMetadata,
StorageBalanceOf,
PausedFlags, // TODO
PausedFlags,
AccountsCounter, // TODO
Erc20FromNep141, // TODO
Nep141FromErc20, // TODO
Erc20FromNep141,
Nep141FromErc20,
}

impl AsRef<str> for View {
Expand Down Expand Up @@ -405,7 +423,7 @@ impl AsRef<str> for SelfCall {
FactoryUpdateAddressVersion => "factory_update_address_version",
RefundOnError => "refund_on_error",
FinishDeposit => "finish_deposit",
FtResolveTransfer => "resolve_transfer",
FtResolveTransfer => "ft_resolve_transfer",
SetPausedFlags => "set_paused_flags",
}
}
Expand Down
1 change: 1 addition & 0 deletions workspace/tests/ft_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use aurora_engine::parameters::TransferCallArgs;
use aurora_workspace_types::AccountId;
use std::str::FromStr;

Expand Down
Loading

0 comments on commit 460e0a4

Please sign in to comment.