部署合约
1.用Solidity语言编写合约内容
pragma solidity ^0.4.4;
contract Car {
// Vehicle identifier, can be VIN-number, etc.
bytes public vehicleName = "Tesla";
// We’ll use public so no need to have getOwner method
bytes16 public owner;
function Car() {
// constructor
}
function setOwner(bytes16 newOwner)
{
owner = newOwner;
}
}
2.对合约类进行编译,获得abiDefinition和bytecode
因为geth 1.6已经取消了eth.complie,所以合约的编译不能在geth中完成。只能通过solc或者在线工具https://ethereum.github.io/browser-solidity 完成。因为solc只能在linux下编译,实验环境是windows,选择在线工具。
用浏览器打开在线工具,并将步骤一的合约粘贴入工具左侧的文本区中
因为当前自动编译选项为勾选状态,所以工具会对合约进行自动编译。这时,我们需要点击“Details”按钮
3.通过abiDefinition和bytecode实例化合约并进行布署
// 以下两段即是步骤二获取的abiDefinition和bytecode数据
abi=[ { "constant": true, "inputs": [], "name": "owner", "outputs": [ { "name": "", "type": "bytes16" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "vehicleName", "outputs": [ { "name": "", "type": "bytes" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "newOwner", "type": "bytes16" } ], "name": "setOwner", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor" } ]
bytecode="0x60606040526040805190810160405280600581526020017f5465736c610000000000000000000000000000000000000000000000000000008152506000908051906020019061004f929190610060565b50341561005b57600080fd5b610105565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a157805160ff19168380011785556100cf565b828001600101855582156100cf579182015b828111156100ce5782518255916020019190600101906100b3565b5b5090506100dc91906100e0565b5090565b61010291905b808211156100fe5760008160009055506001016100e6565b5090565b90565b61029a806101146000396000f300606060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680638da5cb5b1461005c578063d602f858146100ab578063d9a4dfa614610139575b600080fd5b341561006757600080fd5b61006f61016f565b60405180826fffffffffffffffffffffffffffffffff19166fffffffffffffffffffffffffffffffff1916815260200191505060405180910390f35b34156100b657600080fd5b6100be610192565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100fe5780820151818401526020810190506100e3565b50505050905090810190601f16801561012b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561014457600080fd5b61016d60048080356fffffffffffffffffffffffffffffffff1916906020019091905050610230565b005b600160009054906101000a90047001000000000000000000000000000000000281565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102285780601f106101fd57610100808354040283529160200191610228565b820191906000526020600020905b81548152906001019060200180831161020b57829003601f168201915b505050505081565b80600160006101000a8154816fffffffffffffffffffffffffffffffff021916908370010000000000000000000000000000000090040217905550505600a165627a7a723058202ba2be8d3625b2ec2dd1fb9c379e2a107063b2b356f6ab744ba5581aae3a03c40029"
// 创建合约
var contract = eth.contract(abi);
var initializer = {from: web3.eth.accounts[0], data: bytecode, gas: 300000};
// 在合约调用之前,需要对用户进行解锁
personal.unlockAccount(eth.accounts[0],"123456")
var token = contract.new(initializer)
// 合约创建完成之后,打印token.address为空
// 需要通过挖矿这一步骤,对合约地址进行确认
miner.start(4);
admin.sleepBlocks(2);
miner.stop();
swift代码
func loadHtml() {
if let path = Bundle.main.path(forResource: "web3", ofType: "html") {
let url = URL(fileURLWithPath: path)
let request = URLRequest(url: url)
webView?.load(request)
webView?.navigationDelegate = self
}
}
WKNavigationDelegate代理方法
判断web3是否连接
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webViewIsConnect()
}
func webViewIsConnect() {
self.webView?.evaluateJavaScript("web3.isConnected()",
completionHandler: {(res, error) in
if let connected = res, connected as! NSInteger == 1
{
self.isConnectedLabel.text = "Connected"
print("Connected")
}
else
{
self.isConnectedLabel.text = "Disabled"
print("Disabled")
}
})
}
}
调用本地html js代码
@IBAction func getOwner(_ sender: Any) {
webView!.evaluateJavaScript("getNameAndOwner()",
completionHandler: {(res, error) in
if let carData = res as? NSDictionary
{
self.owner.text = "The owner of the \"\(carData["name"]!)\" car is \"\(carData["owner"]!)\""
}
}
)
}
js代码
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("http://192.168.192.169:8200"));
console.log(web3.isConnected())
var abi =[ { "constant": true, "inputs": [], "name": "owner", "outputs": [ { "name": "", "type": "bytes16" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "vehicleName", "outputs": [ { "name": "", "type": "bytes" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "newOwner", "type": "bytes16" } ], "name": "setOwner", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor" } ];
var contractAddress = "0xff657546deda792a482c586ca16b76d6144926e1"
var Car = web3.eth.contract(abi).at(contractAddress);
function getNameAndOwner() {
// Get hex values from the contract and convert them into ascii string
console.log("------------");
console.log(Car)
let name = Car.vehicleName().toString();
let owner = Car.owner().toString();
console.log("name"+name);
console.log("owner"+owner);
return {
name: web3.toAscii(name),
owner: web3.toAscii(owner)
};
}
参考链接
How to run Web3.js in Swift 2 / iOS to work with Ethereum
以太坊私链与智能合约部署学习
网友评论