-
-
Notifications
You must be signed in to change notification settings - Fork 775
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f4a2acd
chore: add polkadot + ksm grants integration
thelostone-mc 0d0effc
fix typo failing the cart to load
octavioamu ec99248
fix tabs prompt web3modal
octavioamu 26f490d
Merge remote-tracking branch 'upstream/master' into polkadot
thelostone-mc 11e4acf
add mig file
thelostone-mc 9961f99
fix bugs
thelostone-mc 328244b
fix sync logic
thelostone-mc d3a8e03
use updated library
thelostone-mc ba5fe2a
add missing KSM model changes
thelostone-mc 310a677
Merge remote-tracking branch 'upstream/master' into polkadot
thelostone-mc c23baff
Merge branch 'master' into polkadot
thelostone-mc 334665d
feedback comments
thelostone-mc 3a60b52
recreate mig
thelostone-mc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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); | ||
}); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😬 debugger line forgotten.
There was a problem hiding this comment.
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