wasmd/x/wasm
Ethan Frey eeb12592f3 Use height from query and return height on query 2020-02-27 17:45:58 +01:00
..
client Use height from query and return height on query 2020-02-27 17:45:58 +01:00
internal Proper json for MsgInstantiateContract.Label 2020-02-27 17:44:10 +01:00
testdata Update test contract, escrow to 0.7 version 2020-02-27 12:24:13 +01:00
README.md Fix markdown 2020-02-27 16:39:50 +01:00
alias.go Update tests 2020-02-26 09:19:26 +01:00
genesis_test.go Update tests 2020-02-26 09:19:26 +01:00
handler.go Properly emit events for every sub-message dispatched by x/wasm 2020-02-27 15:46:27 +01:00
module.go Added rest to module 2020-01-27 17:32:06 +05:30
module_test.go Test and document current tag behavior 2020-02-27 16:11:57 +01:00

README.md

Wasm Module

This should be a brief overview of the functionality

Configuration

You can add the following section to config/app.toml. Below is shown with defaults:

[wasm]
# This is the maximum sdk gas (wasm and storage) that we allow for any x/wasm "smart" queries
query_gas_limit = 300000
# This is the number of wasm vm instances we keep cached in memory for speed-up
# Warning: this is currently unstable and may lead to crashes, best to keep for 0 unless testing locally
lru_size = 0

Events

A number of events are returned to allow good indexing of the transactions from smart contracts.

Every call to Instantiate or Execute will be tagged with the info on the contract that was executed and who executed it. It should look something like this (with different addresses). The module is always wasm, and code_id is only present when Instantiating a contract, so you can subscribe to new instances, it is omitted on Execute:

{
    "Type": "message",
    "Attr": [
        {
            "key": "module",
            "value": "wasm"
        },
        {
            "key": "signer",
            "value": "cosmos1vx8knpllrj7n963p9ttd80w47kpacrhuts497x"
        },
        {
            "key": "code_id",
            "value": "1"
        },
        {
            "key": "contract_address",
            "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5"
        }
    ]
}

If any funds were transferred to the contract as part of the message, or if the contract released funds as part of it's executions, it will receive the typical events associated with sending tokens from bank. In this case, we instantiate the contract and provide a initial balance in the same MsgInstantiateContract. We see the following events in addition to the above one:

[
    {
        "Type": "transfer",
        "Attr": [
            {
                "key": "recipient",
                "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5"
            },
            {
                "key": "amount",
                "value": "100000denom"
            }
        ]
    },
    {
        "Type": "message",
        "Attr": [
            {
                "key": "sender",
                "value": "cosmos1ffnqn02ft2psvyv4dyr56nnv6plllf9pm2kpmv"
            }
        ]
    }
]

This is actually not very ergonomic, as the "sender" (account that sent the funds) is separated from the actual transfer as two separate events, and this may cause confusion, especially if the sender moves funds to the contract and the contract to another recipient in the same transaction. However, this format comes from x/bank, so we would have make a few more changes to our cosmos-sdk fork to simplify this.

Finally, the contract itself can emit a "custom event" on Execute only (not on Init). There is one event per contract, so if one contract calls a second contract, you may receive one event for the original contract and one for the re-invoked contract. All attributes from the contract are passed through verbatim, and we add a contract_address attribute that contains the actual contract that emitted that event. Here is an example from the escrow contract successfully releasing funds to the destination address:

{
    "Type": "wasm",
    "Attr": [
        {
            "key": "contract_address",
            "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5"
        },
        {
            "key": "action",
            "value": "release"
        },
        {
            "key": "destination",
            "value": "cosmos14k7v7ms4jxkk2etmg9gljxjm4ru3qjdugfsflq"
        }
    ]
}

Pulling this all together

We will invoke an escrow contract to release to the designated beneficiary. The escrow was previously loaded with 100000denom (from the above example). In this transaction, we send 5000denom along with the MsgExecuteContract and the contract releases the entire funds (105000denom) to the beneficiary.

We will see all the following events, where you should be able to reconstruct the actions (remember there are two events for each transfer). We see (1) the initial transfer of funds to the contract, (2) the contract custom event that it released funds (3) the transfer of funds from the contract to the beneficiary and (4) the generic x/wasm event stating that the contract was executed (which always appears, while 2 is optional and has information as reliable as the contract):

[
    {
        "Type": "transfer",
        "Attr": [
            {
                "key": "recipient",
                "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5"
            },
            {
                "key": "amount",
                "value": "5000denom"
            }
        ]
    },
    {
        "Type": "message",
        "Attr": [
            {
                "key": "sender",
                "value": "cosmos1zm074khx32hqy20hlshlsd423n07pwlu9cpt37"
            }
        ]
    },
    {
        "Type": "wasm",
        "Attr": [
            {
                "key": "contract_address",
                "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5"
            },
            {
                "key": "action",
                "value": "release"
            },
            {
                "key": "destination",
                "value": "cosmos14k7v7ms4jxkk2etmg9gljxjm4ru3qjdugfsflq"
            }
        ]
    },
    {
        "Type": "transfer",
        "Attr": [
            {
                "key": "recipient",
                "value": "cosmos14k7v7ms4jxkk2etmg9gljxjm4ru3qjdugfsflq"
            },
            {
                "key": "amount",
                "value": "105000denom"
            }
        ]
    },
    {
        "Type": "message",
        "Attr": [
            {
                "key": "module",
                "value": "wasm"
            },
            {
                "key": "signer",
                "value": "cosmos1zm074khx32hqy20hlshlsd423n07pwlu9cpt37"
            },
            {
                "key": "contract_address",
                "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5"
            }
        ]
    }
]

Messages

TODO

CLI

TODO - working, but not the nicest interface (json + bash = bleh). Use to upload, but I suggest to focus on frontend / js tooling

Rest

TODO - main supported interface, under rapid change