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

feat: rsk integration for bounties + hackathons #8259

Merged
merged 19 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from 15 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
1 change: 1 addition & 0 deletions app/assets/v2/images/chains/rsk.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions app/assets/v2/js/pages/bounty_detail/binance_extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const payWithBinanceExtension = (fulfillment_id, to_address, vm, modal) => {
const amount = vm.fulfillment_context.amount;
const token_name = vm.bounty.token_name;
const from_address = vm.bounty.bounty_owner_address;

binance_utils.transferViaExtension(
amount * 10 ** vm.decimals,
to_address,
Expand All @@ -14,13 +14,13 @@ const payWithBinanceExtension = (fulfillment_id, to_address, vm, modal) => {
}).catch(err => {
callback(err);
});

function callback(error, from_address, txn) {
if (error) {
_alert({ message: gettext('Unable to payout bounty due to: ' + error) }, 'error');
console.log(error);
} else {

const payload = {
payout_type: 'binance_ext',
tenant: 'BINANCE',
Expand All @@ -29,22 +29,22 @@ const payWithBinanceExtension = (fulfillment_id, to_address, vm, modal) => {
funder_address: from_address,
payout_tx_id: txn
};

modal.closeModal();
const apiUrlBounty = `/api/v1/bounty/payout/${fulfillment_id}`;

fetchData(apiUrlBounty, 'POST', payload).then(response => {
if (200 <= response.status && response.status <= 204) {
console.log('success', response);

vm.fetchBounty();
_alert('Payment Successful');

} else {
_alert('Unable to make payout bounty. Please try again later', 'error');
console.error(`error: bounty payment failed with status: ${response.status} and message: ${response.message}`);
}
}).catch(function(error) {
}).catch(function (error) {
_alert('Unable to make payout bounty. Please try again later', 'error');
console.log(error);
});
Expand Down
126 changes: 126 additions & 0 deletions app/assets/v2/js/pages/bounty_detail/rsk_extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const payWithRSKExtension = async (fulfillment_id, to_address, vm, modal) => {

const amount = vm.fulfillment_context.amount;
const token_name = vm.bounty.token_name;

// 1. init rsk provider
// const rskHost = "https://public-node.testnet.rsk.co";
const rskHost = "https://public-node.rsk.co";
const rskClient = new Web3();
rskClient.setProvider(
new rskClient.providers.HttpProvider(rskHost)
);

// Prompt user to unlock wallet if ethereum.selectedAddress is not present
if (!provider) {
try {
console.log(ethereum.selectedAddress);
} catch (e) {
modal.closeModal();
_alert({ message: 'Please download or enable Nifty Wallet extension' }, 'error');
return;
}

if (!ethereum.selectedAddress) {
modal.closeModal();
return onConnect().then(() => {
modal.openModal();
});
}
}

// 2. construct + sign txn via nifty
let txArgs;

if (token_name == 'R-BTC') {

balanceInWei = await rskClient.eth.getBalance(ethereum.selectedAddress);

rbtcBalance = rskClient.utils.fromWei(balanceInWei, 'ether');

if (Number(rbtcBalance) < amount) {
_alert({ message: `Insufficent balance in address ${ethereum.selectedAddress}` }, 'error');
return;
}

txArgs = {
to: to_address.toLowerCase(),
from: ethereum.selectedAddress,
value: rskClient.utils.toHex(rskClient.utils.toWei(String(amount))),
gasPrice: rskClient.utils.toHex(await rskClient.eth.getGasPrice()),
gas: rskClient.utils.toHex(318730),
gasLimit: rskClient.utils.toHex(318730)
};

} else {

tokenContract = new rskClient.eth.Contract(token_abi, vm.bounty.token_address);

balance = tokenContract.methods.balanceOf(
ethereum.selectedAddress).call({from: ethereum.selectedAddress});

amountInWei = amount * 1.0 * Math.pow(10, vm.decimals);

if (Number(balance) < amountInWei) {
_alert({ message: `Insufficent balance in address ${ethereum.selectedAddress}` }, 'error');
chibie marked this conversation as resolved.
Show resolved Hide resolved
return;
}

amountAsString = new rskClient.utils.BN(BigInt(amountInWei)).toString();
data = tokenContract.methods.transfer(to_address.toLowerCase(), amountAsString).encodeABI();

txArgs = {
to: vm.bounty.token_address,
from: ethereum.selectedAddress,
gasPrice: rskClient.utils.toHex(await rskClient.eth.getGasPrice()),
gas: rskClient.utils.toHex(318730),
gasLimit: rskClient.utils.toHex(318730),
data: data
chibie marked this conversation as resolved.
Show resolved Hide resolved
};
}

const txHash = await ethereum.request(
{
method: 'eth_sendTransaction',
params: [txArgs],
}
);

callback(null, ethereum.selectedAddress, txHash)

function callback(error, from_address, txn) {
if (error) {
_alert({ message: gettext('Unable to payout bounty due to: ' + error) }, 'error');
console.log(error);
} else {

const payload = {
payout_type: 'rsk_ext',
tenant: 'RSK',
amount: amount,
token_name: token_name,
funder_address: from_address,
payout_tx_id: txn
};

modal.closeModal();
const apiUrlBounty = `/api/v1/bounty/payout/${fulfillment_id}`;

fetchData(apiUrlBounty, 'POST', payload).then(response => {
if (200 <= response.status && response.status <= 204) {
console.log('success', response);

vm.fetchBounty();
_alert('Payment Successful');

} else {
_alert('Unable to make payout bounty. Please try again later', 'error');
console.error(`error: bounty payment failed with status: ${response.status} and message: ${response.message}`);
}
}).catch(function(error) {
_alert('Unable to make payout bounty. Please try again later', 'error');
console.log(error);
});
}
}
};
23 changes: 23 additions & 0 deletions app/assets/v2/js/pages/bounty_details2.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ Vue.mixin({
url = `https://explorer.harmony.one/#/tx/${txn}`;
break;

case 'R-BTC':
case 'RDOC':
case 'DOC':
url = `https://explorer.rsk.co/tx/${txn}`;
break;

default:
url = `https://etherscan.io/tx/${txn}`;

Expand Down Expand Up @@ -184,6 +190,12 @@ Vue.mixin({
url = `https://explorer.harmony.one/#/address/${address}`;
break;

case 'R-BTC':
case 'RDOC':
case 'DOC':
url = `https://explorer.rsk.co/address/${address}`;
break;

default:
url = `https://etherscan.io/address/${address}`;
}
Expand Down Expand Up @@ -399,6 +411,12 @@ Vue.mixin({
tenant = 'HARMONY';
break;

case 'R-BTC':
case 'DOC':
case 'RDOC':
tenant = 'RSK';
break;

default:
tenant = 'ETH';
}
Expand Down Expand Up @@ -481,6 +499,10 @@ Vue.mixin({
case 'harmony_ext':
payWithHarmonyExtension(fulfillment_id, fulfiller_address, vm, modal);
break;

case 'rsk_ext':
payWithRSKExtension(fulfillment_id, fulfiller_address, vm, modal);
break;
}
},
closeBounty: function() {
Expand Down Expand Up @@ -697,6 +719,7 @@ Vue.mixin({

case 'web3_modal':
case 'polkadot_ext':
case 'rsk_ext':
vm.fulfillment_context.active_step = 'payout_amount';
break;
}
Expand Down
37 changes: 4 additions & 33 deletions app/assets/v2/js/pages/hackathon_new_bounty.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ Vue.mixin({
vm.tokens = response;
vm.form.token = vm.filterByChainId[0];
vm.getAmount(vm.form.token.symbol);
vm.injectProvider(vm.form.token.symbol);

}).catch((err) => {
console.log(err);
Expand Down Expand Up @@ -88,38 +87,6 @@ Vue.mixin({
console.log(err);
});
},
injectProvider: function(token) {
let vm = this;
const chainId = vm.chainId;

if (!token || !chainId) {
return;
}

switch (chainId) {
case '58': {
let polkadot_endpoint;

if (token == 'KSM') {
polkadot_endpoint = KUSAMA_ENDPOINT;
} else if (token == 'DOT') {
polkadot_endpoint = POLKADOT_ENDPOINT;
}

polkadot_utils.connect(polkadot_endpoint).then(res =>{
console.log(res);
polkadot_extension_dapp.web3Enable('gitcoin');
}).catch(err => {
console.log(err);
});
break;
}

default:
break;

}
},
calcValues: function(direction) {
let vm = this;

Expand Down Expand Up @@ -183,6 +150,10 @@ Vue.mixin({
// ethereum
type = 'web3_modal';
break;
case '30':
// rsk
type = 'rsk_ext';
break;
case '58':
// polkadot
type = 'polkadot_ext';
Expand Down
38 changes: 4 additions & 34 deletions app/assets/v2/js/pages/new_bounty.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ Vue.mixin({
vm.tokens = response;
vm.form.token = vm.filterByChainId[0];
vm.getAmount(vm.form.token.symbol);
vm.injectProvider(vm.form.token.symbol);

}).catch((err) => {
console.log(err);
Expand Down Expand Up @@ -105,39 +104,6 @@ Vue.mixin({
console.log(err);
});
},
injectProvider: function(token) {
let vm = this;
const chainId = vm.chainId;

if (!token || !chainId) {
return;
}

switch (chainId) {
case '59':
case '58': {
let polkadot_endpoint;

if (token == 'KSM') {
polkadot_endpoint = KUSAMA_ENDPOINT;
} else if (token == 'DOT') {
polkadot_endpoint = POLKADOT_ENDPOINT;
}

polkadot_utils.connect(polkadot_endpoint).then(res =>{
console.log(res);
polkadot_extension_dapp.web3Enable('gitcoin');
}).catch(err => {
console.log(err);
});
break;
}


default:
break;
}
},
chibie marked this conversation as resolved.
Show resolved Hide resolved
calcValues: function(direction) {
let vm = this;

Expand Down Expand Up @@ -223,6 +189,10 @@ Vue.mixin({
// ethereum
type = 'web3_modal';
break;
case '30':
// rsk
type = 'rsk_ext';
break;
case '59':
case '58':
// 58 - polkadot, 59 - kusama
Expand Down
19 changes: 1 addition & 18 deletions app/dashboard/management/commands/sync_pending_fulfillments.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,12 @@ def handle(self, *args, **options):
)

# Extensions
ext_payout_types= ['web3_modal', 'polkadot_ext', 'harmony_ext']
ext_payout_types= ['web3_modal', 'polkadot_ext', 'harmony_ext', 'binance_ext', 'rsk_ext']
for ext_payout_type in ext_payout_types:
ext_pending_fulfillments = pending_fulfillments.filter(payout_type=ext_payout_type)
for fulfillment in ext_pending_fulfillments.all():
sync_payout(fulfillment)

# polkadot extension
polkadot_pending_fulfillments = pending_fulfillments.filter(payout_type='polkadot_ext')
if polkadot_pending_fulfillments:
for fulfillment in polkadot_pending_fulfillments.all():
sync_payout(fulfillment)

# binance extension
binance_pending_fulfillments = pending_fulfillments.filter(payout_type='binance_ext')
if binance_pending_fulfillments:
for fulfillment in binance_pending_fulfillments.all():
sync_payout(fulfillment)

# harmony extension
harmony_pending_fulfillments = pending_fulfillments.filter(payout_type='harmony_ext')
if harmony_pending_fulfillments:
for fulfillment in harmony_pending_fulfillments.all():
sync_payout(fulfillment)

# QR
qr_pending_fulfillments = pending_fulfillments.filter(payout_type='qr')
Expand Down
Loading