- 地址型別 address 是一個值型別, 20 位元組(一個以太坊地址的長度),地址型別也有成員,地址是所有合約的基礎,
- 所有合約都繼承了 address 的成員
- 地址型別的成員
- balance屬性和transfer( )
- send( )
tips:
send() 執行有一些風險:
如果呼叫堆疊的深度超過 1024 或 gas 耗光,交易都會失敗,
因此,為保證安全,須檢查 send 的回傳值,如果交易失敗,會回退以太幣,
如果用 transfer 會更好,
- call(), callcode() 和 delegatecall() 函式
tips:上述的函式都是底層的函式,使用時要例外小心,
當呼叫一個未知的,可能是惡意的合約時.
當把控制權交給它,它可能回呼回你的合約,要準備好在呼叫回傳時,應對你的狀態變數可能被惡意篡改的情況,
- 合約事例代碼
pragma solidity ^0.4.0;
contract AddrTest{
event logdata(bytes data);
function() payable {
logdata(msg.data);
}
function getBalance() returns (uint) {
return this.balance;
}
uint score = 0;
function setScore(uint s) public {
score = s;
}
function getScore() returns ( uint){
return score;
}
}
contract CallTest{
function deposit() payable {
}
event logSendEvent(address to, uint value);
function transferEther(address towho) payable {
towho.transfer(10);
logSendEvent(towho, 10);
}
function callNoFunc(address addr) returns (bool){
return addr.call("tinyxiong", 1234);
}
function callfunc(address addr) returns (bool){
bytes4 methodId = bytes4(keccak256("setScore(uint256)"));
return addr.call(methodId, 100);
}
function getBalance() returns (uint) {
return this.balance;
}
}
參考資料
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/262098.html
標籤:區塊鏈
上一篇:使用 Harbor 提供可信鏡像
