Skip to content

Commit

Permalink
Removed Default trait from storage. (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaplas authored May 10, 2022
1 parent 9d8eccd commit 1d8d781
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 116 deletions.
2 changes: 1 addition & 1 deletion dao-contracts/src/dao_nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ impl DaoOwnedNftContractInterface for DaoOwnedNftContract {
}

fn token_id(&self, address: Address) -> Option<TokenId> {
self.tokens.get(&address)
self.tokens.get(&address).unwrap_or(None)
}
}
2 changes: 1 addition & 1 deletion dao-contracts/src/mocks/mock_voter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl MockVoterContractInterface for MockVoterContract {
}

fn get_variable(&self) -> String {
self.variable.get()
self.variable.get().unwrap_or_default()
}

delegate! {
Expand Down
2 changes: 1 addition & 1 deletion dao-contracts/src/reputation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl ReputationContractInterface for ReputationContract {
}

fn total_onboarded(&self) -> U256 {
self.total_onboarded.get()
self.total_onboarded.get().unwrap_or_default()
}

fn set_total_onboarded(&mut self, total: U256) {
Expand Down
23 changes: 11 additions & 12 deletions dao-contracts/src/voting/governance_voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ pub trait GovernanceVotingTrait {
/// For example implementation see [AdminContract](crate::admin::AdminContract)
#[derive(Instance)]
pub struct GovernanceVoting {
variable_repo: Variable<Option<Address>>,
reputation_token: Variable<Option<Address>>,
variable_repo: Variable<Address>,
reputation_token: Variable<Address>,
votings: Mapping<VotingId, Option<Voting>>,
ballots: Mapping<(VotingId, Address), Ballot>,
voters: VecMapping<VotingId, Option<Address>>,
voters: VecMapping<VotingId, Address>,
votings_count: Variable<U256>,
dust_amount: Variable<U256>,
}
Expand All @@ -62,8 +62,8 @@ impl GovernanceVoting {
/// # Events
/// Emits [`VotingContractCreated`](VotingContractCreated)
pub fn init(&mut self, variable_repo: Address, reputation_token: Address) {
self.variable_repo.set(Some(variable_repo));
self.reputation_token.set(Some(reputation_token));
self.variable_repo.set(variable_repo);
self.reputation_token.set(reputation_token);

emit(VotingContractCreated {
variable_repo,
Expand Down Expand Up @@ -183,6 +183,7 @@ impl GovernanceVoting {
let creator_stake = self
.ballots
.get(&(voting.voting_id(), creator_address))
.unwrap_or_revert_with(Error::BallotDoesNotExist)
.stake;

// Formal voting is created and first vote cast
Expand Down Expand Up @@ -306,7 +307,7 @@ impl GovernanceVoting {
revert(Error::VoteOnCompletedVotingNotAllowed)
}

let mut vote = self.ballots.get(&(voting_id, voter));
let mut vote = self.ballots.get(&(voting_id, voter)).unwrap_or_default();
match vote.voter {
Some(_) => {
// Cannot vote twice on the same voting
Expand All @@ -324,7 +325,7 @@ impl GovernanceVoting {
stake,
};
// Add a voter to the list
self.voters.add(voting_id, Some(voter));
self.voters.add(voting_id, voter);
}
}

Expand All @@ -348,7 +349,7 @@ impl GovernanceVoting {
/// Those are leftovers from redistribution of reputation tokens. For example, when 10 tokens needs to be redistributed between 3 voters,
/// each will recieve 3 reputation, with 1 reputation left in the contract's balance.
pub fn get_dust_amount(&self) -> U256 {
self.dust_amount.get()
self.dust_amount.get().unwrap_or_default()
}

/// Returns the address of [Variable Repo](crate::VariableRepositoryContract) connected to the contract
Expand Down Expand Up @@ -377,9 +378,7 @@ impl GovernanceVoting {

/// Returns the address of nth voter who voted on Voting with `voting_id`
pub fn get_voter(&self, voting_id: VotingId, at: u32) -> Option<Address> {
self.voters
.get_or_none(voting_id, at)
.map(|x| x.unwrap_or_revert())
self.voters.get_or_none(voting_id, at)
}

/// Returns the [Voting](Voting) for given id
Expand All @@ -394,7 +393,7 @@ impl GovernanceVoting {
}

fn next_voting_id(&mut self) -> U256 {
let voting_id = self.votings_count.get();
let voting_id = self.votings_count.get().unwrap_or_default();
self.votings_count.set(voting_id + 1);
voting_id
}
Expand Down
6 changes: 3 additions & 3 deletions dao-contracts/src/voting/kyc_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use crate::{DaoOwnedNftContractCaller, DaoOwnedNftContractInterface};

#[derive(Instance)]
pub struct KycInfo {
kyc_token: Variable<Option<Address>>,
kyc_token: Variable<Address>,
votings: Mapping<Address, bool>,
}

impl KycInfo {
pub fn init(&mut self, kyc_token: Address) {
self.kyc_token.set(Some(kyc_token));
self.kyc_token.set(kyc_token);
}

pub fn get_kyc_token_address(&self) -> Address {
Expand All @@ -37,6 +37,6 @@ impl KycInfo {
}

pub(crate) fn exists_ongoing_voting(&self, address: &Address) -> bool {
self.votings.get(address)
self.votings.get(address).unwrap_or(false)
}
}
6 changes: 3 additions & 3 deletions dao-contracts/src/voting/onboarding_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use casper_dao_utils::{

#[derive(Instance)]
pub struct OnboardingInfo {
va_token: Variable<Option<Address>>,
va_token: Variable<Address>,
votings: Mapping<Address, bool>,
}

impl OnboardingInfo {
pub fn init(&mut self, va_token: Address) {
self.va_token.set(Some(va_token));
self.va_token.set(va_token);
}

pub fn get_va_token_address(&self) -> Address {
Expand All @@ -32,7 +32,7 @@ impl OnboardingInfo {
}

pub fn exists_ongoing_voting(&self, address: &Address) -> bool {
self.votings.get(address)
self.votings.get(address).unwrap_or(false)
}

pub fn is_onboarded(&self, &address: &Address) -> bool {
Expand Down
21 changes: 10 additions & 11 deletions dao-erc20/src/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ impl ERC20Interface for ERC20 {
}

fn name(&self) -> String {
self.name.get()
self.name.get_or_revert()
}

fn symbol(&self) -> String {
self.symbol.get()
self.symbol.get_or_revert()
}

fn decimals(&self) -> u8 {
self.decimals.get()
self.decimals.get_or_revert()
}

fn total_supply(&self) -> U256 {
self.total_supply.get()
self.total_supply.get().unwrap_or_default()
}

fn balance_of(&self, address: Address) -> U256 {
self.balances.get(&address)
self.balances.get(&address).unwrap_or_default()
}

fn transfer(&mut self, recipient: Address, amount: U256) {
Expand All @@ -84,7 +84,7 @@ impl ERC20Interface for ERC20 {
}

fn allowance(&self, owner: Address, spender: Address) -> U256 {
self.allowances.get(&(owner, spender))
self.allowances.get(&(owner, spender)).unwrap_or_default()
}

fn transfer_from(&mut self, owner: Address, recipient: Address, amount: U256) {
Expand All @@ -96,8 +96,8 @@ impl ERC20Interface for ERC20 {

impl ERC20 {
pub fn raw_transfer(&mut self, owner: Address, recipient: Address, amount: U256) {
let owner_balance = self.balances.get(&owner);
let recipient_balance = self.balances.get(&recipient);
let owner_balance = self.balance_of(owner);
let recipient_balance = self.balance_of(recipient);
if owner_balance < amount {
casper_env::revert(Error::InsufficientBalance)
}
Expand All @@ -112,12 +112,11 @@ impl ERC20 {
}

pub fn spend_allowance(&mut self, owner: Address, spender: Address, amount: U256) {
let key = (owner, spender);
let allowance = self.allowances.get(&key);
let allowance = self.allowance(owner, spender);
if amount > allowance {
casper_env::revert(Error::InsufficientAllowance);
}
self.allowances.set(&key, allowance - amount);
self.allowances.set(&(owner, spender), allowance - amount);

emit(Approval {
owner,
Expand Down
23 changes: 15 additions & 8 deletions dao-erc721/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use casper_dao_utils::{
casper_contract::unwrap_or_revert::UnwrapOrRevert,
casper_dao_macros::Instance,
casper_env::{self, emit},
Address, Error, Mapping, Variable,
Expand Down Expand Up @@ -29,15 +30,15 @@ impl ERC721Token {
if !self.exists(&token_id) {
casper_env::revert(Error::TokenDoesNotExist)
}
self.owners.get(&token_id)
self.owners.get(&token_id).unwrap_or(None)
}

pub fn balance_of(&self, owner: Address) -> U256 {
self.balances.get(&owner)
self.balances.get(&owner).unwrap_or_default()
}

pub fn total_supply(&self) -> U256 {
self.total_supply.get()
self.total_supply.get().unwrap_or_default()
}

pub fn approve(&mut self, approved: Option<Address>, token_id: TokenId) {
Expand All @@ -58,7 +59,7 @@ impl ERC721Token {
casper_env::revert(Error::TokenDoesNotExist)
}

self.token_approvals.get(&token_id)
self.token_approvals.get(&token_id).unwrap_or(None)
}

pub fn set_approval_for_all(&mut self, operator: Address, approved: bool) {
Expand All @@ -76,7 +77,9 @@ impl ERC721Token {
}

pub fn is_approved_for_all(&self, owner: Address, operator: Address) -> bool {
self.operator_approvals.get(&(owner, operator))
self.operator_approvals
.get(&(owner, operator))
.unwrap_or(false)
}

pub fn transfer_from(&mut self, owner: Address, recipient: Address, token_id: TokenId) {
Expand Down Expand Up @@ -105,7 +108,11 @@ impl ERC721Token {
if !self.exists(&token_id) {
casper_env::revert(Error::TokenDoesNotExist)
}
match self.owners.get(&token_id) {
match self
.owners
.get(&token_id)
.unwrap_or_revert_with(Error::InvalidTokenOwner)
{
Some(owner) => owner,
None => casper_env::revert(Error::InvalidTokenOwner),
}
Expand Down Expand Up @@ -167,8 +174,8 @@ impl ERC721Token {
// Clear approvals from the previous owner
self.approve_owner(Some(owner), None, token_id);

self.balances.set(&from, self.balances.get(&from) - 1);
self.balances.set(&to, self.balances.get(&to) + 1);
self.balances.set(&from, self.balance_of(from) - 1);
self.balances.set(&to, self.balance_of(to) + 1);
self.owners.set(&token_id, Some(to));

emit(Transfer {
Expand Down
6 changes: 3 additions & 3 deletions dao-erc721/src/extensions/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ impl MetadataERC721 {
}

pub fn name(&self) -> String {
self.name.get()
self.name.get_or_revert()
}

pub fn symbol(&self) -> String {
self.symbol.get()
self.symbol.get_or_revert()
}

pub fn token_uri(&self, erc721: &ERC721Token, token_id: TokenId) -> TokenUri {
Expand All @@ -32,6 +32,6 @@ impl MetadataERC721 {
}

pub fn base_uri(&self) -> TokenUri {
self.base_uri.get()
self.base_uri.get_or_revert()
}
}
4 changes: 2 additions & 2 deletions dao-modules/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use self::events::OwnerChanged;
/// The Owner module.
#[derive(Instance)]
pub struct Owner {
pub owner: Variable<Option<Address>>,
pub owner: Variable<Address>,
}

impl Owner {
Expand All @@ -24,7 +24,7 @@ impl Owner {

/// Set the owner to the new address.
pub fn change_ownership(&mut self, owner: Address) {
self.owner.set(Some(owner));
self.owner.set(owner);
emit(OwnerChanged { new_owner: owner });
}

Expand Down
18 changes: 11 additions & 7 deletions dao-modules/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ impl TokenWithStaking {
/// Stake `amount` of tokens for the `address`. It decrements `address`'s balance by `amount`.
pub fn stake(&mut self, address: Address, amount: U256) {
self.ensure_balance(&address, amount);
self.stakes
.set(&address, self.stakes.get(&address) + amount);
self.stakes.set(
&address,
self.stakes.get(&address).unwrap_or_default() + amount,
);
emit(TokensStaked { address, amount });
}

/// Unstake `amount` of tokens for the `address`. It increments `address`'s balance by `amount`.
pub fn unstake(&mut self, address: Address, amount: U256) {
self.ensure_staked_balance(&address, amount);
self.stakes
.set(&address, self.stakes.get(&address) - amount);
self.stakes.set(
&address,
self.stakes.get(&address).unwrap_or_default() - amount,
);
emit(TokensUnstaked { address, amount });
}

Expand All @@ -59,16 +63,16 @@ impl TokenWithStaking {
}

pub fn get_stake_of(&self, address: &Address) -> U256 {
self.stakes.get(address)
self.stakes.get(address).unwrap_or_default()
}

fn ensure_balance(&mut self, address: &Address, amount: U256) {
let staked_amount = self.stakes.get(address);
let staked_amount = self.stakes.get(address).unwrap_or_default();
self.token.ensure_balance(address, staked_amount + amount);
}

fn ensure_staked_balance(&mut self, address: &Address, amount: U256) {
if self.stakes.get(address) < amount {
if self.stakes.get(address).unwrap_or_default() < amount {
casper_env::revert(Error::InsufficientBalance);
}
}
Expand Down
Loading

0 comments on commit 1d8d781

Please sign in to comment.