This guide outlines how to create a template that can be used with https://rimeissner.dev/transaction-templates/
A template is a json file that has a root object with the properties name
, inputs
, txs
.
The inputs
are used together with the txs
to generated the final transactions. Also a simple interface with input fiels is generated for the specified inputs.
The inputs
property is a mapping of where the key is the input id and the value descripes the input. The input description has the required field details
and the optional field label
The label
is displayed to the user when generating the input field.
The details
has the required property type
to indicate what kind of input it is.
There are different types of inputs currently supported: fixed
, string
, json
, bn
, contractCall
A fixed input is a predefined input that can be reused anywhere.
Example:
"swapContract": {
"details": {
"type": "fixed",
"value": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"
}
}
The string
input can be used to request a simple string from the user (e.g. an address, as there currently is no specific address input).
Example:
"sellToken": {
"label": "Token to sell",
"details": {
"type": "string"
}
},
The json
input can be used to request more complex input from the user (e.g. an array of values, as there currently is no specific for this).
Example:
"acceptedTokens": {
"label": "Array of token addresses to accept",
"details": {
"type": "json"
}
},
The bn
input can be used ask the user for a number input. The input is converted into a BigNumber object with some decimals. This object has the additional key decimals
.
The decimal
param can either be a number, then it is used directly as the amount of decimals for the BigNumber. Or a string that references another input.
Example:
"daiAmount": {
"label": "Amount of DAI to transfer",
"details": {
"type": "bn",
"decimals": 18 // We know how many decimals DAI has
}
},
"tokenAmount": {
"label": "Amount of tokens to transfer",
"details": {
"type": "bn",
"decimals": "tokenDecimals" // We require the token decimals to properly convert the input
}
},
The contractCall
input can be used to retrieve information from the blockchain via an eth_call
. This object has the additional keys target
, signature
and inputs
.
The target
param is the input id that is used to resolve the target address.
The signatures
is a string that represents the contract function signature.
The inputs
specifies the inputs used the we signature to generate the call data. This should be an array of input ids that are resolved to the parameters used witht he function.
Example:
"tokenDecimals": {
"details": {
"type": "contractCall",
"target": "tokenContract",
"signature": "decimals() returns (uint256)",
"inputs": []
}
},
The specified inputs can be used to generate an array of transactions. This array needs to specified as the txs
parameter.
Each element of the array should be an object with the keys: description
, to
, value
, data
. Only description
is required.
Example:
"txs": [
{
"description": "Approve to use token",
"to": "tokenContract",
"data": {
"signature": "approve(address sender, uint256 amount)",
"inputs": [
"vaultContract",
"tokenAmount"
]
}
},
{
"description": "Swap tokens",
"to": "swapContract",
"data": {
"signature": "swap(uint256 amount)",
"inputs": [
"tokenAmount"
]
}
}
]
Examples for some templates can be found at https://github.com/rmeissner/transaction-templates/blob/main/templates/