Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
Signed-off-by: carlos.vdr <[email protected]>
  • Loading branch information
carlosvdr committed Oct 25, 2023
1 parent bbe0c6c commit b96375a
Show file tree
Hide file tree
Showing 5 changed files with 3,452 additions and 3,244 deletions.
13 changes: 6 additions & 7 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,29 @@ jobs:
runs-on: ubuntu-latest
name: Build and Test
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
name: Check out repository
with:
submodules: recursive
- name: Checkout submodules
run: git submodule update --init --recursive
- uses: actions/setup-node@v2
name: Set up Node.js
with:
node-version: "lts/gallium"
- name: Install graph CLI
run: |
npm install -g @graphprotocol/graph-cli
run: npm install -g @graphprotocol/graph-cli
- uses: actions/setup-python@v4
with:
python-version: '3.8'
cache: pip
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
- name: Start Docker Compose
run: |
docker-compose up -d
run: docker-compose up -d
working-directory: ./tests
- name: Installing python requirements
run: |
pip install -r requirements.txt
run: pip install -r requirements.txt
working-directory: ./tests
- run: |
yarn
Expand Down
3 changes: 1 addition & 2 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dataSources:
name: Escrow
network: mainnet
source:
address: '0x63De3720298826EB4EC14F1214b33a2808e5DADf'
address: '0x45F97e4bEe19580807501C32a71D06f7F7c72f4f'
abi: Escrow
startBlock: 0
mapping:
Expand All @@ -34,4 +34,3 @@ dataSources:
handler: handleSignerAuthorization
- event: RevokeAuthorizedSigner(indexed address,indexed address)
handler: handleRevokeSignerAuthorization

2 changes: 1 addition & 1 deletion tests/contract_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ yarn deploy-local

cd $current_dir
echo "Running escrow contract calls"
python contract_calls.py "$ESCROW_AD" "$TAP_VERIFIER_AD" "$GRAPH_TOKEN" "$ISTAKING_AD"
python local_contract_calls.py "$ESCROW_AD" "$TAP_VERIFIER_AD" "$GRAPH_TOKEN" "$ISTAKING_AD"

if [ $? -ne 0 ]; then
exit 1
Expand Down
223 changes: 223 additions & 0 deletions tests/local_contract_calls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import json
import sys
import time

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)

# This script will help test that the subgraph is actually catching the required information
ESCROW_ADDRESS = sys.argv[1]
TAP_ADDRESS = sys.argv[2]
GRAPH_TOKEN = sys.argv[3]
STAKING_ADDRESS = sys.argv[4]
verified_transactions = []

w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545"))


class ReceiptAggregateVoucher(EIP712Message):
_chainId_ = 1337
_name_ = "tapVerifier"
_verifyingContract_ = TAP_ADDRESS
_version_ = "1.0"

allocationId: "address"
timestampNs: "uint64"
valueAggregate: "uint128"


msg = ReceiptAggregateVoucher(
allocationId=ALLOCATION_ID, timestampNs=1691694025, valueAggregate=5
)

# EIP712
rav_signature = w3.eth.account.sign_message(
msg.signable_message, private_key=SIGNER_PK
).signature

# Normal proof
message_hash = Web3.solidity_keccak(
["uint256", "address", "address", "address"],
[1337, GATEWAY, ALLOCATION_ID, ESCROW_ADDRESS],
)
allocation_id_digest = encode_defunct(message_hash)
signature_proof = w3.eth.account.sign_message(
allocation_id_digest, private_key=ALLOCATIONID_PK
)
# print("SIGNATURE PROOOF: ", signature_proof.signature.hex())

# RAV
rav = (ALLOCATION_ID, 1691694025, 5)
signedRAV = (rav, rav_signature)
# print("RAV SIGNATURE", rav_signature.hex())
timer = int(time.time()) + 86400

# Authorization
hashed_data = Web3.solidity_keccak(
["uint256", "uint256", "address"], [1337, timer, GATEWAY]
)
encode_data = encode_defunct(hashed_data)
signature_authorization = w3.eth.account.sign_message(
encode_data, private_key=SIGNER_PK
)

# Load abi to make calls to the Escrow contract
escrow_abi = open("../abis/Escrow.abi.json")
escrow_abi_json = json.load(escrow_abi)
escrow = w3.eth.contract(address=ESCROW_ADDRESS, abi=escrow_abi_json)

# Load MockStacking
mockStaking_abi = open("../abis/MockStaking.abi.json")
mockStaking_abi_json = json.load(mockStaking_abi)
mockStaking = w3.eth.contract(address=STAKING_ADDRESS, abi=mockStaking_abi_json)

# Load ERC20
erc20_abi = open("../abis/ERC20.abi.json")
erc20_abi_json = json.load(erc20_abi)
erc20 = w3.eth.contract(address=GRAPH_TOKEN, abi=erc20_abi_json)

# ERC20 CONTRACT CALLS
try:
print("Approving escrow contracts")
erc20.functions.approve(ESCROW_ADDRESS, 10000).transact({"from": GATEWAY})
erc20.functions.approve(ESCROW_ADDRESS, 10000).transact({"from": SIGNER})
print("Making sure all addresses have available founds")
erc20.functions.transfer(SIGNER, 500).transact({"from": GATEWAY})
erc20.functions.transfer(RECEIVER, 500).transact({"from": GATEWAY})
except ContractCustomError as e:
print("Custom Error: %s" % e)
print(decode_custom_error(erc20_abi_json, str(e), w3))
except ContractLogicError as e:
print("Logic Error: %s" % e)

# MOCK STAKING CONTRACT CALLS
try:
print("Allocate receiver with allocationid")
mockStaking.functions.allocate(ALLOCATION_ID, RECEIVER).transact({"from": GATEWAY})
except ContractCustomError as e:
print("Custom Error: %s" % e)
print(decode_custom_error(mockStaking_abi_json, str(e), w3))
except ContractLogicError as e:
print("Logic Error: %s" % e)

# ESCROW CONTRACT CALLS
try:
print("--- Starting with deposits")
txn = escrow.functions.deposit(RECEIVER, 15).transact({"from": GATEWAY})
print(f"Deposit txn hash: {txn.hex()}")
check_subgraph_transaction(txn.hex(), GATEWAY, RECEIVER, "deposit", 15)
check_subgraph_escrow_account(GATEWAY, RECEIVER, 0, 15)

txn = escrow.functions.deposit(RECEIVER, 33).transact({"from": SIGNER})
print(f"Deposit txn hash: {txn.hex()}")
check_subgraph_transaction(txn.hex(), GATEWAY, RECEIVER, "deposit", 33)
check_subgraph_escrow_account(SIGNER, RECEIVER, 0, 33)

txn = escrow.functions.deposit(SIGNER, 44).transact({"from": GATEWAY})
print(f"Deposit txn hash: {txn.hex()}")
check_subgraph_transaction(txn.hex(), GATEWAY, RECEIVER, "deposit", 44)
check_subgraph_escrow_account(GATEWAY, SIGNER, 0, 44)

print("--- Start thawing")
escrow.functions.thaw(SIGNER, 10).transact({"from": GATEWAY})
check_subgraph_escrow_account(GATEWAY, SIGNER, 10, 44)

print("--- Cancelling thaw")
escrow.functions.thaw(SIGNER, 0).transact({"from": GATEWAY})
check_subgraph_escrow_account(GATEWAY, SIGNER, 0, 44)

print("--- Start thawing again")
escrow.functions.thaw(SIGNER, 10).transact({"from": GATEWAY})
check_subgraph_escrow_account(GATEWAY, SIGNER, 10, 44)

thawing = True
while thawing:
try:
print("--- Trying to withdraw")
txn = escrow.functions.withdraw(SIGNER).transact({"from": GATEWAY})
thawing = False
except ContractCustomError as e:
error = decode_custom_error(escrow_abi_json, str(e), w3)
if "EscrowStillThawing" in error:
time_left = time_remaining(error)
print(f"Escrow still thawing, time left: {time_left}")
time.sleep(time_left + 1)
print(f"Withdraw txn hash: {txn.hex()}")
print("--- Amount withdrawn")
check_subgraph_transaction(txn.hex(), GATEWAY, RECEIVER, "withdraw", 10)
check_subgraph_escrow_account(GATEWAY, SIGNER, 0, 34)

print("--- Approving")
escrow.functions.approveAll().transact({"from": GATEWAY})
print("--- Approving signer")
try:
escrow.functions.authorizeSigner(
SIGNER, timer, signature_authorization.signature
).transact({"from": GATEWAY})
except ContractCustomError as e:
error = decode_custom_error(escrow_abi_json, str(e), w3)
if "SignerAlreadyAuthorized" in error:
print("Skip, signer already authorized")
else:
print(error)
check_subgraph_signer(SIGNER, True, False)

print("--- Executing deposit for redeem")
txn = escrow.functions.deposit(RECEIVER, 12).transact({"from": GATEWAY})
print(f"Deposit txn hash: {txn.hex()}")
check_subgraph_transaction(txn.hex(), GATEWAY, RECEIVER, "deposit", 12)
check_subgraph_escrow_account(GATEWAY, RECEIVER, 0, 27)

print("--- Executing redeem")
txn = escrow.functions.redeem(signedRAV, signature_proof.signature).transact(
{"from": RECEIVER}
)
print(f"Redeem txn hash: {txn.hex()}")
check_subgraph_transaction(txn.hex(), GATEWAY, RECEIVER, "redeem", 5)
check_subgraph_escrow_account(GATEWAY, RECEIVER, 0, 22)

print("--- Thawing signer")
escrow.functions.thawSigner(SIGNER).transact({"from": GATEWAY})
check_subgraph_signer(SIGNER, True, True)

print("--- Cancelling thaw signer")
escrow.functions.cancelThawSigner(SIGNER).transact({"from": GATEWAY})
check_subgraph_signer(SIGNER, True, False)

print("--- Thawing signer again")
escrow.functions.thawSigner(SIGNER).transact({"from": GATEWAY})
check_subgraph_signer(SIGNER, True, True)

thawing = True
while thawing:
try:
print("--- Trying to revoke signer")
escrow.functions.revokeAuthorizedSigner(SIGNER).transact({"from": GATEWAY})
thawing = False
except ContractCustomError as e:
error = decode_custom_error(escrow_abi_json, str(e), w3)
if "SignerStillThawing" in error:
time_left = time_remaining(error)
print(f"Signer still thawing, time left: {time_left}")
time.sleep(time_left + 1)
else:
print(error)
print("Done revoking")

check_subgraph_signer(SIGNER, False, False)

print("Transactions ran succesfully")
except ContractCustomError as e:
print("Custom Error: %s" % e)
print(decode_custom_error(escrow_abi_json, str(e), w3))
except ContractLogicError as e:
print("Logic Error: %s" % e)
except Exception as e:
print(e)
Loading

0 comments on commit b96375a

Please sign in to comment.