Skip to content

Commit

Permalink
feat: genralized token contract (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itshyphen authored Nov 15, 2024
1 parent 275b159 commit fd85a27
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "balanced-dollar"
name = "spoke-token"
version = "0.1.0"
edition = "2021"

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//! interface.
use crate::allowance::{read_allowance, spend_allowance, write_allowance};
use crate::balance::{read_balance, receive_balance, spend_balance};
use crate::{balanced_dollar, storage_types};
use crate::{spoke_token, storage_types};
use crate::errors::ContractError;
use crate::metadata::{read_decimal, read_name, read_symbol, write_metadata};
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 crate::storage_types::{get_upgrade_authority, set_icon_hub_token, 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 @@ -23,14 +23,10 @@ pub struct BalancedDollar;

#[contractimpl]
impl BalancedDollar {
pub fn initialize(e: Env, xcall: Address, xcall_manager: Address, icon_bnusd: String, upgrade_auth: Address) {
pub fn initialize(e: Env, xcall: Address, xcall_manager: Address, hub_token: String, upgrade_auth: Address, name: String, symbol: String, decimal: u32) {
if storage_types::has_upgrade_auth(&e) {
panic_with_error!(e, ContractError::ContractAlreadyInitialized)
}
//initialize token properties
let decimal = 18;
let name = String::from_str(&e, "Balanced Dollar");
let symbol = String::from_str(&e, "bnUSD");

write_metadata(
&e,
Expand All @@ -41,7 +37,7 @@ impl BalancedDollar {
},
);
set_xcall(&e, xcall);
set_icon_bnusd(&e, icon_bnusd);
set_icon_hub_token(&e, hub_token);
set_xcall_manager(&e, xcall_manager);
set_upgrade_authority(&e, upgrade_auth);
}
Expand All @@ -55,7 +51,7 @@ impl BalancedDollar {
) -> Result<(), ContractError> {
from.require_auth();
let transfer_data = data.unwrap_or(Bytes::from_array(&e, &[0u8; 32]));
return balanced_dollar::_cross_transfer(e.clone(), from, amount, to, transfer_data);
return spoke_token::_cross_transfer(e.clone(), from, amount, to, transfer_data);
}

pub fn handle_call_message(
Expand All @@ -64,7 +60,7 @@ impl BalancedDollar {
data: Bytes,
protocols: Vec<String>,
) -> Result<(), ContractError> {
return balanced_dollar::_handle_call_message(e, from, data, protocols);
return spoke_token::_handle_call_message(e, from, data, protocols);
}

pub fn is_initialized(e: Env) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub enum ContractError {
ContractAlreadyInitialized = 2,
DecimalMustFitInAu8 = 3,
ProtocolMismatch = 4,
OnlyIconBnUSD = 5,
OnlyIconHubToken = 5,
OnlyCallService = 6,
UnknownMessageType = 7,
InvalidAddress = 8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

mod allowance;
mod balance;
pub mod balanced_dollar;
pub mod spoke_token;
pub mod contract;
mod errors;
mod metadata;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::balance::{receive_balance, spend_balance};
use crate::storage_types::{get_icon_bnusd, get_xcall, get_xcall_manager};
use crate::storage_types::{get_icon_hub_token, get_xcall, get_xcall_manager};
use soroban_sdk::{xdr::ToXdr, Address, Bytes, Env, String, Vec};
mod xcall {
soroban_sdk::contractimport!(file = "../../wasm/xcall.wasm");
Expand Down Expand Up @@ -32,7 +32,7 @@ pub fn _cross_transfer(
}
let xcall_message = CrossTransfer::new(from.clone().to_string(), to, amount, data);
let rollback = CrossTransferRevert::new(from.clone(), amount);
let icon_bn_usd = get_icon_bnusd(&e)?;
let icon_bn_usd = get_icon_hub_token(&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));
Expand Down Expand Up @@ -76,10 +76,10 @@ pub fn _handle_call_message(
xcall.require_auth();

let method = CrossTransfer::get_method(&e, data.clone());
let icon_bn_usd: String = get_icon_bnusd(&e)?;
let icon_bn_usd: String = get_icon_hub_token(&e)?;
if method == String::from_str(&e, &CROSS_TRANSFER) {
if from != icon_bn_usd {
return Err(ContractError::OnlyIconBnUSD);
return Err(ContractError::OnlyIconHubToken);
}
let message = CrossTransfer::decode(&e, data);
let to_network_address: Address = get_address(message.to, &e)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub enum DataKey {
Balance(Address),
XcallManager,
XCall,
IconBnusd,
HubToken,
UpgradeAuthority
}

Expand All @@ -41,8 +41,8 @@ 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_icon_hub_token(e: &Env, value: String) {
e.storage().instance().set(&DataKey::HubToken, &value);
}

pub fn set_upgrade_authority(e: &Env, value: Address) {
Expand All @@ -68,8 +68,8 @@ pub fn get_xcall(e: &Env) -> Result<Address, ContractError> {
.get(&key)
.ok_or(ContractError::Uninitialized)}

pub fn get_icon_bnusd(e: &Env) -> Result<String, ContractError> {
let key = DataKey::IconBnusd;
pub fn get_icon_hub_token(e: &Env) -> Result<String, ContractError> {
let key = DataKey::HubToken;
e.storage()
.instance()
.get(&key)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ impl TestContext {
icon_bn_usd: self.icon_bn_usd.clone(),
upgrade_authority: self.upgrade_authority.clone(),
};
client.initialize(&config.xcall, &config.xcall_manager, &config.icon_bn_usd, &config.upgrade_authority);
//initialize token properties
let decimal = 18;
let name = String::from_str(&self.env, "Balanced Dollar");
let symbol = String::from_str(&self.env, "bnUSD");
client.initialize(&config.xcall, &config.xcall_manager, &config.icon_bn_usd, &config.upgrade_authority, &name, &symbol, &decimal);
}

pub fn init_xcall_manager_context(&self) {
Expand Down

0 comments on commit fd85a27

Please sign in to comment.