Contract-Development

如何部署合約,然後從中發送和檢索字元串?

  • December 5, 2017

我希望將字元串儲存在鏈上。

據我目前了解,我需要建立一個契約,然後我可以將交易(帶有字元串)發送到將被儲存的契約。這是最好的方法嗎?

到目前為止,我已經能夠建立一個契約,但如果我以前的方法是合適的,我將如何:

  1. 設置我的契約以接收和發送字元串
  2. 設置我的交易以發送和接收字元串

到目前為止的契約:

contract mortal {
   /* Define variable owner of the type address */
   address owner;
   string str;

   /* This function is executed at initialization and sets the owner of the contract */
   function mortal() { owner = msg.sender; }

   /* Function to recover the funds on the contract */
   function kill() { if (msg.sender == owner) selfdestruct(owner); }


   function set(string val) public {
       str = val;
   }

   function get() public constant returns (string) {
       return str;
   }
}

不確定如何進行發送/接收交易?我需要同時呼叫web3.eth.sendTransaction發送和web3.eth.call接收嗎?如果是這樣,我該怎麼做?

請確保您在繼續之前eth.account[0]通過執行解鎖您的personal.unlockAccount(eth.account[0])。(或您發送的任何帳戶)。

此程式碼會將合約部署到網路:

var mortalContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"val","type":"string"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
var mortal = mortalContract.new(
  {
    from: web3.eth.accounts[0], 
    data: '0x6060604052341561000f57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506103948061005e6000396000f300606060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806341c0e1b51461005c5780634ed3885e146100715780636d4ce63c146100ce575b600080fd5b341561006757600080fd5b61006f61015c565b005b341561007c57600080fd5b6100cc600480803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506101ed565b005b34156100d957600080fd5b6100e1610207565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610121578082015181840152602081019050610106565b50505050905090810190601f16801561014e5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156101eb576000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b565b80600190805190602001906102039291906102af565b5050565b61020f61032f565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102a55780601f1061027a576101008083540402835291602001916102a5565b820191906000526020600020905b81548152906001019060200180831161028857829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102f057805160ff191683800117855561031e565b8280016001018555821561031e579182015b8281111561031d578251825591602001919060010190610302565b5b50905061032b9190610343565b5090565b602060405190810160405280600081525090565b61036591905b80821115610361576000816000905550600101610349565b5090565b905600a165627a7a72305820a73bbe00b6880e0b6a1a9642179e82cc6ad84cac8cd8d5e76180484b2462d68c0029', 
    gas: 3000000
  }, function(e, contract){
   console.log(e, contract);
   if (typeof contract.address != 'undefined') {
        console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
   }
})

此程式碼將有助於與合約的進一步互動:

var MortalContract = web3.eth.contract(mortalContract.abi);        
var mortalContractInstance = MortalContract.at(mortal.address);        
mortalContractInstance.set("thisIsAnInterestingString", {from: eth.accounts[0], gas: 3000000});
mortalContractInstance.get();

上面的程式碼將部署你的mortal合約,設置字元串,然後檢索字元串。最後一次呼叫mortalContractInstance.get();將返回設置的字元串,即"thisIsAnInterestingString"

更新:作為旁注,我從此處mortal檢索了您的契約的 abi 和編譯字節碼。我可以手動完成,但這個線上工具讓它變得更容易了。


有用的螢幕截圖

在此處輸入圖像描述 在此處輸入圖像描述


希望這可以幫助。

引用自:https://ethereum.stackexchange.com/questions/30763