I hope I mad the tags correctly!
Mikolaj instructed me to come here and post my first ethereum project here. He has sent me a list and I have chosen the first to be the following: Make a contract that sends funds and checks the ledger for the transaction.
I have done as many things I think it should be in the contract so far but I think it is not ready ( I am not entirely sure that I understand everything regarding how the ledger things work as a smart contract).
So my question is, where can I improve this and do I understand everything correctly regarding the Ledger check?
I have made an addBalance function and also add a transfer inside the CreateTransaction function. Numbers are moving okay as far as I have checked and also my way of thinking was regarding to checking the ledger is to add a struct with the two addresses and I have chosen an amount which has been sent and one which was remaining after the transaction.
The getTransaction function is getting those 4 array elements out by the transaction Id which I chose to be the array length.
What I do not know is that how the ledger checks works anonimously . So does this mean I have to convert the addresses to some kind of number (I cannot really imagine a string working but I am open to everything).
Also any upgradement can be made I will do as I want to get out the most of this project before I move on. I have came to the Academy in January with 0 knowledge so I believe I am in still need of much assistance but I think if I can get 1 or 2 tips I can expan this project.
Here is the github link for the coding: https://github.com/Riki0923/LedgerFunds/blob/main/First%20Project%20-%20Ethereum%20Programming%20.md
Also pasting it here just in case:
//SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.4; pragma abicoder v2;
contract LedgerFunds {
address[] private participants;
constructor(address[] memory _participants){
participants = _participants;
}
modifier onlyOwner (){
bool participant = false;
for(uint i =0; i<participants.length; i++){
if (participants[i] == msg.sender){
participant = true;
}
}
require(participant = true);
_;
}
struct Transactions {
uint256 id;
address payable _receiver;
address _sender;
uint256 _amountSent;
uint256 _balanceRemained;
}
Transactions[] transactionNumber;
mapping(address => uint256) balances;
event balanceAdded(uint256 addedAmount);
function addBalance(uint256 _toAdd) onlyOwner public payable {
balances[msg.sender] += _toAdd;
emit balanceAdded(_toAdd);
}
function createTransaction(address payable _receiver, uint _amount) public payable onlyOwner {
require(balances[msg.sender] >= _amount, "insufficient balance");
uint256 _balanceRemained = balances[msg.sender] -=_amount;
balances[_receiver] += _amount;
Transactions memory newTransaction = Transactions(transactionNumber.length, _receiver, msg.sender, _amount, _balanceRemained);
transactionNumber.push(newTransaction);
}
function getTransaction(uint256 _id) external view returns(address, address, uint256, uint256 ){
return (transactionNumber[_id]._receiver, transactionNumber[_id]._sender, transactionNumber[_id]._amountSent, transactionNumber[_id]._balanceRemained);
}
function getBalance() public view returns(uint256){
return balances[msg.sender];
}
Thanks Everyone.