Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web3/1.0 #2994

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/assets/ens/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function signMsgAndCreateSubdomain(message, from) {
var msg = web3.toHex(message);
var msg = window.web3.utils.toHex(message);
var params = [ msg, from ];
var method = 'personal_sign';

web3.currentProvider.sendAsync({
window.web3.currentProvider.sendAsync({
method,
params,
from
Expand Down
50 changes: 0 additions & 50 deletions app/assets/ens/pending.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,3 @@
var waitforWeb3 = function(callback) {
if (document.web3network) {
callback();
} else {
var wait_callback = function() {
waitforWeb3(callback);
};

setTimeout(wait_callback, 100);
}
};

function getNetwork(id) {
var networks = {
'1': 'mainnet',
'2': 'morden',
'3': 'ropsten',
'4': 'rinkeby',
'42': 'kovan'
};

return networks[id] || 'custom network';
}

// figure out what version of web3 this is
window.addEventListener('load', function() {
var timeout_value = 100;

setTimeout(function() {
if (typeof web3 != 'undefined') {
web3.version.getNetwork(function(error, netId) {
if (!error) {
document.web3network = getNetwork(netId);
}
});
}
}, timeout_value);
});

var ts = function() {
return Math.round((new Date()).getTime() / 1000);
Expand All @@ -53,18 +15,6 @@ setInterval(function() {
jQuery('#timeestimate').text('estimated confirmation time: ' + text);
}, 1000);

var callFunctionWhenTransactionMined = function(txHash, f) {
var transactionReceipt = web3.eth.getTransactionReceipt(txHash, function(error, result) {
if (result) {
f();
} else {
setTimeout(function() {
callFunctionWhenTransactionMined(txHash, f);
}, 10000);
}
});
};

callFunctionWhenTransactionMined($('#txn_hash').attr('value'), function() {
location.reload(1);
});
12 changes: 4 additions & 8 deletions app/assets/ens/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ window.onload = function() {
if ($('#tos')[0].checked === false) {
return _alert({ message: gettext('Please check the TOS checkbox.')}, 'error');
}
if (typeof web3 == 'undefined') {
return _alert({ message: gettext('Please install metamask in your browser and try again.')}, 'error');
if (!window.web3.currentProvider) {
return _alert({ message: gettext('Please install MetaMask in your browser and try again.')}, 'error');
}
// Get the github handle
githubHandle = $('#githubHandle').text();
// Get the current account and call sign function:
web3.eth.getAccounts(function(err, accounts) {
if (!accounts)
return;
signMsgAndCreateSubdomain('Github Username : ' + $('#githubHandle').text(), accounts[0]);
});
// call the sign function:
signMsgAndCreateSubdomain('Github Username : ' + $('#githubHandle').text(), document.coinbase);
};
};
170 changes: 83 additions & 87 deletions app/assets/onepager/js/receive.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,8 @@ var combine_secrets = function(secret1, secret2) {
return secrets.combine(shares);
};

var sign_and_send = function(rawTx, success_callback, private_key) {
// sign & serialize raw transaction
var tx = new EthJS.Tx(rawTx);

tx.sign(new EthJS.Buffer.Buffer.from(private_key, 'hex'));
var serializedTx = tx.serialize();

// send raw transaction
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), success_callback);
};

window.onload = function() {
waitforWeb3(function() {
waitForWeb3(function() {
ipfs.ipfsApi = IpfsApi(ipfsConfig);
ipfs.setProvider(ipfsConfig);
if (typeof document.ipfs_key_to_secret == 'undefined') {
Expand All @@ -31,17 +20,17 @@ window.onload = function() {
document.priv_key = combine_secrets(key2, document.gitcoin_secret);
});
});
waitforWeb3(function() {
waitForWeb3(function() {
if (document.web3network != document.network) {
if (document.web3network == 'locked') {
_alert({ message: gettext('Please authorize Metamask in order to continue.')}, 'info');
approve_metamask();
approveMetamask();
} else {
_alert({ message: gettext('You are not on the right web3 network. Please switch to ') + document.network }, 'error');
}

} else if (!$('#forwarding_address').val()) {
$('#forwarding_address').val(web3.eth.coinbase);
$('#forwarding_address').val(document.coinbase);
}
$('#network').val(document.web3network);
});
Expand Down Expand Up @@ -100,86 +89,93 @@ $(document).ready(function() {

// redeem tip

var gas_price_wei = new BigNumber(document.gas_price * 10 ** 9);
var gas_price_wei = window.web3.utils.toBN(document.gas_price * 10 ** 9);
var is_eth = document.tip['token_address'] == '0x0' || document.tip['token_address'] == '0x0000000000000000000000000000000000000000';
var token_address = document.tip['token_address'];
var token_contract = web3.eth.contract(token_abi).at(token_address);
var holding_address = document.tip['holding_address'];
var amount_in_wei = new BigNumber(document.tip['amount_in_wei']);
var amount_in_wei = window.web3.utils.toBN(document.tip['amount_in_wei']);

web3.eth.getTransactionCount(holding_address, function(error, result) {
var nonce = result;
window.web3.eth.getTransactionCount(holding_address).then(
function(result) {
var nonce = result;

if (!nonce) {
nonce = 0;
}
// find existing balance
web3.eth.getBalance(holding_address, function(error, result) {
var balance = new BigNumber(result.toString());

if (balance == 0) {
_alert('You must wait until the senders transaction confirm before claiming this tip.');
return;
if (!nonce) {
nonce = 0;
}
var rawTx;

if (is_eth) {
// send ETH
rawTx = {
nonce: web3.toHex(nonce),
to: forwarding_address,
from: holding_address,
value: amount_in_wei.toString()
};
web3.eth.estimateGas(rawTx, function(err, gasLimit) {
var buffer = new BigNumber(0);

gasLimit = new BigNumber(gasLimit);
var send_amount = balance.minus(gasLimit.times(gas_price_wei)).minus(buffer);

rawTx['value'] = web3.toHex(send_amount.toString()); // deduct gas costs from amount to send
rawTx['gasPrice'] = web3.toHex(gas_price_wei.toString());
rawTx['gas'] = web3.toHex(gasLimit.toString());
rawTx['gasLimit'] = web3.toHex(gasLimit.toString());
show_console = false;
if (show_console) {
console.log('addr ', holding_address);
console.log('balance ', balance.toString());
console.log('sending ', send_amount.toString());
console.log('gas ', (gasLimit.times(gas_price_wei)).toString());
console.log('gas price ', (gas_price_wei.toString()));
console.log('buffer ', (buffer.toString()));
console.log('balance > value ', balance > send_amount);
console.log(rawTx);
// find existing balance
window.web3.eth.getBalance(holding_address).then(
function(result) {
var balance = window.web3.utils.toBN(result);

if (balance == 0) {
_alert('You must wait until the senders transaction confirm before claiming this tip.');
return;
}
sign_and_send(rawTx, success_callback, document.priv_key);
});
} else {

// send ERC20
var data = token_contract.transfer.getData(forwarding_address, amount_in_wei.toString());

rawTx = {
nonce: web3.toHex(nonce),
to: token_address,
from: holding_address,
value: '0x00',
data: data
};

web3.eth.estimateGas(rawTx, function(err, gasLimit) {
rawTx['gasPrice'] = gas_price_wei.toNumber();
rawTx['gas'] = gasLimit;
rawTx['gasLimit'] = gasLimit;
var will_fail_at_this_gas_price = (gas_price_wei * gasLimit) > balance;

if (will_fail_at_this_gas_price) { // adjust if gas prices have increased since this tx was created
rawTx['gasPrice'] = Math.floor(balance / gasLimit / 10 ** 9);
var rawTx;

if (is_eth) {
// send ETH
rawTx = {
nonce: window.web3.utils.toHex(nonce),
to: forwarding_address,
from: holding_address,
value: amount_in_wei.toString()
};
window.web3.eth.estimateGas(rawTx).then(
function(gasLimit) {
gasLimit = window.web3.utils.toBN(gasLimit);

var buffer = window.web3.utils.toBN(0);
var send_amount = balance.sub(gasLimit.mul(gas_price_wei)).sub(buffer);

rawTx['value'] = window.web3.utils.toHex(send_amount.toString()); // deduct gas costs from amount to send
rawTx['gasPrice'] = window.web3.utils.toHex(gas_price_wei.toString());
rawTx['gas'] = window.web3.utils.toHex(gasLimit.toString());
rawTx['gasLimit'] = rawTx['gas'];
show_console = false;
if (show_console) {
console.log('addr ', holding_address);
console.log('balance ', balance.toString());
console.log('sending ', send_amount.toString());
console.log('gas ', (gasLimit.mul(gas_price_wei)).toString());
console.log('gas price ', (gas_price_wei.toString()));
console.log('buffer ', (buffer.toString()));
console.log('balance > value ', balance > send_amount);
console.log(rawTx);
}
sign_and_send(rawTx, success_callback, document.priv_key);
});
} else {

// send ERC20
const token_contract = new window.web3.eth.Contract(token_abi, token_address);
const data = token_contract.methods.transfer(forwarding_address, amount_in_wei.toString()).encodeABI();

rawTx = {
nonce: window.web3.utils.toHex(nonce),
to: token_address,
from: holding_address,
value: '0x00',
data: data
};

window.web3.eth.estimateGas(rawTx).then(
function(gasLimit) {
gasLimit = window.web3.utils.toBN(gasLimit);

rawTx['gasPrice'] = window.web3.utils.toHex(gas_price_wei.toString());
rawTx['gas'] = window.web3.utils.toHex(gasLimit.toString());
rawTx['gasLimit'] = rawTx['gas'];
var will_fail_at_this_gas_price = (gas_price_wei.mul(gasLimit)) > balance;

if (will_fail_at_this_gas_price) { // adjust if gas prices have increased since this tx was created
rawTx['gasPrice'] = window.web3.utils.toHex(balance.div(gasLimit));
}
sign_and_send(rawTx, success_callback, document.priv_key);
}
);
}
sign_and_send(rawTx, success_callback, document.priv_key);
});
}
});
});
});
});
});
51 changes: 34 additions & 17 deletions app/assets/onepager/js/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ $(document).ready(function() {

});

waitforWeb3(function() {
waitForWeb3(function() {
tokens(document.web3network).forEach(function(ele) {
if (ele && ele.addr) {
var html = '<option value=' + ele.addr + '>' + ele.name + '</option>';
Expand Down Expand Up @@ -158,7 +158,7 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p
return;
}
// setup
var fromAccount = web3.eth.accounts[0];
var fromAccount = document.coinbase;

if (username.indexOf('@') == -1) {
username = '@' + username;
Expand All @@ -176,7 +176,7 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p
tokenName = tokenDetails.name;
weiConvert = Math.pow(10, tokenDetails.decimals);
}
var amountInWei = amountInEth * 1.0 * weiConvert;
var amountInWei = Math.round(amountInEth * weiConvert);
// validation
var hasEmail = email != '';

Expand Down Expand Up @@ -281,24 +281,41 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p
};

if (isSendingETH) {
web3.eth.sendTransaction({
to: destinationAccount,
value: amountInWei,
gasPrice: web3.toHex(get_gas_price())
}, post_send_callback);
window.web3.eth.sendTransaction(
{
from: document.coinbase,
to: destinationAccount,
value: window.web3.utils.toHex(amountInWei),
gasPrice: window.web3.utils.toHex(get_gas_price())
},
post_send_callback
);
} else {
var send_erc20 = function() {
var token_contract = web3.eth.contract(token_abi).at(tokenAddress);

token_contract.transfer(destinationAccount, amountInWei, {gasPrice: web3.toHex(get_gas_price())}, post_send_callback);
const token_contract = new window.web3.eth.Contract(token_abi, tokenAddress);

token_contract.methods.transfer(
destinationAccount,
window.web3.utils.toHex(amountInWei)
).send(
{
from: document.coinbase,
gasPrice: window.web3.utils.toHex(get_gas_price())
},
post_send_callback
);
};
var send_gas_money_and_erc20 = function() {
_alert({ message: gettext('You will now be asked to confirm two transactions. The first is gas money, so your receipient doesnt have to pay it. The second is the actual token transfer. (note: check Metamask extension, sometimes the 2nd confirmation window doesnt popup)') }, 'info');
web3.eth.sendTransaction({
to: destinationAccount,
value: gas_money,
gasPrice: web3.toHex(get_gas_price())
}, send_erc20);
window.web3.eth.sendTransaction(
{
from: document.coinbase,
to: destinationAccount,
value: window.web3.utils.toHex(gas_money),
gasPrice: window.web3.utils.toHex(get_gas_price())
},
send_erc20
);
};

if (is_direct_to_recipient) {
Expand Down Expand Up @@ -359,4 +376,4 @@ var etherscanDomain = function() {
// mainnet
}
return etherscanDomain;
};
};
Loading