Skip to content

Instantly share code, notes, and snippets.

@johnzweng
Last active June 13, 2017 07:35
Show Gist options
  • Save johnzweng/a07a235ce3fe2f73f4f203a90feb8ace to your computer and use it in GitHub Desktop.
Save johnzweng/a07a235ce3fe2f73f4f203a90feb8ace to your computer and use it in GitHub Desktop.
/**
* Johannes Zweng <john@zweng.at>, 2017
*
* This simple script just loads the 2 bancor contracts (Crowdsale and BNT Token)
* into the geth client and then provides a single status function which you
* can call on the geth console to query the current status of the crowdsale.
*
* Usage:
* ------
* 1) Start geth instance
* > geth
*
* 2) Download this script
*
* 3) In a second window attach to the running geth instance and load this script:
* > geth --preload /path/where/you/downloaded/this/script/web3-monitor-bancor-crowdsale.js attach
*
* 4) Now in the geth console you can call the bancorStatus() function as often
* as you want to see the current status of the Bancor crowdsale live from the
* ethereum blockchain:
*
* > bancorStatus()
*
*
* This will output the following:
*
* -------------------------------------------------------------------------------
* CURRENT STATE OF BANCOR CROWDSALE:
* -------------------------------------------------------------------------------
* Time until public sale starts: 21025 seconds (5.84 hours)
* Total ETH collected until now: 0.01 ETH
* Is hidden cap already revealed: false
* Current active cap is: 1000000 ETH
* Remaining ETH until (current) cap is reached: 999999.99 ETH
* Total number of BNT created until now: 2 BNT
* -------------------------------------------------------------------------------
* undefined
*
*
*/
// Note:
// the first long part of the script is just the contract interface (also called ABI)
// which is needed to instantiate the contracts and is just copied from the etherscan.io
// page (you can verify it there).
// The interesting part starts at the end around line 511.
// Bancor ABI JSON
// (copied from https://etherscan.io/address/0xBbc79794599b19274850492394004087cBf89710#code):
var bancorCrowdsaleABI = [{
"constant": true,
"inputs": [],
"name": "BTCS_ETHER_CAP",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "TOKEN_PRICE_D",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [],
"name": "contributeETH",
"outputs": [{"name": "amount", "type": "uint256"}],
"payable": true,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "DURATION",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [{"name": "_contribution", "type": "uint256"}],
"name": "computeReturn",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_newOwner", "type": "address"}],
"name": "transferTokenOwnership",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "endTime",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "totalEtherCap",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [],
"name": "acceptTokenOwnership",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "beneficiary",
"outputs": [{"name": "", "type": "address"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_token", "type": "address"}, {"name": "_to", "type": "address"}, {
"name": "_amount",
"type": "uint256"
}],
"name": "withdrawFromToken",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "TOKEN_PRICE_N",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_to", "type": "address"}, {"name": "_amount", "type": "uint256"}],
"name": "issueTokens",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "version",
"outputs": [{"name": "", "type": "string"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_token", "type": "address"}, {"name": "_to", "type": "address"}, {
"name": "_amount",
"type": "uint256"
}],
"name": "withdrawTokens",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "startTime",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [],
"name": "acceptOwnership",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_disable", "type": "bool"}],
"name": "disableTokenTransfers",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [{"name": "", "type": "address"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "realEtherCapHash",
"outputs": [{"name": "", "type": "bytes32"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_cap", "type": "uint256"}, {"name": "_key", "type": "uint256"}],
"name": "enableRealCap",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [],
"name": "contributeBTCs",
"outputs": [{"name": "amount", "type": "uint256"}],
"payable": true,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "totalEtherContributed",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "btcs",
"outputs": [{"name": "", "type": "address"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_from", "type": "address"}, {"name": "_amount", "type": "uint256"}],
"name": "destroyTokens",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "newOwner",
"outputs": [{"name": "", "type": "address"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [{"name": "_cap", "type": "uint256"}, {"name": "_key", "type": "uint256"}],
"name": "computeRealCap",
"outputs": [{"name": "", "type": "bytes32"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "MAX_GAS_PRICE",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_newOwner", "type": "address"}],
"name": "transferOwnership",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "token",
"outputs": [{"name": "", "type": "address"}],
"payable": false,
"type": "function"
}, {
"inputs": [{"name": "_token", "type": "address"}, {
"name": "_startTime",
"type": "uint256"
}, {"name": "_beneficiary", "type": "address"}, {"name": "_btcs", "type": "address"}, {
"name": "_realEtherCapHash",
"type": "bytes32"
}], "payable": false, "type": "constructor"
}, {"payable": true, "type": "fallback"}, {
"anonymous": false,
"inputs": [{"indexed": true, "name": "_contributor", "type": "address"}, {
"indexed": false,
"name": "_amount",
"type": "uint256"
}, {"indexed": false, "name": "_return", "type": "uint256"}],
"name": "Contribution",
"type": "event"
}, {
"anonymous": false,
"inputs": [{"indexed": false, "name": "_prevOwner", "type": "address"}, {
"indexed": false,
"name": "_newOwner",
"type": "address"
}],
"name": "OwnerUpdate",
"type": "event"
}];
// ABI of Bancor Token:
// (copied from https://etherscan.io/address/0x1F573D6Fb3F13d689FF844B4cE37794d79a7FF1C#code)
var bancorTokenABI = [{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [{"name": "", "type": "string"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_spender", "type": "address"}, {"name": "_value", "type": "uint256"}],
"name": "approve",
"outputs": [{"name": "success", "type": "bool"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_disable", "type": "bool"}],
"name": "disableTransfers",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_from", "type": "address"}, {"name": "_to", "type": "address"}, {
"name": "_value",
"type": "uint256"
}],
"name": "transferFrom",
"outputs": [{"name": "success", "type": "bool"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [{"name": "", "type": "uint8"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "version",
"outputs": [{"name": "", "type": "string"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "standard",
"outputs": [{"name": "", "type": "string"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_token", "type": "address"}, {"name": "_to", "type": "address"}, {
"name": "_amount",
"type": "uint256"
}],
"name": "withdrawTokens",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [{"name": "", "type": "address"}],
"name": "balanceOf",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [],
"name": "acceptOwnership",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_to", "type": "address"}, {"name": "_amount", "type": "uint256"}],
"name": "issue",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [{"name": "", "type": "address"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [{"name": "", "type": "string"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_from", "type": "address"}, {"name": "_amount", "type": "uint256"}],
"name": "destroy",
"outputs": [],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_to", "type": "address"}, {"name": "_value", "type": "uint256"}],
"name": "transfer",
"outputs": [{"name": "success", "type": "bool"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "transfersEnabled",
"outputs": [{"name": "", "type": "bool"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "newOwner",
"outputs": [{"name": "", "type": "address"}],
"payable": false,
"type": "function"
}, {
"constant": true,
"inputs": [{"name": "", "type": "address"}, {"name": "", "type": "address"}],
"name": "allowance",
"outputs": [{"name": "", "type": "uint256"}],
"payable": false,
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_newOwner", "type": "address"}],
"name": "transferOwnership",
"outputs": [],
"payable": false,
"type": "function"
}, {
"inputs": [{"name": "_name", "type": "string"}, {"name": "_symbol", "type": "string"}, {
"name": "_decimals",
"type": "uint8"
}], "payable": false, "type": "constructor"
}, {
"anonymous": false,
"inputs": [{"indexed": false, "name": "_token", "type": "address"}],
"name": "NewSmartToken",
"type": "event"
}, {
"anonymous": false,
"inputs": [{"indexed": false, "name": "_amount", "type": "uint256"}],
"name": "Issuance",
"type": "event"
}, {
"anonymous": false,
"inputs": [{"indexed": false, "name": "_amount", "type": "uint256"}],
"name": "Destruction",
"type": "event"
}, {
"anonymous": false,
"inputs": [{"indexed": false, "name": "_prevOwner", "type": "address"}, {
"indexed": false,
"name": "_newOwner",
"type": "address"
}],
"name": "OwnerUpdate",
"type": "event"
}, {
"anonymous": false,
"inputs": [{"indexed": true, "name": "_from", "type": "address"}, {
"indexed": true,
"name": "_to",
"type": "address"
}, {"indexed": false, "name": "_value", "type": "uint256"}],
"name": "Transfer",
"type": "event"
}, {
"anonymous": false,
"inputs": [{"indexed": true, "name": "_owner", "type": "address"}, {
"indexed": true,
"name": "_spender",
"type": "address"
}, {"indexed": false, "name": "_value", "type": "uint256"}],
"name": "Approval",
"type": "event"
}];
var bancorCrowdsaleContractAddress = '0xBbc79794599b19274850492394004087cBf89710';
var bancorTokenContractAddress = '0x1F573D6Fb3F13d689FF844B4cE37794d79a7FF1C';
// // initiate contract instances
var bancorInstance = web3.eth.contract(bancorCrowdsaleABI).at(bancorCrowdsaleContractAddress);
var bntToken = web3.eth.contract(bancorTokenABI).at(bancorTokenContractAddress);
//
// Single helper function for the crowdsale below..
//
/**
* Status function, just queries current state of bancor contract and
* displays human-readable output to the console.
*/
function bancorStatus() {
// call constant function at bancor contract
var publicSaleHasAlreadyStarted, secondsUntilSale, saleFinished;
var totalWei = bancorInstance.totalEtherContributed();
var capInWei = bancorInstance.totalEtherCap();
var totalBntInexistence = bntToken.totalSupply();
var nowInMilliseconds = new Date().getTime();
if (nowInMilliseconds >= bancorInstance.startTime() * 1000) {
publicSaleHasAlreadyStarted = true;
secondsUntilSale = 0;
} else {
publicSaleHasAlreadyStarted = false;
secondsUntilSale = bancorInstance.startTime() - nowInMilliseconds / 1000;
}
// TODO BUG BUG: sorry, I forgot to use BigNumber here, this will overflow!!
// just commenting it out fir the moment...
//if (bancorInstance.totalEtherContributed() >= bancorInstance.totalEtherCap()) {
// saleFinished = true;
//} else {
// saleFinished = false;
//}
saleFinished = false;
console.log("-------------------------------------------------------------------------------");
console.log(' CURRENT STATE OF BANCOR CROWDSALE:');
console.log("-------------------------------------------------------------------------------");
if (publicSaleHasAlreadyStarted) {
console.log(' Has sale already started: YES');
} else {
// round hours to 2 decimals (thats why there is this * 100 / 100 stuff here)
console.log(' Time until public sale starts: ' + Math.floor(secondsUntilSale) + ' seconds (' + Math.round((secondsUntilSale / 3600) * 100) / 100 + ' hours)');
}
console.log(' Total ETH collected until now: ' + web3.fromWei(totalWei, 'ether') + ' ETH');
console.log(' Is hidden cap already revealed: ' + (capInWei != 1e+24));
console.log(' Current active cap is: ' + web3.fromWei(capInWei, 'ether') + ' ETH');
if (saleFinished) {
console.log('');
console.log(' ********************************************************************');
console.log(' THE SALE IS FINISHED!!!! NO MORE BNT CAN BE BOUGHT IN THIS ICO!!!');
console.log(' ********************************************************************');
console.log('');
} else {
console.log(' Remaining ETH until (current) cap is reached: ' + web3.fromWei((capInWei - totalWei), 'ether') + ' ETH');
}
console.log(' Total number of BNT created until now: ' + web3.fromWei(totalBntInexistence, 'ether') + ' BNT');
console.log("-------------------------------------------------------------------------------");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment