Skip to content

Commit

Permalink
update (#85)
Browse files Browse the repository at this point in the history
* update

* update format

* update invite amount

* update swap
  • Loading branch information
StewartYe authored Aug 31, 2023
1 parent 98d2d1b commit 6b481f3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
47 changes: 36 additions & 11 deletions pallets/abyss-tournament/src/betting_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::mock::*;
use crate::{BattleType, Betting, BettingType, Error, OddsItem, Pallet, NPC};
use ascii::AsciiChar::v;
use frame_support::{assert_noop, assert_ok};
use fuso_support::XToken;
use pallet_fuso_token::TokenAccountData;
Expand Down Expand Up @@ -162,12 +163,14 @@ pub fn set_result() {

pub fn do_bet() {
let alice: AccountId = AccountKeyring::Alice.into();
let bob: AccountId = AccountKeyring::Bob.into();
assert_noop!(
Tournament::go_bet(
RuntimeOrigin::signed(alice.clone()),
1u32,
1,
10_000_000_000_000_000_000
10_000_000_000_000_000_000,
vec![]
),
Error::<Test>::BettingAmountTooSmall
);
Expand All @@ -176,7 +179,8 @@ pub fn do_bet() {
RuntimeOrigin::signed(alice.clone()),
1u32,
1,
100_000_000_000_000_000_000
100_000_000_000_000_000_000,
vec![]
),
Error::<Test>::BettingAmountOverflow
);
Expand All @@ -185,7 +189,8 @@ pub fn do_bet() {
RuntimeOrigin::signed(alice.clone()),
1u32,
3,
20_000_000_000_000_000_000
20_000_000_000_000_000_000,
vec![]
),
Error::<Test>::SelectIndexOverflow
);
Expand All @@ -194,7 +199,8 @@ pub fn do_bet() {
RuntimeOrigin::signed(alice.clone()),
1u32,
1,
20_000_000_000_000_000_000
20_000_000_000_000_000_000,
vec![]
));

assert_eq!(
Expand Down Expand Up @@ -234,7 +240,8 @@ pub fn do_bet() {
RuntimeOrigin::signed(alice.clone()),
2u32,
0,
10_000_000_000_000_000_000
10_000_000_000_000_000_000,
vec![]
),
Error::<Test>::BettingAmountTooSmall
);
Expand All @@ -243,7 +250,8 @@ pub fn do_bet() {
RuntimeOrigin::signed(alice.clone()),
2u32,
0,
100_000_000_000_000_000_000
100_000_000_000_000_000_000,
vec![]
),
Error::<Test>::BettingAmountOverflow
);
Expand All @@ -252,16 +260,24 @@ pub fn do_bet() {
RuntimeOrigin::signed(alice.clone()),
2u32,
6,
20_000_000_000_000_000_000
20_000_000_000_000_000_000,
vec![]
),
Error::<Test>::SelectIndexOverflow
);
assert_ok!(Tournament::go_bet(
RuntimeOrigin::signed(alice.clone()),
2u32,
0,
20_000_000_000_000_000_000
20_000_000_000_000_000_000,
Tournament::addr_to_invite_code(bob.clone().into())
));

assert_eq!(
Tournament::get_invite_amount((1, &bob), 1),
20_000_000_000_000_000_000
);

assert_ok!(Tournament::update_odds(
RuntimeOrigin::signed(TREASURY),
2u32,
Expand All @@ -271,14 +287,22 @@ pub fn do_bet() {
RuntimeOrigin::signed(alice.clone()),
2u32,
0,
20_000_000_000_000_000_000
20_000_000_000_000_000_000,
Tournament::addr_to_invite_code(bob.clone().into())
));

assert_eq!(
Tournament::get_invite_amount((1, &bob), 1),
40_000_000_000_000_000_000
);

assert_noop!(
Tournament::go_bet(
RuntimeOrigin::signed(alice.clone()),
2u32,
0,
40_000_000_000_000_000_000
40_000_000_000_000_000_000,
vec![]
),
Error::<Test>::BettingAmountOverflow
);
Expand All @@ -287,7 +311,8 @@ pub fn do_bet() {
RuntimeOrigin::signed(alice.clone()),
5u32,
0,
20_000_000_000_000_000_000
20_000_000_000_000_000_000,
vec![]
),
Error::<Test>::BettingNotFound
);
Expand Down
33 changes: 28 additions & 5 deletions pallets/abyss-tournament/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,18 @@ pub mod pallet {
#[pallet::getter(fn get_npc_info)]
pub type Npcs<T: Config> = StorageMap<_, Twox64Concat, NpcId, NPC, OptionQuery>;

#[pallet::storage]
#[pallet::getter(fn get_invite_amount)]
pub type BettingInviteAmount<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
(SeasonId, T::AccountId),
Blake2_128Concat,
AssetId<T>,
BalanceOf<T>,
ValueQuery,
>;

#[pallet::storage]
#[pallet::getter(fn get_bettings_by_battle)]
pub type BettingByBattle<T: Config> =
Expand Down Expand Up @@ -748,6 +760,7 @@ pub mod pallet {
betting_id: BettingId,
item_index: SelectIndex,
amount: BalanceOf<T>,
invite_code: Vec<u8>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let betting: Betting<T::AccountId, BalanceOf<T>, AssetId<T>> =
Expand All @@ -756,6 +769,8 @@ pub mod pallet {
amount >= betting.min_betting_amount,
Error::<T>::BettingAmountTooSmall
);
let season_id = betting.season;
let token_id = betting.token_id;
ensure!(
usize::from(item_index) < betting.odds.len(),
Error::<T>::SelectIndexOverflow
Expand Down Expand Up @@ -802,6 +817,13 @@ pub mod pallet {
Self::deposit_event(Event::BettingUpdate(betting_id, betting.clone()));
b.replace(betting);
});
if let Some(invitor) = Self::invite_code_to_addr(invite_code) {
if invitor != who {
BettingInviteAmount::<T>::mutate((season_id, invitor), token_id, |balance| {
*balance += amount;
});
}
}
Ok(().into())
}

Expand Down Expand Up @@ -1180,22 +1202,23 @@ pub mod pallet {
let external_decimals = T::Fungibles::token_external_decimals(&token_id)?;
let unified_amount =
T::Fungibles::transform_decimals_to_standard(amt, external_decimals);
let price: u128 = T::Oracle::get_price(&token_id).into();
let price: u128 = T::Oracle::get_price(&T::AwtTokenId::get()).into();
if price.is_zero() {
return Ok(());
}
let awt_amount: u128 = unified_amount.into() / price * QUINTILL
let awt_amount: BalanceOf<T> = (unified_amount.into() / price * QUINTILL
+ Perquintill::from_rational::<u128>(unified_amount.into() % price, price)
.deconstruct() as u128;
.deconstruct() as u128)
.into();
if T::Fungibles::free_balance(&T::AwtTokenId::get(), &T::SwapPoolAccount::get())
< awt_amount.into()
< awt_amount
{
return Ok(());
}
T::Fungibles::transfer_token(
&T::SwapPoolAccount::get(),
T::AwtTokenId::get(),
awt_amount.into(),
awt_amount,
&who,
)?;
T::Fungibles::transfer_token(
Expand Down

0 comments on commit 6b481f3

Please sign in to comment.