Repository written by Jose (move package) and Thouny (interaction scripts) as part of WBA Sui Cohort.
struct CapWrapper has key {
id: UID,
cap: TreasuryCap<SUI_DOLLAR>
}
It allows us to add access control to shared objects by wrapping them inside an object.
struct Bank has key {
id: UID,
balance: Balance<SUI>,
admin_balance: Balance<SUI>,
}
struct Account has key, store {
id: UID,
user: address,
debt: u64,
deposit: u64
}
We do store the user balance and debt inside an owned object that can be held by the user of another protocol. The owner of the Account
object has the custody of the funds.
public fun destroy_empty_account(account: Account) {
let Account { id, debt: _, deposit, user: _ } = account;
assert!(deposit == 0, EAccountMustBeEmpty);
object::delete(id);
}
The Sui blockchain gives Sui rebates when objects are deleted as it frees up storage slots. It is best practice to provide means for objects to be destroyed. Do keep in mind that you should ensure that the objects being deleted are empty to prevent loss of funds.
Using bun.sh. Create your own .env.
Publish the package on Sui testnet. It writes the created objects to .created.json. This file is then used by ./utils/getId to easily retrieve the objects created during deployment.
Creates and sends a new Account object to the signer address.
Fetch a potential Account object for the user. Create one if it doesn't exist. Deposit the split SUI and borrow the given amount of DOLLAR.
You can practice by writing the reverse transaction. Repay the borrowed amount, then withdraw the deposited amount and finally destroy the Account object.