-
-
Notifications
You must be signed in to change notification settings - Fork 775
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grants): binance crosschain integration (#8306)
* feat: add binance chain sync + new migration * defer makemigration * feat: sync payout/pending tx flow * update admin + repr + cart_payload * update contribute view * update grant new + edit view * fix redundancy * add binance to grant creation flow + edit flow * add contribution with binance extension * binance cart flow * handle BUSD contribution * fix isBinanceExtInstalled * new migration * fix chain selection * include binance html + handle decimals
- Loading branch information
Showing
16 changed files
with
405 additions
and
19 deletions.
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
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,92 @@ | ||
const contributeWithBinanceExtension = async (grant, vm) => { | ||
let token_name = grant.grant_donation_currency; | ||
let decimals = vm.filterByChainId.filter(token => { | ||
return token.name == token_name })[0].decimals | ||
let amount = grant.grant_donation_amount * 10 ** decimals; | ||
let to_address = grant.binance_payout_address; | ||
let from_address; | ||
|
||
try { | ||
from_address = await binance_utils.getSelectedAccount(); | ||
} catch (error) { | ||
_alert({ message: `Please ensure your Binance Chain Extension wallet is installed and enabled`}, 'error'); | ||
return; | ||
} | ||
|
||
if (!token_name) { | ||
token_name = 'BNB'; | ||
} | ||
|
||
if (token_name === 'BNB') { | ||
const account_balance = await binance_utils.getAddressBalance(from_address); | ||
|
||
if (Number(account_balance) < amount) { | ||
_alert({ message: `Account needs to have more than ${amount / 10 ** decimals} BNB for payout` }, 'error'); | ||
return; | ||
} | ||
} else if (token_name === 'BUSD') { | ||
const busd_contract_address = '0xe9e7cea3dedca5984780bafc599bd69add087d56' | ||
|
||
const account_balance = await binance_utils.getAddressTokenBalance(from_address, busd_contract_address); | ||
|
||
if (Number(account_balance) < amount ) { | ||
_alert({ message: `Account needs to have more than ${amount / 10 ** decimals} BUSD for payout` }, 'error'); | ||
return; | ||
} | ||
} | ||
|
||
binance_utils.transferViaExtension( | ||
amount, | ||
to_address, | ||
from_address, | ||
token_name | ||
).then(txn => { | ||
if (txn) { | ||
callback(null, from_address, txn); | ||
} else { | ||
callback('error in signing transaction'); | ||
} | ||
}).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 { | ||
|
||
const payload = { | ||
'contributions': [{ | ||
'grant_id': grant.grant_id, | ||
'contributor_address': from_address, | ||
'tx_id': txn, | ||
'token_symbol': grant.grant_donation_currency, | ||
'tenant': 'BINANCE', | ||
'comment': grant.grant_comments, | ||
'amount_per_period': grant.grant_donation_amount | ||
}] | ||
}; | ||
|
||
const apiUrlGrant = `v1/api/contribute`; | ||
|
||
fetchData(apiUrlGrant, '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
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,28 @@ | ||
# Generated by Django 2.2.4 on 2021-02-08 18:39 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('grants', '0109_auto_20210203_1419'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='grant', | ||
name='binance_payout_address', | ||
field=models.CharField(blank=True, default='0x0', help_text='The binance wallet address where subscription funds will be sent.', max_length=255, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name='contribution', | ||
name='checkout_type', | ||
field=models.CharField(blank=True, choices=[('eth_std', 'eth_std'), ('eth_zksync', 'eth_zksync'), ('zcash_std', 'zcash_std'), ('celo_std', 'celo_std'), ('zil_std', 'zil_std'), ('polkadot_std', 'polkadot_std'), ('harmony_std', 'harmony_std'), ('binance_std', 'binance_std')], help_text='The checkout method used while making the contribution', max_length=30, 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'), ('BINANCE', 'BINANCE')], default='ETH', help_text='specific tenant in which contribution is made', max_length=10, null=True), | ||
), | ||
] |
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,64 @@ | ||
import requests | ||
from grants.sync.helpers import record_contribution_activity | ||
|
||
|
||
def get_binance_txn_status(contribution): | ||
txnid = contribution.tx_id | ||
|
||
if not txnid or txnid == "0x0": | ||
return None | ||
|
||
response = { 'status': 'pending' } | ||
|
||
try: | ||
binance_url = f'https://bsc-dataseed.binance.org' | ||
|
||
data = { | ||
'id': 0, | ||
'jsonrpc': '2.0', | ||
'method': 'eth_getTransactionReceipt', | ||
'params': [ txnid ] | ||
} | ||
|
||
headers = { | ||
'Host': 'gitcoin.co' | ||
} | ||
|
||
binance_response = requests.post(binance_url, json=data).json() | ||
|
||
result = binance_response['result'] | ||
|
||
response = { 'status': 'pending' } | ||
|
||
if result: | ||
tx_status = int(result.get('status'), 16) # convert hex to decimal | ||
|
||
if tx_status == 1: | ||
response = { 'status': 'done' } | ||
elif tx_status == 0: | ||
response = { 'status': 'expired' } | ||
|
||
except Exception as e: | ||
logger.error(f'error: get_binance_txn_status - {e}') | ||
|
||
finally: | ||
return response | ||
|
||
|
||
def sync_binance_payout(contribution): | ||
if contribution.tx_id or contribution.tx_id == "0x0": | ||
txn_status = get_binance_txn_status(contribution) | ||
|
||
if txn_status: | ||
status_description = txn_status.get('status') | ||
|
||
if status_description == 'done': | ||
contribution.success = True | ||
contribution.tx_cleared = True | ||
contribution.checkout_type = 'binance_std' | ||
record_contribution_activity(contribution) | ||
contribution.save() | ||
elif status_description == 'expired': | ||
contribution.success = True | ||
contribution.tx_cleared = False | ||
contribution.save() |
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
Oops, something went wrong.