-
-
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.
core: base commit for celo + zil + refactor (#6313)
* core: base commit for celo + zil - update mgmt command to to cater for all cross-chain pending fulfillments - refactor and move all token specific logic to it's own file within sync/ - remove un-used url - update bounty creation to include celo * set fulfillments > 5 min as expired * add migration file * update API wiring for ZIL * update API wiring for celo * update bounty creation flow * bug: rename variable * celo: update to support cUSD and cGLD * fixup ZIL backend * recreate migration * bug: fix up celo * Update etc.py * fix migration Co-authored-by: Dan Lipert <[email protected]>
- Loading branch information
1 parent
cee8e8f
commit f1d43c2
Showing
14 changed files
with
274 additions
and
103 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
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,18 @@ | ||
# Generated by Django 2.2.4 on 2020-04-06 14:36 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('dashboard', '0096_auto_20200401_2038'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='bountyfulfillment', | ||
name='payout_status', | ||
field=models.CharField(blank=True, choices=[('expired', 'expired'), ('pending', 'pending'), ('done', 'done')], max_length=10), | ||
), | ||
] |
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,69 @@ | ||
from django.utils import timezone | ||
|
||
import requests | ||
from dashboard.sync.helpers import txn_already_used | ||
|
||
|
||
def find_txn_on_celo_explorer(fulfillment, network='mainnet'): | ||
token_name = fulfillment.token_name | ||
if token_name != 'cGLD' and token_name != 'cUSD': | ||
return None | ||
|
||
funderAddress = fulfillment.bounty.bounty_owner_address | ||
amount = fulfillment.payout_amount | ||
payeeAddress = fulfillment.fulfiller_address | ||
|
||
# TODO: UPDATE WITH MAINNET URL. Using alfajores until then | ||
blockscout_url = f'https://alfajores-blockscout.celo-testnet.org/api?module=account&action=tokentx&address={funderAddress}' | ||
|
||
blockscout_response = requests.get(blockscout_url).json() | ||
if blockscout_response['message'] and blockscout_response['result']: | ||
for txn in blockscout_response['result']: | ||
if ( | ||
txn['from'] == funderAddress.lower() and | ||
txn['to'] == payeeAddress.lower() and | ||
float(txn['value']) == float(amount) and | ||
not txn_already_used(txn['hash'], token_name) | ||
): | ||
return txn | ||
return None | ||
|
||
|
||
def get_celo_txn_status(txnid, network='mainnet'): | ||
if not txnid: | ||
return None | ||
|
||
# TODO: UPDATE WITH MAINNET URL. Using alfajores until then | ||
blockscout_url = f'https://alfajores-blockscout.celo-testnet.org/api?module=transaction&action=gettxinfo&txhash={txnid}' | ||
|
||
blockscout_response = requests.get(blockscout_url).json() | ||
|
||
if blockscout_response['status'] and blockscout_response['result']: | ||
|
||
response = { | ||
'blockNumber': int(blockscout_response['result']['blockNumber']), | ||
'confirmations': int(blockscout_response['result']['confirmations']) | ||
} | ||
|
||
if response['confirmations'] > 0: | ||
response['has_mined'] = True | ||
else: | ||
response['has_mined'] = False | ||
return response | ||
|
||
return None | ||
|
||
|
||
def sync_celo_payout(fulfillment): | ||
if not fulfillment.payout_tx_id: | ||
txn = find_txn_on_celo_explorer(fulfillment) | ||
if txn: | ||
fulfillment.payout_tx_id = txn['hash'] | ||
|
||
if fulfillment.payout_tx_id: | ||
txn_status = get_celo_txn_status(fulfillment.payout_tx_id) | ||
if txn_status and txn_status.get('has_mined'): | ||
fulfillment.payout_status = 'done' | ||
fulfillment.accepted_on = timezone.now() | ||
fulfillment.accepted = True | ||
fulfillment.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from django.utils import timezone | ||
|
||
import requests | ||
from dashboard.sync.helpers import txn_already_used | ||
|
||
|
||
def find_txn_on_etc_explorer(fulfillment, network='mainnet'): | ||
token_name = fulfillment.token_name | ||
if token_name != 'ETC': | ||
return None | ||
|
||
funderAddress = fulfillment.bounty.bounty_owner_address | ||
amount = fulfillment.payout_amount | ||
payeeAddress = fulfillment.fulfiller_address | ||
|
||
blockscout_url = f'https://blockscout.com/etc/{network}/api?module=account&action=txlist&address={funderAddress}' | ||
blockscout_response = requests.get(blockscout_url).json() | ||
if blockscout_response['message'] and blockscout_response['result']: | ||
for txn in blockscout_response['result']: | ||
if ( | ||
txn['from'] == funderAddress.lower() and | ||
txn['to'] == payeeAddress.lower() and | ||
float(txn['value']) == float(amount) and | ||
not txn_already_used(txn['hash'], token_name) | ||
): | ||
return txn | ||
return None | ||
|
||
|
||
def get_etc_txn_status(txnid, network='mainnet'): | ||
if not txnid: | ||
return None | ||
|
||
blockscout_url = f'https://blockscout.com/etc/{network}/api?module=transaction&action=gettxinfo&txhash={txnid}' | ||
blockscout_response = requests.get(blockscout_url).json() | ||
|
||
if blockscout_response['status'] and blockscout_response['result']: | ||
|
||
response = { | ||
'blockNumber': int(blockscout_response['result']['blockNumber']), | ||
'confirmations': int(blockscout_response['result']['confirmations']) | ||
} | ||
|
||
if response['confirmations'] > 0: | ||
response['has_mined'] = True | ||
else: | ||
response['has_mined'] = False | ||
return response | ||
|
||
return None | ||
|
||
|
||
def sync_etc_payout(fulfillment): | ||
if not fulfillment.payout_tx_id: | ||
txn = find_txn_on_etc_explorer(fulfillment) | ||
if txn: | ||
fulfillment.payout_tx_id = txn['hash'] | ||
if fulfillment.payout_tx_id: | ||
txn_status = get_etc_txn_status(fulfillment.payout_tx_id) | ||
if txn_status and txn_status.get('has_mined'): | ||
fulfillment.payout_status = 'done' | ||
fulfillment.accepted_on = timezone.now() | ||
fulfillment.accepted = True | ||
fulfillment.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from dashboard.models import BountyFulfillment | ||
|
||
|
||
def txn_already_used(txn, token_name): | ||
return BountyFulfillment.objects.filter( | ||
payout_tx_id = txn, | ||
token_name=token_name | ||
).exists() |
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,74 @@ | ||
import json | ||
|
||
from django.conf import settings | ||
from django.utils import timezone | ||
|
||
import requests | ||
from dashboard.sync.helpers import txn_already_used | ||
|
||
headers = { | ||
"X-APIKEY" : settings.VIEW_BLOCK_API_KEY | ||
} | ||
|
||
def find_txn_on_zil_explorer(fulfillment, network='mainnet'): | ||
token_name = fulfillment.token_name | ||
if token_name != 'ZIL': | ||
return None | ||
|
||
funderAddress = fulfillment.bounty.bounty_owner_address | ||
amount = fulfillment.payout_amount | ||
payeeAddress = fulfillment.fulfiller_address | ||
|
||
url = f'https://api.viewblock.io/v1/zilliqa/addresses/{funderAddress}/txs?network={network}' | ||
|
||
response = requests.get(url, headers=headers).json() | ||
|
||
if len(response): | ||
for txn in response: | ||
if ( | ||
txn['from'] == funderAddress.lower() and | ||
txn['to'] == payeeAddress.lower() and | ||
txn['direction'] == 'out' and | ||
float(txn['value']) == float(amount) and | ||
not txn_already_used(txn['hash'], token_name) | ||
): | ||
return txn | ||
return None | ||
|
||
|
||
def get_zil_txn_status(txnid, network='mainnet'): | ||
if not txnid: | ||
return None | ||
|
||
url = f'https://api.viewblock.io/v1/zilliqa/txs/{txnid}?network={network}' | ||
view_block_response = requests.get(url, headers=headers).json() | ||
|
||
if view_block_response: | ||
|
||
response = { | ||
'blockHeight': int(view_block_response['blockHeight']), | ||
'receiptSuccess': view_block_response['receiptSuccess'] | ||
} | ||
|
||
if response['receiptSuccess']: | ||
response['has_mined'] = True | ||
else: | ||
response['has_mined'] = False | ||
return response | ||
|
||
return None | ||
|
||
|
||
def sync_zil_payout(fulfillment): | ||
if not fulfillment.payout_tx_id: | ||
txn = find_txn_on_zil_explorer(fulfillment) | ||
if txn: | ||
fulfillment.payout_tx_id = txn['hash'] | ||
|
||
if fulfillment.payout_tx_id: | ||
txn_status = get_zil_txn_status(fulfillment.payout_tx_id) | ||
if txn_status and txn_status.get('has_mined'): | ||
fulfillment.payout_status = 'done' | ||
fulfillment.accepted_on = timezone.now() | ||
fulfillment.accepted = True | ||
fulfillment.save() |
Oops, something went wrong.