Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
5984c19
cherrypick part of changes
Dec 1, 2021
613a469
create indexer.py
Dec 1, 2021
c0d0e6c
remove solana_receipts_update.py
Dec 1, 2021
3f6b5c4
Cherry pick files from old branch
Dec 1, 2021
0790298
add requirement
Dec 1, 2021
340c854
fix refactoring issues
Dec 1, 2021
7449b38
Fix inspection issues
Dec 1, 2021
b3dacfa
fix last issue
Dec 1, 2021
a50cd47
Merge branch '336_indexer_refactoring' into 337_сreate_base_airdroppe…
Dec 1, 2021
2b8f879
Merge remote-tracking branch 'origin/develop' into 337_сreate_base_ai…
Dec 2, 2021
f51f2ed
simplify tests
Dec 2, 2021
6678924
add test
Dec 2, 2021
5d454b7
Merge remote-tracking branch 'origin/develop' into 337_сreate_base_ai…
Dec 3, 2021
add136a
add price provider
Dec 1, 2021
9a4be44
fix PriceProvider, add test
Dec 1, 2021
07aaca8
Add tests. Check worn on all nets
Dec 1, 2021
7a46c12
refactoring
Dec 1, 2021
2fc1424
integrate price_provider into airdropper
Dec 2, 2021
d157d67
integrate price provider
Dec 2, 2021
8a6abfd
use new faucet method
Dec 3, 2021
3d4dec9
add new parameter to airdropper main
Dec 3, 2021
5c29832
Test discriptions for airdropper
Dec 3, 2021
6a4efdc
Comments for price provider tests
Dec 3, 2021
14aeeed
remove unnecessary comment
Dec 3, 2021
bd35791
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
Dec 3, 2021
ff1f557
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
Dec 6, 2021
c28ca8e
fix error
Dec 6, 2021
07dd112
copy from erc20 contract test
Dec 5, 2021
023993d
Merge remote-tracking branch 'origin/develop' into 344_сreate_integra…
Dec 7, 2021
8c68755
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
Dec 7, 2021
f372b08
Merge branch '338_create_sol_price_provider' into 344_сreate_integrat…
Dec 7, 2021
ae5bddb
create integration tests
Dec 7, 2021
7f4c62c
fix oneline
Dec 7, 2021
b9cf0a6
revert changes on test_neon_faucet
Dec 7, 2021
1012a6b
fix some errors
Dec 7, 2021
dab9c2a
not working
Dec 7, 2021
0a4a2a0
add old variant
Dec 8, 2021
941f5ba
add helper functions
Dec 8, 2021
e223c46
transaction works!
Dec 8, 2021
24bc1f1
prepare refactoring
Dec 8, 2021
2401784
remove unnecessary code
Dec 8, 2021
5325895
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
Dec 8, 2021
9743411
Merge branch '338_create_sol_price_provider' into 344_сreate_integrat…
Dec 8, 2021
0f5c559
add account creation methods
Dec 9, 2021
b3cda28
first test almost ready
Dec 9, 2021
8e74e86
fix tests
Dec 9, 2021
18d88c7
fir airdropper startup
Dec 9, 2021
9ec00c9
One test is completely ready!
Dec 9, 2021
89714f6
add complex test case
Dec 9, 2021
48a96e7
add services to docker-compose file
Dec 9, 2021
89b951a
improve tests
Dec 10, 2021
9acbb38
add price update interval parameter
Dec 10, 2021
726cb1c
remove unnecessary imports
Dec 10, 2021
a1cbad2
fix airdropper run
Dec 10, 2021
5167613
Merge branch '338_create_sol_price_provider' into 344_сreate_integrat…
Dec 10, 2021
34dcedf
prepare for CI
Dec 10, 2021
4ec437f
remove commented
Dec 10, 2021
5a03842
fix compose-file
Dec 10, 2021
6119fe5
create wrapper class
Dec 13, 2021
1ac8877
make tests work!
Dec 13, 2021
0f3d41b
fix naming
Dec 13, 2021
50faa45
refactor test_erc20_wrapper_contract
Dec 13, 2021
4ed6d78
remove duplicated code
Dec 15, 2021
c3ce59b
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
Dec 15, 2021
e7d92fb
Merge branch '338_create_sol_price_provider' into 344_сreate_integrat…
Dec 15, 2021
090a7ec
Changes in base algorithm
Dec 16, 2021
ac839d6
simplify deployment
Dec 16, 2021
bbbf4d7
fix price estimation
Dec 16, 2021
36178d0
Merge remote-tracking branch 'origin/develop' into 344_сreate_integra…
Dec 16, 2021
768f33f
Merge branch '344_сreate_integration_tests_for_airdropper' into br_im…
Dec 16, 2021
ecad137
add test
Dec 17, 2021
c1b9ad9
edit assertions
Dec 17, 2021
d4b991d
fix tests
Dec 17, 2021
15e2505
comments
Dec 19, 2021
f57e474
Merge branch 'develop' into 390_improve_base_indexer_algorithm_perfor…
Dec 22, 2021
b06e55c
Merge branch 'develop' into 390_improve_base_indexer_algorithm_perfor…
Dec 23, 2021
6384d8d
Merge branch 'develop' into 390_improve_base_indexer_algorithm_perfor…
Jan 3, 2022
9b828cc
Fix after merge and rename methods
Jan 3, 2022
fc25fcb
remove unnecessary code
Jan 3, 2022
1d442d6
Fix tests
Jan 3, 2022
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
Prev Previous commit
Next Next commit
Changes in base algorithm
  • Loading branch information
ivanl committed Dec 16, 2021
commit 090a7ec7726a65da7300be8ea71cf5568375b0be
60 changes: 35 additions & 25 deletions proxy/indexer/airdropper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from web3 import eth
from proxy.indexer.indexer_base import IndexerBase, logger
from proxy.indexer.price_provider import PriceProvider, mainnet_solana, mainnet_price_accounts
from typing import List, Dict
import os
import requests
import base58
import json
import logging
from datetime import date, datetime

try:
from utils import check_error
Expand All @@ -17,6 +20,7 @@
AIRDROP_AMOUNT_SOL = ACCOUNT_CREATION_PRICE_SOL / 2
NEON_PRICE_USD = 0.25


class Airdropper(IndexerBase):
def __init__(self,
solana_url,
Expand All @@ -32,6 +36,7 @@ def __init__(self,
# collection of eth-address-to-create-accout-trx mappings
# for every addresses that was already funded with airdrop
self.airdrop_ready = SQLDict(tablename="airdrop_ready")
self.airdrop_scheduled = SQLDict(tablename="airdrop_scheduled")
self.wrapper_whitelist = wrapper_whitelist
self.faucet_url = faucet_url

Expand Down Expand Up @@ -72,15 +77,18 @@ def _check_transfer(self, account_keys, create_token_acc, token_transfer) -> boo
return account_keys[create_token_acc['accounts'][1]] == account_keys[token_transfer['accounts'][1]]


def _airdrop_to(self, create_acc):
def _schedule_airdrop(self, create_acc):
eth_address = "0x" + bytearray(base58.b58decode(create_acc['data'])[20:][:20]).hex()
if eth_address in self.airdrop_ready: # transaction already processed
return
self.airdrop_scheduled[eth_address] = { 'scheduled': datetime.now().timestamp() }


def _airdrop_to(self, eth_address, schedule_time):
sol_price_usd = self.price_provider.get_price('SOL/USD')
if sol_price_usd is None:
logger.warning("Failed to get SOL/USD price")
return
return False

logger.info(f'SOL/USD = ${sol_price_usd}')
airdrop_amount_usd = AIRDROP_AMOUNT_SOL * sol_price_usd
Expand All @@ -94,12 +102,15 @@ def _airdrop_to(self, create_acc):
resp = requests.post(self.faucet_url + '/request_neon_in_galans', json = json_data)
if not resp.ok:
logger.warning(f'Failed to airdrop: {resp.status_code}')
return
return False

self.airdrop_ready[eth_address] = create_acc
self.airdrop_ready[eth_address] = { 'amount': airdrop_galans,
'scheduled': schedule_time,
'finished': datetime.now().timestamp() }
return True


def process_trx_airdropper_mode(self, trx):
def _process_trx_airdropper_mode(self, trx):
if check_error(trx):
return

Expand Down Expand Up @@ -133,30 +144,29 @@ def find_instructions(trx, predicate):
for token_transfer in token_transfer_list:
if not self._check_transfer(account_keys, create_token_acc, token_transfer):
continue
self._airdrop_to(create_acc)
self._schedule_airdrop(create_acc)


def _process_scheduled_trxs(self):
success_addresses = set()
for eth_address, sched_info in self.airdrop_scheduled.items():
if not self._airdrop_to(eth_address, sched_info['scheduled']):
continue
success_addresses.add(eth_address)

for eth_address in success_addresses:
del self.airdrop_scheduled[eth_address]


def process_functions(self):
IndexerBase.process_functions(self)
logger.debug("Process receipts")
self.process_receipts()


def process_receipts(self):
counter = 0
for signature in self.transaction_order:
counter += 1
if signature in self.transaction_receipts:
trx = self.transaction_receipts[signature]
if trx is None:
logger.error("trx is None")
del self.transaction_receipts[signature]
continue
if 'slot' not in trx:
logger.debug("\n{}".format(json.dumps(trx, indent=4, sort_keys=True)))
exit()
if trx['transaction']['message']['instructions'] is not None:
self.process_trx_airdropper_mode(trx)
logger.debug("Airdropping...")
self._process_scheduled_trxs()


def handle_new_transactions(self, ordered_signs, receipts):
for signature in ordered_signs:
self._process_trx_airdropper_mode(receipts[signature])


def run_airdropper(solana_url,
Expand Down
26 changes: 26 additions & 0 deletions proxy/indexer/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import time
import logging
from multiprocessing.dummy import Pool as ThreadPool
from typing import List, Dict


try:
Expand Down Expand Up @@ -65,6 +66,8 @@ def __init__(self,
if 'last_block' not in self.constants:
self.constants['last_block'] = 0
self.blocked_storages = {}
self.transaction_order = []
self.transaction_receipts = SQLDict(tablename="known_transactions")


def process_functions(self):
Expand All @@ -78,6 +81,29 @@ def process_functions(self):
self.blocked_storages = {}


def handle_new_transactions(self, ordered_signs, receipts):
# Combine transactions received just now with previous ones
for signature in ordered_signs:
if signature not in self.transaction_receipts:
trx = receipts.get(signature, None)
if not trx: #Normally, this will not happen (means error in upper level logic)
logger.error(f'Signature {signature} not found in receipts dictionary')
continue
self.transaction_receipts[signature] = trx

# merge current transaction sequence with rest of the history
if len(self.transaction_order):
index = 0
try:
index = ordered_signs.index(self.transaction_order[0])
except ValueError:
self.transaction_order = ordered_signs + self.transaction_order
else:
self.transaction_order = ordered_signs[:index] + self.transaction_order
else:
self.transaction_order = ordered_signs


def process_receipts(self):
counter = 0
holder_table = {}
Expand Down
38 changes: 14 additions & 24 deletions proxy/indexer/indexer_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from solana.rpc.api import Client
from multiprocessing.dummy import Pool as ThreadPool
from typing import Dict, Union
from typing import Dict, List, Set, Union

try:
from sql_dict import SQLDict
Expand Down Expand Up @@ -40,10 +40,9 @@ def __init__(self,

self.evm_loader_id = evm_loader_id
self.client = Client(solana_url)
self.transaction_receipts = SQLDict(tablename="known_transactions")
self.last_slot = start_slot
self.current_slot = 0
self.transaction_order = []

self.counter_ = 0


Expand All @@ -62,7 +61,7 @@ def process_functions(self):

def gather_unknown_transactions(self):
poll_txs = set()
ordered_txs = []
ordered_signs = []

minimal_tx = None
continue_flag = True
Expand Down Expand Up @@ -93,10 +92,8 @@ def gather_unknown_transactions(self):
continue_flag = False
break

ordered_txs.append(solana_signature)

if solana_signature not in self.transaction_receipts:
poll_txs.add(solana_signature)
ordered_signs.append(solana_signature)
poll_txs.add(solana_signature)

if slot < minimal_slot:
minimal_slot = slot
Expand All @@ -111,33 +108,26 @@ def gather_unknown_transactions(self):

logger.debug("start getting receipts")
pool = ThreadPool(PARALLEL_REQUESTS)
pool.map(self.get_tx_receipts, poll_txs)

if len(self.transaction_order):
index = 0
try:
index = ordered_txs.index(self.transaction_order[0])
except ValueError:
self.transaction_order = ordered_txs + self.transaction_order
else:
self.transaction_order = ordered_txs[:index] + self.transaction_order
else:
self.transaction_order = ordered_txs
receipts = { entry[0]:entry[1] for entry in pool.map(self.get_tx_receipts, poll_txs) }

self.last_slot = maximum_slot
self.current_slot = current_slot

self.counter_ = 0

logger.debug("Start processing received receipts")
self.handle_new_transactions(ordered_signs, receipts)


def handle_new_transactions(self, ordered_signs, receipts): None


def get_tx_receipts(self, solana_signature):
# trx = None
trx = None
retry = True

while retry:
try:
trx = self.client.get_confirmed_transaction(solana_signature)['result']
self.transaction_receipts[solana_signature] = trx
retry = False
except Exception as err:
logger.debug(err)
Expand All @@ -147,4 +137,4 @@ def get_tx_receipts(self, solana_signature):
if self.counter_ % 100 == 0:
logger.debug(self.counter_)

# return (solana_signature, trx)
return (solana_signature, trx)