/ethereum-solidity-charity-donation

A simple smart contract that sends funds (donations) to some charities as part of a transaction between users

Primary LanguageSolidityApache License 2.0Apache-2.0

ethereum-solidity-charity-donation

A simple smart contract that facilitates donations to different charities. When a user wants to send some funds to a destination address, instead of sending them directly to that address, they will use the smart contract. A part of the funds will be sent to the charity the user specified, while the rest will go to the destination address.

Dependencies

In order to reproduce this project you will need to clone it in your machine and install Node JS and some of its modules.

Clone project

git clone https://github.com/ZisisFl/ethereum-solidity-charity-donation

Install Node JS

install node js
sudo apt install nodejs
sudo apt install npm
nodejs -v

Install needed node modules

cd ethereum-solidity-charity-donation

Install solcjs solidity compiler

npm install solc

Install development tools

npm install ganache-cli
npm install truffle

For Linux machines it is recommended to add node_modules/.bin to PATH

export PATH=$PATH:`pwd`/node_modules/.bin

Reproduce project

On a first terminal start ganache

ganache-cli

On a second one initialize truffle

truffle init

Then edit truffle-config.js and navigate to networks and uncomment development

Connect to ganache network (node repl with web3)

truffle console

Open a third terminal
Generate binary code of smart contract

solc --bin CharityDonation.sol

Get contract ABI

solc --abi CharityDonation.sol

Get an estimation of the Gas needed

solc --gas CharityDonation.sol

Deployment

In truffle console (second terminal): Set a variable contractBin with binary code in the third terminal

contractBin = '{bin_from_third_terminal}'

Do the same for the ABI

contractAbi = [{abi_from_third_terminal}]

Define DonationContract variable using ABI

DonationContract = new web3.eth.Contract(contractAbi);

Set contract owner address

web3.eth.getAccounts().then(a => contractOwnerAddress = a[0])

Set some sender addresses

web3.eth.getAccounts().then(a => senderAddresses = a.slice(1,3))

Set some charity addresses

web3.eth.getAccounts().then(a => charityAddresses = a.slice(3,6))

Set some destination addresses

web3.eth.getAccounts().then(a => destinationAddresses = a.slice(6,10))

Store current gas price to a variable

web3.eth.getGasPrice().then(a => currentGasPrice = a)

Finally deploy contract

DonationContract.deploy({data: contractBin, arguments: [charityAddresses]}).send({from: contractOwnerAddress, gas: 800000, gasPrice:currentGasPrice}).then( (instance) => { console.log(instance.options.address) });

Store contract's address in a contractAddress variable

contractAddress = '{contract_address}'

Test contract methods

Init deployed contract

deployedDonationContract = new web3.eth.Contract(contractAbi, contractAddress)

Set an amount to send

amountToSend = web3.utils.toWei("4", "ether")

Use sendFunds variant a (public)

deployedDonationContract.methods.sendFunds(destinationAddresses[0], 1).send({from: contractOwnerAddress, gasPrice:currentGasPrice, gas: 110000, value:amountToSend})

Use sendFunds variant b (public)

deployedDonationContract.methods.sendFunds(destinationAddresses[0], (amountToSend/6).toString(), 1).send({from: contractOwnerAddress, gasPrice:currentGasPrice, gas: 110000, value:amountToSend})

Get overall donations info (public)

deployedDonationContract.methods.getOverallDonationInfo().call()

Get top donation made (owner only)

deployedDonationContract.methods.getTopDonation().call()

Destroy (owner only)

deployedDonationContract.methods.destroyContract().send({from: contractOwnerAddress, gasPrice:currentGasPrice, gas: 30000})