taohe

Move smart contracts implementing nestable resources on MoveVM blockchains

View on GitHub

Module 0x2f66c09143acc52a85fec529a4e20c85::Root

Root is not technically a tao, since it can’t be nested. Instead it’s a special kind of resource used to host taos.

use 0x1::signer;

Resource Root

Root resource used to host other resources (can be taos).

struct Root<Content: store, key> has store, key
Fields
content: vector<Content>

Function push_content

fun push_content<Content: store, key>(account: &signer, content: Content)
Implementation
fun push_content<Content: key + store>(account: &signer, content: Content) acquires Root {
    let address = signer::address_of(account);
    if (exists<Root<Content>>(address)) {
        let root = borrow_global_mut<Root<Content>>(address);
        vector::push_back<Content>(&mut root.content, content);
    } else {
        let vec1 = vector::empty<Content>();
        vector::push_back<Content>(&mut vec1, content);
        move_to<Root<Content>>(account, Root<Content> { content: vec1 });
    }
}

Function pop_content

fun pop_content<Content: store, key>(account: &signer): Content
Implementation
fun pop_content<Content: key + store>(account: &signer): Content acquires Root {
    let address = signer::address_of(account);
    let root = borrow_global_mut<Root<Content>>(address);
    vector::pop_back<Content>(&mut root.content)
}

Function create

Place a resource into a Root for account. Create one if neccessary.

public fun create<Content: store, key>(account: &signer, content: Content)
Implementation
public fun create<Content: key + store>(account: &signer, content: Content) acquires Root {
    push_content<Content>(account, content);
}

Function extract

Extract a resource from a Root of an account.

public fun extract<Content: store, key>(account: &signer): Content
Implementation
public fun extract<Content: store + key>(account: &signer): Content acquires Root {
    pop_content<Content>(account)
}