//tips
//smart contract
Solidityバージョン変更に伴う変更点を追っていたが0.4→0.5だけでなく0.5→0.6でもかなりあるようで承継部分のvirtual記載が追加されたのもこの部分のよう。現在0.8だが使用バージョンは確認しないとエラーが起こるので気を付ける。
Walletの機能を簡単に作成する中で、そもそものお送り主のアカウント表示がなかったのでそちらも作成。
//対象アドレス送金元の残高
function getPlayerBalance(address player)public view returns(uint) {
return player.balance;
}
これで表現できた。remixなので引数の取り方が特殊だが、流れは、
//コントラクトの財布の残高
function getBalance()public view returns(uint){
return address (this).balance;
}
のものと同じ。
ただ、由来をきちんと分けて考えることが必要。
また、externalでの資金移動は下記だけで資金移動を成立させられるので覚えておく。
//Aからアカウントに1ethを移動
function ()external payable{
}
またOwnwbale箇所を
import "https://github.com/OpenZeppelin/openzeppelin-contracts/tree/solc-0.6/contracts/access";
などしてインポートしようとしたがsolidityのバージョン問題で対応できないとのこと。
フォーラムにて議論されていて、0.5系は下記でインポートできるとのこと。
https://github.com/OpenZeppelin/openzeppelin-contracts/issues/2032
インポートできない場合はgithubの適切な旧バージョンサイトからインポートしなければならないよう。
pragma solidity ^0.5.13;
//remixの場合url貼り付け可能
//openzeppelin-contracts/contracts/access/Ownable.sol
//こうするとremixのフォルダ欄にも追加される
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/ownership/Ownable.sol";
//import "@openzeppelin/contracts/access/Ownable.sol";
contract SimpleWallet is Ownable{
/*
//managerの作成
address public owner;
constructor()public{
owner=msg.sender;
}
modifier onlyOwner(){
//managerのみ操作可能
require(msg.sender==owner,"you are not allowed");
_;
}
*/
//スマコンアカウントの1ethをAからBに送金
//一回スマートコントラクトの財布に移しているという考え方を忘れない
function withdrawMoney(address payable _to,uint _amount)public onlyOwner{
_to.transfer(_amount);
}
//Aからアカウントに1ethを移動
function ()external payable{
}
//コントラクトの財布の残高
function getBalance()public view returns(uint){
return address (this).balance;
}
//対象アドレス送金元の残高
function getPlayerBalance(address player)public view returns(uint) {
return player.balance;
}
}
ここにmappingを追加。また少し構造も整理した。
pragma solidity ^0.5.13;
//remixの場合url貼り付け可能
//openzeppelin-contracts/contracts/access/Ownable.sol
//こうするとremixのフォルダ欄にも追加される
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/ownership/Ownable.sol";
contract Allowance is Ownable{
mapping(address=>uint)public allowance;
//可能送金金額の設定
function addAllowance(address _who,uint _amount)public onlyOwner{
allowance[_who]=_amount;
}
modifier ownerOrAllowed(uint _amount){
require(isOwner()||allowance[msg.sender]>= _amount,"You are not allowed");
_;
}
//送金後にはallowanceを減少させる
function reduceAllowance(address _who,uint _amount)internal{
allowance[_who]-=_amount;
}
}
contract SimpleWallet is Allowance{
function withdrawMoney(address payable _to,uint _amount)public ownerOrAllowed(_amount){
require(_amount<=address(this).balance,"there are not enough funds");
//送金後にはマイナスすること
if(!isOwner()){
reduceAllowance(msg.sender,_amount);
}
_to.transfer(_amount);
}
//Aからアカウントに1ethを移動
function ()external payable{
}
//コントラクトの財布の残高
function getBalance()public view returns(uint){
return address (this).balance;
}
//対象アドレス送金元の残高
function getPlayerBalance(address player)public view returns(uint) {
return player.balance;
}
}