Trabajo de la Trufa

El siguiente es mi código en solidez.

pragma solidity ^0.4.2;

contract DappToken {
  //Constructor
  // set no of Tokens
  // Read the total number of Tokens
  uint256 public totalSupply;

  function DappToken () public {
    totalSupply = 10000000;
  }
}

Ahora voy a interactuar con Truffle 1) He hecho la migración de Truffle

2) En la consola de Truffle usé DappToken.deployed().then(function(i) {token = i ;})

3) deployed () devuelve una promesa y luego responde una llamada (corríjame si me equivoco)

4) Luego usé

 token.totalSupply 
{ [Function]
  call: [Function],
  sendTransaction: [Function],
  request: [Function: bound ],
  estimateGas: [Function] }

// Muestra el siguiente resultado

totalSupply es el nombre de la variable que he dado, pero muestra la Función entre corchetes. ¿Alguien puede explicar cómo se compila truffle en el interior y por qué me muestra como Función? Intenté token.DappToken.totalSupply . Me muestra un error. irá dentro del contrato y llamará a la variable

Respuestas (2)

La solidez no expone las variables directamente, pero crea una función captadora pública.

Su ejemplo se comportará así.

contract DappToken {
  uint256 public _totalSupply;

  function DappToken () public {
    _totalSupply = 10000000;
  }
  function totalSupply() public view returns (uint256) {
    return _totalSupply;
  }
}

Para obtener el suministro total, debe realizar una llamada como función regular.

token.totalSupply().then((result) => { console.log(result); } )

Aquí está la documentación . Creo que debe llamar al token.totalSupplyinterior de forma then()similar al siguiente ejemplo:

var account_one = "0x1234..."; // an address

var meta;
MetaCoin.deployed().then(function(instance) {
  meta = instance;
  return meta.getBalance.call(account_one, {from: account_one});
}).then(function(balance) {
  // If this callback is called, the call was successfully executed.
  // Note that this returns immediately without any waiting.
  // Let's print the return value.
  console.log(balance.toNumber());
}).catch(function(e) {
  // There was an error! Handle it.
})