This tutorial will show you how to create synthetic tokens from the command line using UMA’s synthetic token template. Before beginning this tutorial, please make sure your environment is set up correctly by following the instructions in the setup guide. After completing this section, you should:
- Have the protocol repo cloned.
- Be running an instance of Ganache on port 9545.
- Have installed dependencies using
yarnand have run
yarn qbuildto build the contracts.
Below, we’ll discuss how to create and manage a token sponsor position via the command line on a local testnet. UMA also has a command-line interface (CLI) tool that bundles and renames some of these steps. These tutorials discuss how to create and manage a token sponsor position using this CLI tool.
- Begin here if you are creating a new type of synthetic token on a local testnet.
- Begin here if you are creating synthetic tokens from an existing contract on a local testnet.
Parameterize and deploy a contract
- Open the truffle console and connect it to the test network.
- Migrate the contracts within the truffle console with the migrate command:
- Create an instance of the expiring multiparty creator (the contract factory for synthetic tokens). This command should return “undefined”.
- Define the parameters for the synthetic tokens you would like to create.
Note that in this example,
syntheticSymbol are set to "UMATEST", "Test UMA Token", and "UMATEST", respectively, but you can set these parameters to any names you prefer in the local environment.
- Before the contract for the synthetic tokens can be created, the price identifier for the synthetic tokens must be registered with
IdentifierWhitelist. This is important to ensure that the UMA DVM can resolve any disputes for these synthetic tokens.
- We also need to register the
empCreatorfactory with the
registryto give it permission to create new expiring multiparty (emp) synthetic tokens.
- We also need to register the collateral token with the
- Now, we can create a new expiring multiparty synthetic token with the factory instance.
Create new tokens from an existing contract
- Now that we’ve parameterized and deployed the synthetic token contract, we will create synthetic tokens from that contract. The first step is to create an instance of the Test token and mint 10,000 to the wallet. This is the token that will serve as collateral for the synthetic token. Give permission to the empCreator to spend the collateral tokens on our behalf.
- We can now create a synthetic token position. We will deposit 150 units of collateral (the first argument) to create 100 units of synthetic tokens (the second argument).
- Let’s check that we now have synthetic tokens. We should have 100 synthetic tokens and 9,850 collateral tokens remaining.
Redeem tokens against a contract
- Because we are a token sponsor for this synthetic token contract, we can redeem some of the tokens we minted even before the synthetic token expires. Let's redeem half.
- Let’s check that our synthetic token balance has decreased and our collateral token balance has increased. Our synthetic token balance should now be 50. Because the contract does not have an on-chain price feed to determine the token redemption value for the tokens, it will give us collateral equal to the proportional value value of the total collateral deposited to back the 100 tokens (50/100 * 150 = 75). Our collateral token balance should increase to 9,925.
Deposit and withdraw collateral
- As a token sponsor, we may wish to add additional collateral to our position to avoid being liquidated by a token holder. Let’s deposit 10 additional collateral tokens to our position and see our updated balance, from 9,925 to 9,915.
- For a token sponsor to withdraw collateral from his position, there are typically 2 ways to do this. Read this explainer for more information. In this scenario, because we are the only token sponsor, we will have to withdraw collateral the “slow” way. First, we need to request a withdrawal of 10 collateral tokens.
- Now, we need to simulate the withdrawal liveness period passing without a dispute of our withdrawal request. The
ExpiringMultipartyCreatorused in step 8 has a strict withdrawal liveness of 7200 seconds, or 2 hours. This means that in order for a withdrawal request to be processed at least 2 hours must pass before attempting to withdraw from the position. We can simulate time advancing until after this withdrawal liveness period by using an the deployed instance of
Timer. This contact acts to simulate time changes within the UMA ecosystem when testing smart contracts.
- Let’s now check that our collateral token balance has returned to 9,925.