Skip to content

Commit f43670f

Browse files
authored
Harmony grants checkout with MetaMask (web3modal) (#10558)
* update harmony_extension.js * use web3.currentProvider * remove unused libs + d.r.y * use switchChain function in #10564 * use rpc method to broadcast tx
1 parent 56d4dd3 commit f43670f

File tree

3 files changed

+36
-43
lines changed

3 files changed

+36
-43
lines changed

app/assets/v2/js/cart.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,6 @@ Vue.component('grants-cart', {
113113
`${static_url}v2/js/grants/cart/binance_extension.js`
114114
],
115115
'HARMONY': [
116-
`${static_url}v2/js/lib/harmony/HarmonyUtils.browser.js`,
117-
`${static_url}v2/js/lib/harmony/HarmonyJs.browser.js`,
118-
`${static_url}v2/js/lib/harmony/HarmonyAccount.browser.js`,
119-
`${static_url}v2/js/lib/harmony/HarmonyCrypto.browser.js`,
120-
`${static_url}v2/js/lib/harmony/HarmonyNetwork.browser.js`,
121-
`${static_url}v2/js/lib/harmony/utils.js`,
122116
`${static_url}v2/js/grants/cart/harmony_extension.js`
123117
],
124118
'RSK': [
@@ -428,7 +422,7 @@ Vue.component('grants-cart', {
428422
},
429423

430424
isHarmonyExtInstalled() {
431-
return window.onewallet && window.onewallet.isOneWallet;
425+
return window.ethereum && window.ethereum.isMetaMask;
432426
},
433427

434428
isBinanceExtInstalled() {
@@ -673,7 +667,7 @@ Vue.component('grants-cart', {
673667
vm.chainId = '102';
674668
break;
675669
case 'HARMONY':
676-
vm.chainId = '1000';
670+
vm.chainId = '1666600000';
677671
break;
678672
case 'BINANCE':
679673
vm.chainId = '56';

app/assets/v2/js/grants/cart/harmony_extension.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
1-
const contributeWithHarmonyExtension = async(grant, vm, modal) => {
1+
const contributeWithHarmonyExtension = async(grant, vm) => {
22

3-
if (!harmony_utils.isOnewalletInstalled()) {
4-
_alert({ message: 'Please ensure your Harmony One wallet is installed and unlocked'}, 'danger');
5-
return;
3+
// Connect to Harmony Mainnet Shard 0 network with MetaMask
4+
if (!provider) {
5+
await onConnect();
66
}
77

88
const amount = grant.grant_donation_amount;
99
const to_address = grant.harmony_payout_address;
10+
const from_address = selectedAccount;
1011

11-
// step 1: init harmony
12-
let hmy = harmony_utils.initHarmony();
13-
14-
// step 2: init extension
15-
let harmonyExt = await harmony_utils.initHarmonyExtension();
12+
await switchChain(1666600000);
1613

17-
// step 3: check balance
18-
const from_address = await harmony_utils.loginHarmonyExtension(harmonyExt);
19-
const account_balance = await harmony_utils.getAddressBalance(hmy, from_address);
14+
// Check balance sufficiency
15+
const account_balance = web3.utils.fromWei(balance, 'ether');
2016

21-
if (account_balance < amount) {
17+
if (Number(account_balance) < amount) {
2218
_alert({ message: `Account needs to have more than ${amount} ONE in shard 0 for payout`}, 'danger');
23-
harmony_utils.logoutHarmonyExtension(harmonyExt);
2419
return;
2520
}
2621

27-
// step 4: payout
28-
harmony_utils.transfer(
29-
hmy,
30-
harmonyExt,
31-
from_address,
32-
to_address,
33-
amount
34-
).then(txn => {
35-
if (txn) {
36-
callback(null, from_address, txn);
37-
} else {
38-
callback('error in signing transaction');
39-
}
40-
}).catch(err => callback(err));
22+
// Payout
23+
// Gas limit as indicated here: https://docs.harmony.one/home/general/wallets/browser-extensions-wallets/metamask-wallet/sending-and-receiving#sending-a-regular-transaction
4124

25+
try {
26+
let web3 = new Web3(provider);
27+
28+
const txHash = await web3.currentProvider.request({
29+
method: 'eth_sendTransaction',
30+
params: [{
31+
to: to_address,
32+
from: from_address,
33+
value: web3.utils.toHex(web3.utils.toWei(String(amount))),
34+
gasPrice: web3.utils.toHex(30 * Math.pow(10, 9)),
35+
gas: web3.utils.toHex(25000),
36+
gasLimit: web3.utils.toHex(25000)
37+
}]
38+
});
39+
40+
callback(null, from_address, txHash);
41+
} catch (e) {
42+
callback(e.message);
43+
}
4244

4345
function callback(error, from_address, txn) {
4446
if (error) {
@@ -54,15 +56,13 @@ const contributeWithHarmonyExtension = async(grant, vm, modal) => {
5456
'tx_id': txn,
5557
'token_symbol': grant.grant_donation_currency,
5658
'tenant': 'HARMONY',
57-
'amount_per_period': grant.grant_donation_amount
59+
'amount_per_period': amount
5860
}]
5961
};
6062

6163
const apiUrlBounty = 'v1/api/contribute';
6264

6365
fetchData(apiUrlBounty, 'POST', JSON.stringify(payload)).then(response => {
64-
console.log(response);
65-
console.log(payload);
6666
if (200 <= response.status && response.status <= 204) {
6767
console.log('success', response);
6868
MauticEvent.createEvent({
@@ -78,19 +78,18 @@ const contributeWithHarmonyExtension = async(grant, vm, modal) => {
7878
}
7979
]
8080
});
81+
console.log('e reach here');
8182

8283
vm.updatePaymentStatus(grant.grant_id, 'done', txn);
8384

8485
} else {
8586
vm.updatePaymentStatus(grant.grant_id, 'failed');
8687
_alert('Unable to contribute to grant. Please try again later', 'danger');
87-
harmony_utils.logoutHarmonyExtension(harmonyExt);
8888
console.error(`error: grant contribution failed with status: ${response.status} and message: ${response.message}`);
8989
}
9090
}).catch(function(error) {
9191
vm.updatePaymentStatus(grant.grant_id, 'failed');
9292
_alert('Unable to contribute to grant. Please try again later', 'danger');
93-
harmony_utils.logoutHarmonyExtension(harmonyExt);
9493
console.log(error);
9594
});
9695
}

app/grants/templates/grants/cart/extension/harmony.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<template v-slot:title><img class="mr-2" src="{% static 'v2/images/chains/harmony.svg' %}" alt="" height="16"> <b>Harmony ([[grantsCountByTenant.HARMONY || 0]])</b></template>
44

55
<p v-if="!isHarmonyExtInstalled" class="gc-alert-yellow p-3 font-smaller-2">
6-
You'll need a Harmony wallet to contribute to Harmony grants. We recommend
7-
<a href="https://docs.harmony.one/home/network/wallets/browser-extensions-wallets/one-wallet" target="_blank">Harmony ONE Wallet.</a>
6+
You'll need a Harmony supported wallet to contribute to Harmony grants. We recommend
7+
<a href="https://chrome.google.com/webstore/detail/metamask/nkbihfbeogaeaoehlefnkodbefgpgknn" target="_blank">MetaMask.</a>
88
</p>
99

1010
<div class="row">

0 commit comments

Comments
 (0)