Skip to content

Commit

Permalink
Change all web3 coinbase calls to be asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
KiChjang committed Mar 19, 2020
1 parent 987c630 commit 54025b2
Show file tree
Hide file tree
Showing 22 changed files with 518 additions and 446 deletions.
4 changes: 3 additions & 1 deletion app/assets/onepager/js/receive.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ window.onload = function() {
}

} else if (!$('#forwarding_address').val()) {
$('#forwarding_address').val(web3.eth.coinbase);
web3.eth.getCoinbase(function(_, coinbase) {
$('#forwarding_address').val(coinbase);
});
}
$('#network').val(document.web3network);
});
Expand Down
64 changes: 33 additions & 31 deletions app/assets/v2/js/avatar_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,37 +200,39 @@ function purchaseOption(option, value, target) {

to_address = '0x00De4B13153673BCAE2616b67bf822500d325Fc3'; // TODO: make dynamic
indicateMetamaskPopup();
web3.eth.sendTransaction({
'from': web3.eth.coinbase,
'to': to_address,
'value': cost_wei
}, function(error, result) {
indicateMetamaskPopup(true);
if (error) {
_alert('There was an error.', 'error');
return;
}
showBusyOverlay();
_alert('Waiting for tx to mine...', 'info');
callFunctionWhenTransactionMined(result, function() {
var request_url = '/revenue/attestations/new';
var txid = result;
var data = {
'txid': txid,
'amount': cost_eth,
'network': document.web3network,
'from_address': web3.eth.coinbase,
'to_address': to_address,
'type': 'avatar',
'option': option,
'value': value
};

$.post(request_url, data).then(function(result) {
hideBusyOverlay();
_alert('Success ✅ Loading your purchase now.', 'success');
setTimeout(function() {
location.reload();
web3.eth.getCoinbase(function(_, coinbase) {
web3.eth.sendTransaction({
'from': coinbase,
'to': to_address,
'value': cost_wei
}, function(error, result) {
indicateMetamaskPopup(true);
if (error) {
_alert('There was an error.', 'error');
return;
}
showBusyOverlay();
_alert('Waiting for tx to mine...', 'info');
callFunctionWhenTransactionMined(result, function() {
var request_url = '/revenue/attestations/new';
var txid = result;
var data = {
'txid': txid,
'amount': cost_eth,
'network': document.web3network,
'from_address': coinbase,
'to_address': to_address,
'type': 'avatar',
'option': option,
'value': value
};

$.post(request_url, data).then(function(result) {
hideBusyOverlay();
_alert('Success ✅ Loading your purchase now.', 'success');
setTimeout(function() {
location.reload();
});
});
});
});
Expand Down
4 changes: 3 additions & 1 deletion app/assets/v2/js/event_ethdenver2019/kudos_bulk_receive.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ $(document).ready(function() {
_alert({ message: gettext('You are not on the right web3 network. Please switch to ') + document.network }, 'error');
$('#receive').attr('disabled', 'disabled');
} else {
$('#forwarding_address').val(web3.eth.coinbase);
web3.eth.getCoinbase(function(_, coinbase) {
$('#forwarding_address').val(coinbase);
});
}
});

Expand Down
16 changes: 8 additions & 8 deletions app/assets/v2/js/pages/bounty_details.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,14 @@ waitforWeb3(function() {

if (typeof document.lastCoinbase == 'undefined') {

try {
// invoke infura synchronous call, if it fails metamask is locked
document.lastCoinbase = web3.eth.coinbase;
} catch (error) {
document.lastCoinbase = null;
// catch error so sentry doesn't alert on metamask call failure
console.log('web3.eth.coinbase could not be loaded');
}
web3.eth.getCoinbase(function(error, coinbase) {
if (error) {
console.log('web3.eth.coinbase could not be loaded');
document.lastCoinbase = null;
return;
}
document.lastCoinbase = coinbase;
});
return;
}

Expand Down
11 changes: 6 additions & 5 deletions app/assets/v2/js/pages/bounty_request_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
var trigger_form_hooks = function() {
callFunctionWhenweb3Available(
function() {
const addr = web3.eth.coinbase;
const input = $('[name=eth_address]');
web3.eth.getCoinbase(function(_, addr) {
const input = $('[name=eth_address]');

if (addr && !input.val()) {
input.val(addr);
}
if (addr && !input.val()) {
input.val(addr);
}
});
}
);
};
Expand Down
14 changes: 8 additions & 6 deletions app/assets/v2/js/pages/bulk_payout.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ $(document).ready(function($) {
_alert(msg, 'info');
sendTransaction(i + 1);

// tell frontend that this issue has a pending tx
localStorage[$('#issueURL').text()] = JSON.stringify({
timestamp: timestamp(),
dataHash: null,
issuer: web3.eth.coinbase,
txid: txid
web3.eth.getCoinbase(function(_, coinbase) {
// tell frontend that this issue has a pending tx
localStorage[$('#issueURL').text()] = JSON.stringify({
timestamp: timestamp(),
dataHash: null,
issuer: coinbase,
txid: txid
});
});
}
};
Expand Down
90 changes: 47 additions & 43 deletions app/assets/v2/js/pages/cancel_bounty/ETH.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,56 +66,60 @@ const ethCancelBounty = data => {
errormsg = gettext(
'No active funded issue found at this address. Are you sure this is an active funded issue?'
);
} else if (fromAddress != web3.eth.coinbase) {
errormsg = gettext(
'Only the address that submitted this funded issue may kill the bounty.'
);
}

if (errormsg) {
_alert({ message: errormsg });
unloading_button($('.js-submit'));
return;
}
web3.eth.getCoinbase(function(_, coinbase) {
if (fromAddress != coinbase) {
errormsg = gettext(
'Only the address that submitted this funded issue may kill the bounty.'
);
}

const final_callback = function(error, result) {
indicateMetamaskPopup(true);
const next = function() {
web3.eth.getAccounts(function(_, accounts) {
// setup inter page state
localStorage[issueURL] = JSON.stringify({
timestamp: timestamp(),
dataHash: null,
issuer: accounts[0],
txid: result
if (errormsg) {
_alert({ message: errormsg });
unloading_button($('.js-submit'));
return;
}

const final_callback = function(error, result) {
indicateMetamaskPopup(true);
const next = function() {
web3.eth.getAccounts(function(_, accounts) {
// setup inter page state
localStorage[issueURL] = JSON.stringify({
timestamp: timestamp(),
dataHash: null,
issuer: accounts[0],
txid: result
});
});
});

_alert({ message: gettext('Cancel bounty submitted to web3.') }, 'info');
setTimeout(() => {
document.location.href = '/funding/details/?url=' + issueURL;
}, 1000);
_alert({ message: gettext('Cancel bounty submitted to web3.') }, 'info');
setTimeout(() => {
document.location.href = '/funding/details/?url=' + issueURL;
}, 1000);
};

if (error) {
console.error('err', error);
_alert({ message: gettext('There was an error') });
unloading_button($('.js-submit'));
} else {
next();
}
};

if (error) {
console.error('err', error);
_alert({ message: gettext('There was an error') });
unloading_button($('.js-submit'));
} else {
next();
}
};

indicateMetamaskPopup();
web3.eth.getAccounts(function(_, accounts) {
bounty.killBounty(
bountyId,
{
from: accounts[0],
gasPrice: web3.toHex($('#gasPrice').val() * Math.pow(10, 9))
},
final_callback
);
indicateMetamaskPopup();
web3.eth.getAccounts(function(_, accounts) {
bounty.killBounty(
bountyId,
{
from: accounts[0],
gasPrice: web3.toHex($('#gasPrice').val() * Math.pow(10, 9))
},
final_callback
);
});
});

};
Expand Down
46 changes: 24 additions & 22 deletions app/assets/v2/js/pages/change_bounty.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,28 +310,30 @@ $(document).ready(function() {

const payFeaturedBounty = function() {
indicateMetamaskPopup();
web3.eth.sendTransaction({
to: '0x00De4B13153673BCAE2616b67bf822500d325Fc3',
from: web3.eth.coinbase,
value: web3.toWei(ethFeaturedPrice, 'ether'),
gasPrice: web3.toHex(5 * Math.pow(10, 9)),
gas: web3.toHex(318730),
gasLimit: web3.toHex(318730)
},
function(error, result) {
indicateMetamaskPopup(true);
if (error) {
_alert({ message: gettext('Unable to upgrade to featured bounty. Please try again.') }, 'error');
console.log(error);
} else {
saveAttestationData(
result,
ethFeaturedPrice,
'0x00De4B13153673BCAE2616b67bf822500d325Fc3',
'featuredbounty'
);
saveBountyChanges();
}
web3.eth.getCoinbase(function(_, coinbase) {
web3.eth.sendTransaction({
to: '0x00De4B13153673BCAE2616b67bf822500d325Fc3',
from: coinbase,
value: web3.toWei(ethFeaturedPrice, 'ether'),
gasPrice: web3.toHex(5 * Math.pow(10, 9)),
gas: web3.toHex(318730),
gasLimit: web3.toHex(318730)
},
function(error, result) {
indicateMetamaskPopup(true);
if (error) {
_alert({ message: gettext('Unable to upgrade to featured bounty. Please try again.') }, 'error');
console.log(error);
} else {
saveAttestationData(
result,
ethFeaturedPrice,
'0x00De4B13153673BCAE2616b67bf822500d325Fc3',
'featuredbounty'
);
saveBountyChanges();
}
});
});
};

Expand Down
23 changes: 18 additions & 5 deletions app/assets/v2/js/pages/create_bounty/ETH.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@
* Data is stored on IPFS + the data is stored in
* standard bounties contract on the ethereum blockchain
*/
const ethCreateBounty = data => {

const promisify = (fun, params=[]) => {
return new Promise((resolve, reject) => {
fun(...params, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
};

const ethCreateBounty = async (data) => {
try {
bounty_address();
} catch (exception) {
Expand Down Expand Up @@ -96,7 +109,7 @@ const ethCreateBounty = data => {
// to the node.js package. github.com/ethereum/web3.js
const isETH = tokenAddress == '0x0000000000000000000000000000000000000000';
web3.eth.contract(token_abi).at(tokenAddress);
const account = web3.eth.coinbase;
const account = await promisify(web3.eth.getCoinbase);
const amountNoDecimal = amount;

amount = amount * decimalDivisor;
Expand Down Expand Up @@ -228,7 +241,7 @@ const ethCreateBounty = data => {
if (isETH) {
web3.eth.sendTransaction({
to: to_address,
from: web3.eth.coinbase,
from: account,
value: web3.toWei(fee, 'ether'),
gasPrice: gas_price
}, function(error, txnId) {
Expand Down Expand Up @@ -322,7 +335,7 @@ const ethCreateBounty = data => {
indicateMetamaskPopup();
web3.eth.sendTransaction({
to: '0x00De4B13153673BCAE2616b67bf822500d325Fc3',
from: web3.eth.coinbase,
from: web3.account,
value: web3.toWei(ethFeaturedPrice, 'ether'),
gasPrice: web3.toHex($('#gasPrice').val() * Math.pow(10, 9)),
gas: web3.toHex(318730),
Expand Down Expand Up @@ -369,7 +382,7 @@ const ethCreateBounty = data => {

function check_balance_and_alert_user_if_not_enough(tokenAddress, amount, msg) {
const token_contract = web3.eth.contract(token_abi).at(tokenAddress);
const from = web3.eth.coinbase;
const from = account;
const token_details = tokenAddressToDetails(tokenAddress);
const token_decimals = token_details['decimals'];
const token_name = token_details['name'];
Expand Down
Loading

0 comments on commit 54025b2

Please sign in to comment.