Skip to content

Commit

Permalink
Fix NEP-141 tests ft_transfer_call impl
Browse files Browse the repository at this point in the history
  • Loading branch information
mrLSD committed Mar 8, 2023
1 parent 532766c commit ef44736
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
19 changes: 14 additions & 5 deletions res/mock_evm/src/ft.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
use crate::*;
use near_sdk::json_types::U128;
use near_sdk::serde_json;
use near_sdk::PromiseOrValue;

#[near_bindgen]
impl MockEvmContract {
pub fn ft_transfer(&mut self, receiver_id: String, amount: U128, memo: Option<String>) {
assert_eq!(receiver_id, "some_account.test");
#[allow(unused_variables)]
#[payable]
pub fn ft_transfer(&mut self, receiver_id: AccountId, amount: U128, memo: Option<String>) {
use std::str::FromStr;
assert_eq!(
receiver_id,
AccountId::from_str("some_account.test").unwrap()
);
assert_eq!(amount.0, 10);
assert_eq!(memo, Some("some message".to_string()));
}

#[allow(unused_variables)]
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")
}

#[result_serializer(borsh)]
#[allow(unused_variables)]
#[payable]
pub fn ft_transfer_call(
&mut self,
receiver_id: AccountId,
amount: U128,
memo: Option<String>,
msg: String,
) -> u64 {
amount.0.try_into().unwrap()
) -> PromiseOrValue<U128> {
PromiseOrValue::Value(amount)
}
}
19 changes: 10 additions & 9 deletions workspace/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{EngineCallTransaction, Result};
use aurora_engine::parameters::ViewCallArgs;
use aurora_engine::parameters::{
GetStorageAtArgs, StorageBalance, StorageDepositCallArgs, StorageWithdrawCallArgs,
TransactionStatus, TransferCallArgs, TransferCallCallArgs,
TransactionStatus, TransferCallArgs,
};
use aurora_workspace_types::input::IsUsedProofCallArgs;
use aurora_workspace_types::input::ProofInput;
Expand All @@ -22,6 +22,7 @@ use borsh::BorshSerialize;
use ethabi::{ParamType, Token};
use near_contract_standards::fungible_token::metadata::FungibleTokenMetadata;
use near_sdk::json_types::U128;
use serde_json::json;
use std::borrow::{Borrow, BorrowMut};
use std::marker::PhantomData;
use std::path::Path;
Expand Down Expand Up @@ -284,17 +285,17 @@ impl<U: UserFunctions> EvmAccount<U> {
pub fn ft_transfer_call<R: AsRef<str>>(
&self,
receiver_id: R,
amount: u128,
amount: U128,
memo: Option<String>,
message: String,
msg: String,
) -> CallFtTransferCall<'_> {
let args = TransferCallCallArgs {
receiver_id: aurora_engine_types::account_id::AccountId::new(receiver_id.as_ref())
let args = json!( {
"receiver_id": aurora_engine_types::account_id::AccountId::new(receiver_id.as_ref())
.unwrap(),
amount: aurora_engine_types::types::NEP141Wei::new(amount),
memo,
msg: message,
};
"amount": amount,
"memo": memo,
"msg": msg,
});
CallFtTransferCall(self.near_call(&Call::FtTransferCall).args_json(args))
}

Expand Down
11 changes: 8 additions & 3 deletions workspace/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use crate::Result;
#[cfg(feature = "deposit-withdraw")]
use aurora_engine::parameters::WithdrawResult;
use aurora_engine::parameters::{StorageBalance, TransactionStatus};
use aurora_engine_sdk::promise::PromiseId;
use aurora_engine_types::types::Wei;
use aurora_workspace_types::AccountId;
use borsh::BorshDeserialize;
#[cfg(feature = "ethabi")]
use ethabi::{ParamType, Token};
use ethereum_types::{Address, H256, U256};
use near_contract_standards::fungible_token::metadata::FungibleTokenMetadata;
use near_sdk::json_types::U128;
use near_sdk::PromiseOrValue;
use workspaces::operations::CallTransaction;
use workspaces::result::ExecutionFinalResult;

Expand Down Expand Up @@ -54,9 +55,13 @@ impl_call_return![
(CallEvm, ExecutionSuccess<SubmitResult>, try_from_borsh),
(CallSubmit, ExecutionSuccess<SubmitResult>, try_from_borsh),
(CallRegisterRelayer, ExecutionSuccess<()>, try_from),
(CallFtOnTransfer, ExecutionSuccess<String>, try_from_json),
(CallFtOnTransfer, ExecutionSuccess<U128>, try_from_json),
(CallFtTransfer, ExecutionSuccess<()>, try_from),
(CallFtTransferCall, ExecutionSuccess<PromiseId>, try_from),
(
CallFtTransferCall,
ExecutionSuccess<PromiseOrValue<U128>>,
try_from
),
(CallStorageDeposit, ExecutionSuccess<()>, try_from),
(CallStorageUnregister, ExecutionSuccess<()>, try_from),
(CallStorageWithdraw, ExecutionSuccess<()>, try_from)
Expand Down
16 changes: 16 additions & 0 deletions workspace/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::Result;
use aurora_engine_sdk::promise::PromiseId;
use borsh::BorshDeserialize;
use ethereum_types::Address;
use near_sdk::json_types::U128;
use near_sdk::PromiseOrValue;
use serde::de::DeserializeOwned;
use std::borrow::Borrow;
use std::fmt::Debug;
Expand All @@ -27,6 +29,20 @@ impl TryFrom<ExecutionFinalResult> for ExecutionSuccess<PromiseId> {
}
}

impl TryFrom<ExecutionFinalResult> for ExecutionSuccess<PromiseOrValue<U128>> {
type Error = Error;

fn try_from(result: ExecutionFinalResult) -> Result<Self> {
let inner = result.into_result()?;
let value: U128 = inner.json()?;

Ok(ExecutionSuccess {
inner,
value: PromiseOrValue::Value(value),
})
}
}

impl TryFrom<ExecutionFinalResult> for ExecutionSuccess<()> {
type Error = Error;

Expand Down
21 changes: 16 additions & 5 deletions workspace/tests/ft_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use aurora_workspace_types::AccountId;
use near_sdk::json_types::U128;
use near_sdk::PromiseOrValue;
use std::str::FromStr;

mod common;
Expand All @@ -8,9 +9,11 @@ mod common;
async fn test_ft_transfer() -> anyhow::Result<()> {
let contract = common::init_and_deploy_contract().await?;

contract
let res = contract
.as_account()
.ft_transfer("some_account.test", 10, Some("some message".to_string()))
.max_gas()
.deposit(1)
.transact()
.await?;

Expand All @@ -28,11 +31,11 @@ async fn test_ft_on_transfer() -> anyhow::Result<()> {
U128::from(100),
String::new(),
)
.max_gas()
.transact()
.await?
.into_value();

assert_eq!(0u8.to_string(), res);
assert_eq!(U128::from(0), res);

Ok(())
}
Expand All @@ -41,17 +44,25 @@ async fn test_ft_on_transfer() -> anyhow::Result<()> {
async fn test_ft_transfer_call() -> anyhow::Result<()> {
let contract = common::init_and_deploy_contract().await?;

let res = contract
let res: PromiseOrValue<U128> = contract
.as_account()
.ft_transfer_call(
"receiver.test",
10,
U128::from(33),
Some("some memo".to_string()),
"some message".to_string(),
)
.max_gas()
.deposit(1)
.transact()
.await?
.into_value();

let val = match res {
PromiseOrValue::Value(v) => v,
_ => panic!("Failed parse"),
};
assert_eq!(U128::from(33), val);

Ok(())
}

0 comments on commit ef44736

Please sign in to comment.