Creating a variable to calculate an amount in TON at the current exchange rate

In this article we walk through creating a variable that calculates an amount in the TON cryptocurrency at the current exchange rate.

Learn more about how variables work: Variables section overview.

539e78eb93d805905785dbc9c8fa79b1.png

1. Create an input form to record the required amount in dollars. Name the variable {{value_USD}}.

2. Create a variable that will store the TON exchange rate. To do this, use the free CoinGecko API.

8eb431e2d920c30fec003c7861aa5592.png
3ec1fb25745173d923162f02ecb060a6.png

3. Check the result and copy the URL: https://api.coingecko.com/api/v3/simple/price?ids=the-open-network&vs_currencies=usd

29e3eaa45eed0081b2e2226da1388808.png

4. Create a global integrated variable on the Variables tab.

  • Select the Global type, because this value is the same for every user.

  • Enter the name {{price_TON}}.

  • Select the Integrated value type, because the value will come from a third-party service.

  • Value format: Number.

  • Set the default value to 0.

  • Paste the JSON link: https://api.coingecko.com/api/v3/simple/price?ids=the-open-network&vs_currencies=usd.

  • Click Check request.

  • Select the response that contains the exchange rate.

  • Save the variable.

Important! In this example the JSON request returns only 1 response. In other cases, expand the list in the Response field and select the response you need.

2807b4f64289c2e40501fa0ac084129d.jpg

5. Create a personal variable with the "Formula" value format.

  • Select the Personal type, because the variable will use a formula with another personal variable.

  • Enter the name {{formula}}.

  • Value format: Formula.

  • Enter the expression: {{value_USD}} / {{price_TON}}.

  • Save the variable.

7d2cb8f1aa98461299bd39d1d121a88c.png

6. Create a second command that outputs the result of the {{formula}} variable.

95c6d0e8a7d6be1ae88e6f3f8b54276a.png

7. Link the two commands: create an action that sends the next command.

06f10523b7810f6ec7d17bcae1168225.png

8. Publish the changes and test the bot.

0888883e98ad76b49371efe23bf694ec.jpg

Done! The bot works correctly.

Was this page helpful?