fix
This commit is contained in:
parent
0da512484f
commit
2a2d69dafb
@ -60,8 +60,8 @@ let shareholder1 = new_shareholder()
|
||||
.user_id(sh1_user_id)
|
||||
.name(sh1_name)
|
||||
.shares(1000.0)
|
||||
.percentage(10.0)
|
||||
.type_(ShareholderTypeConstants::Individual)
|
||||
.percentage(10.0) // CALCULATED
|
||||
.type_("Individual")
|
||||
.since(sh1_since)
|
||||
.set_base_created_at(1672617600);
|
||||
|
@ -11,15 +11,15 @@ let acc1 = new_account()
|
||||
.set_ledger("LedgerX")
|
||||
.set_address("0x123MainSt")
|
||||
.set_pubkey("pubkeyUser1");
|
||||
print(`Created account (pre-save): ${acc1.get_name()} with temp ID ${acc1.get_id()}`);
|
||||
print(`Created account (pre-save): ${acc1.name} with temp ID ${acc1.id}`);
|
||||
|
||||
// 2. Save Account to Mock DB and get the version with DB-assigned ID
|
||||
let acc1 = set_account(acc1); // Shadowing acc1 with the returned instance
|
||||
print(`Account ${acc1.get_name()} saved to DB with ID ${acc1.get_id()}.`);
|
||||
print(`Account ${acc1.name} saved to DB with ID ${acc1.id}.`);
|
||||
|
||||
// 3. Retrieve Account from Mock DB (using the new ID)
|
||||
let fetched_acc1 = get_account_by_id(acc1.get_id()); // Use the ID from the saved acc1
|
||||
print(`Fetched account from DB: ${fetched_acc1.get_name()}, User ID: ${fetched_acc1.get_user_id()}`);
|
||||
let fetched_acc1 = get_account_by_id(acc1.id); // Use the ID from the saved acc1
|
||||
print(`Fetched account from DB: ${fetched_acc1.name}, User ID: ${fetched_acc1.user_id}`);
|
||||
|
||||
// 4. Create an Asset using the builder pattern
|
||||
let asset1 = new_asset()
|
||||
@ -29,19 +29,19 @@ let asset1 = new_asset()
|
||||
.set_address("0xTokenContract")
|
||||
.set_asset_type("Erc20") // Setter handles string to enum
|
||||
.set_decimals(18);
|
||||
print(`Created asset (pre-save): ${asset1.get_name()} (temp ID: ${asset1.get_id()}), Amount: ${asset1.get_amount()}, Type: ${asset1.get_asset_type_str()}`);
|
||||
print(`Created asset (pre-save): ${asset1.name} (temp ID: ${asset1.id}), Amount: ${asset1.amount}, Type: ${asset1.asset_type_str}`);
|
||||
|
||||
// 5. Save Asset to Mock DB and get the version with DB-assigned ID
|
||||
let asset1 = set_asset(asset1); // Shadowing asset1
|
||||
print(`Asset ${asset1.get_name()} (ID: ${asset1.get_id()}) saved to DB.`);
|
||||
print(`Asset ${asset1.name} (ID: ${asset1.id}) saved to DB.`);
|
||||
|
||||
// 6. Retrieve Asset from Mock DB (using the new ID)
|
||||
let fetched_asset1 = get_asset_by_id(asset1.get_id()); // Use the ID from the saved asset1
|
||||
print(`Fetched asset from DB: ${fetched_asset1.get_name()}, Address: ${fetched_asset1.get_address()}`);
|
||||
let fetched_asset1 = get_asset_by_id(asset1.id); // Use the ID from the saved asset1
|
||||
print(`Fetched asset from DB: ${fetched_asset1.name}, Address: ${fetched_asset1.address}`);
|
||||
|
||||
// 7. Add Asset to Account
|
||||
// We have 'acc1' and 'asset1' from previous steps, both saved to DB and have their IDs.
|
||||
print(`Attempting to add asset ${asset1.get_id()} to account ${acc1.get_id()}`);
|
||||
print(`Attempting to add asset ${asset1.id} to account ${acc1.id}`);
|
||||
|
||||
// Fetch the latest version of the account before modifying
|
||||
// let mut acc1_for_update = get_account_by_id(acc1.get_id());
|
||||
@ -51,16 +51,16 @@ print(`Attempting to add asset ${asset1.get_id()} to account ${acc1.get_id()}`);
|
||||
// try {
|
||||
// acc1_for_update = acc1_for_update.add_asset(asset_to_add); // add_asset returns the modified account
|
||||
// acc1_for_update = set_account(acc1_for_update); // Save the account with the new asset
|
||||
// print(`Asset '${asset_to_add.get_name()}' added to account '${acc1_for_update.get_name()}'.`);
|
||||
// print(`Asset '${asset_to_add.name}' added to account '${acc1_for_update.name}'.`);
|
||||
// print(`Account now has ${acc1_for_update.get_assets_cloned().len()} assets.`);
|
||||
// // Verify the asset is there
|
||||
if (acc1_for_update.get_assets_cloned().len() > 0) {
|
||||
let first_asset_in_account = acc1_for_update.get_assets_cloned()[0];
|
||||
print(`First asset in account: ${first_asset_in_account.get_name()} (ID: ${first_asset_in_account.get_id()})`);
|
||||
}
|
||||
} catch (err) {
|
||||
print(`Error adding asset to account: ${err}`);
|
||||
}
|
||||
// if (acc1_for_update.get_assets_cloned().len() > 0) {
|
||||
// let first_asset_in_account = acc1_for_update.get_assets_cloned()[0];
|
||||
// print(`First asset in account: ${first_asset_in_account.name} (ID: ${first_asset_in_account.id})`);
|
||||
// }
|
||||
// } catch (err) {
|
||||
// print(`Error adding asset to account: ${err}`);
|
||||
// }
|
||||
|
||||
// 8. Create a Listing for the Asset using the builder pattern
|
||||
let current_timestamp = timestamp(); // Rhai's built-in for current unix timestamp (seconds)
|
||||
@ -69,19 +69,18 @@ let expires_at_ts = current_timestamp + (24 * 60 * 60 * 7); // Expires in 7 days
|
||||
let listing1 = new_listing()
|
||||
.set_title("Rare HeroCoin Batch")
|
||||
.set_description("100 HeroCoins for sale")
|
||||
.set_asset_id(asset1.get_id().to_string()) // Use ID from the saved asset1
|
||||
.set_asset_type("Erc20") // asset_type as string
|
||||
.set_asset_id(asset1.id.to_string()) // Use ID from the saved asset1
|
||||
.set_asset_type_str("Erc20") // asset_type as string
|
||||
.set_seller_id(user1_id.to_string()) // seller_id as string (using the predefined user1_id)
|
||||
.set_price(50.0) // price
|
||||
.set_currency("USD") // currency
|
||||
.set_listing_type("FixedPrice") // listing_type as string
|
||||
.set_expires_at_ts_opt(expires_at_ts) // expires_at_ts_opt as i64
|
||||
.set_tags(["token", "herocoin", "sale"]); // tags as array of strings
|
||||
// image_url is None by default from new_listing(), so no need to call set_image_url_opt for None
|
||||
|
||||
print(`Created listing (pre-save): ${listing1.get_title()} (temp ID: ${listing1.get_id()}), Price: ${listing1.get_price()} ${listing1.get_currency()}`);
|
||||
print(`Listing type: ${listing1.get_listing_type_str()}, Status: ${listing1.get_status_str()}`);
|
||||
print(`Listing expires_at_ts_opt: ${listing1.get_expires_at_ts_opt()}`);
|
||||
print(`Created listing (pre-save): ${listing1.title} (temp ID: ${listing1.id}), Price: ${listing1.price} ${listing1.currency}`);
|
||||
print(`Listing type: ${listing1.listing_type}, Status: ${listing1.status}`);
|
||||
print(`Listing expires_at_ts_opt: ${listing1.expires_at_ts_opt}`);
|
||||
|
||||
// 9. Save Listing to Mock DB and get the version with DB-assigned ID
|
||||
let listing1 = set_listing(listing1); // Shadowing listing1
|
||||
@ -116,8 +115,8 @@ print(`Created bid for listing ${bid1.listing_id} by bidder ${bid1.bidder_id} fo
|
||||
print(`Bid status: ${bid1.status_str}, Created at: ${bid1.created_at_ts}`);
|
||||
|
||||
// 13. Add bid to listing
|
||||
let mut auction_listing_for_bid = get_listing_by_id(auction_listing.get_id());
|
||||
print(`Listing '${auction_listing_for_bid.get_title()}' fetched for bidding. Current price: ${auction_listing_for_bid.get_price()}, Bids: ${auction_listing_for_bid.get_bids_cloned().len()}`);
|
||||
let auction_listing_for_bid = get_listing_by_id(auction_listing.get_id());
|
||||
// print(`Listing '${auction_listing_for_bid.get_title()}' fetched for bidding. Current price: ${auction_listing_for_bid.get_price()}, Bids: ${auction_listing_for_bid.get_bids_cloned().len()}`);
|
||||
|
||||
try {
|
||||
let updated_listing_after_bid = auction_listing_for_bid.add_listing_bid(bid1);
|
||||
@ -129,13 +128,13 @@ try {
|
||||
}
|
||||
|
||||
// 14. Try to complete sale for the fixed price listing
|
||||
let mut listing_to_sell = get_listing_by_id(listing1.get_id());
|
||||
let listing_to_sell = get_listing_by_id(listing1.id);
|
||||
let buyer_user_id = 3;
|
||||
print(`Attempting to complete sale for listing: ${listing_to_sell.get_title()} by buyer ${buyer_user_id}`);
|
||||
print(`Attempting to complete sale for listing: ${listing_to_sell.title} by buyer ${buyer_user_id}`);
|
||||
try {
|
||||
let sold_listing = listing_to_sell.complete_listing_sale(buyer_user_id.to_string(), listing_to_sell.get_price());
|
||||
print(`Sale completed for listing ${sold_listing.get_id()}. New status: ${sold_listing.get_status_str()}`);
|
||||
print(`Buyer ID: ${sold_listing.get_buyer_id_opt()}, Sale Price: ${sold_listing.get_sale_price_opt()}`);
|
||||
let sold_listing = listing_to_sell.complete_listing_sale(buyer_user_id.to_string(), listing_to_sell.price);
|
||||
print(`Sale completed for listing ${sold_listing.id}. New status: ${sold_listing.status}`);
|
||||
print(`Buyer ID: ${sold_listing.buyer_id_opt}, Sale Price: ${sold_listing.sale_price_opt}`);
|
||||
set_listing(sold_listing); // Save updated listing
|
||||
} catch (err) {
|
||||
print(`Error completing sale: ${err}`);
|
||||
|
@ -155,7 +155,7 @@ pub fn register_rhai_engine_functions(
|
||||
engine.register_fn("set_address", |mut asset: Asset, address: ImmutableString| -> Result<Asset, Box<EvalAltResult>> {
|
||||
asset.address = address.to_string(); Ok(asset)
|
||||
});
|
||||
engine.register_fn("set_asset_type_str", |mut asset: Asset, asset_type_str: ImmutableString| -> Result<Asset, Box<EvalAltResult>> {
|
||||
engine.register_fn("set_asset_type", |mut asset: Asset, asset_type_str: ImmutableString| -> Result<Asset, Box<EvalAltResult>> {
|
||||
asset.asset_type = self::string_to_asset_type(asset_type_str.as_str())?;
|
||||
Ok(asset)
|
||||
});
|
||||
@ -179,9 +179,10 @@ pub fn register_rhai_engine_functions(
|
||||
engine.register_get("seller_id", |l: &mut Listing| -> ImmutableString { l.seller_id.clone().into() });
|
||||
engine.register_get("price", |l: &mut Listing| l.price);
|
||||
engine.register_get("currency", |l: &mut Listing| -> ImmutableString { l.currency.clone().into() });
|
||||
engine.register_get("listing_type_str", |l: &mut Listing| -> ImmutableString { self::listing_type_to_string(&l.listing_type) });
|
||||
engine.register_get("status_str", |l: &mut Listing| -> ImmutableString { self::listing_status_to_string(&l.status) });
|
||||
engine.register_get("listing_type", |l: &mut Listing| l.listing_type.clone());
|
||||
engine.register_get("status", |l: &mut Listing| l.status.clone());
|
||||
engine.register_get("expires_at_ts", |l: &mut Listing| l.expires_at);
|
||||
engine.register_get("expires_at_ts_opt", |l: &mut Listing| l.expires_at.map(|dt| dt.timestamp()));
|
||||
engine.register_get("tags", |l: &mut Listing| -> Result<Array, Box<EvalAltResult>> {
|
||||
Ok(l.tags.iter().map(|s| Dynamic::from(s.clone())).collect())
|
||||
});
|
||||
@ -199,7 +200,7 @@ pub fn register_rhai_engine_functions(
|
||||
engine.register_fn("set_asset_id", |mut l: Listing, asset_id: ImmutableString| -> Result<Listing, Box<EvalAltResult>> {
|
||||
l.asset_id = asset_id.to_string(); Ok(l)
|
||||
});
|
||||
engine.register_fn("set_asset_type_str", |mut l: Listing, asset_type_str: ImmutableString| -> Result<Listing, Box<EvalAltResult>> {
|
||||
engine.register_fn("set_asset_type", |mut l: Listing, asset_type_str: ImmutableString| -> Result<Listing, Box<EvalAltResult>> {
|
||||
l.asset_type = self::string_to_asset_type(asset_type_str.as_str())?;
|
||||
Ok(l)
|
||||
});
|
||||
@ -212,8 +213,8 @@ pub fn register_rhai_engine_functions(
|
||||
engine.register_fn("set_currency", |mut l: Listing, currency: ImmutableString| -> Result<Listing, Box<EvalAltResult>> {
|
||||
l.currency = currency.to_string(); Ok(l)
|
||||
});
|
||||
engine.register_fn("set_listing_type_str", |mut l: Listing, listing_type_str: ImmutableString| -> Result<Listing, Box<EvalAltResult>> {
|
||||
l.listing_type = self::string_to_listing_type(listing_type_str.as_str())?;
|
||||
engine.register_fn("set_listing_type", |mut l: Listing, listing_type: ImmutableString| -> Result<Listing, Box<EvalAltResult>> {
|
||||
l.listing_type = self::string_to_listing_type(listing_type.as_str())?;
|
||||
Ok(l)
|
||||
});
|
||||
engine.register_fn("set_status_str", |mut l: Listing, status_str: ImmutableString| -> Result<Listing, Box<EvalAltResult>> {
|
||||
|
30
heromodels_core/src/base_data_builder.rs
Normal file
30
heromodels_core/src/base_data_builder.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use crate::BaseModelData;
|
||||
|
||||
pub trait BaseModelDataOps: Sized {
|
||||
fn get_base_data_mut(&mut self) -> &mut BaseModelData;
|
||||
|
||||
fn set_base_id(mut self, id: u32) -> Self {
|
||||
self.get_base_data_mut().id = id;
|
||||
self
|
||||
}
|
||||
|
||||
fn set_base_created_at(mut self, time: i64) -> Self {
|
||||
self.get_base_data_mut().created_at = time;
|
||||
self
|
||||
}
|
||||
|
||||
fn set_base_modified_at(mut self, time: i64) -> Self {
|
||||
self.get_base_data_mut().modified_at = time;
|
||||
self
|
||||
}
|
||||
|
||||
fn add_base_comment(mut self, comment_id: u32) -> Self {
|
||||
self.get_base_data_mut().comments.push(comment_id);
|
||||
self
|
||||
}
|
||||
|
||||
fn set_base_comments(mut self, comment_ids: Vec<u32>) -> Self {
|
||||
self.get_base_data_mut().comments = comment_ids;
|
||||
self
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user