Data Structures and Storage

Define Your Own Data Structures

Similar to Solana, Aptos also leverages parallel execution to achieve high throughput. Therefore we should model our data in in a more distributed way. On Solana we store each holding in a PDA where seed is [issuer_pubkey, holder_pubkey].


#[
account(
init,
seeds = [
b"holding",
issuer_pubkey.key.as_ref(),
issuer_pubkey.key.as_ref()
],
bump,
payer = signer,
space = std::mem::size_of::< Holding > () + 8
)
]
pub holding: Account<'info, Holding>

On Aptos we store each holding in an Object where seed is similar. Note we also added contract address in the seed, if we don't include contract address in the seed, we deploy the contract twice, same holding in 2 contracts will have an Object address collision. Because in both Objects, creator is the same (both are tx sender), seed is the same, so the Object address is the same.


module friend_tech_addr::friend_tech {
public entry fun issue_key(
sender: &signer,
username: string::String,
) acquires User, IssuerRegistry {
// ... other stuff
let holding_obj_constructor_ref = &object::create_named_object(
sender,
construct_holding_object_seed(sender_addr, sender_addr)
);
// ... other stuff
}
fun construct_holding_object_seed(
issuer_addr: address,
holder_addr: address
): vector<u8> {
bcs::to_bytes(
&string_utils::format3(
&b"{}_key_issued_by_{}_hold_by_{}",
@friend_tech_addr,
issuer_addr,
holder_addr,
)
)
}
}