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 9, 2023
1 parent 317947d commit 6ada07b
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 40 deletions.
26 changes: 22 additions & 4 deletions res/mock_evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ impl MockEvmContract {
}
}

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 All @@ -70,4 +66,26 @@ impl MockEvmContract {
pub fn register_relayer(&mut self, #[serializer(borsh)] input: Raw) {
assert_eq!(input.0.len(), 20);
}

//
// SELF CALL METHODS
//

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

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

//
// CALLBACK HANDLER METHODS
//

#[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
}
}
94 changes: 70 additions & 24 deletions workspace/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
use crate::operation::{
Call, CallDeployCode, CallDeployErc20, CallEvm, CallFtOnTransfer, CallFtTransfer,
CallFtTransferCall, CallRegisterRelayer, CallStorageDeposit, CallStorageUnregister,
CallStorageWithdraw, CallSubmit, View, ViewResultDetails,
Call, CallDeployCode, CallDeployErc20, CallEvm, CallFactoryUpdateAddressVersion,
CallFtOnTransfer, 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, StorageBalance, StorageDepositCallArgs,
StorageWithdrawCallArgs, TransactionStatus, TransferCallArgs, TransferCallCallArgs,
};
use aurora_engine::parameters::PauseEthConnectorCallArgs;
use aurora_engine::{fungible_token::FungibleTokenMetadata, parameters::ViewCallArgs};
use aurora_engine::{
parameters::{
GetStorageAtArgs, SetContractDataCallArgs, StorageBalance, StorageDepositCallArgs,
StorageWithdrawCallArgs, TransactionStatus, TransferCallArgs, TransferCallCallArgs,
},
xcc::AddressVersionUpdateArgs,
};
use aurora_workspace_types::input::IsUsedProofCallArgs;
use aurora_workspace_types::input::ProofInput;
#[cfg(feature = "deposit-withdraw")]
Expand Down Expand Up @@ -185,6 +190,64 @@ impl<U> EvmAccount<U> {
self.account.id()
}

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 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 @@ -669,23 +732,6 @@ impl EvmContract {
.await?
.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(
eth_prover_config.account_id.as_str(),
)
.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()?;
}

Ok(())
}

Expand Down
30 changes: 18 additions & 12 deletions workspace/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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 @@ -33,7 +33,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 @@ -54,7 +54,19 @@ impl_call_return![
(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),
(
CallFactoryUpdateAddressVersion,
ExecutionSuccess<u8>,
try_from_borsh
),
(CallRefundOnError, ExecutionSuccess<u8>, try_from_borsh),
];

#[cfg(feature = "deposit-withdraw")]
Expand Down Expand Up @@ -312,10 +324,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 @@ -391,25 +403,19 @@ impl AsRef<str> for OwnerCall {

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SelfCall {
NewEthConnector,
SetEthConnectorContractData,
FactoryUpdateAddressVersion,
RefundOnError,
FinishDeposit,
FtResolveTransfer,
SetPausedFlags,
}

impl AsRef<str> for SelfCall {
fn as_ref(&self) -> &str {
use SelfCall::*;
match self {
NewEthConnector => "new_eth_connector",
SetEthConnectorContractData => "set_eth_connector_contract_data",
FactoryUpdateAddressVersion => "factory_update_address_version",
RefundOnError => "refund_on_error",
FinishDeposit => "finish_deposit",
FtResolveTransfer => "resolve_transfer",
SetPausedFlags => "set_paused_flags",
}
}
Expand Down
52 changes: 52 additions & 0 deletions workspace/tests/self_call_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use aurora_engine::fungible_token::FungibleTokenMetadata;
use aurora_workspace_types::Address;

mod common;

#[tokio::test]
async fn test_set_eth_connector_contract_data() {
let contract = common::init_and_deploy_contract().await.unwrap();

contract
.as_account()
.set_eth_connector_contract_data(
"prover.test.near",
"custodian.test.near",
FungibleTokenMetadata::default(),
)
.transact()
.await
.unwrap();
}

#[tokio::test]
async fn test_factory_update_address_version() {
let contract = common::init_and_deploy_contract().await.unwrap();

let res = contract
.as_account()
.factory_update_address_version(Address::default(), 0)
.transact()
.await
.unwrap()
.into_value();

let expected = 0;
assert_eq!(expected, res);
}

#[tokio::test]
async fn test_refund_on_error() {
let contract = common::init_and_deploy_contract().await.unwrap();

let res = contract
.as_account()
.refund_on_error(Address::default(), Some(Address::default()), 0.into())
.transact()
.await
.unwrap()
.into_value();

let expected = 0;
assert_eq!(expected, res);
}

0 comments on commit 6ada07b

Please sign in to comment.