Skip to content

Instantly share code, notes, and snippets.

@rmeissner
Last active October 9, 2021 08:44
Show Gist options
  • Save rmeissner/ee9e75f867319d3315e5d6fd94127f40 to your computer and use it in GitHub Desktop.
Save rmeissner/ee9e75f867319d3315e5d6fd94127f40 to your computer and use it in GitHub Desktop.

Template Creation Guide

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.

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

fixed

A fixed input is a predefined input that can be reused anywhere.

Example:

"swapContract": {
    "details": {
        "type": "fixed",
        "value": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"
    }
}

string

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"
    }
},

json

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"
    }
},

bn

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
    }
},

contractCall

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": []
    }
},

Transaction Generation

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

Examples for some templates can be found at https://github.com/rmeissner/transaction-templates/blob/main/templates/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment