Create Elrond delegation contract with erdpy and web wallet

Posted May 21, 2021 by Ștefan Rusu ‐ 4 min read

Creating your Elrond delegation contract is the first step for setting up a staking provider. This is a guide on how to create a staking provider from scratch using erdpy.

There are two main parts for setting up a staking provider for Elrond:

  • Delegation smart contract - mandatory. This is the minimum you need to run a staking provider.
  • Delegation manager dashboard - optional. This is highly recommended though.

This topic is detailed by the upstream documentation, however, executing those transactions against Elrond is a lot easier using a tool to deal with the argument the encoding for you, such as erdpy.

I won’t be going into the intricate details for setting up a delegation manager. There’s already an excellent video on this topic. A delegation smart contract is a prerequisite for deploying the delegation manager dashboard.

Install erdpy

Follow the upstream documentation on the matter. I elected to use the recommended erdpy-up method of installation, so if you experience any issues with erdpy, make sure it is properly installed.

You must install this under some sort of *nix environment i.e macOS or Linux. To run this on a Windows machine, you need a proper WSL/WSL2 setup and a distribution such as Ubuntu under that WSL environment.

erdpy deficiencies

erdpy requires some plaintext credentials to be dumped to disk - whether the wallet’s PEM (which contains the wallet’s private key in plaintext) or the JSON Keystore plus its corresponding decryption password as erdpy does not support prompting for the password (like the web wallet). For this reason, to avoid credentials compromise:

  • You need to use this on a machine that uses disk encryption.
  • You need to make sure all of the plaintext credentials are removed after the transactions have been executed.

Any of those leaking is equally bad as it could lead to total loss of funds.

For the time being, erdpy does not support Ledger. However, there’s work in progress to support Ledger signing which would make this process a lot more secure and the interaction with the web wallet unnecessary.

So, because the work on erdpy is currently incomplete, this article describes the use of a hybrid approach:

  • Reuse some functionality from erdpy to encode the function arguments.
  • Run the transactions on the web wallet so you can control the security of the wallet private key, use Ledger, etc.

While this option is more secure, it is also prone to errors, so you must understand what you’re doing. Unlike erdpy, there’s no --simulate flag, so safety’s off. Test this on testnet before going to mainnet.

Create delegation contract via encoder script and web wallet

Encoding values by hand is not great. Therefore, I wrote a fairly tiny script that reuses erdpy’s environment, but not its CLI interface. So, fire up a terminal and run the following:

# load erdpy's virtualenv
source ~/elrondsdk/erdpy-venv/bin/activate

# download encoder script
curl -O https://raw.githubusercontent.com/mr-staker/tools/main/create-delegation-contract

# set executable permissions
chmod +x create-delegation-contract

# example run for encoder script - accepts delegation cap and service fee arguments
./create-delegation-contract 2500 10
Send

To: erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqylllslmq6y6
Amount: 1250 (at minimum)
Gas Limit: 60000000
Data: createNewDelegationContract@0000878678326eac900000@03e8

Explanation: the create-delegation-contract script output instructs you to send a transaction to the delegation manager smart contract address with 1250 EGLD at minimum, a gas limit of 60000000, and a data field encoding the createNewDelegationContract arguments.

For convenience reasons, the delegation cap argument is automatically multiplied by the denomination factor of 1018 and the percentage is automatically multiplied by 100 to obtain the numbers the createNewDelegationContract is expecting. Makes the arguments easier to use as they are readable and work as people rather than machines would expect. You must be 1000% sure that the transaction is sent to the delegation manager smart contract address (per upstream docs and erdpy hardcoded value).

n.b this can only be run once per wallet. There is no delegation SC removal procedure
at the moment.

This transaction returned a delegation smart contract address of: erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqz0llllsup4dew. To unpack the result of the delegation manager smart contract output, this is where this address is returned:

blog-img

Each delegation smart contract has its address, so do not use this value! You must determine which is yours by locating the address in the delegation manager smart contract output.

Note that it may take a while for the funds to show as staked and the delegation provider to appear in your web wallet’s Staking tab.

For managing the staking provider, by far the easiest way is to use the delegation manager dashboard so I won’t be going into alternative methods.

If you followed Trust Staking’s video about how to deploy a Delegation Manager Dashboard, the result should look like this:

blog-img

n.b the testnet is reset frequently so the Delegation SC address is wiped when this
happens. Post genesis, you must re-run this `createNewDelegationContract` transaction
to maintain a Staking Provider on testnet.