Skip to content

Commit

Permalink
fix: bnusd storage migration (#19)
Browse files Browse the repository at this point in the history
* fix: storage migration on bnusd

* fix: storage migration to bnusd

* fix: nid data type removed

* fix: removed admin and package renamed

* fix: duplicate script removed

* fix: unwrap changes

* fix: unwrap fix
  • Loading branch information
Itshyphen authored Nov 14, 2024
1 parent ae966c0 commit 275b159
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 407 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use crate::balance::{receive_balance, spend_balance};
use crate::storage_types::{get_icon_bnusd, get_xcall, get_xcall_manager};
use soroban_sdk::{xdr::ToXdr, Address, Bytes, Env, String, Vec};
mod xcall {
soroban_sdk::contractimport!(file = "../../wasm/xcall.wasm");
}

use crate::contract;
use crate::errors::ContractError;
use crate::states::read_administrator;
use crate::{
config::{get_config, set_config, ConfigData},
xcall_manager_interface::XcallManagerClient,
};
use crate::xcall_manager_interface::XcallManagerClient;
use soroban_rlp::balanced::address_utils::is_valid_bytes_address;
use soroban_rlp::balanced::messages::{
cross_transfer::CrossTransfer, cross_transfer_revert::CrossTransferRevert,
Expand All @@ -20,9 +17,6 @@ use xcall::{AnyMessage, CallMessageWithRollback, Client, Envelope};
const CROSS_TRANSFER: &str = "xCrossTransfer";
const CROSS_TRANSFER_REVERT: &str = "xCrossTransferRevert";

pub fn configure(env: Env, config: ConfigData) {
set_config(&env, config);
}

pub fn _cross_transfer(
e: Env,
Expand All @@ -38,13 +32,12 @@ pub fn _cross_transfer(
}
let xcall_message = CrossTransfer::new(from.clone().to_string(), to, amount, data);
let rollback = CrossTransferRevert::new(from.clone(), amount);
let config = get_config(&e);
let icon_bn_usd = config.icon_bn_usd;
let icon_bn_usd = get_icon_bnusd(&e)?;

let rollback_bytes = rollback.encode(&e, String::from_str(&e, CROSS_TRANSFER_REVERT));
let message_bytes = xcall_message.encode(&e, String::from_str(&e, CROSS_TRANSFER));

let (sources, destinations) = xcall_manager_client(&e, &config.xcall_manager).get_protocols();
let (sources, destinations) = xcall_manager_client(&e, &get_xcall_manager(&e)?).get_protocols();

let message = AnyMessage::CallMessageWithRollback(CallMessageWithRollback {
data: message_bytes,
Expand All @@ -57,7 +50,7 @@ pub fn _cross_transfer(
};

let current_address = e.current_contract_address();
xcall_client(&e, &config.xcall).send_call(&from, &current_address, envelope, &icon_bn_usd);
xcall_client(&e, &get_xcall(&e)?).send_call(&from, &current_address, envelope, &icon_bn_usd);
Ok(())
}

Expand All @@ -79,12 +72,11 @@ pub fn _handle_call_message(
data: Bytes,
protocols: Vec<String>,
) -> Result<(), ContractError> {
let config: ConfigData = get_config(&e);
let xcall = config.xcall;
let xcall = get_xcall(&e)?;
xcall.require_auth();

let method = CrossTransfer::get_method(&e, data.clone());
let icon_bn_usd: String = config.icon_bn_usd;
let icon_bn_usd: String = get_icon_bnusd(&e)?;
if method == String::from_str(&e, &CROSS_TRANSFER) {
if from != icon_bn_usd {
return Err(ContractError::OnlyIconBnUSD);
Expand All @@ -110,7 +102,7 @@ pub fn _handle_call_message(
} else {
return Err(ContractError::UnknownMessageType);
}
verify_protocol(&e, &config.xcall_manager, protocols)?;
verify_protocol(&e, &get_xcall_manager(&e)?, protocols)?;
Ok(())
}

Expand Down Expand Up @@ -150,8 +142,7 @@ pub fn get_address(network_address: String, env: &Env) -> Result<Address, Contra

fn _mint(e: &Env, to: Address, amount: i128) {
contract::check_nonnegative_amount(amount);
let admin: Address = read_administrator(&e);

let admin = e.current_contract_address();
receive_balance(e, to.clone(), amount);
TokenUtils::new(e).events().mint(admin, to, amount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
//! interface.
use crate::allowance::{read_allowance, spend_allowance, write_allowance};
use crate::balance::{read_balance, receive_balance, spend_balance};
use crate::balanced_dollar;
use crate::config::{self, ConfigData};
use crate::{balanced_dollar, storage_types};
use crate::errors::ContractError;
use crate::metadata::{read_decimal, read_name, read_symbol, write_metadata};
use crate::states::{has_administrator, read_administrator, write_administrator};
use crate::storage_types::{INSTANCE_BUMP_AMOUNT, INSTANCE_LIFETIME_THRESHOLD};
use crate::storage_types::{get_upgrade_authority, set_icon_bnusd, set_upgrade_authority, set_xcall, set_xcall_manager, INSTANCE_BUMP_AMOUNT, INSTANCE_LIFETIME_THRESHOLD
};
use soroban_sdk::{
contract, contractimpl, panic_with_error, Address, Bytes, BytesN, Env, String, Vec,
};
Expand All @@ -24,21 +23,15 @@ pub struct BalancedDollar;

#[contractimpl]
impl BalancedDollar {
pub fn initialize(e: Env, admin: Address, config: ConfigData) {
if has_administrator(&e) {
pub fn initialize(e: Env, xcall: Address, xcall_manager: Address, icon_bnusd: String, upgrade_auth: Address) {
if storage_types::has_upgrade_auth(&e) {
panic_with_error!(e, ContractError::ContractAlreadyInitialized)
}
write_administrator(&e, &admin);

//initialize token properties
let decimal = 18;
let name = String::from_str(&e, "Balanced Dollar");
let symbol = String::from_str(&e, "bnUSD");

if decimal > u8::MAX.into() {
panic_with_error!(e, ContractError::DecimalMustFitInAu8)
}

write_metadata(
&e,
TokenMetadata {
Expand All @@ -47,19 +40,10 @@ impl BalancedDollar {
symbol,
},
);
balanced_dollar::configure(e, config);
}

pub fn set_admin(e: Env, new_admin: Address) {
let admin = read_administrator(&e);
admin.require_auth();

write_administrator(&e, &new_admin);
TokenUtils::new(&e).events().set_admin(admin, new_admin);
}

pub fn get_admin(e: Env) -> Address {
read_administrator(&e)
set_xcall(&e, xcall);
set_icon_bnusd(&e, icon_bnusd);
set_xcall_manager(&e, xcall_manager);
set_upgrade_authority(&e, upgrade_auth);
}

pub fn cross_transfer(
Expand All @@ -84,22 +68,18 @@ impl BalancedDollar {
}

pub fn is_initialized(e: Env) -> bool {
has_administrator(&e)
storage_types::has_upgrade_auth(&e)
}

pub fn set_upgrade_authority(e: Env, upgrade_authority: Address) {
let mut config = config::get_config(&e);

config.upgrade_authority.require_auth();

config.upgrade_authority = upgrade_authority;
config::set_config(&e, config);
pub fn set_upgrade_authority(e: Env, new_upgrade_authority: Address) {
let upgrade_authority = get_upgrade_authority(&e).unwrap();
upgrade_authority.require_auth();
set_upgrade_authority(&e, new_upgrade_authority);
}

pub fn upgrade(e: Env, new_wasm_hash: BytesN<32>) {
let config = config::get_config(&e);
config.upgrade_authority.require_auth();

let upgrade_authority = get_upgrade_authority(&e).unwrap();
upgrade_authority.require_auth();
e.deployer().update_current_contract_wasm(new_wasm_hash);
}

Expand Down Expand Up @@ -159,4 +139,13 @@ impl BalancedDollar {
pub fn symbol(e: Env) -> String {
read_symbol(&e)
}

pub fn xcall_manager(e: Env) -> Address {
storage_types::get_xcall_manager(&e).unwrap()

}

pub fn xcall(e: Env) -> Address {
storage_types::get_xcall(&e).unwrap()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ pub enum ContractError {
InvalidAddress = 8,
InvalidNetworkAddressLength = 9,
InvalidNetworkAddress = 10,
InvalidAmount = 11
InvalidAmount = 11,
Uninitialized = 12
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
mod allowance;
mod balance;
pub mod balanced_dollar;
mod config;
pub mod contract;
mod errors;
mod metadata;
mod states;
mod storage_types;
mod tests;
mod xcall_manager_interface;
File renamed without changes.
87 changes: 87 additions & 0 deletions contracts/balanced_dollar/src/storage_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use soroban_sdk::{contracttype, Address, Env, String};

use crate::errors::ContractError;

pub(crate) const DAY_IN_LEDGERS: u32 = 17280;
pub(crate) const INSTANCE_BUMP_AMOUNT: u32 = 7 * DAY_IN_LEDGERS;
pub(crate) const INSTANCE_LIFETIME_THRESHOLD: u32 = INSTANCE_BUMP_AMOUNT - DAY_IN_LEDGERS;

pub(crate) const BALANCE_BUMP_AMOUNT: u32 = 30 * DAY_IN_LEDGERS;
pub(crate) const BALANCE_LIFETIME_THRESHOLD: u32 = BALANCE_BUMP_AMOUNT - DAY_IN_LEDGERS;

#[derive(Clone)]
#[contracttype]
pub struct AllowanceDataKey {
pub from: Address,
pub spender: Address,
}

#[contracttype]
pub struct AllowanceValue {
pub amount: i128,
pub expiration_ledger: u32,
}

#[derive(Clone)]
#[contracttype]
pub enum DataKey {
Allowance(AllowanceDataKey),
Balance(Address),
XcallManager,
XCall,
IconBnusd,
UpgradeAuthority
}

pub fn set_xcall_manager(e: &Env, value: Address) {
e.storage().instance().set(&DataKey::XcallManager, &value);
}

pub fn set_xcall(e: &Env, value: Address) {
e.storage().instance().set(&DataKey::XCall, &value);
}

pub fn set_icon_bnusd(e: &Env, value: String) {
e.storage().instance().set(&DataKey::IconBnusd, &value);
}

pub fn set_upgrade_authority(e: &Env, value: Address) {
e.storage().instance().set(&DataKey::UpgradeAuthority, &value);
}

pub fn has_upgrade_auth(e: &Env) -> bool {
let key = DataKey::UpgradeAuthority;
e.storage().instance().has(&key)
}

pub fn get_xcall_manager(e: &Env) -> Result<Address, ContractError> {
let key = DataKey::XcallManager;
e.storage()
.instance()
.get(&key)
.ok_or(ContractError::Uninitialized)}

pub fn get_xcall(e: &Env) -> Result<Address, ContractError> {
let key = DataKey::XCall;
e.storage()
.instance()
.get(&key)
.ok_or(ContractError::Uninitialized)}

pub fn get_icon_bnusd(e: &Env) -> Result<String, ContractError> {
let key = DataKey::IconBnusd;
e.storage()
.instance()
.get(&key)
.ok_or(ContractError::Uninitialized)}

pub fn get_upgrade_authority(e: &Env) -> Result<Address, ContractError> {
let key = DataKey::UpgradeAuthority;
e.storage()
.instance()
.get(&key)
.ok_or(ContractError::Uninitialized)}




Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#![cfg(test)]
extern crate std;

use crate::{config, contract::BalancedDollarClient};
use crate::{contract::BalancedDollarClient, storage_types::get_upgrade_authority};

use super::setup::*;
use soroban_rlp::balanced::messages::{
cross_transfer::CrossTransfer, cross_transfer_revert::CrossTransferRevert,
};
use soroban_sdk::{
symbol_short,
testutils::{Address as _, AuthorizedFunction, AuthorizedInvocation},
Address, Bytes, IntoVal, String, Symbol, Vec,
};
Expand All @@ -24,31 +23,6 @@ fn test_initialize() {
assert_eq!(initialized, true)
}

#[test]
fn test_set_admin() {
let ctx = TestContext::default();
let client = BalancedDollarClient::new(&ctx.env, &ctx.registry);
ctx.init_context(&client);

let new_admin: Address = Address::generate(&ctx.env);
client.set_admin(&new_admin);
assert_eq!(
ctx.env.auths(),
std::vec![(
ctx.admin.clone(),
AuthorizedInvocation {
function: AuthorizedFunction::Contract((
ctx.registry.clone(),
symbol_short!("set_admin"),
(&new_admin,).into_val(&ctx.env)
)),
sub_invocations: std::vec![]
}
)]
);
assert_eq!(client.get_admin(), new_admin);
}

#[test]
fn test_cross_transfer_with_to_and_data() {
let ctx = TestContext::default();
Expand Down Expand Up @@ -398,7 +372,7 @@ fn test_set_upgrade_authority() {
);

ctx.env.as_contract(&client.address, || {
let config = config::get_config(&ctx.env);
assert_eq!(config.upgrade_authority, new_upgrade_authority)
let upgrade_authority = get_upgrade_authority(&ctx.env).unwrap();
assert_eq!(upgrade_authority, new_upgrade_authority)
});
}
File renamed without changes.
Loading

0 comments on commit 275b159

Please sign in to comment.