2022-07-21 07:14:29 +00:00
|
|
|
# Instructions
|
|
|
|
|
2022-07-22 03:27:21 +00:00
|
|
|
Instructions are used to communicate with networks connected to Serai, and they
|
|
|
|
come in two forms:
|
|
|
|
|
2023-01-20 16:00:18 +00:00
|
|
|
- In Instructions are programmable specifications paired with incoming coins,
|
|
|
|
encoded into transactions on connected networks. Serai will parse included
|
|
|
|
instructions when it receives coins, executing the included specs.
|
2022-07-22 03:27:21 +00:00
|
|
|
|
2023-01-20 16:00:18 +00:00
|
|
|
- Out Instructions detail how to transfer coins, either to a Serai address or
|
2023-07-30 20:11:30 +00:00
|
|
|
an address native to the network of the coins in question.
|
2022-07-22 03:27:21 +00:00
|
|
|
|
|
|
|
A transaction containing an In Instruction and an Out Instruction (to a native
|
2023-01-20 16:00:18 +00:00
|
|
|
address) will receive coins to Serai and send coins from Serai, without
|
2022-07-22 03:27:21 +00:00
|
|
|
requiring directly performing any transactions on Serai itself.
|
|
|
|
|
|
|
|
All instructions are encoded under [Shorthand](#shorthand). Shorthand provides
|
|
|
|
frequent use cases to create minimal data representations on connected networks.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
|
|
|
Instructions are interpreted according to their non-Serai network. Addresses
|
2023-01-20 16:00:18 +00:00
|
|
|
have no validation performed unless otherwise noted. If the processor is
|
|
|
|
instructed to act on invalid data, it will drop the entire instruction.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
|
|
|
### Serialization
|
|
|
|
|
2023-01-20 16:00:18 +00:00
|
|
|
Instructions are SCALE encoded.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
2023-01-20 16:00:18 +00:00
|
|
|
### In Instruction
|
|
|
|
|
2023-07-26 16:45:51 +00:00
|
|
|
InInstruction is an enum of:
|
|
|
|
|
|
|
|
- `Transfer`
|
|
|
|
- `Dex(Data)`
|
2023-01-20 16:00:18 +00:00
|
|
|
|
|
|
|
The specified target will be minted an appropriate amount of the respective
|
2023-07-26 16:45:51 +00:00
|
|
|
Serai token. If `Dex`, the encoded call will be executed.
|
2023-01-20 16:00:18 +00:00
|
|
|
|
|
|
|
### Refundable In Instruction
|
|
|
|
|
2023-01-28 06:47:13 +00:00
|
|
|
- `origin` (Option\<ExternalAddress>): Address, from the network of
|
|
|
|
origin, which sent coins in.
|
|
|
|
- `instruction` (InInstruction): The action to perform with the
|
|
|
|
incoming coins.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
|
|
|
Networks may automatically provide `origin`. If they do, the instruction may
|
2023-01-20 16:00:18 +00:00
|
|
|
still provide `origin`, overriding the automatically provided value.
|
|
|
|
|
|
|
|
If the instruction fails, coins are scheduled to be returned to `origin`,
|
|
|
|
if provided.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
2023-01-28 06:47:13 +00:00
|
|
|
### Out Instruction
|
2022-07-21 07:14:29 +00:00
|
|
|
|
2023-01-28 06:47:13 +00:00
|
|
|
- `address` (ExternalAddress): Address to transfer the coins included with
|
|
|
|
this instruction to.
|
|
|
|
- `data` (Option<Data>): Data to include when transferring coins.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
2023-01-28 06:47:13 +00:00
|
|
|
No validation of external addresses/data is performed on-chain. If data is
|
|
|
|
specified for a chain not supporting data, it is silently dropped.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
2023-01-28 06:47:13 +00:00
|
|
|
### Destination
|
2022-07-21 07:14:29 +00:00
|
|
|
|
2023-01-28 06:47:13 +00:00
|
|
|
Destination is an enum of SeraiAddress and OutInstruction.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
|
|
|
### Shorthand
|
|
|
|
|
2023-01-20 16:00:18 +00:00
|
|
|
Shorthand is an enum which expands to an Refundable In Instruction.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
|
|
|
##### Raw
|
|
|
|
|
2023-07-26 18:00:30 +00:00
|
|
|
Raw Shorthand contains a Refundable In Instruction directly. This is a verbose
|
|
|
|
fallback option for infrequent use cases not covered by Shorthand.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
|
|
|
##### Swap
|
|
|
|
|
2023-01-20 16:00:18 +00:00
|
|
|
- `origin` (Option\<ExternalAddress>): Refundable In Instruction's `origin`.
|
|
|
|
- `coin` (Coin): Coin to swap funds for.
|
|
|
|
- `minimum` (Amount): Minimum amount of `coin` to receive.
|
2023-01-28 06:47:13 +00:00
|
|
|
- `out` (Destination): Final destination for funds.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
|
|
|
which expands to:
|
|
|
|
|
|
|
|
```
|
2023-01-20 16:00:18 +00:00
|
|
|
RefundableInInstruction {
|
2022-07-21 07:32:39 +00:00
|
|
|
origin,
|
2023-07-26 16:45:51 +00:00
|
|
|
instruction: InInstruction::Dex(swap(Incoming Asset, coin, minimum, out)),
|
2022-07-21 07:14:29 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
where `swap` is a function which:
|
|
|
|
|
2022-07-21 08:06:29 +00:00
|
|
|
1) Swaps the incoming funds for SRI.
|
|
|
|
2) Swaps the SRI for `coin`.
|
|
|
|
3) Checks the amount of `coin` received is greater than `minimum`.
|
|
|
|
4) Executes `out` with the amount of `coin` received.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
|
|
|
##### Add Liquidity
|
|
|
|
|
2023-01-20 16:00:18 +00:00
|
|
|
- `origin` (Option\<ExternalAddress>): Refundable In Instruction's `origin`.
|
|
|
|
- `minimum` (Amount): Minimum amount of SRI tokens to swap
|
|
|
|
half for.
|
|
|
|
- `gas` (Amount): Amount of SRI to send to `address` to
|
|
|
|
cover gas in the future.
|
|
|
|
- `address` (Address): Account to send the created liquidity
|
|
|
|
tokens.
|
2022-07-21 07:14:29 +00:00
|
|
|
|
|
|
|
which expands to:
|
|
|
|
|
|
|
|
```
|
2023-01-20 16:00:18 +00:00
|
|
|
RefundableInInstruction {
|
2022-07-21 07:32:39 +00:00
|
|
|
origin,
|
2023-07-26 16:45:51 +00:00
|
|
|
instruction: InInstruction::Dex(
|
|
|
|
swap_and_add_liquidity(Incoming Asset, minimum, gas, address)
|
|
|
|
),
|
2022-07-21 07:14:29 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
where `swap_and_add_liquidity` is a function which:
|
|
|
|
|
2022-07-21 08:06:29 +00:00
|
|
|
1) Swaps half of the incoming funds for SRI.
|
|
|
|
2) Checks the amount of SRI received is greater than `minimum`.
|
|
|
|
3) Calls `swap_and_add_liquidity` with the amount of SRI received - `gas`, and
|
2023-01-20 16:00:18 +00:00
|
|
|
a matching amount of the incoming coin.
|
2022-07-21 08:06:29 +00:00
|
|
|
4) Transfers any leftover funds to `address`.
|