Skip to content
This repository was archived by the owner on Feb 14, 2021. It is now read-only.
This repository was archived by the owner on Feb 14, 2021. It is now read-only.

idea for better rust wasm contract development/testing experience #10

@snd

Description

@snd

not sure if this is the right repo for this issue. if someone points me to a better repo i'll move the issue there.

i know rust wasm contract development is super early days. even so the contract developer/tester experience could already be improved a lot.

room for improvement imho:

it would be great if we could write rust contracts a bit like this (wishful thinking):

#[contract]
mod token_contract {
    static TOTAL_SUPPLY_KEY: H256 = H256([2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
    static OWNER_KEY: H256 = H256([3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);

    fn constructor<C: Context>(&mut context: C, total_supply: U256) {
        let sender = context.sender();
        // Set up the total supply for the token
        context.storage().write(&TOTAL_SUPPLY_KEY, &total_supply.into()).unwrap();
        // Give all tokens to the contract owner
        context.storage().write(&balance_key(&sender), &total_supply.into()).unwrap();
        // Set the contract owner
        context.storage().write(&OWNER_KEY, &H256::from(sender).into()).unwrap();
    }

    fn totalSupply<C: Context>(&context: C) -> U256 {
        context.storage().read(&TOTAL_SUPPLY_KEY).unwrap_or([0u8; 32]).into()
    }

    // ...
}

this looks much cleaner to me.

it requires no knowledge about global mutable state to understand.

testing would be more natural, require no macros and no setting of global mutable state:

#[test]
fn should_succeed_in_creating_max_possible_amount_of_tokens() {
    let mut context = SomeContextBuilder::new().build();
    // set total supply to maximum value of an unsigned 256 bit integer
    let total_supply = U256::from_dec_str("115792089237316195423570985008687907853269984665640564039457584007913129639935").unwrap();
    assert_eq!(total_supply, U256::max_value());
    constructor(&mut context, total_supply);
    assert_eq!(totalSupply(&context), total_supply);
}

what do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions