Skip to content

Commit

Permalink
test: Added extra checks into subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosvdr committed Jan 18, 2024
1 parent f90b6e7 commit 73c2336
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
25 changes: 25 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ def increment_nonce(address, nonce_data):
return nonce_data


def decrement_nonce(address, nonce_data):
nonce_data[address] -= 1
print(f"Nonce data: {nonce_data[address]}")
return nonce_data


@backoff.on_exception(backoff.expo, Exception, max_tries=MAX_TRIES)
def obtain_subgraph_info_backoff(endpoint, request_data, entity_to_check):
print(f"Checking subgraph data for {entity_to_check}")
Expand Down Expand Up @@ -256,3 +262,22 @@ def verify_data_in_subgraph_response(
if str(extracted_data_to_check) == str(expected_value):
return True, verified_transactions
return False, verified_transactions


@backoff.on_exception(backoff.expo, Exception, max_tries=MAX_TRIES)
def get_escrow_balance(sender, receiver, endpoint=subgraph_endpoint):
graphql_query = """
query($id: String!){
escrowAccounts(where:{id: $id}){
balance
}
}
"""
vars = {"id": sender.lower() + "-" + receiver.lower()}
request_data = {"query": graphql_query, "variables": vars}
resp = obtain_subgraph_info_backoff(endpoint, request_data, "escrowAccounts")
print(f" ==== Subgraph response ==== \n {resp.text}")

data = json.loads(resp.text)["data"]["escrowAccounts"]
print(data)
return data[0]["balance"]
38 changes: 32 additions & 6 deletions tests/testnet_contract_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
from web3.exceptions import ContractCustomError, ContractLogicError

from helpers import (TEST_NET_VARS, check_deployed_subgraph_transaction,
check_subgraph_signer, increment_nonce, init_nonce)
check_subgraph_escrow_account, check_subgraph_signer,
decrement_nonce, get_escrow_balance, increment_nonce,
init_nonce)

testnet = sys.argv[1]

Expand Down Expand Up @@ -210,7 +212,7 @@ def deposit_to_escrow(sender_account, receiver, amount, nonce_data):
)


def thaw(sender_account, receiver, amount, nonce_data):
def thaw(sender_account, receiver, amount, nonce_data, balance):
nonce_data = increment_nonce(sender_account.address, nonce_data)
thaw_txn = escrow_contract.functions.thaw(receiver, amount).build_transaction(
{
Expand All @@ -223,6 +225,13 @@ def thaw(sender_account, receiver, amount, nonce_data):

# Send escrow thaw
print("txn hash thaw: ", sign_and_send_tx(sender_account, thaw_txn).hex())
check_subgraph_escrow_account(
sender=sender_account.address,
receiver=receiver,
total_amount_thawing=amount,
balance=balance,
endpoint=SUBGRAPH_ENDPOINT,
)


@backoff.on_exception(backoff.expo, ValueError, max_tries=10)
Expand Down Expand Up @@ -393,7 +402,7 @@ def redeem_signed_rav(


@backoff.on_exception(backoff.expo, ValueError, max_tries=10)
def cancel_thaw(sender_account, receiver, nonce_data):
def cancel_thaw(sender_account, receiver, nonce_data, balance):
nonce_data = increment_nonce(sender_account.address, nonce_data)
thaw_txn = escrow_contract.functions.thaw(receiver, 0).build_transaction(
{
Expand All @@ -408,6 +417,13 @@ def cancel_thaw(sender_account, receiver, nonce_data):
thaw_txn = sign_and_send_tx(sender_account, thaw_txn).hex()

print("txn hash Cancel thaw: ", thaw_txn)
check_subgraph_escrow_account(
sender=sender_account.address,
receiver=receiver,
total_amount_thawing=0,
balance=balance,
endpoint=SUBGRAPH_ENDPOINT,
)


@backoff.on_exception(backoff.expo, ValueError, max_tries=10)
Expand Down Expand Up @@ -445,10 +461,15 @@ def cancel_thaw_signer(sender_account, signer, nonce_data):

# Only run this next line once
# stake_funds(INDEXER_ACCOUNT)

create_allocation(ALLOCATIONID_ACCOUNT, INDEXER_ACCOUNT, nonce_data)
deposit_to_escrow(GATEWAY_ACCOUNT, INDEXER_ACCOUNT.address, 100, nonce_data)
authorize_signer(GATEWAY_ACCOUNT, SIGNER_ACCOUNT.address, nonce_data, 86000)

try:
authorize_signer(GATEWAY_ACCOUNT, SIGNER_ACCOUNT.address, nonce_data, 86000)
except ValueError as e:
print("Signer already authorized")
nonce_data = decrement_nonce(GATEWAY_ACCOUNT.address, nonce_data)

msg = ReceiptAggregateVoucher(
allocationId=ALLOCATIONID_ACCOUNT.address,
Expand All @@ -461,12 +482,17 @@ def cancel_thaw_signer(sender_account, signer, nonce_data):

redeem_signed_rav(msg, rav_signature, INDEXER_ACCOUNT, nonce_data)

# Need to obtain current balance to corroborate data
current_balance = get_escrow_balance(
GATEWAY_ACCOUNT.address, INDEXER_ACCOUNT.address, SUBGRAPH_ENDPOINT
)

print("Thawing escrow account")
thaw(GATEWAY_ACCOUNT, INDEXER_ACCOUNT.address, 10, nonce_data)
thaw(GATEWAY_ACCOUNT, INDEXER_ACCOUNT.address, 10, nonce_data, current_balance)
print("Thawing signer")
thaw_signer(GATEWAY_ACCOUNT, SIGNER_ACCOUNT.address, nonce_data)
print("Cancelling escrow account thawing")
cancel_thaw(GATEWAY_ACCOUNT, INDEXER_ACCOUNT.address, nonce_data)
cancel_thaw(GATEWAY_ACCOUNT, INDEXER_ACCOUNT.address, nonce_data, current_balance)

print("Canel the Thawing of a signer")
cancel_thaw_signer(GATEWAY_ACCOUNT, SIGNER_ACCOUNT.address, nonce_data)
Expand Down

0 comments on commit 73c2336

Please sign in to comment.