Follows is the specification for an integration of the Chronos conditional scheduler with the Melon Fund to allow for on-chain autonomous operations such as automatic stop-loss order making, or take-profit order making, that only needs to be set once by the owner and will be trustlessly executed when a specific condition is met such as the change of a price feed. So far we estimate that the integration will be compatible with the ZeroEx v2.0.0 exchange contracts, and any other exchanges which implement a similar Signature Validation scheme.
Flow
Overview
- Deploy a Proxy Wallet
- Deploy Melon Fund through Proxy Wallet
- Schedule transaction on Proxy Wallet
- Use ScheduledTransaction address
- Success [Automation]
The creator of an autonomous Melon Fund will first deploy a Proxy Wallet, a wallet contract that has an integration with the Chronos interface and allows the scheduling of transactions to pass through the wallet. The creator will then deploy a new Melon Fund by sending a proxy transaction through the Proxy Wallet, thereby making the Proxy Wallet the owner of the new Melon Fund. Any function on the Melon Fund which checks the onlyOwner modifier will have to be sent through the Proxy Wallet.
Now if the creator would like to set on-chain, trustless order making which will create a new order when a condition is met (the price of an asset reaches some threshold), they will prepare the data for the transaction, then send it to the Proxy Wallet's schedule function which will deploy a new ScheduledTransaction. The ScheduledTransaction will be executed by the decentralized network of Chronos TimeNodes when the conditions return true and the block.number or block.timestamp is within the executionWindow. The ScheduledTransaction address will be whitelisted with the Proxy Wallet so that when the transaction is executed it is allowed to send data through the proxy function on the Proxy Wallet and into the Melon Fund with the context of being owner of the Melon Fund.
Proxy Wallet
A sample implementation of the proxy wallet may look like this:
This allows for any transaction scheduled by a whitelisted account to itself be whitelisted and therefore have owner priviledges on this contract.
Off-chain scheduled order making
In ZeroEx v2 the order book is off-chain. This implies, that for setting up a stop-loss (make order when price < X), three things need to be done on-chain:
Fund tokens should be approved, so ZeroEx exchange can use it to settle order
Transaction needs to be scheduled with execution window and conditional check set up
Chronos ZeroEx Validator needs to be approved to validate signatures
After these steps, fund owner can make order on the ZeroEx v2 exchange, by signing order hash using Chronos specific signature. Then signed order needs to be published to the ZeroEx relayers, which maintain the order book.
The order will be published in the order book only when signature will be valid. The signature of order will only be valid when scheduled transaction is in execution window and the condition is met (ex. price < X).
Prepared Data (SerializedTransaction)
To schedule with the Chronos scheduler requires the data of the transaction beforehand. Usually with decentralized exchanges they require the signature of the order, which would cause a problem since it would make the order able to be executed immediately even if we wanted it executed later. However, as we show later the new SignatureValidator scheme implemented in v2.0.0 of ZeroEx protocol opens the door for use to prepare the data in a certain way to allow us to send the entire transaction with signature before hand.
ZeroEx v2 SignatureValidator
Validator contract approval by signer
Before using validator contract, signer has to approve validator contract address. This can be done by calling:
Last byte of signature needs to be: 0x06 - SignatureType.Validator. The structure of signature of such type is:
// A signature using this type should be encoded as:
// | Offset | Length | Contents |
// | 0x00 | x | Signature to validate |
// | 0x00 + x | 20 | Address of validator contract |
// | 0x14 + x | 1 | Signature type is always "\x06" |
This will send internal transaction call to the address of validator contract:
Validator contract needs to implement IValidator interface and specifically method isValidSignature:
contract IValidator {
/// @dev Verifies that a signature is valid.
/// @param hash Message hash that is signed.
/// @param signerAddress Address that should have signed the given hash.
/// @param signature Proof of signing.
/// @return Validity of order signature.
function isValidSignature(
bytes32 hash,
address signerAddress,
bytes signature
)
external
view
returns (bool isValid);
}
In case of making order the call parameter should be as below:
Autonomous Melon Funds with Chronos [Specification]
Authors: Logan Saether <lsaether@protonmail.com>, Daniel Kmak <daniel@chronologic.network> Status: Dependent on MIP3
Summary
Follows is the specification for an integration of the Chronos conditional scheduler with the Melon Fund to allow for on-chain autonomous operations such as automatic stop-loss order making, or take-profit order making, that only needs to be set once by the owner and will be trustlessly executed when a specific condition is met such as the change of a price feed. So far we estimate that the integration will be compatible with the ZeroEx v2.0.0 exchange contracts, and any other exchanges which implement a similar Signature Validation scheme.
Flow
Overview
The creator of an autonomous Melon Fund will first deploy a Proxy Wallet, a wallet contract that has an integration with the Chronos interface and allows the scheduling of transactions to pass through the wallet. The creator will then deploy a new Melon Fund by sending a proxy transaction through the Proxy Wallet, thereby making the Proxy Wallet the owner of the new Melon Fund. Any function on the Melon Fund which checks the
onlyOwner
modifier will have to be sent through the Proxy Wallet.Now if the creator would like to set on-chain, trustless order making which will create a new order when a condition is met (the price of an asset reaches some threshold), they will prepare the data for the transaction, then send it to the Proxy Wallet's
schedule
function which will deploy a newScheduledTransaction
. TheScheduledTransaction
will be executed by the decentralized network of Chronos TimeNodes when the conditions returntrue
and theblock.number
orblock.timestamp
is within theexecutionWindow
. TheScheduledTransaction
address will be whitelisted with the Proxy Wallet so that when the transaction is executed it is allowed to send data through theproxy
function on the Proxy Wallet and into the Melon Fund with the context of beingowner
of the Melon Fund.Proxy Wallet
A sample implementation of the proxy wallet may look like this:
This allows for any transaction scheduled by a whitelisted account to itself be whitelisted and therefore have owner priviledges on this contract.
Off-chain scheduled order making
In ZeroEx v2 the order book is off-chain. This implies, that for setting up a stop-loss (make order when
price < X
), three things need to be done on-chain:After these steps, fund owner can make order on the ZeroEx v2 exchange, by signing order hash using Chronos specific signature. Then signed order needs to be published to the ZeroEx relayers, which maintain the order book.
The order will be published in the order book only when signature will be valid. The signature of order will only be valid when scheduled transaction is in execution window and the condition is met (ex.
price < X
).Prepared Data (SerializedTransaction)
To schedule with the Chronos scheduler requires the data of the transaction beforehand. Usually with decentralized exchanges they require the signature of the order, which would cause a problem since it would make the order able to be executed immediately even if we wanted it executed later. However, as we show later the new
SignatureValidator
scheme implemented in v2.0.0 of ZeroEx protocol opens the door for use to prepare the data in a certain way to allow us to send the entire transaction with signature before hand.ZeroEx v2 SignatureValidator
Validator contract approval by signer
Before using validator contract, signer has to approve validator contract address. This can be done by calling:
with arguments:
address validatorAddress = validatorContractAddress
bool approval = true
ChronosValidator
ChronosValidator implements
IValidator
. It needs to be deployed once per blockchain.When validator contract is approved, we call:
Last byte of
signature
needs to be:0x06
-SignatureType.Validator
. The structure ofsignature
of such type is:This will send internal transaction call to the address of validator contract:
Validator contract needs to implement
IValidator
interface and specifically methodisValidSignature
:In case of making order the call parameter should be as below:
Where
CHRONOS_SPECIFIC_SIGNATURE
scheme is:Example implementation of
isValidSignature
method by Chronos Validator:Repository with tests
The repository with Solidity code and tests implementing ChronosValidator: https://github.com/chronologic/chronos-melonport.
References