Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 99cd5a7

Browse files
committed
donate
1 parent 88be63e commit 99cd5a7

File tree

6 files changed

+194
-0
lines changed

6 files changed

+194
-0
lines changed

contracts/donatesol.sol

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.4;
3+
4+
5+
contract Contract {
6+
address public owner;
7+
address public charity;
8+
9+
constructor(address _charity) {
10+
owner = msg.sender;
11+
charity = _charity;
12+
}
13+
14+
receive() external payable {
15+
//console.log(msg.value); // 100000
16+
}
17+
18+
function tip() public payable {
19+
//console.log(msg.value); // 100000
20+
owner.call{value: msg.value}("");
21+
}
22+
23+
function donate() public {
24+
payable(charity).transfer(address(this).balance);
25+
26+
}
27+
}

contracts/donatetest.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const { assert } = require('chai');
2+
describe('Contract', function () {
3+
const charity = ethers.Wallet.createRandom().address;
4+
const donation = ethers.utils.parseEther("1");
5+
let contract;
6+
let owner;
7+
let tipper;
8+
before(async () => {
9+
const Contract = await ethers.getContractFactory("Contract");
10+
contract = await Contract.deploy(charity);
11+
await contract.deployed();
12+
13+
owner = ethers.provider.getSigner(0);
14+
await owner.sendTransaction({ to: contract.address, value: donation });
15+
tipper = ethers.provider.getSigner(1);
16+
});
17+
18+
it('should store the owner', async () => {
19+
const _owner = await contract.owner.call();
20+
assert.equal(_owner, await owner.getAddress());
21+
});
22+
23+
it('should receive the initial donation', async () => {
24+
const balance = await ethers.provider.getBalance(contract.address);
25+
assert(balance.eq(donation), "expected the ether to be received");
26+
});
27+
28+
describe('after two .25 ether tips', () => {
29+
const tip = ethers.utils.parseEther("0.25");
30+
let balanceBefore;
31+
before(async () => {
32+
balanceBefore = await ethers.provider.getBalance(await owner.getAddress());
33+
await contract.connect(tipper).tip({ value: tip });
34+
await contract.connect(tipper).tip({ value: tip });
35+
});
36+
37+
it('should add .5 ether to the owners balance', async () => {
38+
const balanceAfter = await ethers.provider.getBalance(await owner.getAddress());
39+
assert.equal(balanceAfter.sub(balanceBefore).toString(), tip.mul(2).toString());
40+
});
41+
});
42+
43+
describe('after donating', () => {
44+
before(async () => {
45+
await contract.connect(tipper).donate();
46+
});
47+
48+
it('should add the donations to the charity balance', async () => {
49+
const _donation = await ethers.provider.getBalance(charity);
50+
assert.equal(_donation.toString(), donation.toString());
51+
});
52+
});
53+
});

contracts/payable.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.4;
3+
4+
5+
contract Contract {
6+
address public owner;
7+
8+
constructor(){
9+
owner = msg.sender;
10+
}
11+
12+
receive() external payable {
13+
//console.log(msg.value); // 100000
14+
}
15+
16+
}

contracts/payabletest.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { assert } = require('chai');
2+
describe('Contract', function () {
3+
let contract;
4+
let value = ethers.utils.parseEther("1");
5+
let owner;
6+
before(async () => {
7+
const Contract = await ethers.getContractFactory("Contract");
8+
contract = await Contract.deploy();
9+
await contract.deployed();
10+
11+
owner = ethers.provider.getSigner(0);
12+
await owner.sendTransaction({ to: contract.address, value });
13+
});
14+
15+
it('should store the owner', async () => {
16+
const _owner = await contract.owner.call();
17+
assert.equal(_owner, await owner.getAddress());
18+
});
19+
20+
it('should receive the ether', async () => {
21+
const balance = await ethers.provider.getBalance(contract.address);
22+
assert(balance.eq(value), "expected the ether to be received");
23+
});
24+
});

contracts/transfercall.sol

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.4;
3+
4+
5+
contract Contract {
6+
address public owner;
7+
8+
constructor(){
9+
owner = msg.sender;
10+
}
11+
12+
receive() external payable {
13+
//console.log(msg.value); // 100000
14+
}
15+
16+
function tip() public payable {
17+
//console.log(msg.value); // 100000
18+
owner.call{value: msg.value}("");
19+
}
20+
}

contracts/transfercalltest.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const { assert } = require('chai');
2+
describe('Contract', function () {
3+
let contract;
4+
let value = ethers.utils.parseEther("1");
5+
let owner;
6+
let tipper;
7+
before(async () => {
8+
const Contract = await ethers.getContractFactory("Contract");
9+
contract = await Contract.deploy();
10+
await contract.deployed();
11+
12+
owner = ethers.provider.getSigner(0);
13+
await owner.sendTransaction({ to: contract.address, value });
14+
tipper = ethers.provider.getSigner(1);
15+
});
16+
17+
it('should store the owner', async () => {
18+
const _owner = await contract.owner.call();
19+
assert.equal(_owner, await owner.getAddress());
20+
});
21+
22+
it('should receive the ether', async () => {
23+
const balance = await ethers.provider.getBalance(contract.address);
24+
assert(balance.eq(value), "expected the ether to be received");
25+
});
26+
27+
describe('after a .25 ether tip', () => {
28+
const tip = ethers.utils.parseEther("0.25");
29+
let balanceBefore;
30+
before(async () => {
31+
balanceBefore = await ethers.provider.getBalance(await owner.getAddress());
32+
await contract.connect(tipper).tip({ value: tip });
33+
});
34+
35+
it('should send the tip to the owner', async () => {
36+
const balanceAfter = await ethers.provider.getBalance(await owner.getAddress());
37+
assert.equal(balanceAfter.sub(balanceBefore).toString(), tip);
38+
});
39+
});
40+
41+
describe('after a .5 ether tip', () => {
42+
const tip = ethers.utils.parseEther("0.5");
43+
let balanceBefore;
44+
before(async () => {
45+
balanceBefore = await ethers.provider.getBalance(await owner.getAddress());
46+
await contract.connect(tipper).tip({ value: tip });
47+
});
48+
49+
it('should send the tip to the owner', async () => {
50+
const balanceAfter = await ethers.provider.getBalance(await owner.getAddress());
51+
assert.equal(balanceAfter.sub(balanceBefore).toString(), tip);
52+
});
53+
});
54+
});

0 commit comments

Comments
 (0)