Skip to content

Commit

Permalink
refactor: renamed account jars to account
Browse files Browse the repository at this point in the history
  • Loading branch information
VladasZ committed Sep 10, 2024
1 parent 26f7020 commit 8bffe42
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 100 deletions.
6 changes: 3 additions & 3 deletions contract/src/claim/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait ClaimCallbacks {
impl ClaimApi for Contract {
fn claim_total(&mut self, detailed: Option<bool>) -> PromiseOrValue<ClaimedAmountView> {
let account_id = env::predecessor_account_id();
self.migrate_account_jars_if_needed(&account_id);
self.migrate_account_if_needed(&account_id);
let jar_ids = self.account_jars(&account_id).iter().map(|a| U32(a.id)).collect();
self.claim_jars_internal(account_id, jar_ids, detailed)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ impl Contract {
.unwrap_or_default();

let jar = self
.account_jars
.accounts
.get_mut(&jar_before_transfer.account_id)
.unwrap_or_else(|| {
env::panic_str(&format!("Account '{}' doesn't exist", jar_before_transfer.account_id))
Expand Down Expand Up @@ -200,7 +200,7 @@ impl Contract {
}

if let Some(score) = score_before_transfer {
self.account_jars
self.accounts
.get_mut(&account_id)
.unwrap_or_else(|| panic!("Account: {account_id} does not exist"))
.score = score;
Expand Down
2 changes: 1 addition & 1 deletion contract/src/common/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Context {
.push(jar.id);

self.contract()
.account_jars
.accounts
.entry(jar.account_id.clone())
.or_default()
.push(jar.clone());
Expand Down
6 changes: 3 additions & 3 deletions contract/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Contract {
return record.jars.clone();
}

self.account_jars
self.accounts
.get(account_id)
.map_or(vec![], |record| record.jars.clone())
}
Expand All @@ -71,8 +71,8 @@ impl Contract {
}

pub(crate) fn add_new_jar(&mut self, account_id: &AccountId, jar: Jar) {
self.migrate_account_jars_if_needed(account_id);
let jars = self.account_jars.entry(account_id.clone()).or_default();
self.migrate_account_if_needed(account_id);
let jars = self.accounts.entry(account_id.clone()).or_default();
jars.last_id = jar.id;
jars.push(jar);
}
Expand Down
4 changes: 4 additions & 0 deletions contract/src/jar/account/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod v1;
pub mod versioned;

pub type AccountJarsLastVersion = v1::AccountV1;
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ use crate::{

#[near]
#[derive(Default, Debug, PartialEq)]
pub struct AccountJarsV1 {
pub struct AccountV1 {
/// The last jar ID. Is used as nonce in `get_ticket_hash` method.
pub last_id: JarId,
pub jars: Vec<Jar>,
pub score: AccountScore,
}

impl Deref for AccountJarsV1 {
impl Deref for AccountV1 {
type Target = Vec<Jar>;

fn deref(&self) -> &Self::Target {
&self.jars
}
}

impl DerefMut for AccountJarsV1 {
impl DerefMut for AccountV1 {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.jars
}
}

impl From<AccountJarsLegacy> for AccountJarsV1 {
impl From<AccountJarsLegacy> for AccountV1 {
fn from(value: AccountJarsLegacy) -> Self {
Self {
last_id: value.last_id,
Expand All @@ -42,7 +42,7 @@ impl From<AccountJarsLegacy> for AccountJarsV1 {
}
}

impl From<AccountJarsNonVersioned> for AccountJarsV1 {
impl From<AccountJarsNonVersioned> for AccountV1 {
fn from(value: AccountJarsNonVersioned) -> Self {
Self {
last_id: value.last_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ use near_sdk::borsh::{BorshDeserialize, BorshSerialize};

use crate::{
jar::{
account_jars::{v1::AccountJarsV1, AccountJarsLastVersion},
account::{v1::AccountV1, AccountJarsLastVersion},
model::AccountJarsLegacy,
},
migration::account_jars_non_versioned::AccountJarsNonVersioned,
score::AccountScore,
};

pub type AccountJars = AccountJarsVersioned;
pub type Account = AccountVersioned;

#[derive(BorshSerialize, Debug, PartialEq)]
#[borsh(crate = "near_sdk::borsh")]
pub enum AccountJarsVersioned {
V1(AccountJarsV1),
pub enum AccountVersioned {
V1(AccountV1),
}

impl AccountJarsVersioned {
impl AccountVersioned {
pub fn score(&self) -> Option<&AccountScore> {
if self.has_score_jars() {
Some(&self.score)
Expand All @@ -46,12 +46,12 @@ impl AccountJarsVersioned {

/// Custom `BorshDeserialize` implementation is needed to automatically
/// convert old versions to latest version
impl BorshDeserialize for AccountJarsVersioned {
impl BorshDeserialize for AccountVersioned {
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, Error> {
let tag: u8 = BorshDeserialize::deserialize_reader(reader)?;

let result = match tag {
0 => AccountJarsVersioned::V1(BorshDeserialize::deserialize_reader(reader)?),
0 => AccountVersioned::V1(BorshDeserialize::deserialize_reader(reader)?),
// Add new versions here:
_ => return Err(Error::new(InvalidData, format!("Unexpected variant tag: {tag:?}"))),
};
Expand All @@ -60,13 +60,13 @@ impl BorshDeserialize for AccountJarsVersioned {
}
}

impl Default for AccountJarsVersioned {
impl Default for AccountVersioned {
fn default() -> Self {
Self::V1(AccountJarsV1::default())
Self::V1(AccountV1::default())
}
}

impl Deref for AccountJarsVersioned {
impl Deref for AccountVersioned {
type Target = AccountJarsLastVersion;
fn deref(&self) -> &Self::Target {
match self {
Expand All @@ -77,7 +77,7 @@ impl Deref for AccountJarsVersioned {
}
}

impl DerefMut for AccountJarsVersioned {
impl DerefMut for AccountVersioned {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
Self::V1(jars) => jars,
Expand All @@ -87,13 +87,13 @@ impl DerefMut for AccountJarsVersioned {
}
}

impl From<AccountJarsLegacy> for AccountJars {
impl From<AccountJarsLegacy> for Account {
fn from(value: AccountJarsLegacy) -> Self {
Self::V1(value.into())
}
}

impl From<AccountJarsNonVersioned> for AccountJars {
impl From<AccountJarsNonVersioned> for Account {
fn from(value: AccountJarsNonVersioned) -> Self {
Self::V1(value.into())
}
Expand Down
4 changes: 0 additions & 4 deletions contract/src/jar/account_jars/mod.rs

This file was deleted.

15 changes: 6 additions & 9 deletions contract/src/jar/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl JarApi for Contract {
return jar.into();
}

self.account_jars
self.accounts
.get(&account_id)
.unwrap_or_else(|| panic_str(&format!("Account '{account_id}' doesn't exist")))
.get_jar(jar_id.0)
Expand Down Expand Up @@ -167,7 +167,7 @@ impl JarApi for Contract {
}

fn restake(&mut self, jar_id: JarIdView) -> JarView {
self.migrate_account_jars_if_needed(&env::predecessor_account_id());
self.migrate_account_if_needed(&env::predecessor_account_id());
let (old_id, jar) = self.restake_internal(jar_id);

emit(EventKind::Restake(RestakeData {
Expand All @@ -181,14 +181,14 @@ impl JarApi for Contract {
fn restake_all(&mut self, jars: Option<Vec<JarIdView>>) -> Vec<JarView> {
let account_id = env::predecessor_account_id();

self.migrate_account_jars_if_needed(&account_id);
self.migrate_account_if_needed(&account_id);

let now = env::block_timestamp_ms();

let jars_filter: Option<Vec<JarId>> = jars.map(|jars| jars.into_iter().map(|j| j.0).collect());

let mut jars: Vec<Jar> = self
.account_jars
.accounts
.get(&account_id)
.unwrap_or_else(|| {
panic_str(&format!("Jars for account {account_id} don't exist"));
Expand Down Expand Up @@ -223,12 +223,9 @@ impl JarApi for Contract {

fn unlock_jars_for_account(&mut self, account_id: AccountId) {
self.assert_manager();
self.migrate_account_jars_if_needed(&account_id);
self.migrate_account_if_needed(&account_id);

let jars = self
.account_jars
.get_mut(&account_id)
.expect("Account doesn't have jars");
let jars = self.accounts.get_mut(&account_id).expect("Account doesn't have jars");

for jar in &mut jars.jars {
jar.is_pending_withdraw = false;
Expand Down
2 changes: 1 addition & 1 deletion contract/src/jar/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod account_jars;
pub mod account;
pub mod api;
pub mod model;
mod tests;
Expand Down
18 changes: 9 additions & 9 deletions contract/src/jar/model/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl Contract {
// Time zone already set. No actions required.
(Some(_) | None, Some(_)) => (),
(Some(timezone), None) => {
self.account_jars.entry(account_id.clone()).or_default().score = AccountScore::new(timezone);
self.accounts.entry(account_id.clone()).or_default().score = AccountScore::new(timezone);
}
(None, None) => {
panic_str(&format!(
Expand All @@ -249,7 +249,7 @@ impl Contract {
}

pub(crate) fn top_up(&mut self, account: &AccountId, jar_id: JarId, amount: U128) -> U128 {
self.migrate_account_jars_if_needed(account);
self.migrate_account_if_needed(account);

let jar = self.get_jar_internal(account, jar_id).clone();
let product = self.get_product(&jar.product_id).clone();
Expand All @@ -271,7 +271,7 @@ impl Contract {

pub(crate) fn delete_jar(&mut self, account_id: &AccountId, jar_id: JarId) {
let jars = self
.account_jars
.accounts
.get_mut(account_id)
.unwrap_or_else(|| panic_str(&format!("Account '{account_id}' doesn't exist")));

Expand All @@ -289,15 +289,15 @@ impl Contract {
}

pub(crate) fn get_score(&self, account: &AccountId) -> Option<&AccountScore> {
self.account_jars.get(account).and_then(|a| a.score())
self.accounts.get(account).and_then(|a| a.score())
}

pub(crate) fn get_score_mut(&mut self, account: &AccountId) -> Option<&mut AccountScore> {
self.account_jars.get_mut(account).and_then(|a| a.score_mut())
self.accounts.get_mut(account).and_then(|a| a.score_mut())
}

pub(crate) fn get_jar_mut_internal(&mut self, account: &AccountId, id: JarId) -> &mut Jar {
self.account_jars
self.accounts
.get_mut(account)
.unwrap_or_else(|| env::panic_str(&format!("Account '{account}' doesn't exist")))
.get_jar_mut(id)
Expand All @@ -324,7 +324,7 @@ impl Contract {
.clone();
}

self.account_jars
self.accounts
.get(account)
.unwrap_or_else(|| env::panic_str(&format!("Account '{account}' doesn't exist")))
.get_jar(id)
Expand All @@ -338,9 +338,9 @@ impl Contract {
ticket: &JarTicket,
signature: Option<Base64VecU8>,
) {
self.migrate_account_jars_if_needed(account_id);
self.migrate_account_if_needed(account_id);

let last_jar_id = self.account_jars.get(account_id).map(|jars| jars.last_id);
let last_jar_id = self.accounts.get(account_id).map(|jars| jars.last_id);
let product = self.get_product(&ticket.product_id);

if let Some(pk) = &product.public_key {
Expand Down
8 changes: 4 additions & 4 deletions contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use sweat_jar_model::{api::InitApi, jar::JarId, ProductId};

use crate::{
jar::{
account_jars::versioned::AccountJars,
account::versioned::Account,
model::{AccountJarsLegacy, Jar},
},
migration::account_jars_non_versioned::AccountJarsNonVersioned,
Expand Down Expand Up @@ -55,7 +55,7 @@ pub struct Contract {
pub last_jar_id: JarId,

/// A lookup map that associates account IDs with sets of jars owned by each account.
pub account_jars: LookupMap<AccountId, AccountJars>,
pub accounts: LookupMap<AccountId, Account>,

pub account_jars_non_versioned: LookupMap<AccountId, AccountJarsNonVersioned>,
pub account_jars_v1: LookupMap<AccountId, AccountJarsLegacy>,
Expand All @@ -72,7 +72,7 @@ pub(crate) enum StorageKey {
ProductsV1,
/// Products migrated to step jars
ProductsV2,
AccountJarsVersioned,
AccountsVersioned,
}

#[near_bindgen]
Expand All @@ -88,7 +88,7 @@ impl InitApi for Contract {
account_jars_non_versioned: LookupMap::new(StorageKey::AccountJarsV1),
account_jars_v1: LookupMap::new(StorageKey::AccountJarsLegacy),
last_jar_id: 0,
account_jars: LookupMap::new(StorageKey::AccountJarsVersioned),
accounts: LookupMap::new(StorageKey::AccountsVersioned),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions contract/src/migration/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl Contract {

let id = self.increment_and_get_last_jar_id();

self.migrate_account_jars_if_needed(&ce_fi_jar.account_id);
let account_jars = self.account_jars.entry(ce_fi_jar.account_id.clone()).or_default();
self.migrate_account_if_needed(&ce_fi_jar.account_id);
let account_jars = self.accounts.entry(ce_fi_jar.account_id.clone()).or_default();

let jar = JarLastVersion {
id,
Expand Down
Loading

0 comments on commit 8bffe42

Please sign in to comment.