Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contract unit tests #53

Merged
merged 38 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b410ac0
Add barge into test workflow
trizin Aug 17, 2023
75bbf75
Fix
trizin Aug 17, 2023
72ec628
Add token tests
trizin Aug 17, 2023
5f0044f
Wait for contracts deployment
trizin Aug 17, 2023
8af3c80
Add address file env
trizin Aug 17, 2023
b00897d
os.path.expanduser
trizin Aug 17, 2023
ceba4ae
Fixes
trizin Aug 18, 2023
cc327b5
Fix
trizin Aug 18, 2023
b8da4f4
test
trizin Aug 18, 2023
6fabf99
Update error message
trizin Aug 18, 2023
7cfde0a
Fixes
trizin Aug 18, 2023
3d29e42
Add conftest
trizin Aug 18, 2023
c86a7d6
Update tests
trizin Aug 18, 2023
5c22124
Refactoring publisher
trizin Aug 18, 2023
636477e
Formatting
trizin Aug 18, 2023
8dd823e
Add return
trizin Aug 18, 2023
265a0c3
Add test_PredictoorContract
trizin Aug 18, 2023
c7c34fa
Test
trizin Aug 18, 2023
c87548a
Fix
trizin Aug 18, 2023
b81b955
Add tests
trizin Aug 18, 2023
351d736
Fixes
trizin Aug 18, 2023
720b476
Improvements
trizin Aug 18, 2023
2e7028d
add web3 config fixture
trizin Aug 18, 2023
dbd442d
Add more tests!
trizin Aug 18, 2023
4427f1b
Fix get prediction
trizin Aug 18, 2023
dea7067
Fix ref
trizin Aug 18, 2023
b383f68
Fixes
trizin Aug 18, 2023
ec67b9b
Add ocean_token fixture
trizin Aug 18, 2023
d2ed5a5
Fixes and formatting
trizin Aug 18, 2023
5a9be64
Formatting
trizin Aug 18, 2023
58694a2
Fix
trizin Aug 18, 2023
2663437
More tests go!
trizin Aug 18, 2023
9894d67
Fix address file
trizin Aug 18, 2023
412eb42
Fixes
trizin Aug 18, 2023
87ef746
Fix tests
trizin Aug 18, 2023
a8b2340
Remove duplicate
trizin Aug 18, 2023
184228e
Delete redundant line
trizin Aug 18, 2023
f5f19b5
Don't check for ~
trizin Aug 18, 2023
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
35 changes: 31 additions & 4 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,47 @@ jobs:
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: '3.8'
python-version: "3.8"

- uses: actions/checkout@v2
name: Checkout Barge
with:
repository: "oceanprotocol/barge"
ref: "issue374-replace-pdr-components-with-pdr-backend"
path: "barge"

- name: Run Barge
working-directory: ${{ github.workspace }}/barge
env:
SUBGRAPH_VERSION: main
run: |
bash -x start_ocean.sh --no-aquarius --no-elasticsearch --no-provider --no-dashboard --with-thegraph --predictoor > start_ocean.log &

- name: Wait for contracts deployment
working-directory: ${{ github.workspace }}/barge
run: |
for i in $(seq 1 250); do
sleep 5
[ -f "$HOME/.ocean/ocean-contracts/artifacts/ready" ] && break
done

- name: Install dependencies
working-directory: ${{ github.workspace }}
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest
pip install pytest

- name: Set env variables
run: |
echo "ADDRESS_FILE=${{env.HOME}}/.ocean/ocean-contracts/artifacts/address.json" >> $GITHUB_ENV

- name: Test with pytest
id: pytest
env:
ADDRESS_FILE: "${{env.HOME}}/.ocean/ocean-contracts/artifacts/address.json"
RPC_URL: "http://127.0.0.1:8545"
SUBGRAPH_URL: "http://172.15.0.15:8000/subgraphs/name/oceanprotocol/ocean-subgraph"
PRIVATE_KEY: "0xef4b441145c1d0f3b4bc6d61d29f5c6e502359481152f869247c7a4244d45209"
run: |
coverage run --omit="*test*" -m pytest
coverage report
coverage report
19 changes: 18 additions & 1 deletion pdr_backend/publisher/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
from pdr_backend.publisher.publish import publish, fund_dev_accounts
from pdr_backend.utils.contract import (
DataNft,
Web3Config,
Token,
get_address,
)
from pdr_backend.utils import env

rpc_url = env.get_rpc_url_or_exit()
private_key = env.get_private_key_or_exit()
web3_config = Web3Config(rpc_url, private_key)
ocean_address = get_address(web3_config.w3.eth.chain_id, "Ocean")


OCEAN = Token(web3_config, ocean_address)

accounts_to_fund = [
# account_key_env, OCEAN_to_send
Expand All @@ -12,7 +26,7 @@
("PDR_MM_USER", 10000.0),
]

fund_dev_accounts(accounts_to_fund)
fund_dev_accounts(accounts_to_fund, web3_config.owner, OCEAN)

publish(
s_per_epoch=300,
Expand All @@ -25,6 +39,7 @@
feeCollector_addr="0xe2DD09d719Da89e5a3D0F2549c7E24566e947260",
rate=3,
cut=0.2,
web3_config=web3_config,
)

publish(
Expand All @@ -38,6 +53,7 @@
feeCollector_addr="0xe2DD09d719Da89e5a3D0F2549c7E24566e947260",
rate=3,
cut=0.2,
web3_config=web3_config,
)

publish(
Expand All @@ -51,4 +67,5 @@
feeCollector_addr="0xe2DD09d719Da89e5a3D0F2549c7E24566e947260",
rate=3,
cut=0.2,
web3_config=web3_config,
)
26 changes: 11 additions & 15 deletions pdr_backend/publisher/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,20 @@
from pdr_backend.utils.contract import (
DataNft,
ERC721Factory,
Web3Config,
Token,
get_address,
)
from pdr_backend.utils import env

rpc_url = env.get_rpc_url_or_exit()
private_key = env.get_private_key_or_exit()
web3_config = Web3Config(rpc_url, private_key)
owner = web3_config.owner
ocean_address = get_address(web3_config.w3.eth.chain_id, "Ocean")
fre_address = get_address(web3_config.w3.eth.chain_id, "FixedPrice")
factory = ERC721Factory(web3_config)
OCEAN = Token(web3_config, ocean_address)

MAX_UINT256 = 2**256 - 1


def fund_dev_accounts(accounts_to_fund):
def fund_dev_accounts(accounts_to_fund, owner, token):
for env_key, amount in accounts_to_fund:
if env_key in os.environ:
account = Account.from_key(os.getenv(env_key))
print(
f"Sending OCEAN to account defined by envvar key {env_key}, with address {account.address}"
)
OCEAN.transfer(account.address, amount * 1e18, owner)
token.transfer(account.address, amount * 1e18, owner)


def publish(
Expand All @@ -44,9 +32,14 @@ def publish(
feeCollector_addr,
rate,
cut,
web3_config,
):
pair = base + "/" + quote
trueval_timeout = 4 * 12 * s_per_epoch
owner = web3_config.owner
ocean_address = get_address(web3_config.w3.eth.chain_id, "Ocean")
fre_address = get_address(web3_config.w3.eth.chain_id, "FixedPrice")
factory = ERC721Factory(web3_config)

feeCollector = web3_config.w3.to_checksum_address(feeCollector_addr)
trueval_submiter = web3_config.w3.to_checksum_address(trueval_submitter_addr)
Expand Down Expand Up @@ -79,9 +72,10 @@ def publish(
[18, 18, rate, cut, 1],
)

data_nft_address = factory.createNftWithErc20WithFixedRate(
logs_nft, logs_erc = factory.createNftWithErc20WithFixedRate(
nft_data, erc_data, fre_data
)
data_nft_address = logs_nft["newTokenAddress"]
print(f"Deployed NFT: {data_nft_address}")
data_nft = DataNft(web3_config, data_nft_address)
tx = data_nft.set_data("pair", pair)
Expand All @@ -96,3 +90,5 @@ def publish(
print(f"timeframe set to {timeframe} in {tx.hex()}")
tx = data_nft.add_erc20_deployer(trueval_submiter)
print(f"Erc20Deployer set to {trueval_submiter} in {tx.hex()}")

return (nft_data, erc_data, fre_data, logs_nft, logs_erc)
22 changes: 14 additions & 8 deletions pdr_backend/utils/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def string_to_bytes32(self, data):
return bytes(myBytes32, "utf-8")

def get_auth_signature(self):
valid_until = int(time.time()) + 3600
valid_until = self.config.w3.eth.get_block("latest").timestamp + 3600
message_hash = self.config.w3.solidity_keccak(
["address", "uint256"],
[self.config.owner, valid_until],
Expand Down Expand Up @@ -419,10 +419,11 @@ def submit_prediction(
def get_trueValSubmitTimeout(self):
return self.contract_instance.functions.trueValSubmitTimeout().call()

def get_prediction(self, slot):
return self.contract_instance.functions.getPrediction(slot).call(
{"from": self.config.owner}
)
def get_prediction(self, slot: int, address: str):
auth_signature = self.get_auth_signature()
return self.contract_instance.functions.getPrediction(
slot, address, auth_signature
).call({"from": self.config.owner})

def submit_trueval(
self, true_val, timestamp, float_value, cancel_round, wait_for_receipt=True
Expand Down Expand Up @@ -501,10 +502,13 @@ def createNftWithErc20WithFixedRate(self, NftCreateData, ErcCreateData, FixedDat
if receipt["status"] != 1:
raise ValueError(f"createNftWithErc20WithFixedRate failed in {tx.hex()}")
# print(receipt)
logs = self.contract_instance.events.NFTCreated().process_receipt(
logs_nft = self.contract_instance.events.NFTCreated().process_receipt(
receipt, errors=DISCARD
)
logs_erc = self.contract_instance.events.TokenCreated().process_receipt(
receipt, errors=DISCARD
)
return logs[0]["args"]["newTokenAddress"]
return logs_nft[0]["args"], logs_erc[0]["args"]


class DataNft:
Expand Down Expand Up @@ -580,12 +584,13 @@ def get_addresses(chain_id):
address_filename = os.getenv("ADDRESS_FILE")
path = None
if address_filename:
address_filename = os.path.expanduser(address_filename)
path = Path(address_filename)
else:
path = Path(str(os.path.dirname(addresses.__file__)) + "/address.json")

if not path.exists():
raise TypeError("Cannot find address.json")
raise TypeError(f"Cannot find address.json file at {path}")

with open(path) as f:
data = json.load(f)
Expand Down Expand Up @@ -616,6 +621,7 @@ def get_contract_filename(contract_name):
address_filename = os.getenv("ADDRESS_FILE")
path = None
if address_filename:
calina-c marked this conversation as resolved.
Show resolved Hide resolved
address_filename = os.path.expanduser(address_filename)
address_dir = os.path.dirname(address_filename)
root_dir = os.path.join(address_dir, "..")
os.chdir(root_dir)
Expand Down
43 changes: 43 additions & 0 deletions pdr_backend/utils/test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
import pytest
from pdr_backend.utils.contract import (
Token,
Web3Config,
get_address,
)

_BARGE_ADDRESS_FILE = "~/.ocean/ocean-contracts/artifacts/address.json"


@pytest.fixture(autouse=True)
def barge_address_file():
os.environ["ADDRESS_FILE"] = os.path.expanduser(_BARGE_ADDRESS_FILE)


@pytest.fixture(scope="session")
def rpc_url():
return os.environ.get("TEST_RPC_URL", default="http://127.0.0.1:8545")


@pytest.fixture(scope="session")
def private_key():
return os.environ.get(
"TEST_PRIVATE_KEY",
default="0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58",
)


@pytest.fixture(scope="session")
def chain_id():
return 8996


@pytest.fixture(scope="session")
def web3_config(rpc_url, private_key):
return Web3Config(rpc_url, private_key)


@pytest.fixture(scope="session")
def ocean_token(web3_config, chain_id) -> Token:
token_address = get_address(chain_id, "Ocean")
return Token(web3_config, token_address)
Loading