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

chore: add polkadot + ksm grants integration #8269

Merged
merged 13 commits into from
Feb 3, 2021
5 changes: 5 additions & 0 deletions app/assets/v2/css/grants/cart.css
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
padding: 0.5rem 1rem;
}

.disabled-btn-address {
opacity: 0.8;
border-color: #eee;
}

.black {
color: black;
}
Expand Down
1 change: 1 addition & 0 deletions app/assets/v2/images/chains/kusama.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: 16 additions & 0 deletions app/assets/v2/js/cart-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ class CartData {
if (!grantData.grant_donation_currency) {
grantData.grant_donation_currency = 'ONE';
}
} else if (grantData.tenants.includes('POLKADOT')) {

if (!grantData.grant_donation_amount) {
grantData.grant_donation_amount = 1;
}
if (!grantData.grant_donation_currency) {
grantData.grant_donation_currency = 'DOT';
}
} else if (grantData.tenants.includes('KUSAMA')) {

if (!grantData.grant_donation_amount) {
grantData.grant_donation_amount = 1;
}
if (!grantData.grant_donation_currency) {
grantData.grant_donation_currency = 'KSM';
}
} else if (acceptsAllTokens || 'DAI' == accptedTokenName) {
if (!grantData.grant_donation_amount) {
grantData.grant_donation_amount = 5;
Expand Down
25 changes: 23 additions & 2 deletions app/assets/v2/js/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ Vue.component('grants-cart', {

isHarmonyExtInstalled() {
return window.onewallet && window.onewallet.isOneWallet;
},

isPolkadotExtInstalled() {
return polkadot_extension_dapp.isWeb3Injected;
}
},

Expand Down Expand Up @@ -376,6 +380,12 @@ Vue.component('grants-cart', {
case 'HARMONY':
vm.chainId = '1000';
break;
case 'KUSAMA':
vm.chainId = '59';
break;
case 'POLKADOT':
vm.chainId = '58';
break;
}
},
confirmQRPayment: function(e, grant) {
Expand Down Expand Up @@ -435,13 +445,21 @@ Vue.component('grants-cart', {
vm.$set(grant, 'loading', false);
});
},
contributeWithExtension: function(grant, tenant) {
contributeWithExtension: function(grant, tenant, data) {
let vm = this;

switch (tenant) {
case 'HARMONY':
contributeWithHarmonyExtension(grant, vm);
break;
case 'POLKADOT':
case 'KUSAMA':
if (data) {
contributeWithPolkadotExtension(grant, vm, data);
} else {
initPolkadotConnection(grant, vm);
}
break;
}
},
loginWithGitHub() {
Expand Down Expand Up @@ -479,7 +497,7 @@ Vue.component('grants-cart', {
// $('input[type=textarea]').focus();
},

updatePaymentStatus(grant_id, step = 'waiting', txnid) {
updatePaymentStatus(grant_id, step = 'waiting', txnid, additionalAttributes) {
let vm = this;
let grantData = vm.grantData;

Expand All @@ -489,6 +507,9 @@ Vue.component('grants-cart', {
if (txnid) {
vm.grantData[index].txnid = txnid;
}
if (additionalAttributes) {
vm.grantData[index].additionalAttributes = additionalAttributes;
}
}
});
},
Expand Down
8 changes: 8 additions & 0 deletions app/assets/v2/js/grants/_new.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ Vue.mixin({
vm.$set(vm.errors, 'zil_payout_address', 'Please enter Zilliqa address');
} else if (vm.chainId == 'harmony' && !vm.form.harmony_payout_address) {
vm.$set(vm.errors, 'harmony_payout_address', 'Please enter Harmony address');
} else if (vm.chainId == 'polkadot' && !vm.form.polkadot_payout_address) {
vm.$set(vm.errors, 'polkadot_payout_address', 'Please enter Polkadot address');
} else if (vm.chainId == 'kusama' && !vm.form.kusama_payout_address) {
vm.$set(vm.errors, 'kusama_payout_address', 'Please enter Kusama address');
}

if (!vm.form.grant_type) {
Expand Down Expand Up @@ -165,6 +169,8 @@ Vue.mixin({
'celo_payout_address': form.celo_payout_address,
'zil_payout_address': form.zil_payout_address,
'harmony_payout_address': form.harmony_payout_address,
'polkadot_payout_address': form.polkadot_payout_address,
'kusama_payout_address': form.kusama_payout_address,
'grant_type': form.grant_type,
'categories[]': form.grant_categories,
'network': form.network,
Expand Down Expand Up @@ -324,6 +330,8 @@ if (document.getElementById('gc-new-grant')) {
celo_payout_address: '',
zil_payout_address: '',
harmony_payout_address: '',
polkadot_payout_address: '',
kusama_payout_address: '',
grant_type: '',
grant_categories: [],
network: 'mainnet'
Expand Down
147 changes: 147 additions & 0 deletions app/assets/v2/js/grants/cart/polkadot_extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
const initPolkadotConnection = async(grant, vm) => {

// step 1: check if web3 is injected
if (!polkadot_utils.isWeb3Injected) {
vm.updatePaymentStatus(grant.grant_id, 'failed');
_alert({ message: `Please ensure your Polkadot One wallet is installed and unlocked`}, 'error');
return;
}

// step 2: init connection based on token
let polkadot_endpoint;
let decimals;
let format;
if (grant.grant_donation_currency == 'KSM') {
polkadot_endpoint = KUSAMA_ENDPOINT;
decimals = 12;
format = 0;
} else if (grant.grant_donation_currency == 'DOT') {
polkadot_endpoint = POLKADOT_ENDPOINT;
decimals = 10;
format = 2;
}

polkadot_utils.connect(polkadot_endpoint).then(() =>{
polkadot_extension_dapp.web3Enable('gitcoin').then(() => {
initComplete(null, grant, vm);
}).catch(err => {
initComplete(err);
});
}).catch(err => {
console.log(err);
_alert('Error connecting to polkadot network', 'error');
});

// step 3: allow user to select address on successful connection
async function initComplete(error, grant, vm) {

if (error) {
vm.updatePaymentStatus(grant.grant_id, 'failed');
_alert('Please ensure you\'ve connected your polkadot extension to Gitcoin', 'error');
console.log(error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬 debugger line forgotten.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was intentional ! just so that the error stays ! cause this is behind a staff flag -> will remove this next week once folks test and we are sure it's smooth

return;
}

const addresses = await polkadot_utils.getExtensionConnectedAccounts();
for (let i = 0; i < addresses.length; i++) {
const balance = (await polkadot_utils.getAddressBalance(addresses[i].address)) / 10 ** decimals;
addresses[i].balance = balance;
addresses[i].token_symbol = grant.grant_donation_currency;
addresses[i].chain_address = polkadot_keyring.encodeAddress(addresses[i].address, format);
addresses[i].sufficent_balance = balance >= grant.grant_donation_amount;
}

vm.updatePaymentStatus(grant.grant_id, 'waiting-on-user-input', null, {addresses: addresses});
}
}


const contributeWithPolkadotExtension = async(grant, vm, from_address) => {

let decimals;

if (grant.grant_donation_currency == 'KSM') {
decimals = 12;
} else if (grant.grant_donation_currency == 'DOT') {
decimals = 10;
}

// step 1. set modal to waiting state
vm.updatePaymentStatus(grant.grant_id, 'waiting');

const amount = grant.grant_donation_amount;

let to_address;
if (grant.grant_donation_currency == 'DOT') {
to_address = grant.polkadot_payout_address;
} else if (grant.grant_donation_currency == 'KSM') {
to_address = grant.kusama_payout_address;
}

// step 2. balance check
const account_balance = await polkadot_utils.getAddressBalance(from_address);
if (account_balance < amount * 10 ** decimals) {
_alert({ message: `Account needs to have more than ${amount} ${grant.grant_donation_currency}`}, 'error');
return;
}

// step 3: payout
polkadot_utils.transferViaExtension(
amount * 10 ** decimals,
to_address,
from_address
).then(txn => {
callback(null, from_address, txn.hash.toString());
}).catch(err => {
callback(err);
});


function callback(error, from_address, txn) {
if (error) {
vm.updatePaymentStatus(grant.grant_id, 'failed');
_alert({ message: gettext('Unable to contribute to grant due to ' + error) }, 'error');
console.log(error);
} else {

let tenant;
if (grant.grant_donation_currency == 'DOT') {
tenant = 'POLKADOT';
} else if (grant.grant_donation_currency == 'KSM') {
tenant = 'KUSAMA';
}

const payload = {
'contributions': [{
'grant_id': grant.grant_id,
'contributor_address': from_address,
'tx_id': txn,
'token_symbol': grant.grant_donation_currency,
'tenant': tenant,
'comment': grant.grant_comments,
'amount_per_period': grant.grant_donation_amount
}]
};

const apiUrlBounty = `v1/api/contribute`;

fetchData(apiUrlBounty, 'POST', JSON.stringify(payload)).then(response => {

if (200 <= response.status && response.status <= 204) {
console.log('success', response);

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

} else {
vm.updatePaymentStatus(grant.grant_id, 'failed');
_alert('Unable to make contribute to grant. Please try again later', 'error');
console.error(`error: grant contribution failed with status: ${response.status} and message: ${response.message}`);
}
}).catch(function(error) {
vm.updatePaymentStatus(grant.grant_id, 'failed');
_alert('Unable to make contribute to grant. Please try again later', 'error');
console.log(error);
});
}
}
};
6 changes: 6 additions & 0 deletions app/assets/v2/js/grants/funding.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ function tokenOptionsForGrant(grant) {
} else if (grant.tenants && grant.tenants.includes('HARMONY')) {
tokenDataList = tokenDataList.filter(token => token.chainId === 1000);
tokenDefault = 'ONE';
} else if (grant.tenants && grant.tenants.includes('POLKADOT')) {
tokenDataList = tokenDataList.filter(token => token.chainId === 58);
tokenDefault = 'DOT';
} else if (grant.tenants && grant.tenants.includes('KUSAMA')) {
tokenDataList = tokenDataList.filter(token => token.chainId === 59);
tokenDefault = 'KSM';
} else {
tokenDataList = tokenDataList.filter(token => token.chainId === 1);
}
Expand Down
12 changes: 7 additions & 5 deletions app/assets/v2/js/lib/polkadot/core.min.js

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions app/assets/v2/js/lib/polkadot/extension.min.js

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion app/assets/v2/js/lib/polkadot/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,11 @@ polkadot_utils.transferViaExtension = async(amount, to_address, from_address) =>
reject('transferViaExtension: something went wrong' + error);
});
});
};
};

/**
* Checks to see if polkadot web3 is injected
*/
polkadot_utils.isWeb3Injected = () => {
return polkadot_extension_dapp.isWeb3Injected;
}
4 changes: 3 additions & 1 deletion app/assets/v2/js/pages/new_bounty.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Vue.mixin({
}

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

Expand Down Expand Up @@ -222,8 +223,9 @@ Vue.mixin({
// ethereum
type = 'web3_modal';
break;
case '59':
case '58':
// polkadot
// 58 - polkadot, 59 - kusama
type = 'polkadot_ext';
break;
case '56':
Expand Down
2 changes: 1 addition & 1 deletion app/dashboard/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from adminsortable2.admin import SortableInlineAdminMixin

from .models import (
Activity, Answer, BlockedURLFilter, BlockedUser, BlockedIP, Bounty, BountyEvent, BountyFulfillment, BountyInvites,
Activity, Answer, BlockedIP, BlockedURLFilter, BlockedUser, Bounty, BountyEvent, BountyFulfillment, BountyInvites,
BountySyncRequest, CoinRedemption, CoinRedemptionRequest, Coupon, Earning, FeedbackEntry, FundRequest,
HackathonEvent, HackathonProject, HackathonRegistration, HackathonSponsor, HackathonWorkshop, Interest,
Investigation, LabsResearch, MediaFile, ObjectView, Option, Poll, PollMedia, PortfolioItem, Profile,
Expand Down
4 changes: 4 additions & 0 deletions app/dashboard/templates/bounty/new_bounty.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ <h1 class="text-center">Fund Issue</h1>
<input type="radio" name="bounty_chain" id="58_chain" value="58" v-model="chainId"><img class="mr-2" src="{% static 'v2/images/chains/polkadot.svg' %}" alt="" width="16"> Polkadot
</label>

<label class="btn btn-radio chain-btn d-flex align-items-center mr-2 mb-2 font-weight-bold py-2 px-4" :class="{'active': chainId === '59'}">
<input type="radio" name="bounty_chain" id="59_chain" value="59" v-model="chainId"><img class="mr-2" src="{% static 'v2/images/chains/kusama.svg' %}" alt="" width="16"> Kusama
</label>

<label class="btn btn-radio chain-btn d-flex align-items-center mr-2 mb-2 font-weight-bold py-2 px-4" :class="{'active': chainId === '61'}">
<input type="radio" name="bounty_chain" id="61_chain" value="61" v-model="chainId"><img class="mr-2" src="{% static 'v2/images/chains/ethereum-classic.svg' %}" alt="" width="16"> ETC
</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def handle(self, *args, **options):
# Auto expire pending transactions
timeout_period = timezone.now() - timedelta(minutes=60)

tenants = ['ZCASH', 'ZIL', 'CELO', 'POLKADOT', 'HARMONY']
tenants = ['ZCASH', 'ZIL', 'CELO', 'POLKADOT', 'HARMONY', 'KUSAMA']

for tenant in tenants:
tenant_pending_contributions = pending_contribution.filter(subscription__tenant=tenant)
Expand Down
23 changes: 23 additions & 0 deletions app/grants/migrations/0109_auto_20210203_1419.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.2.4 on 2021-02-03 14:19

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('grants', '0108_auto_20210114_0810'),
]

operations = [
migrations.AddField(
model_name='grant',
name='kusama_payout_address',
field=models.CharField(blank=True, default='0x0', help_text='The kusama wallet address where subscription funds will be sent.', max_length=255, null=True),
),
migrations.AlterField(
model_name='subscription',
name='tenant',
field=models.CharField(blank=True, choices=[('ETH', 'ETH'), ('ZCASH', 'ZCASH'), ('CELO', 'CELO'), ('ZIL', 'ZIL'), ('POLKADOT', 'POLKADOT'), ('KUSAMA', 'KUSAMA'), ('HARMONY', 'HARMONY')], default='ETH', help_text='specific tenant in which contribution is made', max_length=10, null=True),
),
]
Loading