快速开始

普通合约和以太坊上一致,合约创建者负责合约的部署,部署 需要花费一定的Kal,合约调用者调用时需要花费Kal,在调用的交易中指定gas_limit和gas_price。

安装solidity编译器

和以太坊一样使用前需要安装npm的环境,然后再安装solifity的编译器环境

npm install -g solc@0.4.24
# or
npm install -g solc-cli@0.4.24
# or
sudo apt-get install solc@0.4.24

创建合约文件

创建新文件game.sol,并输入以下内容。

pragma solidity ^0.4.0;

contract Game
{
	address public creator;
	string greeting;

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

	function greet() constant returns (string){
		return greeting;
	}

	function setGreeting(string _newgreeting){
		greeting = _newgreeting;
	}

	function kill(){
		require(msg.sender == creator,"not creator");
		suicide(creator);
	}
}

编译部署合约

编译合约代码game.sol,得到ABI文件(Game_sol_Game.abi) 和二进制文件(Game_sol_Game.bin):

solcjs –abi –bin Game.sol

接下来使用命令行连接节点部署合约。

这里假设你在本地运行了一个节点,并开启了8545端口,你需要使用命令kalgo attach http://127.0.0.1:8545连接到你的节点。如果没有,你可以使用测试网络的公开服务节点来代替kalgo attach http://testnet.kaleidochain.io:8545

在打开的终端命令行中,执行以下代码:

var gameabi = [{"constant":true,"inputs":[],"name":"creator","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_newgreeting","type":"string"}],"name":"setGreeting","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}];

var gamebin = "0x"+"60806040526040516105b53803806105b583398101806040528101908080518201929190505050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550806001908051906020019061007c929190610083565b5050610128565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100c457805160ff19168380011785556100f2565b828001600101855582156100f2579182015b828111156100f15782518255916020019190600101906100d6565b5b5090506100ff9190610103565b5090565b61012591905b80821115610121576000816000905550600101610109565b5090565b90565b61047e806101376000396000f300608060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806302d05d3f1461006757806341c0e1b5146100be578063a4136862146100d5578063cfae32171461013e575b600080fd5b34801561007357600080fd5b5061007c6101ce565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3480156100ca57600080fd5b506100d36101f3565b005b3480156100e157600080fd5b5061013c600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506102f1565b005b34801561014a57600080fd5b5061015361030b565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610193578082015181840152602081019050610178565b50505050905090810190601f1680156101c05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156102b7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252600b8152602001807f6e6f742063726561746f7200000000000000000000000000000000000000000081525060200191505060405180910390fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b80600190805190602001906103079291906103ad565b5050565b606060018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103a35780601f10610378576101008083540402835291602001916103a3565b820191906000526020600020905b81548152906001019060200180831161038657829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106103ee57805160ff191683800117855561041c565b8280016001018555821561041c579182015b8281111561041b578251825591602001919060010190610400565b5b509050610429919061042d565b5090565b61044f91905b8082111561044b576000816000905550600101610433565b5090565b905600a165627a7a723058200a8b27b1fc751aab13bbf1db698f2f9c0835fd1b5a6dbd4e77ee8cb89f0939950029";

var gameContract = web3.eth.contract(gameabi);
var gameToken = gameContract.new("construct1",{from:eth.accounts[0],data:gamebin,gas:1000000},function (e,contract){if(contract.address != "undefined" ){console.log("game contract success:"+contract.address+" "+contract.transactionHash);} else{console.log("game contract fail")}});

// test call
gameToken.greet();