-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(airdrop): setup package * feat(contract): add airdrop role * feat(contract): add airdrop action * feat(contract): add use airdrop * chore(version): bump contract to 2.4.0
- Loading branch information
Showing
12 changed files
with
271 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
resolver = "2" | ||
|
||
members = [ | ||
"airdrop", | ||
"contract", | ||
"contract-proxy", | ||
"contract-version-base", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[package] | ||
name = "airdrop" | ||
version = "1.0.0" | ||
authors = ["Yeboster"] | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[dependencies] | ||
pbc_contract_common = { workspace = true } | ||
pbc_contract_codegen = { workspace = true } | ||
pbc_traits = { workspace = true } | ||
pbc_lib = { workspace = true } | ||
read_write_rpc_derive = { workspace = true } | ||
read_write_state_derive = { workspace = true } | ||
create_type_spec_derive = { workspace = true } | ||
|
||
utils = { path = "../utils" } | ||
rpc-msg-derive = { path = "../rpc-msg-derive" } | ||
|
||
serde_json = { workspace = true } | ||
|
||
[features] | ||
abi = [ | ||
"pbc_contract_common/abi", | ||
"pbc_contract_codegen/abi", | ||
"pbc_traits/abi", | ||
"create_type_spec_derive/abi", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use pbc_contract_common::{address::Address, avl_tree_map::AvlTreeMap}; | ||
|
||
use crate::state::AirdropState; | ||
|
||
pub fn execute_init() -> AirdropState { | ||
AirdropState { | ||
inventory: AvlTreeMap::new(), | ||
} | ||
} | ||
|
||
pub fn execute_airdrop(state: &mut AirdropState, address: &Address) { | ||
assert!(state.has_airdrop(address), "No airdrop for the address"); | ||
|
||
state._use_airdrop(address); | ||
} | ||
|
||
pub fn execute_add_airdrop(state: &mut AirdropState, address: &Address) { | ||
state._add_airdrop(address); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod actions; | ||
pub mod state; | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use create_type_spec_derive::CreateTypeSpec; | ||
use pbc_contract_common::{address::Address, avl_tree_map::AvlTreeMap}; | ||
use read_write_state_derive::ReadWriteState; | ||
|
||
#[repr(C)] | ||
#[derive(ReadWriteState, CreateTypeSpec, Default, Debug)] | ||
pub struct AirdropState { | ||
pub inventory: AvlTreeMap<Address, u128>, | ||
} | ||
|
||
impl AirdropState { | ||
/// Check if the address has an airdrop | ||
pub fn has_airdrop(&self, address: &Address) -> bool { | ||
self.inventory.contains_key(address) | ||
} | ||
|
||
/// Use airdrop from the address | ||
pub fn _use_airdrop(&mut self, address: &Address) { | ||
if let Some(airdrop) = self.inventory.get(address) { | ||
let remaining = airdrop - 1; | ||
if remaining == 0 { | ||
self.inventory.remove(address); | ||
} else { | ||
self.inventory.insert(*address, remaining); | ||
} | ||
} | ||
} | ||
|
||
/// Add airdrop to the address | ||
pub fn _add_airdrop(&mut self, address: &Address) { | ||
let airdrop = self.inventory.get(address).unwrap_or(0); | ||
self.inventory.insert(*address, airdrop + 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Setup tests | ||
|
||
use utils::tests::mock_address; | ||
|
||
use crate::actions::{execute_add_airdrop, execute_airdrop, execute_init}; | ||
|
||
#[test] | ||
fn proper_has_airdrop() { | ||
let address = mock_address(0); | ||
let mut state = execute_init(); | ||
|
||
assert_eq!(state.has_airdrop(&address), false); | ||
|
||
execute_add_airdrop(&mut state, &address); | ||
|
||
assert_eq!(state.has_airdrop(&address), true); | ||
} | ||
|
||
#[test] | ||
fn proper_execute_airdrop() { | ||
let address = mock_address(0); | ||
let mut state = execute_init(); | ||
|
||
execute_add_airdrop(&mut state, &address); | ||
execute_airdrop(&mut state, &address); | ||
|
||
assert_eq!(state.has_airdrop(&address), false); | ||
} | ||
|
||
#[test] | ||
fn proper_execute_add_airdrop() { | ||
let address = mock_address(0); | ||
let mut state = execute_init(); | ||
|
||
execute_add_airdrop(&mut state, &address); | ||
execute_add_airdrop(&mut state, &address); | ||
|
||
assert_eq!(state.has_airdrop(&address), true); | ||
assert_eq!(state.inventory.get(&address).unwrap(), 2); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "No airdrop for the address")] | ||
fn proper_execute_airdrop_no_airdrop() { | ||
let address = mock_address(0); | ||
let mut state = execute_init(); | ||
|
||
execute_airdrop(&mut state, &address); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.