Skip to content

Instantly share code, notes, and snippets.

@MatthewStanciu
Last active July 31, 2018 13:24
Show Gist options
  • Save MatthewStanciu/e297679e1798f01dc760da2a0f69af6a to your computer and use it in GitHub Desktop.
Save MatthewStanciu/e297679e1798f01dc760da2a0f69af6a to your computer and use it in GitHub Desktop.
Waiting until a transaction is confirmed to send an email
pragma solidity ^0.4.23;
contract TheWritersBlock {
address public owner;
mapping (address => uint256) public balance;
constructor() public {
owner = msg.sender;
}
function kill() public {
if(msg.sender == owner) selfdestruct(owner);
}
function payWriter(uint amount, address receiver) public returns (bool success) {
if (balance[msg.sender] < amount) throw;
balance[msg.sender] -= amount;
receiver.transfer(amount);
return true;
}
}
// This contract is already deployed at 0x8ac6f809862eae42fefafcca212efb61607e13e0
var web3 = window.web3;
function payUser(amt, addr) {
var contractAbi = [[{"constant": false,"inputs": [{"name": "amount","type": "uint256"},{"name": "receiver","type": "address"}],"name": "payWriter","outputs": [{"name": "success","type": "bool"}],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": false,"inputs": [],"name": "kill","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": true,"inputs": [],"name": "owner","outputs": [{"name": "","type": "address"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": true,"inputs": [{"name": "","type": "address"}],"name": "balance","outputs": [{"name": "","type": "uint256"}],"payable": false,"stateMutability": "view","type": "function"},{"inputs": [],"payable": false,"stateMutability": "nonpayable","type": "constructor"}]];
var contract = web3.eth.contract(contractAbi).at('0x8ac6f809862eae42fefafcca212efb61607e13e0');
contract.payWriter(amt, addr, {from: web3.eth.accounts[0]}, funcition(err, txid) {
if (err) { console.log(err); }
else { monitorTransaction(txid); }
});
}
function getTx(txid) {
return web3.eth.getTransactionReceipt(txid);
}
function monitorTransaction(txid) {
var timesCounted = 0;
var confirmed = false;
var receipt = getTx(txid);
while (true) {
setTimeout(getTx(txid), 1000)
timesCounted++;
if (timesCounted == 300) break;
if (receipt['status'] == "0x1" { //Ox0 = failed, 0x1 = confirmed, I assume this will be undefined until the transaction is confirmed
confirmed = true;
break;
}
if (receipt['status'] == "0x0" break;
}
if (confirmed) {
// send email
}
}

I used an already-deployed smart contract for this, but if you would like to be able to write and deploy your own contracts, it's better to just look at the deploying portion of this very in-depth article that I found very useful for learning.

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