Skip to content

Commit

Permalink
Return default value instead of error (#2152)
Browse files Browse the repository at this point in the history
* return default value instead of error

* revert multicurrency

* revert dex and stable_asset

* fix dex
  • Loading branch information
zjb0807 authored May 25, 2022
1 parent 800c08c commit 0807de9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 46 deletions.
42 changes: 15 additions & 27 deletions runtime/common/src/precompile/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,19 @@ where
currency_id_a, currency_id_b
);

let value = <module_dex::Pallet<Runtime> as DEXManager<Runtime::AccountId, Balance, CurrencyId>>::get_liquidity_token_address(currency_id_a, currency_id_b)
.ok_or_else(||
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "Dex get_liquidity_token_address failed".into(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;
// If it does not exist, return address(0x0). Keep the behavior the same as mapping[key]
let address = <module_dex::Pallet<Runtime> as DEXManager<Runtime::AccountId, Balance, CurrencyId>>::get_liquidity_token_address(currency_id_a, currency_id_b)
.unwrap_or_default();

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_address(&value),
output: Output::default().encode_address(&address),
logs: Default::default(),
})
}
Action::GetSwapTargetAmount => {
// solidity abi enocde array will add an offset at input[1]
// solidity abi encode array will add an offset at input[1]
let supply_amount = input.balance_at(2)?;
let path_len = input.u32_at(3)?;
let mut path = vec![];
Expand All @@ -145,24 +141,20 @@ where
path, supply_amount
);

let value = <module_dex::Pallet<Runtime> as DEXManager<Runtime::AccountId, Balance, CurrencyId>>::get_swap_amount(&path, SwapLimit::ExactSupply(supply_amount, Balance::MIN))
// If get_swap_amount fail, return 0.
let target = <module_dex::Pallet<Runtime> as DEXManager<Runtime::AccountId, Balance, CurrencyId>>::get_swap_amount(&path, SwapLimit::ExactSupply(supply_amount, Balance::MIN))
.map(|(_, target)| target)
.ok_or_else(||
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "Dex get_swap_target_amount failed".into(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;
.unwrap_or_default();

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(value),
output: Output::default().encode_u128(target),
logs: Default::default(),
})
}
Action::GetSwapSupplyAmount => {
// solidity abi enocde array will add an offset at input[1]
// solidity abi encode array will add an offset at input[1]
let target_amount = input.balance_at(2)?;
let path_len = input.u32_at(3)?;
let mut path = vec![];
Expand All @@ -175,19 +167,15 @@ where
path, target_amount
);

let value = <module_dex::Pallet<Runtime> as DEXManager<Runtime::AccountId, Balance, CurrencyId>>::get_swap_amount(&path, SwapLimit::ExactTarget(Balance::MAX, target_amount))
// If get_swap_amount fail, return 0.
let supply = <module_dex::Pallet<Runtime> as DEXManager<Runtime::AccountId, Balance, CurrencyId>>::get_swap_amount(&path, SwapLimit::ExactTarget(Balance::MAX, target_amount))
.map(|(supply, _)| supply)
.ok_or_else(||
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "Dex get_swap_supply_amount failed".into(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;
.unwrap_or_default();

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(value),
output: Output::default().encode_u128(supply),
logs: Default::default(),
})
}
Expand Down Expand Up @@ -225,7 +213,7 @@ where
}
Action::SwapWithExactTarget => {
let who = input.account_id_at(1)?;
// solidity abi enocde array will add an offset at input[2]
// solidity abi encode array will add an offset at input[2]
let target_amount = input.balance_at(3)?;
let max_supply_amount = input.balance_at(4)?;
let path_len = input.u32_at(5)?;
Expand Down
26 changes: 10 additions & 16 deletions runtime/common/src/precompile/evm_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,8 @@ where
buf.copy_from_slice(&input_data[..]);
let account_id: Runtime::AccountId = AccountId32::from(buf).into();

let address =
module_evm_accounts::Pallet::<Runtime>::get_evm_address(&account_id).ok_or_else(|| {
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "EvmAddress mapping not found".into(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
}
})?;
// If it does not exist, return address(0x0). Keep the behavior the same as mapping[key]
let address = module_evm_accounts::Pallet::<Runtime>::get_evm_address(&account_id).unwrap_or_default();

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
Expand Down Expand Up @@ -243,14 +237,14 @@ mod tests {
0101010101010101010101010101010101010101010101010101010101010101
"};

assert_noop!(
EVMAccountsPrecompile::execute(&input, Some(10_000), &context, false),
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "EvmAddress mapping not found".into(),
cost: target_gas_limit(Some(10_000)).unwrap(),
}
);
// expect output is address(0)
let expected_output = hex! {"
000000000000000000000000 0000000000000000000000000000000000000000
"};

let resp = EVMAccountsPrecompile::execute(&input, None, &context, false).unwrap();
assert_eq!(resp.exit_status, ExitSucceed::Returned);
assert_eq!(resp.output, expected_output.to_vec());
});
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/common/src/precompile/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ where
}
Action::Cancel => {
let from = input.evm_address_at(1)?;
// solidity abi enocde bytes will add an length at input[2]
// solidity abi encode bytes will add an length at input[2]
let task_id_len = input.u32_at(3)?;
let task_id = input.bytes_at(4, task_id_len as usize)?;

Expand Down
4 changes: 2 additions & 2 deletions runtime/common/src/precompile/stable_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ where
Action::StableAssetMint => {
let who = input.account_id_at(1)?;
let pool_id = input.u32_at(2)?;
// solidity abi enocde array will add an offset at input[3]
// solidity abi encode array will add an offset at input[3]
let min_mint_amount = input.balance_at(4)?;
let amount_len = input.u32_at(5)?;
let mut amounts = vec![];
Expand Down Expand Up @@ -259,7 +259,7 @@ where
let who = input.account_id_at(1)?;
let pool_id = input.u32_at(2)?;
let redeem_amount = input.balance_at(3)?;
// solidity abi enocde array will add an offset at input[4]
// solidity abi encode array will add an offset at input[4]
let amount_len = input.u32_at(5)?;
let mut amounts = vec![];
for i in 0..amount_len {
Expand Down

0 comments on commit 0807de9

Please sign in to comment.