Finschia / cosmwasm

Fast and reusable WebAssembly smart contract runtime(and library) for finschia-sdk.
Apache License 2.0
27 stars 14 forks source link

Improve the event usage of smart contract #143

Closed zemyblue closed 2 years ago

zemyblue commented 3 years ago

Currently, the method to add an event in rustlang's smart contract is to add key and value as add_attribute to the response of function. This is not suitable for outputting multiple events from one tx, and it is like adding a new event every time rather than adding an event with a defined characteristic, so it can be classified as different events even thought it is the same vent type during development.

For example, in the event of Solidity Transfer(sender, receiver, amount) event, the event name is clearly Transfer, and it is defined as a format with three attributes, and because Transfer event is always used in this format, it is easy to distinguish from the result. However, there is no way to control this with the current event usage method.

So, it would be good if the way to define events and use them would be improved.

Improvement points

  1. support to output multiple events from the result of one tx.
  2. improve the way events are defined and used them as calling
loloicci commented 3 years ago

support to output multiple events from the result of one tx.

It has already been solved in the original cosmwasm. We will merge it before long. https://github.com/CosmWasm/cosmwasm/blob/a0cf296c43aa092b81457d96a9c6bc2ab223f6d3/packages/std/src/results/response.rs#L77-L83

loloicci commented 3 years ago

I will make a derive macro to make it easy to make an event from a struct. like: IntoEvent

#[derive(IntoEvent)]
struct Transfer {
  from: Addr,
  receiver: Addr,
  amount: Coin,
}

let transfer_event = Transfer { from: addr_a, receiver: addr_b, amount: coin };

let expected_event = Event::new("Transfer").add_attribute("from", addr_a).add_attribute("receiver", addr_b).add_attribute("amount", coin);

assert_eq!(transfer_event.into(), expected_event)
loloicci commented 2 years ago

Make a PR based on cosmwasm 0.16.2. It can be merged after this repository's version has updted. https://github.com/loloicci/line-cosmwasm/pull/1