Can anyone explain to me where the mapping or pegging takes place? Take PAXG the price of which is pegged to gold. How do I do that with my erc-20 token? Is it even written in the code or does it have something to do with the exchanges?
Most of this is not related to the token code and is an economics topic. Which is offtopic here at StackOverflow, so I'm just going to briefly say something that is only partially correct: As long as there's enough buyers and sellers willing to buy/sell the token for the price of gold, the token is going to have a price of gold.
However, you can define functions in your contract that control its total supply, which affects the price (sometimes affects the price - economics again).
Take a look at the PAXG source code:
/**
* @dev Increases the total supply by minting the specified number of tokens to the supply controller account.
* @param _value The number of tokens to add.
* @return A boolean that indicates if the operation was successful.
*/
function increaseSupply(uint256 _value) public onlySupplyController returns (bool success) {
totalSupply_ = totalSupply_.add(_value);
balances[supplyController] = balances[supplyController].add(_value);
emit SupplyIncreased(supplyController, _value);
emit Transfer(address(0), supplyController, _value);
return true;
}
/**
* @dev Decreases the total supply by burning the specified number of tokens from the supply controller account.
* @param _value The number of tokens to remove.
* @return A boolean that indicates if the operation was successful.
*/
function decreaseSupply(uint256 _value) public onlySupplyController returns (bool success) {
require(_value <= balances[supplyController], "not enough supply");
balances[supplyController] = balances[supplyController].sub(_value);
totalSupply_ = totalSupply_.sub(_value);
emit SupplyDecreased(supplyController, _value);
emit Transfer(supplyController, address(0), _value);
return true;
}
By executing these functions (can be executed only from an authorized address - this check is performed in the onlySupplyController
modifier), you can manipulate the balance of address that is present in the value of the supplyController
property.
Other tokens that peg their value to some off-chain asset, also have a built-in buy
function (and sometimes a sell
function as well) which allows buying/selling the token from/to a predefined holder for a predefined price. That might also create an incentive for the buyers and sellers to use the built-in functions instead of less profitable price on an exchange, which might effectively affect the price.