Skip to content

Instantly share code, notes, and snippets.

@pau1m
Last active August 21, 2019 13:55
Show Gist options
  • Save pau1m/5996a7459b76d90507550bc58b83ae4b to your computer and use it in GitHub Desktop.
Save pau1m/5996a7459b76d90507550bc58b83ae4b to your computer and use it in GitHub Desktop.
AuthorDonationsTest.js
// Include web3 library so we can query accounts.
const Web3 = require('web3')
// Instantiate new web3 object pointing toward an Ethereum node.
let web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
// Include AuthorDonation contract
let AuthorDonation = artifacts.require("./AuthorDonation")
// Test with Mocha
contract('AuthorDonation', function(accounts) {
// Setup a variable to hold the contract object.
var authorDonation
// A convenience to view account balances in the console before making changes.
printBalances(accounts)
// Create a test case for retreiving the deployed contract.
// We pass 'done' to allow us to step through each test synchronously.
it("Should retrive deployed contract.", function(done) {
// Check if our instance has deployed
AuthorDonation.deployed().then(function(instance) {
// Assign our contract instance for later use
authorDonation = instance
console.log('author donation', authorDonation)
// Pass test if we have an object returned.
assert.isOk(authorDonation)
// Tell Mocha move on to the next sequential test.
done()
})
})
// Test for depositing 1 Ether
it("Should deposit 1 ether.", function(done) {
// Call the donate method on the contract. Since that method is tagged payable,
// we can send Ether by passing an object containing from, to and amount.
// All transactions are carried sent in wei. We use a web3 utility to convert from Ether.
authorDonation.donate({from:accounts[3], to:authorDonation.address, value: web3.toWei(1, "ether")})
.then(function(tx) {
// Pass the test if we have a transaction reciept returned.
assert.isOk(tx.receipt)
// For convenience, show the balances of accounts after transaction.
printBalances(accounts)
done()
}, function(error) {
// Force an error if callback fails.
assert.equal(true, false)
console.error(error)
done()
})
})
// Utility function to display the balances of each account.
function printBalances(accounts) {
accounts.forEach(function(ac, i) {
console.log(i, web3.fromWei(web3.eth.getBalance(ac), 'ether').toNumber())
})
}
})
@Bamskki
Copy link

Bamskki commented Aug 21, 2019

Bonjour Pau1m, I'm not a programmer but I could understand what this is about and correct me if I'm wrong; this contract lets you deposit a donation into it and then splits it to whatever percentages you specify into two or more addresses. If that was the case, I think it's super valuable for content creators on platforms like Youtube to collaborate with one another and split payments without relying on copyrights laws and middlemen. I like to educate people about Ethereum and this contract can be used as a demonstration of how dapps can revolutionize the way we exchange value with one another. Can you please help me run it on my device? What tools do I need? I'm a bit familiar with truffle and ganache. Which of these 'gists' should I download? Thanks in advance :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment