Skip to content

Commit

Permalink
test: Added sepolia info to test
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosvdr committed Dec 11, 2023
1 parent 6f1c5d9 commit f90b6e7
Show file tree
Hide file tree
Showing 4 changed files with 3,304 additions and 3,278 deletions.
23 changes: 19 additions & 4 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"graphToken": "0x18C924BD5E8b83b47EFaDD632b7178E2Fd36073D",
"staking": "0xcd549d0C43d915aEB21d3a331dEaB9B7aF186D26",
"allocationIDTracker": "0x08779382cf862c88228F959b3D9140f35aDc28a4",
"eth_rpc": "wss://arbitrum-goerli.publicnode.com",
"eth_rpc": "https://arbitrum-goerli.publicnode.com",
"chainID": 421613,
"startBlock": 44335434,
"ravName": "tapVerifier",
Expand All @@ -41,12 +41,25 @@
"graphToken": "0x5c946740441C12510a167B447B7dE565C20b9E3C",
"staking": "0x35e3Cb6B317690d662160d5d02A5b364578F62c9",
"allocationIDTracker": "0xA65C99688a28bf91b59078298A6F5130c8aEf800",
"eth_rpc": "wss://ethereum-goerli.publicnode.com",
"eth_rpc": "https://ethereum-goerli.publicnode.com",
"chainID": 5,
"startBlock": 9765075,
"ravName": "TAP",
"ravVersion": "1",
},
"arbitrum-sepolia": {
"subgraph-endpoint": "https://api.studio.thegraph.com/query/53925/arb-sepolia-tap-subgraph/version/latest",
"escrow": "0x1e4dC4f9F95E102635D8F7ED71c5CdbFa20e2d02",
"tapVerifier": "0xfC24cE7a4428A6B89B52645243662A02BA734ECF",
"graphToken": "0xf8c05dCF59E8B28BFD5eed176C562bEbcfc7Ac04",
"staking": "0x865365C425f3A593Ffe698D9c4E6707D14d51e08",
"allocationIDTracker": "0xAaC28a10d707bbc6e02029f1bfDAEB5084b2aD11",
"eth_rpc": "https://arbitrum-sepolia.blockpi.network/v1/rpc/public",
"chainID": 421614,
"startBlock": 2164630,
"ravName": "TAP",
"ravVersion": "1",
},
}


Expand Down Expand Up @@ -86,11 +99,13 @@ def init_nonce(gateway, indexer, signer, receiver, w3):
signer: w3.eth.get_transaction_count(signer) - 1,
receiver: w3.eth.get_transaction_count(receiver) - 1,
}
print(f"Starting nonce data: {nonce_data}")
return nonce_data


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


Expand Down Expand Up @@ -164,7 +179,6 @@ def check_subgraph_escrow_account(


def error_in_signer_data(signer_data, thawing, authorized):

error = True
thaw_time_stamp = int(signer_data["thawEndTimestamp"])

Expand Down Expand Up @@ -201,12 +215,13 @@ def check_subgraph_signer(
raise Exception("Data in signer doesnt match expected value")


@backoff.on_exception(backoff.expo, ValueError, max_tries=MAX_TRIES)
def check_deployed_subgraph_transaction(
id, receiver, sender, amount, type, endpoint=subgraph_endpoint
):
graphql_query = """
query($id: String!, $sender: String!, $receiver: String!, $type: String!){
transactions(where: {id: $id, sender: $sender, receiver: $receiver, type: $type}) {
transactions(where: {transactionGroupID: $id, sender: $sender, receiver: $receiver, type: $type}) {
amount
}
}
Expand Down
27 changes: 11 additions & 16 deletions tests/local_contract_calls.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import json
import os
import sys
import time
import os

from eip712.messages import EIP712Message
from eth_account.messages import encode_defunct
from web3 import Web3
from web3.exceptions import ContractCustomError, ContractLogicError

from helpers import (
ALLOCATION_ID,
ALLOCATIONID_PK,
GATEWAY,
RECEIVER,
SIGNER,
SIGNER_PK,
check_subgraph_escrow_account,
check_subgraph_signer,
check_subgraph_transaction,
decode_custom_error,
time_remaining,
)
from helpers import (ALLOCATION_ID, ALLOCATIONID_PK, GATEWAY, RECEIVER, SIGNER,
SIGNER_PK, check_subgraph_escrow_account,
check_subgraph_signer, check_subgraph_transaction,
decode_custom_error, time_remaining)

# This script will help test that the subgraph is actually catching the required information
ESCROW_ADDRESS = sys.argv[1]
Expand Down Expand Up @@ -113,10 +104,14 @@ class ReceiptAggregateVoucher(EIP712Message):
eth_signed_message = encode_defunct(hexstr=message_hash.hex())

# Sign the message with the private key
signature = w3.eth.account.sign_message(eth_signed_message, private_key=ALLOCATIONID_PK)
signature = w3.eth.account.sign_message(
eth_signed_message, private_key=ALLOCATIONID_PK
)
arbitraryBytes32 = os.urandom(32)
print("Allocate receiver with allocationid")
mockStaking.functions.allocate(arbitraryBytes32, 1000, ALLOCATION_ID, arbitraryBytes32, RECEIVER).transact({"from": RECEIVER})
mockStaking.functions.allocate(
arbitraryBytes32, 1000, ALLOCATION_ID, arbitraryBytes32, RECEIVER
).transact({"from": RECEIVER})
except ContractCustomError as e:
raise ContractCustomError(decode_custom_error(mockStaking_abi_json, str(e), w3))
except ContractLogicError as e:
Expand Down
77 changes: 53 additions & 24 deletions tests/testnet_contract_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from web3 import Web3
from web3.exceptions import ContractCustomError, ContractLogicError

from helpers import (TEST_NET_VARS, add_nonce,
check_deployed_subgraph_transaction,
check_subgraph_signer, init_nonce)
from helpers import (TEST_NET_VARS, check_deployed_subgraph_transaction,
check_subgraph_signer, increment_nonce, init_nonce)

testnet = sys.argv[1]

w3 = Web3(Web3.WebsocketProvider(TEST_NET_VARS[testnet]["eth_rpc"]))
w3 = Web3(Web3.HTTPProvider(TEST_NET_VARS[testnet]["eth_rpc"]))
# w3 = Web3(Web3.WebsocketProvider(TEST_NET_VARS[testnet]["eth_rpc"]))
w3.eth.account.enable_unaudited_hdwallet_features()

AMOUNT_TO_REDEEM = 5
Expand Down Expand Up @@ -87,6 +87,38 @@ class ReceiptAggregateVoucher(EIP712Message):
valueAggregate: "uint128"


def stake_funds(indexer_account):
MIN_STAKE_AMOUNT = int("152d02c7e14af6800000", 16)

# Prepare the transaction for ERC20 approval
approve_txn = erc20_contract.functions.approve(
STAKING_ADDRESS, MIN_STAKE_AMOUNT
).build_transaction(
{
"chainId": CHAIN_ID,
"gas": 350000, # you might want to adjust this value based on the real gas consumption
"gasPrice": w3.to_wei("20", "gwei"),
"nonce": w3.eth.get_transaction_count(indexer_account.address),
}
)

# Send ERC20 approval
print("txn hash approve: ", sign_and_send_tx(indexer_account, approve_txn).hex())

allocate_txn = mock_staking_contract.functions.stake(
MIN_STAKE_AMOUNT
).build_transaction(
{
"chainId": CHAIN_ID,
"gas": 350000, # you might want to adjust this value based on the real gas consumption
"gasPrice": w3.to_wei("20", "gwei"),
"nonce": w3.eth.get_transaction_count(indexer_account.address),
}
)
# Send stake
print("txn hash stake: ", sign_and_send_tx(indexer_account, allocate_txn).hex())


def create_allocation(allocation_account, indexer_account, nonce_data):
# Create a keccak256 hash of the receiver and allocationID
message_hash = Web3.solidity_keccak(
Expand All @@ -100,7 +132,7 @@ def create_allocation(allocation_account, indexer_account, nonce_data):
signature = allocation_account.sign_message(eth_signed_message)

print("Broadcasting allocation")
nonce_data = add_nonce(indexer_account.address, nonce_data)
nonce_data = increment_nonce(indexer_account.address, nonce_data)
# create random bytes32 for metadata and subgraphDeploymentID
arbitraryBytes32 = os.urandom(32)
allocate_txn = mock_staking_contract.functions.allocate(
Expand Down Expand Up @@ -137,27 +169,24 @@ def sign_and_send_tx(account, transaction):
return w3.eth.send_raw_transaction(signed_txn.rawTransaction)


@backoff.on_exception(backoff.expo, ValueError, max_tries=10)
def deposit_to_escrow(sender_account, receiver, amount, nonce_data):
nonce_data = add_nonce(sender_account.address, nonce_data)
nonce_data = increment_nonce(sender_account.address, nonce_data)
# Prepare the transaction for ERC20 approval
approve_txn = erc20_contract.functions.approve(
ESCROW_ADDRESS, amount
ESCROW_ADDRESS, amount + 10000
).build_transaction(
{
"chainId": CHAIN_ID,
"gas": 550000, # you might want to adjust this value based on the real gas consumption
"gas": 5500000, # you might want to adjust this value based on the real gas consumption
"gasPrice": w3.to_wei("20", "gwei"),
"nonce": nonce_data[sender_account.address],
}
)

# Send ERC20 approval
print(
"txn hash aprove: ", sign_and_send_tx(sender_account, approve_txn).hex()
)
print("txn hash aprove: ", sign_and_send_tx(sender_account, approve_txn).hex())

nonce_data = add_nonce(sender_account.address, nonce_data)
nonce_data = increment_nonce(sender_account.address, nonce_data)
deposit_txn = escrow_contract.functions.deposit(receiver, amount).build_transaction(
{
"chainId": CHAIN_ID,
Expand All @@ -182,8 +211,7 @@ def deposit_to_escrow(sender_account, receiver, amount, nonce_data):


def thaw(sender_account, receiver, amount, nonce_data):

nonce_data = add_nonce(sender_account.address, nonce_data)
nonce_data = increment_nonce(sender_account.address, nonce_data)
thaw_txn = escrow_contract.functions.thaw(receiver, amount).build_transaction(
{
"chainId": CHAIN_ID,
Expand All @@ -199,8 +227,7 @@ def thaw(sender_account, receiver, amount, nonce_data):

@backoff.on_exception(backoff.expo, ValueError, max_tries=10)
def thaw_signer(sender_account, signer, nonce_data):

nonce_data = add_nonce(sender_account.address, nonce_data)
nonce_data = increment_nonce(sender_account.address, nonce_data)
thaw_txn = escrow_contract.functions.thawSigner(signer).build_transaction(
{
"chainId": CHAIN_ID,
Expand All @@ -224,8 +251,7 @@ def thaw_signer(sender_account, signer, nonce_data):


def withdraw(sender_account, receiver, nonce_data):

nonce_data = add_nonce(sender_account.address, nonce_data)
nonce_data = increment_nonce(sender_account.address, nonce_data)
withdraw_txn = escrow_contract.functions.withdraw(receiver).build_transaction(
{
"chainId": CHAIN_ID,
Expand Down Expand Up @@ -261,7 +287,7 @@ def authorize_signer(sender_account, signer, nonce_data, time_delta=86000):
encode_data, private_key=SIGNER_ACCOUNT.key
)

nonce_data = add_nonce(sender_account.address, nonce_data)
nonce_data = increment_nonce(sender_account.address, nonce_data)
# Prepare the transaction for escrow deposit
authorize_transaction = escrow_contract.functions.authorizeSigner(
signer, timestamp, signature_authorization.signature
Expand Down Expand Up @@ -299,7 +325,7 @@ def unauthorize_signer(sender_account, signer, nonce_data, time_delta=86000):
encode_data, private_key=SIGNER_ACCOUNT.key
)

nonce_data = add_nonce(sender_account.address, nonce_data)
nonce_data = increment_nonce(sender_account.address, nonce_data)
authorize_transaction = escrow_contract.functions.revokeAuthorizedSigner(
signer
).build_transaction(
Expand Down Expand Up @@ -340,7 +366,7 @@ def redeem_signed_rav(
allocation_id_digest, private_key=ALLOCATIONID_ACCOUNT.key
)

nonce_data = add_nonce(receiver_account.address, nonce_data)
nonce_data = increment_nonce(receiver_account.address, nonce_data)
redeem_transaction = escrow_contract.functions.redeem(
((rav.allocationId, rav.timestampNs, rav.valueAggregate), rav_signature),
signature_proof.signature,
Expand Down Expand Up @@ -368,7 +394,7 @@ def redeem_signed_rav(

@backoff.on_exception(backoff.expo, ValueError, max_tries=10)
def cancel_thaw(sender_account, receiver, nonce_data):
nonce_data = add_nonce(sender_account.address, nonce_data)
nonce_data = increment_nonce(sender_account.address, nonce_data)
thaw_txn = escrow_contract.functions.thaw(receiver, 0).build_transaction(
{
"chainId": CHAIN_ID,
Expand All @@ -386,7 +412,7 @@ def cancel_thaw(sender_account, receiver, nonce_data):

@backoff.on_exception(backoff.expo, ValueError, max_tries=10)
def cancel_thaw_signer(sender_account, signer, nonce_data):
nonce_data = add_nonce(sender_account.address, nonce_data)
nonce_data = increment_nonce(sender_account.address, nonce_data)
thaw_txn = escrow_contract.functions.cancelThawSigner(signer).build_transaction(
{
"chainId": CHAIN_ID,
Expand Down Expand Up @@ -416,6 +442,9 @@ def cancel_thaw_signer(sender_account, signer, nonce_data):
RECEIVER_ACCOUNT.address,
w3,
)

# 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)
Expand Down
Loading

0 comments on commit f90b6e7

Please sign in to comment.