Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
open-runtime-module-library
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Noxim
open-runtime-module-library
Commits
bade464b
Commit
bade464b
authored
5 years ago
by
wangjj9219
Committed by
Xiliang Chen
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
add remove_auction function (#68)
* add remove_auction function * add Eq and PartialEq traits for AuctionId
parent
ce13ee06
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
auction/src/lib.rs
+12
-7
12 additions, 7 deletions
auction/src/lib.rs
auction/src/tests.rs
+21
-6
21 additions, 6 deletions
auction/src/tests.rs
traits/src/auction.rs
+7
-2
7 additions, 2 deletions
traits/src/auction.rs
with
40 additions
and
15 deletions
auction/src/lib.rs
+
12
−
7
View file @
bade464b
...
@@ -4,7 +4,7 @@ use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, P
...
@@ -4,7 +4,7 @@ use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, P
use
frame_system
::{
self
as
system
,
ensure_signed
};
use
frame_system
::{
self
as
system
,
ensure_signed
};
use
orml_utilities
::{
LinkedItem
,
LinkedList
};
use
orml_utilities
::{
LinkedItem
,
LinkedList
};
use
sp_runtime
::{
use
sp_runtime
::{
traits
::{
MaybeSerializeDeserialize
,
Member
,
One
,
Saturating
,
SimpleArithmetic
,
Zero
},
traits
::{
MaybeSerializeDeserialize
,
Member
,
One
,
SimpleArithmetic
,
Zero
},
DispatchResult
,
DispatchResult
,
};
};
...
@@ -38,7 +38,7 @@ type AuctionIdLinkedItem<T> = LinkedItem<<T as Trait>::AuctionId>;
...
@@ -38,7 +38,7 @@ type AuctionIdLinkedItem<T> = LinkedItem<<T as Trait>::AuctionId>;
decl_storage!
{
decl_storage!
{
trait
Store
for
Module
<
T
:
Trait
>
as
Auction
{
trait
Store
for
Module
<
T
:
Trait
>
as
Auction
{
pub
Auctions
get
(
fn
auctions
):
map
T
::
AuctionId
=>
Option
<
AuctionInfo
<
T
::
AccountId
,
T
::
Balance
,
T
::
BlockNumber
>>
;
pub
Auctions
get
(
fn
auctions
):
map
T
::
AuctionId
=>
Option
<
AuctionInfo
<
T
::
AccountId
,
T
::
Balance
,
T
::
BlockNumber
>>
;
pub
Auctions
Count
get
(
fn
auctions_
count
):
T
::
AuctionId
;
pub
Auctions
Index
get
(
fn
auctions_
index
):
T
::
AuctionId
;
pub
AuctionEndTime
get
(
fn
auction_end_time
):
map
(
T
::
BlockNumber
,
Option
<
T
::
AuctionId
>
)
=>
Option
<
AuctionIdLinkedItem
<
T
>>
;
pub
AuctionEndTime
get
(
fn
auction_end_time
):
map
(
T
::
BlockNumber
,
Option
<
T
::
AuctionId
>
)
=>
Option
<
AuctionIdLinkedItem
<
T
>>
;
}
}
}
}
...
@@ -105,14 +105,11 @@ decl_error! {
...
@@ -105,14 +105,11 @@ decl_error! {
impl
<
T
:
Trait
>
Module
<
T
>
{
impl
<
T
:
Trait
>
Module
<
T
>
{
fn
_on_finalize
(
now
:
T
::
BlockNumber
)
{
fn
_on_finalize
(
now
:
T
::
BlockNumber
)
{
let
ended_auctions
=
<
AuctionEndTimeList
<
T
>>
::
take_all
(
&
now
);
let
ended_auctions
=
<
AuctionEndTimeList
<
T
>>
::
take_all
(
&
now
);
let
mut
count
=
Self
::
auctions_count
();
ended_auctions
.for_each
(|
auction_id
|
{
ended_auctions
.for_each
(|
auction_id
|
{
if
let
Some
(
auction
)
=
<
Auctions
<
T
>>
::
take
(
&
auction_id
)
{
if
let
Some
(
auction
)
=
<
Auctions
<
T
>>
::
take
(
&
auction_id
)
{
T
::
Handler
::
on_auction_ended
(
auction_id
,
auction
.bid
.clone
());
T
::
Handler
::
on_auction_ended
(
auction_id
,
auction
.bid
.clone
());
count
=
count
.saturating_sub
(
T
::
AuctionId
::
one
());
}
}
});
});
<
AuctionsCount
<
T
>>
::
put
(
count
);
}
}
}
}
...
@@ -141,8 +138,8 @@ impl<T: Trait> Auction<T::AccountId, T::BlockNumber> for Module<T> {
...
@@ -141,8 +138,8 @@ impl<T: Trait> Auction<T::AccountId, T::BlockNumber> for Module<T> {
fn
new_auction
(
start
:
T
::
BlockNumber
,
end
:
Option
<
T
::
BlockNumber
>
)
->
Self
::
AuctionId
{
fn
new_auction
(
start
:
T
::
BlockNumber
,
end
:
Option
<
T
::
BlockNumber
>
)
->
Self
::
AuctionId
{
let
auction
=
AuctionInfo
{
bid
:
None
,
start
,
end
};
let
auction
=
AuctionInfo
{
bid
:
None
,
start
,
end
};
let
auction_id
=
Self
::
auctions_
count
();
let
auction_id
=
Self
::
auctions_
index
();
<
Auctions
Count
<
T
>>
::
mutate
(|
n
|
*
n
+=
Self
::
AuctionId
::
from
(
1
));
<
Auctions
Index
<
T
>>
::
mutate
(|
n
|
*
n
+=
Self
::
AuctionId
::
one
(
));
<
Auctions
<
T
>>
::
insert
(
auction_id
,
auction
);
<
Auctions
<
T
>>
::
insert
(
auction_id
,
auction
);
if
let
Some
(
end_block
)
=
end
{
if
let
Some
(
end_block
)
=
end
{
<
AuctionEndTimeList
<
T
>>
::
append
(
&
end_block
,
auction_id
);
<
AuctionEndTimeList
<
T
>>
::
append
(
&
end_block
,
auction_id
);
...
@@ -150,4 +147,12 @@ impl<T: Trait> Auction<T::AccountId, T::BlockNumber> for Module<T> {
...
@@ -150,4 +147,12 @@ impl<T: Trait> Auction<T::AccountId, T::BlockNumber> for Module<T> {
auction_id
auction_id
}
}
fn
remove_auction
(
id
:
Self
::
AuctionId
)
{
if
let
Some
(
auction
)
=
<
Auctions
<
T
>>
::
take
(
&
id
)
{
if
let
Some
(
end_block
)
=
auction
.end
{
<
AuctionEndTimeList
<
T
>>
::
remove
(
&
end_block
,
id
);
}
}
}
}
}
This diff is collapsed.
Click to expand it.
auction/src/tests.rs
+
21
−
6
View file @
bade464b
...
@@ -71,18 +71,33 @@ fn bid_should_fail() {
...
@@ -71,18 +71,33 @@ fn bid_should_fail() {
});
});
}
}
#[test]
fn
remove_auction_should_work
()
{
ExtBuilder
::
default
()
.build
()
.execute_with
(||
{
assert_eq!
(
AuctionModule
::
new_auction
(
10
,
Some
(
100
)),
0
);
assert_eq!
(
AuctionModule
::
auctions_index
(),
1
);
assert_eq!
(
AuctionModule
::
auctions
(
0
)
.is_some
(),
true
);
assert_eq!
(
AuctionModule
::
auction_end_time
((
100
,
Some
(
0
)))
.is_some
(),
true
);
AuctionModule
::
remove_auction
(
0
);
assert_eq!
(
AuctionModule
::
auctions
(
0
),
None
);
assert_eq!
(
AuctionModule
::
auction_end_time
((
100
,
Some
(
0
))),
None
);
});
}
#[test]
#[test]
fn
cleanup_auction_should_work
()
{
fn
cleanup_auction_should_work
()
{
ExtBuilder
::
default
()
.build
()
.execute_with
(||
{
ExtBuilder
::
default
()
.build
()
.execute_with
(||
{
assert_eq!
(
AuctionModule
::
new_auction
(
10
,
Some
(
100
)),
0
);
assert_eq!
(
AuctionModule
::
new_auction
(
10
,
Some
(
100
)),
0
);
assert_eq!
(
AuctionModule
::
auctions_
count
(),
1
);
assert_eq!
(
AuctionModule
::
auctions_
index
(),
1
);
assert_eq!
(
AuctionModule
::
new_auction
(
10
,
Some
(
50
)),
1
);
assert_eq!
(
AuctionModule
::
new_auction
(
10
,
Some
(
50
)),
1
);
assert_eq!
(
AuctionModule
::
auctions_
count
(),
2
);
assert_eq!
(
AuctionModule
::
auctions_
index
(),
2
);
AuctionModule
::
on_finalize
(
1
);
assert_eq!
(
AuctionModule
::
auctions
(
0
)
.is_some
(),
true
);
assert_eq!
(
AuctionModule
::
auctions
_count
(),
2
);
assert_eq!
(
AuctionModule
::
auctions
(
1
)
.is_some
(),
true
);
AuctionModule
::
on_finalize
(
50
);
AuctionModule
::
on_finalize
(
50
);
assert_eq!
(
AuctionModule
::
auctions_count
(),
1
);
assert_eq!
(
AuctionModule
::
auctions
(
0
)
.is_some
(),
true
);
assert_eq!
(
AuctionModule
::
auctions
(
1
)
.is_some
(),
false
);
AuctionModule
::
on_finalize
(
100
);
AuctionModule
::
on_finalize
(
100
);
assert_eq!
(
AuctionModule
::
auctions_count
(),
0
);
assert_eq!
(
AuctionModule
::
auctions
(
0
)
.is_some
(),
false
);
assert_eq!
(
AuctionModule
::
auctions
(
1
)
.is_some
(),
false
);
});
});
}
}
This diff is collapsed.
Click to expand it.
traits/src/auction.rs
+
7
−
2
View file @
bade464b
use
codec
::
FullCodec
;
use
codec
::
FullCodec
;
use
codec
::{
Decode
,
Encode
};
use
codec
::{
Decode
,
Encode
};
use
rstd
::
fmt
::
Debug
;
use
rstd
::{
cmp
::{
Eq
,
PartialEq
},
fmt
::
Debug
,
};
use
sp_runtime
::{
use
sp_runtime
::{
traits
::{
MaybeSerializeDeserialize
,
SimpleArithmetic
},
traits
::{
MaybeSerializeDeserialize
,
SimpleArithmetic
},
DispatchResult
,
RuntimeDebug
,
DispatchResult
,
RuntimeDebug
,
...
@@ -21,7 +24,7 @@ pub struct AuctionInfo<AccountId, Balance, BlockNumber> {
...
@@ -21,7 +24,7 @@ pub struct AuctionInfo<AccountId, Balance, BlockNumber> {
/// Abstraction over a simple auction system.
/// Abstraction over a simple auction system.
pub
trait
Auction
<
AccountId
,
BlockNumber
>
{
pub
trait
Auction
<
AccountId
,
BlockNumber
>
{
/// The id of an AuctionInfo
/// The id of an AuctionInfo
type
AuctionId
:
FullCodec
+
Default
+
Copy
+
MaybeSerializeDeserialize
+
Debug
;
type
AuctionId
:
FullCodec
+
Default
+
Copy
+
Eq
+
PartialEq
+
MaybeSerializeDeserialize
+
Debug
;
/// The price to bid.
/// The price to bid.
type
Balance
:
SimpleArithmetic
+
FullCodec
+
Copy
+
MaybeSerializeDeserialize
+
Debug
+
Default
;
type
Balance
:
SimpleArithmetic
+
FullCodec
+
Copy
+
MaybeSerializeDeserialize
+
Debug
+
Default
;
...
@@ -31,6 +34,8 @@ pub trait Auction<AccountId, BlockNumber> {
...
@@ -31,6 +34,8 @@ pub trait Auction<AccountId, BlockNumber> {
fn
update_auction
(
id
:
Self
::
AuctionId
,
info
:
AuctionInfo
<
AccountId
,
Self
::
Balance
,
BlockNumber
>
)
->
DispatchResult
;
fn
update_auction
(
id
:
Self
::
AuctionId
,
info
:
AuctionInfo
<
AccountId
,
Self
::
Balance
,
BlockNumber
>
)
->
DispatchResult
;
/// Create new auction with specific startblock and endblock, return the id of the auction
/// Create new auction with specific startblock and endblock, return the id of the auction
fn
new_auction
(
start
:
BlockNumber
,
end
:
Option
<
BlockNumber
>
)
->
Self
::
AuctionId
;
fn
new_auction
(
start
:
BlockNumber
,
end
:
Option
<
BlockNumber
>
)
->
Self
::
AuctionId
;
/// Remove auction by `id`
fn
remove_auction
(
id
:
Self
::
AuctionId
);
}
}
/// The result of bid handling.
/// The result of bid handling.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment