Skip to content

Commit

Permalink
Add ability to change admin of enterprise-versioning contract
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDeeKay committed Sep 2, 2024
1 parent 6bec4ff commit 60f2fb4
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 8 deletions.
27 changes: 23 additions & 4 deletions contracts/enterprise-versioning/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ use cosmwasm_std::{
use cw2::set_contract_version;
use cw_storage_plus::Bound;
use enterprise_versioning_api::api::{
AddVersionMsg, AdminResponse, EditVersionMsg, VersionInfo, VersionParams, VersionResponse,
VersionsParams, VersionsResponse,
AddVersionMsg, AdminResponse, EditVersionMsg, UpdateAdminMsg, VersionInfo, VersionParams,
VersionResponse, VersionsParams, VersionsResponse,
};
use enterprise_versioning_api::error::EnterpriseVersioningError::{
NoVersionsExist, Unauthorized, VersionAlreadyExists, VersionNotFound,
};
use enterprise_versioning_api::error::EnterpriseVersioningResult;
use enterprise_versioning_api::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use enterprise_versioning_api::response::{
execute_add_version_response, execute_edit_version_response, instantiate_response,
execute_add_version_response, execute_edit_version_response, execute_update_admin_response,
instantiate_response,
};

// version info for migration info
Expand Down Expand Up @@ -52,11 +53,29 @@ pub fn execute(
let ctx = &mut Context { deps, env, info };

match msg {
ExecuteMsg::UpdateAdmin(msg) => update_admin(ctx, msg),
ExecuteMsg::AddVersion(msg) => add_version(ctx, msg),
ExecuteMsg::EditVersion(msg) => edit_version(ctx, msg),
}
}

fn update_admin(ctx: &mut Context, msg: UpdateAdminMsg) -> EnterpriseVersioningResult<Response> {
let admin = ADMIN.load(ctx.deps.storage)?;

if ctx.info.sender != admin {
return Err(Unauthorized);
}

let new_admin = ctx.deps.api.addr_validate(&msg.new_admin)?;

ADMIN.save(ctx.deps.storage, &new_admin)?;

Ok(execute_update_admin_response(
admin.to_string(),
new_admin.to_string(),
))
}

fn add_version(ctx: &mut Context, msg: AddVersionMsg) -> EnterpriseVersioningResult<Response> {
let admin = ADMIN.load(ctx.deps.storage)?;

Expand Down Expand Up @@ -91,7 +110,7 @@ fn edit_version(ctx: &mut Context, msg: EditVersionMsg) -> EnterpriseVersioningR
None => {
return Err(VersionNotFound {
version: msg.version,
})
});
}
Some(version_info) => {
let edited_version = apply_edit_changes(version_info, &msg)?;
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"migrate:enterprise-facade": "TERRARIUMS_ARCH_ARM64=true yarn terrariums run tasks/migrate_enterprise_facade.ts --network testnet --signer pisco",
"migrate:enterprise-facade:mainnet": "yarn terrariums run tasks/migrate_enterprise_facade.ts --network mainnet --signer phoenix",
"migrate:enterprise-facade:staging": "TERRARIUMS_ARCH_ARM64=true yarn terrariums run tasks/migrate_enterprise_facade.ts --network staging --signer staging",
"migrate:enterprise-versioning:mainnet": "yarn terrariums run tasks/migrate_enterprise_versioning.ts --network mainnet --signer phoenix",
"migrate:enterprise-versioning:staging": "TERRARIUMS_ARCH_ARM64=true yarn terrariums run tasks/migrate_enterprise_versioning.ts --network staging --signer staging",
"warp:migration-jobs": "yarn terrariums run tasks/warp_migration_jobs.ts --network testnet --signer pisco",
"warp:migration-jobs:mainnet": "yarn terrariums run tasks/warp_migration_jobs.ts --network mainnet --signer phoenix",
"docker:enterprise-api": "docker build . -f apps/enterprise-api/Dockerfile -t payments/api",
Expand Down
5 changes: 5 additions & 0 deletions packages/enterprise-versioning-api/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ pub struct VersionInfo {
pub multisig_membership_code_id: u64,
}

#[cw_serde]
pub struct UpdateAdminMsg {
pub new_admin: String,
}

#[cw_serde]
pub struct AddVersionMsg {
pub version: VersionInfo,
Expand Down
6 changes: 4 additions & 2 deletions packages/enterprise-versioning-api/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::api::{
AddVersionMsg, AdminResponse, EditVersionMsg, VersionParams, VersionResponse, VersionsParams,
VersionsResponse,
AddVersionMsg, AdminResponse, EditVersionMsg, UpdateAdminMsg, VersionParams, VersionResponse,
VersionsParams, VersionsResponse,
};
use cosmwasm_schema::{cw_serde, QueryResponses};

Expand All @@ -11,6 +11,8 @@ pub struct InstantiateMsg {

#[cw_serde]
pub enum ExecuteMsg {
UpdateAdmin(UpdateAdminMsg),

AddVersion(AddVersionMsg),
EditVersion(EditVersionMsg),
}
Expand Down
7 changes: 7 additions & 0 deletions packages/enterprise-versioning-api/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ pub fn instantiate_response(admin: String) -> Response {
.add_attribute("admin", admin)
}

pub fn execute_update_admin_response(old_admin: String, new_admin: String) -> Response {
Response::new()
.add_attribute("action", "update_admin")
.add_attribute("old_admin", old_admin)
.add_attribute("new_admin", new_admin)
}

pub fn execute_add_version_response(version: String) -> Response {
Response::new()
.add_attribute("action", "add_version")
Expand Down
2 changes: 1 addition & 1 deletion refs.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"address": "terra136k05x7uzu0awwxgr8wtqunedjpnv4jzmc4ery40czx5338ht8vq0ja9xq"
},
"enterprise-versioning": {
"codeId": "2220",
"codeId": "3168",
"address": "terra1vff3unw6t795t6zm62j94grscl8qfeurzpvdtpeclmwc0n5hm5csxuhjmw"
},
"cw3_fixed_multisig": {
Expand Down
33 changes: 32 additions & 1 deletion tasks/helper_scripts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import task, {Executor, Refs} from "@terra-money/terrariums";
import task, {Executor, info, Refs} from "@terra-money/terrariums";
import {MsgUpdateContractAdmin} from "@terra-money/terra.js";
import {Signer} from "@terra-money/terrariums/lib/src/signers";

const ENTERPRISE_FACTORY = "enterprise-factory";

Expand Down Expand Up @@ -122,6 +124,35 @@ task(async ({network, executor, refs}) => {
}
});

const updateContractAdmin = async (contract: string, signer: Signer, refs: Refs, network: string): Promise<void> => {
try {
let contractAddr = refs.getAddress(network, contract);

let new_admin = 'terra1n7tt982ksrwhlsa6s8cftyjnsj6gwnl7rln3sj';

info(`Updating admin of ${contract} at ${contractAddr} to ${new_admin}`);

let msg = new MsgUpdateContractAdmin(
signer.key.accAddress,
new_admin,
contractAddr,
);
try {
let tx = await signer.createAndSignTx({
msgs: [msg],
});
await signer.lcd.tx.broadcast(tx);
info(`Updated admin of ${contract} contract.`);
} catch (e) {
info(`Updating admin of ${contract} contract has failed.`);
info(JSON.stringify(e));
console.log(e);
}
} catch (e) {
console.log(e);
}
}

const stakeTokens = async (executor: Executor, token_contract: string, membership_contract: string): Promise<void> => {
await executor.execute(
token_contract,
Expand Down
34 changes: 34 additions & 0 deletions tasks/migrate_enterprise_versioning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {MsgMigrateContract} from "@terra-money/terra.js";
import task, {info} from "@terra-money/terrariums";

const ENTERPRISE_VERSIONING = "enterprise-versioning";

task(async ({deployer, signer, refs, network}) => {
// deployer.buildContract(ENTERPRISE);
// deployer.optimizeContract(ENTERPRISE);
//
await deployer.storeCode(ENTERPRISE_VERSIONING);
await new Promise((resolve) => setTimeout(resolve, 5000));

const contract = refs.getContract(network, ENTERPRISE_VERSIONING);

let msg = new MsgMigrateContract(
signer.key.accAddress,
contract.address!,
parseInt(contract.codeId!),
{},
);

try {
let tx = await signer.createAndSignTx({
msgs: [msg],
});
await signer.lcd.tx.broadcast(tx);
info(`Migrated ${ENTERPRISE_VERSIONING} contract.`);
} catch (e) {
info(`Migrating ${ENTERPRISE_VERSIONING} contract has failed.`);
info(JSON.stringify(e));
}

refs.saveRefs();
});

0 comments on commit 60f2fb4

Please sign in to comment.