Add EVER to your backend
This document describes the various ways to accomplish the most important tasks of running a backend project that supports EVER.
Introduction
This document describes the various ways to accomplish the most important tasks of running a backend application that supports EVER.
There are a few different ways to accomplish the necessary tasks:
Blockchain access may be set up either through the Evercloud or through your own supernode - the DApp server.
User account management can be accomplished either through the everdev command line tool or integrate into your backend with EVER-SDK client libraries. Both of these approaches are compatible with either of the blockchain access setups.
Setting up Blockchain Access
Using Evercloud
Using Evercloud allows you to work with TVM blockchains without having to run your own node. Everdev and SDK can connect to it, as if it were a regular node. It has the same API as a node, and provides all neede capabilities.
This page lists the cloud endpoints. To get access credentials go through this guide.
Whenever you have to specify a network endpoint in the examples given below, use the endpoints and credentials you receive in the Evercloud dashboard.
Note: We recommend testing out the full setup on the developer network first.
Using DApp Server
If you prefer to run your own node, you may set up your own DApp server. It is a client supernode, that may be set up on your own servers and provide full access to TVM networks. To connect to it with Everdev or SDK, it needs to have a domain name and a DNS record. You can specify its URL whenever you have to set the network in the examples given below.
Get the setup scripts in this repository: https://github.com/everx-labs/evernode-ds
1. System Requirements
NVMe SSD disks are recommended for storage.
For simplicity, all services are deployed on one host and the system requirements for it are high, so it makes sense to distribute services across different servers. After understanding this installation process, you can easily customize it for yourself.
itgoldio/everscale-dapp-server: Consider this project if you prefer deployment via Ansible.
2.1 Prerequisites
Host OS: Linux (all scripts tested on Ubuntu 20.04).
DApp server is accessed via HTTPS, so your server must have a fully qualified domain name. A self-signed certificate will be received on start-up and will be renewed automatically.
Installed Git, Docker Engine, Docker CLI, Docker Compose v2 or later.
2.2 Configuration
2.2.1 Set variables
Check configure.sh
and set at least these environment variables:
NETWORK_TYPE
EVERNODE_FQDN
LETSENCRYPT_EMAIL
2.2.2 Generate credentials to access the ArangoDB web interface
Generate credentials (usernames and password) for basic authentication and update .htpasswd
file.
You can generate it by running htpasswd -nb <name> <password>
2.2.3 Run configuration script
This script creates ./deploy
directory
2.3 Deployment
Run ./up.sh
.
After the script completes normally (it takes 30 min approx.), the node starts synchronizing its state, which can take several hours. Use the following command to check the progress:
Script output example:
If the timediff
parameter is less than 10 seconds, synchronization with masterchain is complete.
"sync_status": "synchronization finished"
means synchronization with workchains is complete
Setting up Wallet Account
Currently we can recommend the SetcodeMultisig contract for use in user accounts. It is well tested and secure, supports multiple custodians, and can be set up to require several independent signatures for any transfers.
Using CLI tool
Everdev, the command line tool for development on the Everscale blockchain, allows to write scripts to deploy any smart contracts to the blockchain, call all contract methods, sign transactions, and generally manage an account.
It works with both Evercloud and DApp server.
1. Install Everdev
It requires NPM to be installed.
If you experience any problems with installation, check out our troubleshooting section.
2. Configure network connection
Everdev has a built-in network tool to manage your networks and access credentials.
Using Evercloud endpoints
Add your Evercloud endpoint to everdev and make it default:
Using DApp Server endpoint
If you are setting up a connection via your own DApp server, user the following command to add it to the network list (it will be named
dappserver
).
To set your dappserver
network as default, use the following command:
3. Set a giver contract on your network
On Everscale, you need to sponsor a contract address in advance to be able to deploy the contract.
Everdev provides a way to set an account of your choice as a giver for deployment operations, so you will not have to do a separate step of sending tokens to a new contract address every time you deploy something. This contract can some multisig wallet, for example your Surf account.
Note: To work automatically, the giver contract should have only one custodian.
To set it up, first save the custodian keys of your giver account into a signer that will be used to sign giver transactions (Learn more about the signer tool here):
Then add the giver address specifying the signer to be used with it.
Where
giver_type
is the type of the giver contract you selected (GiverV1 | GiverV2 | GiverV3 | SafeMultisigWallet | MsigV2| SetcodeMultisigWallet)
We recommend using Multisig 2.0 as giver, for that use MsigV2
giver_type.
4. Get wallet account contract files
We recommend using Multisig 2.0 contracts as a wallet. They can be found here. In this guide SetcodeMultisig specifically is used.
Download the contract files and place them in the working folder. Direct links to its files are as follows:
.tvc - Compiled contract code
SetcodeMultisig.tvc direct link:
.abi.json - application binary interface, describing the functions of the contract
SetcodeMultisig.abi.json direct link:
Execute the commands of the following steps from the directory with the contract files.
5. Create wallet account signer
To generate your wallet account signer enter the following command:
Or, if you already have a seed phrase, add it like this:
To deploy multisig wallet account you will need to specify the public key of the signer. To view it, use the following command:
The keys will be displayed in terminal (if you imported the seed phrase, it will be displayed here as well):
Usually a single owner (with a single signer) per wallet account is optimal for any tasks that require automation. However, it is possible to set up accounts with multiple owners. In this case, each of the owners has to generate their own signer and provide their public keys to the deployer. Also, the signer used to deploy the account doesn't have to be among its owners.
6. Deploy the wallet account contract to blockchain
Use the following command for a simple one-owner account:
Where
value
parameter is the amount of nanotokens to be spent on deployment (can be omitted, in which case 10 tokens from giver will be spent)
owner_public_key
is usually the public key of wallet_signer
in the form 0x...
.
lifetime
- time in seconds that a transaction in multi-owner accounts will persits and be available for signing by other owners. For a simple multi owner account may be set to any value, as it will be executed immediately anyway.
Example:
For more complex cases (multiple owners etc.) view Everdev contract tool docs.
Once the contract is deployed, its address will be displayed in terminal.
Using SDK
You may integrate above described process of wallet account deployment into your backend code. The functionality is supported in SDK.
A sample is available in this repository and an overview is given below.
Bindings for a large number of languages have been developed for SDK.
Note, that similar to the Everdev approach described above, you have to sponsor a user account before deploying contract code. The sample assumes you use the devnet faucet of Evercloud Dashboard, where you can request test tokens to the contract address generated by the sample. In a production environment you may set up a giver to sponsor your contract deployment operations. An example of such a set up can be found in this sample.
The recommended SetcodeMultisig contract is used.
Monitoring transactions
Lets assume we need to reliably know when customers receive or transfer funds from their wallets. Samples of transaction pagination and subscription are available in the samples repository. An overview of the relevant parts is given below.
In these samples JS SDK is used. Bindings for a large number of languages have been developed for SDK.
Pagination
The pagination sample queries and displays transactions in workchain 0 (workchain where simple transfers happen, -1 workchain is masterchain where you can find service transactions and validator transactions) from the beginning. We can get all the transaction and filter by account addresses on the backend side.
Note: By default the Blockchain API queries, such as the one used here provide only data from the past 7 days. To retrieve older data, make sure to use the archive: true
flag, as shown in the sample:
Subscription
Subscription sample subscribes to new transactions of the listed accounts and lists them as they appear.
You may test out the demo application running this process on the developer network by cloning the sdk-samples repository, creating a project in https://dashboard.evercloud.dev, exporting the API endpoint as an environment variable:
and running the following command in the /demo/subscribe-transactions
folder:
Not all transactions that are successful are valid transfers and not all transactions that are aborted actually failed. Read here how to understand which transfers are successful transfers and which are not.
Withdrawing from wallet accounts
The specific function that is used to withdraw the funds depends on the contract chosen for the wallet account. Examples provided below are applicable for the SetcodeMultisig contract.
Using CLI tool
Command line Everdev
tool may be used to automate withdrawals from wallet account in your scripts.
If the user made a mistake in the destination address, and has no control over it, these funds will be lost forever. If the account does not exist, and the user makes mistakes deploying it after the funds are transferred, they may end up being lost as well.
So, to perform a simple transfer from a single-owner user account to any specified account, we should make sure that it is already deployed, by setting bounce
flag to true. If the account does not exist, funds will return back.
<wallet_account_address>
- address of the user account. Example: 0:7bf2b2ec80371601f854bff9ed0a1171714d922c8bfc86d39e67a7e3a41b2176
wallet_signer
- name of the user account owner signer
recipient_address
- raw address of the recipient smart contract. Example: 255a3ad9dfa8aa4f3481856aafc7d79f47d50205190bd56147138740e9b177f3
value
: - amount of tokens to transfer in nanotokens (Example: value:10000000000 sets up a transfer of 10 tokens).
bounce
- use true to transfer funds only to deployed accounts.
flags
- use 3 for a simple transfer.
payload
- use "" for simple transfer.
Note: To transfer all funds from the account use sendTransaction
method with flag 130
and value 0.
Example of regular withdrawal transaction on a single-owner multisig:
Basic checks of the address format will be performed by the Everdev utility automatically, only addresses of a valid Everscale format will be accepted.
(Optional) Multi-owner accounts and Confirm transaction
Note, that if your user account has multiple custodians, the transaction has to be confirmed by the required number of signatures to be executed. This transaction ID should be communicated to other custodians, who should use it to confirm the transaction.
To withdraw tokens from a multi-owner account use the following command:
<wallet_account_address>
- address of the user account. Example: 0:7bf2b2ec80371601f854bff9ed0a1171714d922c8bfc86d39e67a7e3a41b2176
wallet_signer
- name of the user account owner signer
value
: - amount of tokens to transfer in nanotokens (Example: value:10000000000 sets up a transfer of 10 tokens).
bounce
- use false to transfer funds to an already deployed account
allBalance
- used to transfer all funds in the wallet. Use false for a simple transfer.
payload
- use "" for simple transfer.
stateInit
- use null
for a simple transfer.
This will generate a transaction and display its transId
that will have to be confirmed by other custodians.
To confirm a transaction, use the following command:
<wallet_account_address>
- address of the user account. Example: 0:7bf2b2ec80371601f854bff9ed0a1171714d922c8bfc86d39e67a7e3a41b2176
wallet_signer2
- signer of another multisig custodian (not the one who initiated the transaction).
transactionId
– the ID of the transaction can be acquired from the custodian who created it.
Mitigating risks of token loss due to user error
The are two main cases regarding transfers to user accounts: a user may already have an active account to which they want to withdraw funds (set bounce to true), or they may want to withdraw funds to a completely new account, that doesn't exist at the time withdraw is requested (set bounce to false).
The status of the account provided by the user may be checked with the following Everdev command:
Example of existing account:
Example of account that doesn't exist yet:
The possible results of this command are the following:
Doesn't exist
- account does not exist. It needs to be sponsored, then deployed, and only then will it be active.
Uninit
- account already has some funds on it but contract code has not been deployed yet. User needs to deploy it.
Active
- account already exists, and its code is deployed.
In the first to cases, the service might first transfer a small portion of the requested amount (~1 EVER) and request that the user deploys their contract. Upon the user's confirmation that the account is deployed, its status may be rechecked, and if it became active, the remaining amount of requested funds may be safely transferred.
If the account is already active, a small portion of the requested amount may be transferred to the user, and the user may be asked what amount they received (note: a small amount of the transfer, usually less than 0.05 EVER, will be spent on fees, so it's best to ask for the whole number of tokens transferred). If the amounts match, the rest of the requested funds may be transferred as well.
Using SDK
You may integrate withdrawals from wallet account into your backend using SDK as well. A sample is available in this repository and an overview of the relevant part is given below.
In this sample JS SDK is used. Bindings for a large number of languages have been developed for SDK.
This example shows how to generate a withdrawal transaction from a Multisig wallet, using its sendTransaction
method. Note, that if Multisig has multiple custodians, the transaction will have to be confirmed with the confirmTransaction
method.
In this example tokens are withdrawn from the user account to the account specified in dest
. In a proper implementation, the account given by user should be used instead.
Mitigating risks of token loss due to user error
Similarly to the everdev approach, you can add the account status check prior to sending tokens.
The are two main cases regarding transfers to user accounts: a user may already have an active account to which they want to withdraw funds, or they may want to withdraw funds to a completely new account, that doesn't exist at the time withdraw is requested.
Here is an example of checking account status in SDK:
Last updated