Custom calculations

If none of the above scenarios work for your use case, you can use the customizable Strategies to leverage your own custom calculation mechanisms. Read on to learn more!

Snapshot provides several Voting Strategies that allow you to use your own API to return user's Voting Power or for example perform mathematical calculations on the results from existing Strategies.

You can also create a new custom Voting or Validation Strategy if none of the available solutions meet your needs. Have a look at the Create a strategy section of this documentation to learn more!

This strategy allows off-chain data to be used as Voting Power through calling a custom API endpoint to request the score for a set of addresses.

It's useful to use this strategy if your API can return the Voting Power directly. It's network agnostic.

Strategy setup:

NameTypeDescriptionDefault

url

string

URL of the API endpoint

undefined

type

string

Type of the API endpoint ( api-get or api-post or ipfs or json )

api-get

additionalParams

string

Additional parameters for the API endpoint (optional)

``

If you are passing an IPFS URL use the following format:

{
  "url": "ipfs://...",
  "type": "ipfs"
}

If you are passing a JSON URL use the following format:

{
  "url": "https://...",
  "type": "json"
}

If you are passing an API URL use the following format:

All voter addresses will be passed in the query string.

{
  "url": "https://...",
  "type": "api-get"
}

If you are passing a API URL with POST method use the following format:

{
  "url": "https://...",
  "type": "api-post"
}

You can test it out in the Playground on Snapshot:

You can use the math strategy flexibly with various tokens and networks. This example demonstrates how to set a threshold taking into account tokens on different chains.

Math strategy is powerful in its composability. It allows you to apply common mathematical operations to the outputs of Voting Strategies.

As an example, you can take a square root of the Voting Power calculated by erc20-balance-of, or the lower (min) value out of two different Voting Strategies. You can see all possibilities on the strategy page.

How to set it up for a multichain scenario?

If the Voting Power calculated for your space is based on tokens on different chains and you want to set a minimum threshold defining if a user is eligible to vote, you can use the a-if-lt-b operand. Don't worry if it sounds cryptic, we will go through it step by step.

OperationOperand countDescription

a-if-lt-b

3

(x, a, b) = x < b ? a : x

Let's look at the formula first: (x, a, b) = x < b ? a : x, where:

  • x - sum of Voting Power calculated by the two Voting Strategies on chains 137 and 1 (as per the example below)

  • a - first constant, in our case it's 0

  • b - first constant, in our case it's 100

Strategy setup:

{
  "symbol": "GHST",
  "operands": [
    {
      "type": "strategy",
      "strategy": {
        "name": "multichain",
        "params": {
          "strategies": [
            {
              "name": "erc20-balance-of",
              "network": "137",
              "params": {
                "address": "0x385eeac5cb85a38a9a07a70c73e0a3271cfb54a7",
                "decimals": 18
              }
            },
            {
              "name": "erc20-balance-of",
              "network": "1",
              "params": {
                "address": "0x3f382dbd960e3a9bbceae22651e88158d2791550",
                "decimals": 18
              }
            }
          ]
        }
      }
    },
    {
      "type": "constant",
      "value": 0
    },
    {
      "type": "constant",
      "value": 1
    }
  ],
  "operation": "a-if-lt-b"
}

The algorithm will check if x is smaller than b, the second constant:

  • if it's below b, the final Voting Power will be set to a, first constant -> 0

  • if it's equal to and higher than b, the final Voting Power will be set to x, the sum of result from the Voting Strategies

As you can see we can easily define what is the minimum Voting Power (our example: 100) coming from different chains that is required for the user to vote!

You can test it out in the Playground on Snapshot:

Strategy used in the example: multichain

Last updated