203 lines
6.7 KiB
Plaintext
203 lines
6.7 KiB
Plaintext
// finance_script.rhai
|
|
// Example Rhai script for working with Finance models
|
|
|
|
// Constants for AssetType
|
|
const NATIVE = "Native";
|
|
const ERC20 = "Erc20";
|
|
const ERC721 = "Erc721";
|
|
const ERC1155 = "Erc1155";
|
|
|
|
// Constants for ListingStatus
|
|
const ACTIVE = "Active";
|
|
const SOLD = "Sold";
|
|
const CANCELLED = "Cancelled";
|
|
const EXPIRED = "Expired";
|
|
|
|
// Constants for ListingType
|
|
const FIXED_PRICE = "FixedPrice";
|
|
const AUCTION = "Auction";
|
|
const EXCHANGE = "Exchange";
|
|
|
|
// Constants for BidStatus
|
|
const BID_ACTIVE = "Active";
|
|
const BID_ACCEPTED = "Accepted";
|
|
const BID_REJECTED = "Rejected";
|
|
const BID_CANCELLED = "Cancelled";
|
|
|
|
// Create a new account using builder pattern
|
|
let alice_account = new_account()
|
|
.name("Alice's Account")
|
|
.user_id(101)
|
|
.description("Alice's primary trading account")
|
|
.ledger("ethereum")
|
|
.address("0x1234567890abcdef1234567890abcdef12345678")
|
|
.pubkey("0xabcdef1234567890abcdef1234567890abcdef12");
|
|
|
|
print(`Created account: ${alice_account.get_name()} (User ID: ${alice_account.get_user_id()})`);
|
|
|
|
// Save the account to the database
|
|
let saved_alice = set_account(alice_account);
|
|
print(`Account saved to database with ID: ${saved_alice.get_id()}`);
|
|
|
|
// Create a new asset using builder pattern
|
|
let token_asset = new_asset()
|
|
.name("HERO Token")
|
|
.description("Herocode governance token")
|
|
.amount(1000.0)
|
|
.address("0x9876543210abcdef9876543210abcdef98765432")
|
|
.asset_type(ERC20)
|
|
.decimals(18);
|
|
|
|
print(`Created asset: ${token_asset.get_name()} (${token_asset.get_amount()} ${token_asset.get_asset_type()})`);
|
|
|
|
// Save the asset to the database
|
|
let saved_token = set_asset(token_asset);
|
|
print(`Asset saved to database with ID: ${saved_token.get_id()}`);
|
|
|
|
// Add the asset to Alice's account
|
|
saved_alice = saved_alice.add_asset(saved_token.get_id());
|
|
saved_alice = set_account(saved_alice);
|
|
print(`Added asset ${saved_token.get_name()} to ${saved_alice.get_name()}`);
|
|
|
|
// Create a new NFT asset
|
|
let nft_asset = new_asset()
|
|
.name("Herocode #42")
|
|
.description("Unique digital collectible")
|
|
.amount(1.0)
|
|
.address("0xabcdef1234567890abcdef1234567890abcdef12")
|
|
.asset_type(ERC721)
|
|
.decimals(0);
|
|
|
|
// Save the NFT to the database
|
|
let saved_nft = set_asset(nft_asset);
|
|
print(`NFT saved to database with ID: ${saved_nft.get_id()}`);
|
|
|
|
// Create Bob's account
|
|
let bob_account = new_account()
|
|
.name("Bob's Account")
|
|
.user_id(102)
|
|
.description("Bob's trading account")
|
|
.ledger("ethereum")
|
|
.address("0xfedcba0987654321fedcba0987654321fedcba09")
|
|
.pubkey("0x654321fedcba0987654321fedcba0987654321fe");
|
|
|
|
// Save Bob's account
|
|
let saved_bob = set_account(bob_account);
|
|
print(`Created and saved Bob's account with ID: ${saved_bob.get_id()}`);
|
|
|
|
// Create a listing for the NFT
|
|
let nft_listing = new_listing()
|
|
.seller_id(saved_alice.get_id())
|
|
.asset_id(saved_nft.get_id())
|
|
.price(0.5)
|
|
.currency("ETH")
|
|
.listing_type(AUCTION)
|
|
.title("Rare Herocode NFT")
|
|
.description("One of a kind digital collectible")
|
|
.image_url("https://example.com/nft/42.png")
|
|
.expires_at(timestamp_now() + 86400) // 24 hours from now
|
|
.add_tag("rare")
|
|
.add_tag("collectible")
|
|
.add_tag("digital art")
|
|
.set_listing();
|
|
|
|
// Save the listing
|
|
print(`Created listing: ${nft_listing.get_title()} (ID: ${nft_listing.get_id()})`);
|
|
print(`Listing status: ${nft_listing.get_status()}, Type: ${nft_listing.get_listing_type()}`);
|
|
print(`Listing price: ${nft_listing.get_price()} ${nft_listing.get_currency()}`);
|
|
|
|
// Create a bid from Bob
|
|
let bob_bid = new_bid()
|
|
.listing_id(nft_listing.get_id().to_string())
|
|
.bidder_id(saved_bob.get_id())
|
|
.amount(1.5)
|
|
.currency("ETH")
|
|
.set_bid();
|
|
|
|
// Save the bid
|
|
print(`Created bid from ${saved_bob.get_name()} for ${bob_bid.get_amount()} ${bob_bid.get_currency()}`);
|
|
|
|
// Add the bid to the listing
|
|
nft_listing.add_bid(bob_bid);
|
|
nft_listing.set_listing();
|
|
print(`Added bid to listing ${nft_listing.get_title()}`);
|
|
|
|
// Create another bid with higher amount
|
|
let charlie_account = new_account()
|
|
.name("Charlie's Account")
|
|
.user_id(103)
|
|
.description("Charlie's trading account")
|
|
.ledger("ethereum")
|
|
.address("0x1122334455667788991122334455667788990011")
|
|
.pubkey("0x8877665544332211887766554433221188776655");
|
|
|
|
let saved_charlie = set_account(charlie_account);
|
|
print(`Created and saved Charlie's account with ID: ${saved_charlie.get_id()}`);
|
|
|
|
let charlie_bid = new_bid()
|
|
.listing_id(nft_listing.get_id().to_string())
|
|
.bidder_id(saved_charlie.get_id())
|
|
.amount(2.5)
|
|
.currency("ETH")
|
|
.set_bid();
|
|
|
|
print(`Created higher bid from ${saved_charlie.get_name()} for ${charlie_bid.get_amount()} ${charlie_bid.get_currency()}`);
|
|
|
|
// Add the higher bid to the listing
|
|
nft_listing.add_bid(charlie_bid)
|
|
.set_listing();
|
|
|
|
|
|
|
|
print(`Added higher bid to listing ${nft_listing.get_title()}`);
|
|
|
|
nft_listing.sale_price(2.5)
|
|
.set_listing();
|
|
|
|
// Complete the sale to the highest bidder (Charlie)
|
|
nft_listing.complete_sale(saved_charlie.get_id())
|
|
.set_listing();
|
|
|
|
print(`Completed sale of ${nft_listing.get_title()} to ${saved_charlie.get_name()}`);
|
|
print(`New listing status: ${saved_listing.get_status()}`);
|
|
|
|
// Retrieve the listing from the database
|
|
let retrieved_listing = get_listing_by_id(saved_listing.get_id());
|
|
print(`Retrieved listing: ${retrieved_listing.get_title()} (Status: ${retrieved_listing.get_status()})`);
|
|
|
|
// Create a fixed price listing
|
|
let token_listing = new_listing()
|
|
.seller_id(saved_alice.get_id())
|
|
.asset_id(saved_token.get_id())
|
|
.price(100.0)
|
|
.currency("USDC")
|
|
.listing_type(FIXED_PRICE)
|
|
.title("HERO Tokens for Sale")
|
|
.description("100 HERO tokens at fixed price")
|
|
.set_listing();
|
|
|
|
// Save the fixed price listing
|
|
print(`Created fixed price listing: ${token_listing.get_title()} (ID: ${token_listing.get_id()})`);
|
|
|
|
// Cancel the listing
|
|
token_listing.cancel();
|
|
token_listing.set_listing();
|
|
print(`Cancelled listing: ${token_listing.get_title()}`);
|
|
print(`Listing status: ${token_listing.get_status()}`);
|
|
|
|
// Print summary of all accounts
|
|
print("\nAccount Summary:");
|
|
print(`Alice (ID: ${saved_alice.get_id()}): ${saved_alice.get_assets().len()} assets`);
|
|
print(`Bob (ID: ${saved_bob.get_id()}): ${saved_bob.get_assets().len()} assets`);
|
|
print(`Charlie (ID: ${saved_charlie.get_id()}): ${saved_charlie.get_assets().len()} assets`);
|
|
|
|
// Print summary of all listings
|
|
print("\nListing Summary:");
|
|
print(`NFT Auction (ID: ${nft_listing.get_id()}): ${nft_listing.get_status()}`);
|
|
print(`Token Sale (ID: ${token_listing.get_id()}): ${token_listing.get_status()}`);
|
|
|
|
// Print summary of all bids
|
|
print("\nBid Summary:");
|
|
print(`Bob's bid: ${bob_bid.get_amount()} ${bob_bid.get_currency()} (Status: ${bob_bid.get_status()})`);
|
|
print(`Charlie's bid: ${charlie_bid.get_amount()} ${charlie_bid.get_currency()} (Status: ${charlie_bid.get_status()})`);
|