Skip to content

Instantly share code, notes, and snippets.

@ilake
Last active September 28, 2017 08:33
Show Gist options
  • Save ilake/a70f7996979098f60bf1ecd37c72c3fd to your computer and use it in GitHub Desktop.
Save ilake/a70f7996979098f60bf1ecd37c72c3fd to your computer and use it in GitHub Desktop.
var wallet = artifacts.require("./MyWallet.sol");
var receivedProposal = function (walletInstance, from, to) {
return new Promise((resolve, reject) => {
var event = walletInstance.proposalReceived({from: from, to: to}, {fromBlock: 0, toBlock: "latest"});
event.watch(function(error, response) {
event.stopWatching();
if (error) {
return reject(error);
}
else {
proposal_id = response.args._counter.toNumber();
return resolve(proposal_id);
}
});
})
}
contract("MyWallet", function(accounts) {
// Wallet has 10 ethers
it('should be possible to put money inside', async function() {
let walletInstance = await wallet.deployed();
await walletInstance.sendTransaction({
value: web3.toWei(10, 'ether'), address: walletInstance.address, from: accounts[0]
});
assert.equal(web3.eth.getBalance(walletInstance.address).toNumber(), web3.toWei(10, 'ether'), 'The Balance is the same');
});
// spendMoneyOn
it('should be possible to get money back as the owner', async function() {
const amountToSend = web3.toWei(5, 'ether');
let walletInstance = await wallet.deployed();
let balanceBeforeSend = await web3.eth.getBalance(walletInstance.address).toNumber();
await walletInstance.spendMoneyOn(accounts[0], amountToSend, "Because I'm the owner", {from: accounts[0]});
const balanceAfterSend = await web3.eth.getBalance(walletInstance.address).toNumber();
assert.equal(balanceBeforeSend - amountToSend, balanceAfterSend, 'Balance is now 5 ether less than before');
});
// spendMoneyOn + confirmProposal
it('should be possible to propose and confirm spending money', async function() {
let walletInstance = await wallet.deployed();
await walletInstance.spendMoneyOn(accounts[1], web3.toWei(5,'ether'), "Because I need money", {from: accounts[1]});
assert.equal(web3.eth.getBalance(walletInstance.address).toNumber(), web3.toWei(5, 'ether'), 'Balance is 5 ether less than before');
let proposal_id = await receivedProposal(walletInstance, accounts[0], accounts[1]);
await walletInstance.confirmProposal(proposal_id, {from: accounts[0]})
assert.equal(web3.eth.getBalance(walletInstance.address).toNumber(), web3.toWei(0, 'ether'), 'Balance is 5 ether less than before');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment