Skip to content

Instantly share code, notes, and snippets.

@klawingco
Created June 25, 2018 04:49
Show Gist options
  • Select an option

  • Save klawingco/23fe273d8018a07f2af472f23334a9cb to your computer and use it in GitHub Desktop.

Select an option

Save klawingco/23fe273d8018a07f2af472f23334a9cb to your computer and use it in GitHub Desktop.
Simple Wallet Contract Created through Factory Contract
pragma solidity ^0.4.23;
contract Wallet{
address public contractFactoryAddress;
address public walletOwnerAddress;
constructor(address _walletOwnerAddress) public{
contractFactoryAddress = msg.sender;
walletOwnerAddress = _walletOwnerAddress;
}
enum Action{ Deposit, Transfer, Withdraw }
struct Transaction{
Action _action;
address _from;
address _to;
uint _value;
uint _date_trans;
address coinbase; //miner
uint blockNumber;
uint gaxused;
}
mapping(uint=> Transaction) public walletTransaction;
Transaction[] public transactions;
modifier onlyWalletOwner(address _sender){
require(msg.sender == walletOwnerAddress);
_;
}
//Deposits Ether fom account to Wallet
//Allows everyone to deposit to this Wallet
function depositToWallet()
external payable returns(bool _isSuccess){
LogTransaction(Action.Deposit, msg.sender ,this, msg.value);
return true;
}
//Get's the balance of this wallet
//Only the wallet owner should be able to use this functions
function getWalletBalance() onlyWalletOwner(msg.sender)
external view returns (uint balance){
return address(this).balance;
}
//Withdraw ether to the wallet owners address
function withdrawToAccount(uint _amount) onlyWalletOwner(msg.sender)
external returns (bool _isSuccess){
require(address(this).balance > _amount);
bool isSuccess = walletOwnerAddress.send(_amount);
if(isSuccess){
LogTransaction(Action.Withdraw,this, msg.sender, _amount);
}
return isSuccess;
}
//Send Ether to other Wallet Contracts or even Accounts
function sendToOther(address _receiver, uint _amount) onlyWalletOwner(msg.sender)
external returns (bool _isSuccess)
{
require(address(this).balance > _amount);
bool isSuccess = _receiver.send(_amount);
if(isSuccess){
LogTransaction(Action.Transfer, msg.sender, _receiver, _amount);
}
return isSuccess;
}
//Logs every transaction of this wallet
function LogTransaction(Action _action, address _from ,address _to, uint value) internal{
Transaction memory newTransaction = Transaction(_action, _from, _to,
value, block.timestamp, block.coinbase, block.number,
tx.gasprice);
uint id = transactions.push(newTransaction) - 1;
walletTransaction[id] = newTransaction;
}
}
//Wallet Factory
//Parent Contract which facilitates the creation of Wallet
import "./Wallet.sol";
contract WalletFactory{
address internal factoryOwner;
address[] public wallets;
constructor() public{
factoryOwner = msg.sender;
}
struct WalletInstance{
address walletAddress;
address owner;
}
WalletInstance[] public walletsCreated;
event NewWalletCreated(address _walletAddress);
//Deploys a new wallet contract
//Returns the address of the wallet
function CreateNewWallet() external returns(address createdWalletAddr){
Wallet _newWallet = new Wallet(msg.sender);
wallets.push(_newWallet);
walletsCreated.push(WalletInstance(_newWallet, msg.sender));
emit NewWalletCreated(_newWallet);
return _newWallet;
}
function GetUserWalletCount() public view returns (uint walletCount){
uint _walletIndex = 0;
for(uint i = 0;i < walletsCreated.length;i++){
if(walletsCreated[i].owner == msg.sender){
_walletIndex++;
}
}
return _walletIndex;
}
function GetUserWalletAddress() external view
returns(address[] walletAddress){
address[] memory _tmp_wallets = new address[](GetUserWalletCount());
uint _walletIndex = 0;
for(uint i = 0;i < walletsCreated.length;i++){
if(walletsCreated[i].owner == msg.sender){
_tmp_wallets[_walletIndex] = walletsCreated[i].walletAddress;
_walletIndex++;
}
}
return _tmp_wallets;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment