Skip to content

Commit

Permalink
migration works
Browse files Browse the repository at this point in the history
  • Loading branch information
petersalomonsen committed Dec 24, 2023
1 parent 7e129a0 commit 11bae7c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
39 changes: 22 additions & 17 deletions nearcontract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,35 @@ pub struct Invitation {
signingkey: PublicKey,
}

#[derive(Default, BorshDeserialize, BorshSerialize)]
#[derive(PanicOnDefault, BorshDeserialize, BorshSerialize)]
pub struct Invitations {
invitations: HashMap<InvitationId, Invitation>,
}

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
#[derive(PanicOnDefault, BorshDeserialize, BorshSerialize)]
pub struct RepositoryPermission {
permission: HashMap<String, HashMap<String, Permission>>,
}

#[near_bindgen]
#[derive(PanicOnDefault,BorshDeserialize, BorshSerialize)]
pub struct RepositoryPermissionV2 {
pub struct Contract {
permission: LookupMap<String, HashMap<String, Permission>>,
}

#[near_bindgen]
impl RepositoryPermissionV2 {
#[init]
impl Contract {
#[init(ignore_state)]
pub fn new() -> Self {
let contract = Self {
let old_state: RepositoryPermission = env::state_read().expect("failed");

let mut contract = Self {
permission: LookupMap::new(REPOSITORY_PERMISSION_KEY.to_vec()),
};
for (key, value) in old_state.permission {
contract.permission.insert(&key, &value);
}
contract
}

Expand Down Expand Up @@ -230,7 +235,7 @@ mod tests {
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract = RepositoryPermissionV2::new();
let mut contract = Contract::new();
assert_eq!(
PERMISSION_FREE,
contract.get_permission("peter".to_string(), "testrepo".to_string())
Expand Down Expand Up @@ -258,7 +263,7 @@ mod tests {
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract = RepositoryPermissionV2::new();
let mut contract = Contract::new();
assert_eq!(
PERMISSION_FREE,
contract.get_permission("johan".to_string(), "testrepo".to_string())
Expand Down Expand Up @@ -306,7 +311,7 @@ mod tests {
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract: RepositoryPermissionV2 = RepositoryPermissionV2::new();
let mut contract: Contract = Contract::new();
assert_eq!(
PERMISSION_FREE,
contract.get_permission("johan".to_string(), "testrepo".to_string())
Expand Down Expand Up @@ -347,7 +352,7 @@ mod tests {
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract = RepositoryPermissionV2::new();
let mut contract = Contract::new();
assert_eq!(
PERMISSION_FREE,
contract.get_permission("peter".to_string(), "testrepo".to_string())
Expand Down Expand Up @@ -392,7 +397,7 @@ mod tests {
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract = RepositoryPermissionV2::new();
let mut contract = Contract::new();
assert_eq!(
PERMISSION_FREE,
contract.get_permission("peter".to_string(), "testrepo".to_string())
Expand Down Expand Up @@ -445,7 +450,7 @@ mod tests {
fn require_attached_deposits() {
let context = get_context("peter".to_string(), vec![], false, 2);
testing_env!(context);
let mut contract = RepositoryPermissionV2::new();
let mut contract = Contract::new();

let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
return contract.set_permission(
Expand All @@ -465,7 +470,7 @@ mod tests {
false,
REQUIRED_ATTACHED_DEPOSIT
));
let mut contract = RepositoryPermissionV2::new();
let mut contract = Contract::new();
let path = "lalala";
contract.set_permission(path.to_string(), "peter".to_string(), PERMISSION_OWNER);
let invitationid = contract.invite(path.to_string(), PERMISSION_READER);
Expand All @@ -485,7 +490,7 @@ mod tests {
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract = RepositoryPermissionV2::new();
let mut contract = Contract::new();
let path = "lalala";
contract.set_permission(
path.to_string(),
Expand All @@ -508,7 +513,7 @@ mod tests {
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract = RepositoryPermissionV2::new();
let mut contract = Contract::new();
let path = "lalala";
contract.set_permission(path.to_string(), "peter".to_string(), PERMISSION_OWNER);
let invitationid = contract.invite(path.to_string(), PERMISSION_READER);
Expand All @@ -531,7 +536,7 @@ mod tests {
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract = RepositoryPermissionV2::new();
let mut contract = Contract::new();
let path = "lalala";
contract.set_permission(path.to_string(), "peter".to_string(), PERMISSION_OWNER);
let invitationid = contract.invite(path.to_string(), PERMISSION_READER);
Expand All @@ -556,7 +561,7 @@ mod tests {
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract = RepositoryPermissionV2::new();
let mut contract = Contract::new();
let mut path = "lalala";
contract.set_permission(path.to_string(), "peter".to_string(), PERMISSION_OWNER);
let mut invitationid = contract.invite(path.to_string(), PERMISSION_READER);
Expand Down
3 changes: 3 additions & 0 deletions nearcontract/tests/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ async fn test_deploy_contract_self_upgrade() -> anyhow::Result<()> {
let contract_deploy_result = contract.as_account().deploy(wasm.as_slice()).await?;
assert!(contract_deploy_result.is_success());

let initialize_result = contract.call("new").transact().await?;
assert!(initialize_result.is_success());

let get_permission_after_upgrade_result: serde_json::Value = contract
.call("get_permission")
.args_json(json!({
Expand Down
2 changes: 0 additions & 2 deletions nearcontract/tests/test_env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use near_units::parse_near;
use near_workspaces::{AccountId, types::NearToken};
use serde_json::json;

const WASMGIT_CONTRACT: &str = "wasmgit.near";

Expand Down

0 comments on commit 11bae7c

Please sign in to comment.