Skip to content

Commit

Permalink
[ECO-1571] Add abstracted unit testing (#37)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt <[email protected]>
  • Loading branch information
alnoki and xbtmatt authored May 3, 2024
1 parent 24e9017 commit ef812e5
Show file tree
Hide file tree
Showing 12 changed files with 796 additions and 584 deletions.
4 changes: 2 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ PR.
- [ ] Did you update relevant documentation?
- [ ] Did you add tests to cover new code or a fixed issue?
- [ ] Did you update the changelog?
- [ ] Did you check off all checkboxes from the linked Linear task? (Ignore if
you are not a member of Econia Labs)
- [ ] Did you check off all checkboxes from the linked Linear task?
(Ignore if you are not a member of Econia Labs)

> If a task does not apply to your PR, strike it through and mark it complete:
Expand Down
16 changes: 4 additions & 12 deletions src/move/coin_factory/Move.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
[addresses]
emojicoin_dot_fun = "_"
market_address = "_"
coin_factory = "_"

[dependencies.AptosFramework]
[dev-dependencies.AptosFramework]
git = "https://github.com/aptos-labs/aptos-core.git"
rev = "mainnet"
subdir = "aptos-move/framework/aptos-framework"

[dev-addresses]
emojicoin_dot_fun = "0xcccccccc"
market_address = "0xbbbbbbbb"

[dev-dependencies.EmojicoinDotFun]
local = "../emojicoin_dot_fun"

[package]
name = "CoinFactory"
upgrade_policy = "compatible"
name = "EmojicoinDotFunCoinFactory"
upgrade_policy = "immutable"
version = "1.0.0"
36 changes: 12 additions & 24 deletions src/move/coin_factory/publish_and_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,15 @@
import os
import subprocess
from pathlib import Path
from typing import Sequence

MARKET_ADDRESS = "market_address"
EMOJICOIN_DOT_FUN = "emojicoin_dot_fun"
FLAG_ADDRESS = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
MARKET_NAMED_ADDRESS = "market_address"
COIN_FACTORY_NAMED_ADDRESS = "coin_factory"
PACKAGE_BYTECODE_PATH = "json/build_publish_payload.json"
SPLIT_BYTECODE_PATH = "json/split_bytecode.json"
METADATA_K, CODE_K = "metadata", "code"

# Mapping of named addresses to placeholder addresses for generating Move bytecode.
named_addresses = [
# market_address
"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
# emojicoin_dot_fun
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
]
NAMED_ADDRESSES = {
MARKET_ADDRESS: named_addresses[0],
EMOJICOIN_DOT_FUN: named_addresses[1],
}


def ensure_parent_directories_exist(s: str) -> Path:
fp = Path(s)
Expand Down Expand Up @@ -61,7 +51,7 @@ def split_and_replace_named_addresses(
return lines_to_replace


def compare_contents_and_log(fp: Path, new_data: dict[str, list[str]]) -> None:
def compare_contents_and_log(fp: Path, new_data: dict[str, Sequence[str]]) -> None:
metadata_bytes_changed = False
module_bytes_changed = False

Expand All @@ -86,7 +76,6 @@ def compare_contents_and_log(fp: Path, new_data: dict[str, list[str]]) -> None:
# - Run this script from the coin factory module directory (where the `Move.toml` is)
if __name__ == "__main__":
json_path = ensure_parent_directories_exist(PACKAGE_BYTECODE_PATH)
named_addresses_args = ",".join([f"{k}={v}" for k, v in NAMED_ADDRESSES.items()])
try:
file_contents_last_updated = os.path.getmtime(PACKAGE_BYTECODE_PATH)
except FileNotFoundError:
Expand All @@ -99,7 +88,7 @@ def compare_contents_and_log(fp: Path, new_data: dict[str, list[str]]) -> None:
"--json-output-file",
json_path,
"--named-addresses",
named_addresses_args,
f"{COIN_FACTORY_NAMED_ADDRESS}={FLAG_ADDRESS}",
"--assume-yes",
"--included-artifacts=none",
],
Expand Down Expand Up @@ -127,22 +116,21 @@ def compare_contents_and_log(fp: Path, new_data: dict[str, list[str]]) -> None:
if metadata.startswith("0x"):
metadata = metadata[2:]

metadata_lines = split_and_replace_named_addresses([metadata], NAMED_ADDRESSES)

bytecode_array: list[str] = cli_json_publish_output["args"][1]["value"]
assert len(bytecode_array) == 1
bytecode = bytecode_array[0]
len_bytecode = len(bytecode_array[0])
if bytecode.startswith("0x"):
bytecode = bytecode[2:]

assert NAMED_ADDRESSES[MARKET_ADDRESS] not in metadata
assert NAMED_ADDRESSES[MARKET_ADDRESS] in bytecode
assert FLAG_ADDRESS not in metadata

bytecode_lines = split_and_replace_named_addresses([bytecode], NAMED_ADDRESSES)
bytecode_lines = split_and_replace_named_addresses(
[bytecode], {MARKET_NAMED_ADDRESS: FLAG_ADDRESS}
)

new_data: dict[str, list[str]] = {
METADATA_K: metadata_lines,
new_data = {
METADATA_K: metadata,
CODE_K: bytecode_lines,
}
split_bytecode_path = ensure_parent_directories_exist(SPLIT_BYTECODE_PATH)
Expand Down
21 changes: 1 addition & 20 deletions src/move/coin_factory/sources/coin_factory.move
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
module market_address::coin_factory {
module coin_factory::coin_factory {
struct Emojicoin {}
struct EmojicoinLP {}

#[test_only] use aptos_std::type_info::{Self};
#[test_only] use emojicoin_dot_fun::emojicoin_dot_fun;

#[test]
fun test_type_info_consts_correct() {
let (module_name, emojicoin_struct, emojicoin_lp_struct) =
emojicoin_dot_fun::get_COIN_FACTORY_TYPE_CONSTANTS();

let emojicoin_type_info = type_info::type_of<Emojicoin>();
let lp_type_info = type_info::type_of<EmojicoinLP>();

assert!(@market_address == type_info::account_address(&emojicoin_type_info), 0);
assert!(@market_address == type_info::account_address(&lp_type_info), 0);
assert!(module_name == type_info::module_name(&emojicoin_type_info), 0);
assert!(module_name == type_info::module_name(&lp_type_info), 0);
assert!(emojicoin_struct == type_info::struct_name(&emojicoin_type_info), 0);
assert!(emojicoin_lp_struct == type_info::struct_name(&lp_type_info), 0);
}
}
4 changes: 4 additions & 0 deletions src/move/emojicoin_dot_fun/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rev = "mainnet"
subdir = "aptos-move/framework/aptos-framework"

[dev-addresses]
coin_factory = "0xaaaa"
emojicoin_dot_fun = "0xc0de"

[dev-dependencies.BlackCatCoinFactory]
Expand All @@ -15,6 +16,9 @@ local = "../test_coin_factories/black_cat"
[dev-dependencies.BlackHeartCoinFactory]
local = "../test_coin_factories/black_heart"

[dev-dependencies.EmojicoinDotFunCoinFactory]
local = "../coin_factory"

[dev-dependencies.YellowHeartCoinFactory]
local = "../test_coin_factories/yellow_heart"

Expand Down
Loading

0 comments on commit ef812e5

Please sign in to comment.