Skip to content

gen

generate kinds of scripts like: test, ptb

usage:

gen test

generate typescript test wrappers

gen ptb

generate typescript ptb wrappers

example

after gen test and ptb, you will get the typescript wrappers for move structs and functions

gen.png

gen2.png

move struct justin_coin_6 will get the following typescript JUSTIN_COIN_6 class

js
module wasm_test::justin_coin_6 {
    use sui::coin::{Self};

    public struct JUSTIN_COIN_6 has drop {}

    fun init(coin_witness: JUSTIN_COIN_6, ctx: &mut TxContext) {
        let (
            treasury_cap,
            coin_metadata
        ) =
            coin::create_currency(
                coin_witness,
                6,
                b"DEC6",
                b"Decimals 6",
                b"Coin with 6 decimals for testing purposes.",
                option::none(),
                ctx
            );

        transfer::public_share_object(coin_metadata);

        transfer::public_share_object(treasury_cap);
    }
}
js
export class JUSTIN_COIN_6 implements StructClass {
    $type: string = `${do_get_package_address()}::${MODULE_NAME}::JUSTIN_COIN_6`;

    dummy_field: boolean;

    constructor(dummy_field: boolean) {
        this.dummy_field = dummy_field;
    }

    into_value() {
        return this.get_value()
    }

    from_bcs_vector_t(bytes: Uint8Array) {
        let args = this.from_bcs_vector(bcs_import.vector(this.get_bcs()).parse(bytes));
        var self = this;
        return args.map(function(arg) {
            arg.$type = self.$type;
            return arg;
        })
    }

    from_bcs_t(bytes: Uint8Array) {
        let result = this.from_bcs(this.get_bcs().parse(bytes));
        result.$type = this.$type;
        return result;
    }

    serialize(arg: any) {
        return this.get_bcs().serialize(arg);
    }

    serialize_bcs() {
        return this.get_bcs()
    }

    return_bcs() {
        return this.get_bcs()
    }

    from_bcs(arg: any) {
        return JUSTIN_COIN_6.from_bcs(arg)
    }

    from_bcs_vector(args: any) {
        return JUSTIN_COIN_6.from_bcs_vector(args)
    }

    get_bcs() {
        return JUSTIN_COIN_6.bcs
    }

    get_value() {
        return this
    }

    static $type() {
        return `${do_get_package_address()}::${MODULE_NAME}::JUSTIN_COIN_6`
    }

    from(arg: JUSTIN_COIN_6) {
        this.dummy_field = arg.dummy_field;
    }

    static from_bcs(arg: {
        dummy_field: boolean
    }): JUSTIN_COIN_6 {
        return new JUSTIN_COIN_6(arg.dummy_field)
    }

    static from_bcs_vector(args: {
        dummy_field: boolean
    } []): JUSTIN_COIN_6[] {
        return args.map(function(arg) {
            return new JUSTIN_COIN_6(arg.dummy_field)
        })
    }

    static get bcs() {
        return bcs_import.struct("JUSTIN_COIN_6", {
            dummy_field: bcs_import.bool(),
        }).transform({
            input: (val: any) => {
                return val
            },
            output: (val) => new JUSTIN_COIN_6(val.dummy_field),
        });
    };
}

test functions wrappers

js
function init(arg0: JUSTIN_COIN_6, arg1: TxContext) {
    let wasm = get_wasm();

    let args: any[] = [
        wasm.new_bytes(JUSTIN_COIN_6.bcs.serialize(arg0).toBytes(), ""),
        wasm.new_bytes(TxContext.bcs.serialize(arg1).toBytes(), "")
    ]

    let [a0] = wasm.call_return_bcs(PACKAGE_ADDRESS, MODULE_NAME, "init", [], args);

    arg1.from(arg1.from_bcs_t(new Uint8Array(a0.Raw[0])));
}

ptb function wrappers

js
export function init(tx: Transaction, arg0: JUSTIN_COIN_6 | TransactionArgument) {
    let args: any[] = [
        isTransactionArgument(arg0) ? arg0 : tx.pure(JUSTIN_COIN_6.bcs.serialize((arg0 as JUSTIN_COIN_6)))
    ]

    return tx.moveCall({
        target: `${do_get_package_address()}::${MODULE_NAME}::init`,
        arguments: args,
    })
}

note

for more information you can visit test wrappers and ptb wrappers

Released under the MIT License.