Created
February 23, 2022 13:41
-
-
Save Act0r1/cbf77808bc40db7d0f6f2ce109853e02 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SPDX-License-Identifier: Unlicense | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/utils/math/SafeMath.sol"; | |
import "@openzeppelin/contracts/utils/Address.sol"; | |
contract Donate { | |
using SafeMath for uint256; | |
address public owner; | |
mapping(address => uint256) public listDonations; | |
uint256 allBalance; | |
constructor() payable { | |
owner = msg.sender; | |
} | |
event Donated(address whoDonate, uint256 amount); | |
function isOwner() public view returns (bool) { | |
return msg.sender == owner; | |
} | |
function deposit() public payable {} | |
// Only owner modifier | |
modifier onlyOwner() { | |
require(isOwner()); | |
_; | |
} | |
fallback() external payable {} | |
receive() external payable {} | |
function depositAmount() public payable { | |
// require(msg.value == amount); | |
emit Donated(msg.sender, msg.value); | |
listDonations[msg.sender] += msg.value; | |
allBalance += msg.value; | |
} | |
function _msgSender() internal view virtual returns (address payable) { | |
return payable(msg.sender); | |
} | |
function getBalance() public view returns (uint256) { | |
return allBalance; | |
} | |
function withdraw(address payable _to, uint256 amount) | |
external | |
payable | |
onlyOwner | |
{ | |
require(_msgSender().balance >= amount, "Insuffucient balance"); | |
(bool succes, ) = _to.call{value: amount}(""); | |
// allBalance -= amount; | |
require(succes, ""); | |
} | |
function getListDonater(address donater) external view returns (uint256) { | |
return listDonations[donater]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { expect } = require("chai"); | |
const BigNumber = require("bignumber.js"); | |
var web3 = require("web3"); | |
describe("Token contract", function () { | |
beforeEach(async function () { | |
const Token = await ethers.getContractFactory("Donate"); | |
[owner, addr1, addr2] = await ethers.getSigners(); | |
hardhatToken = await Token.deploy(); | |
}); | |
describe("transfer and check donate address", function () { | |
it("transfer money", async function () { | |
expect(await hardhatToken.owner()).to.equal(owner.address); | |
value = web3.utils.toWei("1", "ether"); | |
// deposit | |
await hardhatToken.depositAmount({ value: value }); | |
balance = await hardhatToken.getBalance(); // get balance | |
console.log("added money to address"); | |
balanceOnContract = await hardhatToken.getListDonater(owner.address); | |
// amount = await hardhatToken.getDonateAmount(owner.address); | |
console.log("Balance on the contract = ", balanceOnContract.toString()); | |
console.log("Balance on the contract = ", balance.toString()); | |
console.log("Value on the address = ", value); | |
}); | |
}); | |
describe("Transactions", function () { | |
it("transfer from contract address to external address", async function () { | |
// let amount = BigNumber("2"); | |
// console.log("big amount = ", amount); | |
wei = web3.utils.fromWei("1", "ether"); | |
transfer = await hardhatToken.withdraw(addr1.address, 1); | |
console.log("transfered done, amount="); | |
console.log("addres balance=", addr1.address.balance); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment