Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix origin of erc20 xtokens transfer
  • Loading branch information
zqhxuyuan committed Jun 2, 2022
commit 2dcce43bf979d2b79a4cf4a29374e84b7543683d
12 changes: 10 additions & 2 deletions modules/currencies/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,16 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
match currency_id {
CurrencyId::Erc20(contract) => {
let sender = T::AddressMapping::get_evm_address(from).ok_or(Error::<T>::EvmAccountNotFound)?;
let origin = T::EVMBridge::get_origin().ok_or(Error::<T>::RealOriginNotFound)?;
let origin_address = T::AddressMapping::get_or_create_evm_address(&origin);
// withdraw to or deposit from erc20 holding account
let erc20_holding_account = T::AddressMapping::get_account_id(&T::Erc20HoldingAccount::get());
let origin_address = if erc20_holding_account == from.clone() {
T::AddressMapping::get_or_create_evm_address(to)
} else if erc20_holding_account == to.clone() {
T::AddressMapping::get_or_create_evm_address(from)
} else {
let origin = T::EVMBridge::get_origin().ok_or(Error::<T>::RealOriginNotFound)?;
T::AddressMapping::get_or_create_evm_address(&origin)
};
let address = T::AddressMapping::get_or_create_evm_address(to);
T::EVMBridge::transfer(
InvokeContext {
Expand Down
2 changes: 1 addition & 1 deletion orml
2 changes: 1 addition & 1 deletion runtime/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ xcm-emulator = { git = "https://github.com/shaunxw/xcm-simulator", rev = "5a04d6
acala-service = { path = "../../node/service", features = ["with-all-runtime"] }

[features]
default = ["std"]
default = ["std", "with-karura-runtime"]
no_std = []
with-mandala-runtime = [
"mandala-runtime",
Expand Down
46 changes: 33 additions & 13 deletions runtime/integration-tests/src/relaychain/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub fn deploy_erc20_contracts() {

#[test]
fn erc20_transfer_between_sibling() {
env_logger::init();
TestNet::reset();

fn sibling_reserve_account() -> AccountId {
Expand Down Expand Up @@ -109,6 +110,16 @@ fn erc20_transfer_between_sibling() {
&alith.clone(),
1_000_000 * dollar(NATIVE_CURRENCY)
));
assert_ok!(Currencies::deposit(
NATIVE_CURRENCY,
&AccountId::from(BOB),
1_000_000 * dollar(NATIVE_CURRENCY)
));
assert_ok!(Currencies::deposit(
NATIVE_CURRENCY,
&sibling_reserve_account(),
1_000_000 * dollar(NATIVE_CURRENCY)
));

deploy_erc20_contracts();

Expand All @@ -119,23 +130,27 @@ fn erc20_transfer_between_sibling() {
EvmAccounts::eth_sign(&alice_key(), &AccountId::from(ALICE))
));

<EVM as EVMTrait<AccountId>>::set_origin(alith.clone());
let total = 100_000_000_000_000_000_000_000u128;

// <EVM as EVMTrait<AccountId>>::set_origin(alith.clone());

// use Currencies `transfer` dispatch call to transfer erc20 token to bob.
assert_ok!(Currencies::transfer(
Origin::signed(alith),
MultiAddress::Id(AccountId::from(CHARLIE)),
CurrencyId::Erc20(erc20_address_0()),
1_000_000_000_000_000
));
// assert_ok!(Currencies::transfer(
// Origin::signed(alith),
// MultiAddress::Id(AccountId::from(CHARLIE)),
// CurrencyId::Erc20(erc20_address_0()),
// 1_000_000_000_000_000
// ));
// println!("{}", Currencies::free_balance(CurrencyId::Erc20(erc20_address_0()), &alith));
// println!("{}", Currencies::free_balance(CurrencyId::Erc20(erc20_address_0()), &alice()));
assert_eq!(
Currencies::free_balance(CurrencyId::Erc20(erc20_address_0()), &AccountId::from(CHARLIE)),
1_000_000_000_000_000
Currencies::free_balance(CurrencyId::Erc20(erc20_address_0()), &alith),
total
);

// transfer erc20 token to Sibling
assert_ok!(XTokens::transfer(
Origin::signed(CHARLIE.into()),
Origin::signed(alith.clone()),
CurrencyId::Erc20(erc20_address_0()),
10_000_000_000_000,
Box::new(
Expand All @@ -154,10 +169,15 @@ fn erc20_transfer_between_sibling() {
1_000_000_000,
));

assert_eq!(
990_000_000_000_000,
Currencies::free_balance(CurrencyId::Erc20(erc20_address_0()), &AccountId::from(CHARLIE))
println!(
"{}",
Currencies::free_balance(CurrencyId::Erc20(erc20_address_0()), &alith)
);

// assert_eq!(
// total - 10_000_000_000_000,
// Currencies::free_balance(CurrencyId::Erc20(erc20_address_0()), &alith)
// );
assert_eq!(
10_000_000_000_000,
Currencies::free_balance(CurrencyId::Erc20(erc20_address_0()), &sibling_reserve_account())
Expand Down