欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

以太坊智能合约构造函数初始化解密

程序员文章站 2022-07-14 21:20:05
...
1.编译智能合约源代码得到code
2.部署智能合约
   参考rpc命令,web3有完善的接口
   curl -X POST --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{"from":"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826","data":"........."}],"id":1}' http://192.168.10.18:7000/rpc
3.构建data举例,智能合约
pragma solidity ^0.4.24;
contract RI {
    // The keyword "public" makes those variables
    // readable from outside.
    address public minter;
    mapping (address => uint) public balances;
    string public accounts;

    // Events allow light clients to react on
    // changes efficiently.
    event Sent(address from, address to, uint amount);

    // This is the constructor whose code is
    // run only when the contract is created.
    constructor (uint apply,string users) public {
        minter = msg.sender;
        balances[msg.sender]=apply;
        accounts=users;
    }

    function mint(address receiver, uint amount) public {
        if (msg.sender != minter) return;
        balances[receiver] += amount;
    }

    function send(address receiver, uint amount) public {
        if (balances[msg.sender] < amount) return;
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        emit Sent(msg.sender, receiver, amount);
    }
}

4.没有构造器参数的时候,部署智能合约应该发送data
0x608060405234801561001057600080fd5b50604051610460380380610460833981016040908152815160208084015160008054600160a060020a0319163390811782558152600183529390932082905591909201805190916100669160029184019061006e565b505050610109565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100af57805160ff19168380011785556100dc565b828001600101855582156100dc579182015b828111156100dc5782518255916020019190600101906100c1565b506100e89291506100ec565b5090565b61010691905b808211156100e857600081556001016100f2565b90565b610348806101186000396000f30060806040526004361061006c5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166307546172811461007157806327e235e3146100a257806340c10f19146100d557806368cd03f6146100fb578063d0679d3414610185575b600080fd5b34801561007d57600080fd5b506100866101a9565b60408051600160a060020a039092168252519081900360200190f35b3480156100ae57600080fd5b506100c3600160a060020a03600435166101b8565b60408051918252519081900360200190f35b3480156100e157600080fd5b506100f9600160a060020a03600435166024356101ca565b005b34801561010757600080fd5b50610110610204565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561014a578181015183820152602001610132565b50505050905090810190601f1680156101775780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561019157600080fd5b506100f9600160a060020a036004351660243561028f565b600054600160a060020a031681565b60016020526000908152604090205481565b600054600160a060020a031633146101e157610200565b600160a060020a03821660009081526001602052604090208054820190555b5050565b6002805460408051602060018416156101000260001901909316849004601f810184900484028201840190925281815292918301828280156102875780601f1061025c57610100808354040283529160200191610287565b820191906000526020600020905b81548152906001019060200180831161026a57829003601f168201915b505050505081565b336000908152600160205260409020548111156102ab57610200565b33600081815260016020908152604080832080548690039055600160a060020a03861680845292819020805486019055805193845290830191909152818101839052517f3990db2d31862302a685e8086b5755072a6e2b5b780af1ee81ece35ee3cd33459181900360600190a150505600a165627a7a72305820383284c7be6ce7a40cd7ecfd2f334a26968e7b27dec29a70986c60e09e54e8680029000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000


5.构造器参数123,,发送的data

0x608060405234801561001057600080fd5b50604051610460380380610460833981016040908152815160208084015160008054600160a060020a0319163390811782558152600183529390932082905591909201805190916100669160029184019061006e565b505050610109565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100af57805160ff19168380011785556100dc565b828001600101855582156100dc579182015b828111156100dc5782518255916020019190600101906100c1565b506100e89291506100ec565b5090565b61010691905b808211156100e857600081556001016100f2565b90565b610348806101186000396000f30060806040526004361061006c5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166307546172811461007157806327e235e3146100a257806340c10f19146100d557806368cd03f6146100fb578063d0679d3414610185575b600080fd5b34801561007d57600080fd5b506100866101a9565b60408051600160a060020a039092168252519081900360200190f35b3480156100ae57600080fd5b506100c3600160a060020a03600435166101b8565b60408051918252519081900360200190f35b3480156100e157600080fd5b506100f9600160a060020a03600435166024356101ca565b005b34801561010757600080fd5b50610110610204565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561014a578181015183820152602001610132565b50505050905090810190601f1680156101775780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561019157600080fd5b506100f9600160a060020a036004351660243561028f565b600054600160a060020a031681565b60016020526000908152604090205481565b600054600160a060020a031633146101e157610200565b600160a060020a03821660009081526001602052604090208054820190555b5050565b6002805460408051602060018416156101000260001901909316849004601f810184900484028201840190925281815292918301828280156102875780601f1061025c57610100808354040283529160200191610287565b820191906000526020600020905b81548152906001019060200180831161026a57829003601f168201915b505050505081565b336000908152600160205260409020548111156102ab57610200565b33600081815260016020908152604080832080548690039055600160a060020a03861680845292819020805486019055805193845290830191909152818101839052517f3990db2d31862302a685e8086b5755072a6e2b5b780af1ee81ece35ee3cd33459181900360600190a150505600a165627a7a72305820383284c7be6ce7a40cd7ecfd2f334a26968e7b27dec29a70986c60e09e54e8680029000000000000000000000000000000000000000000000000000000000000007b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20576f726c642100000000000000000000000000000000000000


6.上面加粗部分,表达了带参数的时候只能合约的code不同之处。
  如果要仔细体会和分析,可以在geth控制台执行web3.fromAscii来分析。