Contract-Development
與其他合約互動的最佳實踐
我想知道與其他契約互動的最佳方法和最佳實踐是什麼。
這裡是
MyFriendToken.sol
和MyService.sol
:
MyFriendToken.sol
我想知道與其他契約互動的最佳方法和最佳實踐是什麼。這裡是
MyFriendToken.sol
和MyService.sol
:
MyFriendToken.sol
pragma solidity ^0.4.0; contract ERC20 { //here are the same function in the TokenContractFragment //https://theethereum.wiki/w/index.php/ERC20_Token_Standard function allowance(address _owner, address _spender) constant returns (uint remaining); event Transfer(address indexed _from, address indexed _to, uint _value); event Approval(address indexed _owner, address indexed _spender, uint _value); function approve(address _spender, uint256 _amount) returns (bool success) { if ((_amount != 0) && (allowed[msg.sender][_spender] != 0)) { return false; } allowed[msg.sender][_spender] = _amount; Approval(msg.sender, _spender, _amount); return true; } function transfer(address _to, uint256 _amount) onlyPayloadSize(2 * 32) returns (bool success) { if (balances[msg.sender] >= _amount && _amount > 0 && balances[_to] + _amount > balances[_to]) { balances[msg.sender] -= _amount; balances[_to] += _amount; Transfer(msg.sender, _to, _amount); return true; } else { return false; } } function transferFrom(address _from, address _to, uint256 _amount) returns (bool success) { if ( balances[_from] >= _amount && allowed[_from][msg.sender] >= _amount //the transfer is approved && _amount > 0 && balances[_to] + _amount > balances[_to]) { balances[_from] -= _amount; allowed[_from][msg.sender] -= _amount; balances[_to] += _amount; Transfer(_from, _to, _amount); return true; } else { return false; } } uint public totalSupply = 0; mapping(address => uint256) balances; mapping(address => mapping (address => uint256)) allowed; function balanceOf(address _owner) constant returns (uint balance){ return balances[_owner]; } } contract MyFriendToken is ERC20{ string public token_name; string public symbol; uint8 public decimals; address public owner; bool purchase_OK = true; address friend1 = address(0x112233...); address friend2 = address(0x112222...); //..... function isMyFriend(address myFriendAddress) returns (bool){ if(myFriendAddress != friend1 && myFriendAddress != friend2) { return false; } return true; } function purchase_ko() { if(msg.sender != owner){ throw;} purchase_OK = false; } function purchase_ok() { if(msg.sender != owner){ throw;} purchase_OK = true; } function MyFriendToken(){ owner = msg.sender; token_name = "MyFriendToken"; symbol = "MFT"; decimals = 18; } function kill() { if (msg.sender == owner) { selfdestruct(owner); } } function() payable { if(!purchase_OK){throw;} if(!isMyFriend(msg.sender)) {throw;} owner.transfer(msg.value); uint256 MyFriendToken = (msg.value * 1000); totalSupply += MyFriendToken; balances[msg.sender] += MyFriendToken; } }
MyService.sol
pragma solidity ^0.4.0; //interface to MyFriendToken contract MyFriendToken{ function isMyFriend(address myFriendAddress) returns (bool); function transferFrom(address _from, address _to, uint _value) returns (bool success); function approve(address _spender, uint _value) returns (bool success); function balanceOf(address _owner) constant returns (uint balance); } contract MyService { address public owner; function MyService(){ owner = msg.sender; } event DoStuff(address myFriendAddress, uint myFriendBalance); function doStuff(address myFriendTokenContractAddress){ address myFriendAddress = msg.sender; MyFriendToken mft = MyFriendToken(myFriendTokenContractAddress); if(mft.isMyFriend(myFriendAddress)){ //check my friend's balance uint myFriendBalance = mft.balanceOf(myFriendAddress); //the price of the service is 10 MFT uint servicePrice = 10; if(myFriendBalance >= servicePrice){ //decrese myFriend Balance //do stuff DoStuff(myFriendAddress , myFriendBalance); } } } }
以下是我的疑問:
- 這是一個好方法嗎?
doStuff()
好友使用我的服務(功能)後如何減少餘額?
MyService.sol
pragma solidity ^0.4.0; //interface to MyFriendToken contract MyFriendToken{ function isMyFriend(address myFriendAddress) returns (bool); function transferFrom(address _from, address _to, uint _value) returns (bool success); function approve(address _spender, uint _value) returns (bool success); function balanceOf(address _owner) constant returns (uint balance); function transfer(address _to, uint _value) returns (bool success); } contract MyService { address public owner; function MyService(){ owner = msg.sender; } event DoStuff(address myFriendAddress, uint myFriendBalance); function doStuff(address myFriendTokenContractAddress){ address myFriendAddress = msg.sender; MyFriendToken mft = MyFriendToken(myFriendTokenContractAddress); if(mft.isMyFriend(myFriendAddress)){ //check my friend's balance uint myFriendBalance = mft.balanceOf(myFriendAddress); //the price of the service is 10 MFT uint servicePrice = 10; if(myFriendBalance >= servicePrice){ //decrese myFriend Balance transfer(owner, servicePrice); //do stuff DoStuff(myFriendAddress , myFriendBalance); } } } }
以下是我的疑問:
- 這是一個好方法嗎?
doStuff()
好友使用我的服務(功能)後如何減少餘額?
簡而言之 :
1-是的,您使用其地址呼叫另一個合約。
2-您可以在您的第一個契約中創建一個函式,例如:
function decrease(uint256 _value, address user){ balances[user] -= _value; }
然後在你的第二份契約中呼叫它:
if(myFriendBalance >= servicePrice){ mft.decrease(_value, myFriendAddress); }