Me gustaría escribir tres contratos token A, B y C que puedan interactuar entre sí. A es el token principal, B y C son los tokens secundarios. Un comprador puede comprar el token principal (token A) y usarlo para comprar los tokens secundarios (token B y token C). Después de comprar los tokens secundarios, el saldo del token principal del comprador debería disminuir y el saldo de los tokens secundarios debería aumentar. ¿Cómo debo diseñar estos tres contratos? Ahora estoy modificando el token estándar ERC20, pero no sé cómo diseñar la función transferFrom en el ERC20 para satisfacer las condiciones anteriores.
Esto es tan fácil como poner métodos que permitirán que los contratos principales ejecuten funciones en el contrato secundario que realizará acciones.
Podría tener una función en el padre que, cuando la ejecuta un titular de token, les permite transferir fondos a un contrato secundario y, posteriormente, en el contrato secundario para llamar al padre nuevamente.
El siguiente ejemplo implementa un contrato que crea 2 subcontratos y funciones de transferencia que permiten que los fondos se transfieran entre el padre y los hijos.
Inicialmente le dará al creador 1000 tokens principales, luego el propietario podría llamar, por ejemplo Main.buyB(10)
. Entonces, el propietario tendría 990 Main y 10 B. Posteriormente, el propietario podría llamar B.sell(5)
y tener 995 Main y 5 B.
pragma solidity ^0.4.21;
/// @title Tracks ownershipt of the descendant token.
contract Owned {
address owner;
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
}
/// @title Should be a fully functioning token, only a stub is provided below,
/// for demonstration purposes.
contract Token is Owned {
mapping(address => uint) balances;
/// @notice ERC20 - Returns the balance of the requested address.
/// @param _to The address to return the balance for.
/// @return The token balance of `_to`.
function balanceOf(address _to) external view returns (uint) {
return balances[_to];
}
// Contains all the other standard ERC20 token junk.
}
/// @title Is the main contract which can buy B or C tokens.
contract Main is Owned, Token {
// The first "B" sub-token.
Sub public b_token;
// The second "C" sub-token.
Sub public c_token;
/// @notice Creates a new Main token, then creates 2 sub-tokens and saves them.
function Main() public {
// Grant the creator 1000 Main tokens.
balances[msg.sender] = 1000;
// Create 2 new sub contracts and save them as b_token and c_token.
b_token = new Sub();
c_token = new Sub();
}
/// @notice Buys a given number of B tokens using the balance of the Main token.
/// @param _value The amount of B to buy with Main.
function buyB(uint _value) external {
require(balances[msg.sender] > _value);
balances[msg.sender] -= _value;
b_token._give(msg.sender, _value);
}
/// @notice Buys a given number of C tokens using the balance of the Main token.
/// @param _value The amount of C to buy with Main.
function buyC(uint _value) external {
require(balances[msg.sender] > _value);
balances[msg.sender] -= _value;
c_token._give(msg.sender, _value);
}
/// @notice Gives `_value` worth of Main tokens to `_to`.
/// @dev Only callable by B or C children contracts.
/// @param _to The address to give Main to.
/// @param _value The amount of Main to give to `_to`.
function _give(address _to, uint _value) external {
require(msg.sender == address(b_token) || msg.sender == address(c_token));
balances[_to] += _value;
require(balances[_to] >= _value);
}
}
/// @title A sub-token of `Main`, purchasable using Main token, and sellable for
/// Main token.
contract Sub is Token {
/// @notice Creates a new Sub token and assigns the creator (`Main`) as the owner.
function Sub() public {
owner = msg.sender;
}
/// @notice Gives `_value` worth of this tokens to `_to`.
/// @dev Only callable by parent Main contract.
/// @param _to The address to give this token to.
/// @param _value The amount of this token to give to `_to`.
function _give(address _to, uint _value) external onlyOwner {
balances[_to] += _value;
require(balances[_to] >= _value);
}
/// @notice Sells a given value of this token and gives back parent `Main` token.
/// @param _value The amount of this token to sell.
function sell(uint _value) external {
require(balances[msg.sender] >= _value);
balances[msg.sender] -= _value;
Main(owner)._give(msg.sender, _value);
}
}
Una forma de hacerlo es mantener el contrato A como un token ERC20 básico sin nada agregado. Entonces, cuando alguien transfiere A, la balances
matriz se modifica.
Los contratos B y C serían tokens ERC20 un poco especiales. Comience con ERC20, pero agregue algunos métodos de compra (y venta) que modifican el saldo en el contrato A para disminuir sus tokens A y aumentar el saldo en el contrato B/C. En realidad, no creo que haya muchas otras rarezas; de lo contrario, B y C podrían usarse como ERC20 normal.
supakaidad
2341047123413331
magia007
Achala Dissanayake
henk