weave icon indicating copy to clipboard operation
weave copied to clipboard

Provide the scaffolding for the automation of reward distribution

Open annatorok opened this issue 5 years ago • 2 comments

We need a smart contract (on chain, in bnsd entity) that, when sent tokens, reads the termdeposit configuration, performs some calculations, and sends tokens based on the calculations.

The flow of tokens to which this issue refers is Reward Account -> smart contract -> Depositors. This issue is only about creating the scaffolding and is not intended for production deployment, so if we can just have the smart contract read the termdeposit configuration and distribute equal proportions of the sent tokens to the addresses with a CustomRate in the config then we'll be all set.

Goal: Smart contract should be able to read configuration and distribute tokens that are sent to it.

annatorok avatar Jan 28 '20 10:01 annatorok

x/distribution is a good place to start. Once a Revenue entity is created, funds can be transferred to that revenue and released later. Releasing funds distribute them among all participants.

https://github.com/iov-one/weave/blob/888eecdd90d4abb3fd1f533e37ee0298dacf5f81/x/distribution/handler.go#L127-L129

To move funds, cash controller is needed. Cash controller is provided during handler creation and referenced by handler instance. https://github.com/iov-one/weave/blob/888eecdd90d4abb3fd1f533e37ee0298dacf5f81/x/distribution/handler.go#L98

To configure an extension we use gconf package. I provides tooling and code to make configuration management an easy task. To read a gconf managed configuration, use gconf.Load function https://github.com/iov-one/weave/blob/888eecdd90d4abb3fd1f533e37ee0298dacf5f81/cmd/bnsd/x/preregistration/configuration.go#L14-L20

Anything can have a weave address associated. In x/distribution extension, each Revenue entity owns a unique wallet to store funds. It is up to you to decide how do you want to construct that address. https://github.com/iov-one/weave/blob/888eecdd90d4abb3fd1f533e37ee0298dacf5f81/x/distribution/model.go#L93-L95

There are no triggers in weave. Code should not react on generic events. This means that reacting on funds transfer is not part of the design. Creating a hook that will execute certain function when funds are sent from/to certain address was never the design goal. There are at least two ways such hook could be implemented:

  1. Wrap cash controller with additional functionality. This is how the burn wallet functionality is implemented https://github.com/iov-one/weave/blob/888eecdd90d4abb3fd1f533e37ee0298dacf5f81/cmd/bnsd/app/cashctrl.go#L27-L38
  2. Create a middelware/decorator that will introspect each processed transaction and execute a hook function if needed.

Although it is possible, it is recommended to always trigger functionality with a message. Instead of implementing hooks, require to explicitly request a state change. This is how all our functionality is implemented.

Is this description good enough? I think it has more value that writing a fake extension that does not work.

husio avatar Jan 31 '20 12:01 husio