truffle console usage summary

 4.1. Create your own contract
Create your own contract file Greeter.sol in the ./contract directory with the following code:

contract Greeter         
{
    address creator;     
    string greeting;     

    function Greeter(string _greeting) public   
    {
        creator = msg.sender;
        greeting = _greeting;
    }
    

    function greet() public constant returns (string)           
    {
        return greeting;
    }
    
    function setGreeting(string _newgreeting) public
    {
        greeting = _newgreeting;
    }
    
     /**********
     Standard kill() function to recover funds 
     **********/
    
    function kill()public
    { 
        if (msg.sender == creator)
            suicide(creator);  // kills this contract and sends remaining funds back to creator
    }

}

4.2. New release script
Modify the 2_deploy_contracts.js file in the ./migrations/ directory and add the release code as follows:

module.exports = function(deployer) {
  deployer.deploy(Greeter,"Hello, World!");//"参数在第二个变量携带"
};

After that, truffle compile compiles the contract; truffle migrate deploys the contract, the effect is as follows:

insert image description here

4.3. Operation through the console
We can use the truffle console command to enter the console, and input relevant commands through the console to perform related operations. The effect is as follows:

insert image description here

 Perform related operations by entering commands:

insert image description here

Query ----- call call example

MetaCoin.deployed().then(function(contractInstance){contractInstance.getBalance.call('0x2B9f4E75310127Bbd1A13E6a3Cb9cBc8bDc05F91').then(function(v){console.log(v)})})

Settings---sendTransaction

MetaCoin.deployed().then(function(contractInstance){contractInstance.sendCoin.sendTransaction('0xA39b1E127aEd52352b3cc043C94189E46C9E9601',200).then(function(v){console.log(v)})})

 

The construction of the Truffle framework under Ubuntu - Programmer Sought

How to deploy smart contracts with Truffle - 51CTO.COM

Enter the truffle console to debug the contract

After the contract is deployed successfully, execute it on the terminal truffle console, you can enter the Javascript console to debug the contract:

 
 
  1. truffle console

  2. truffle(default)>

in the Javascript console via

ContractName.deployed()orContractName.at(contractAddress)

Get the deployed contract object, and then you can use the object to call the contract's methods for debugging:

Get the deployed contract object:

truffle(default)> var metacoin = MetaCoin.deployed()

First check the MetaCoin balance of the first account:

truffle(default)> metacoin.getBalance.call(web3.eth.accounts[0])

It should return 10000 because the contract creator's initial value is set to 10000 in the MetaCoin constructor.

Next, transfer 30 MetaCoins from the first account to the second account:

truffle(default)> metacoin.sendCoin.sendTransaction(web3.eth.accounts[1],30,{from:web3.eth.accounts[0]})

The above call to sendCoin will send a transaction to the blockchain, which requires node mining to make the transaction effective.

After the transfer is successful, check whether the balance of the two accounts has changed:

 
 
  1. truffle(default)> metacoin.getBalance.call(web3.eth.accounts[0])

  2. truffle(default)> metacoin.getBalance.call(web3.eth.accounts[1])

After the execution, it is found that the balance of accounts[0] has become 9970, and the balance of accounts[1] has become 30, indicating that the transfer is successful. Enter .exitto exit the truffle console.

Ethereum study notes: Truffle development environment configuration and use - Programmer Sought

Guess you like

Origin blog.csdn.net/u013288190/article/details/123850542