Skip to content

Commit

Permalink
♻️ mutualize config data
Browse files Browse the repository at this point in the history
- create config file at each folder level
- create a python file to mutualize hints code
  • Loading branch information
bal7hazar committed Aug 25, 2022
1 parent 30276e5 commit f35de9f
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 104 deletions.
43 changes: 43 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import yaml
import collections.abc
from collections import namedtuple
from pathlib import Path


class Object():
pass


def update(ref: dict, new: dict) -> dict:
"""Update ref recursively with new."""
for key, value in new.items():
if isinstance(value, collections.abc.Mapping):
ref[key] = update(ref.get(key, {}), value)
else:
ref[key] = value
return ref


def objectify(parent: object, attributes: dict) -> object:
"""Set attributes recursively to parent object."""
for key, value in attributes.items():
if isinstance(value, dict):
value = objectify(Object(), value)
setattr(parent, key, value)
return parent


def load(path: str, context: object):
"""Read config files and setup context."""
# load config
path = Path(path)
config = {}
for parent in path.parents:
config_path = parent / path.name
if not config_path.exists():
continue
with open(config_path, 'r') as file_instance:
update(config, yaml.safe_load(file_instance))

# set up context
context = objectify(context, config)
3 changes: 3 additions & 0 deletions tests/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
signers:
admin: 1000
anyone: 1001
9 changes: 9 additions & 0 deletions tests/integrations/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
project:
name: Project
symbol: NFT

token:
name: Token
symbol: TOK
decimals: 6
initial_supply: 1000000
14 changes: 0 additions & 14 deletions tests/integrations/minter/config.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
user:
admin: 1000
anyone: 1001

nft:
name: Project
symbol: NFT

token:
name: Token
symbol: TOK
decimals: 6
initial_supply: 1000000

minter:
public_sale_open: 0
max_buy_per_tx: 5
Expand Down
51 changes: 24 additions & 27 deletions tests/integrations/minter/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,51 @@ func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}():
local merkle_root
%{
# Load config
import yaml
with open("./tests/integrations/minter/config.yml", 'r') as file_instance:
config = yaml.safe_load(file_instance)
for section, subconfig in config.items():
for key, value in subconfig.items():
name = f"{section.lower()}_{key.lower()}"
setattr(context, name, value)
import sys
sys.path.append('.')
from tests import load
load("./tests/integrations/minter/config.yml", context)
# ERC-721 deployment
context.project_nft_contract = deploy_contract(
"./src/nft/project/CarbonableProjectNFT.cairo",
{
"name": context.nft_name,
"symbol": context.nft_symbol,
"owner": context.user_admin,
"name": context.project.name,
"symbol": context.project.symbol,
"owner": context.signers.admin,
},
).contract_address
# ERC-20 deployment
context.payment_token_contract = deploy_contract(
"./tests/mocks/token/erc20.cairo",
{
"name": context.token_name,
"symbol": context.token_symbol,
"decimals": context.token_decimals,
"initial_supply": context.token_initial_supply,
"recipient": context.user_anyone
"name": context.token.name,
"symbol": context.token.symbol,
"decimals": context.token.decimals,
"initial_supply": context.token.initial_supply,
"recipient": context.signers.anyone
},
).contract_address
# Minter deployment
context.carbonable_minter_contract = deploy_contract(
"./src/mint/minter.cairo",
{
"owner": context.user_admin,
"owner": context.signers.admin,
"project_nft_address": context.project_nft_contract,
"payment_token_address": context.payment_token_contract,
"public_sale_open": context.minter_public_sale_open,
"max_buy_per_tx": context.minter_max_buy_per_tx,
"unit_price": context.minter_unit_price,
"max_supply_for_mint": context.minter_max_supply_for_mint,
"reserved_supply_for_mint": context.minter_reserved_supply_for_mint,
"public_sale_open": context.minter.public_sale_open,
"max_buy_per_tx": context.minter.max_buy_per_tx,
"unit_price": context.minter.unit_price,
"max_supply_for_mint": context.minter.max_supply_for_mint,
"reserved_supply_for_mint": context.minter.reserved_supply_for_mint,
},
).contract_address
# Externalize required variables
ids.carbonable_minter = context.carbonable_minter_contract
ids.merkle_root = context.whitelist_merkle_root
ids.merkle_root = context.whitelist.merkle_root
%}

# Transfer project nft ownershop from admin to minter
Expand Down Expand Up @@ -340,7 +337,7 @@ namespace admin_instance:

func get_address() -> (address : felt):
tempvar admin
%{ ids.admin = context.user_admin %}
%{ ids.admin = context.signers.admin %}
return (admin)
end

Expand Down Expand Up @@ -508,27 +505,27 @@ namespace anyone_instance:

func get_address() -> (address : felt):
tempvar anyone
%{ ids.anyone = context.user_anyone %}
%{ ids.anyone = context.signers.anyone %}
return (anyone)
end

func get_slots() -> (slots : felt):
tempvar slots
%{ ids.slots = context.whitelist_slots %}
%{ ids.slots = context.whitelist.slots %}
return (slots)
end

func get_proof_len() -> (proof_len : felt):
tempvar proof_len
%{ ids.proof_len = context.whitelist_merkle_proof_len %}
%{ ids.proof_len = context.whitelist.merkle_proof_len %}
return (proof_len)
end

func get_proof() -> (proof : felt*):
alloc_locals
let (local proof : felt*) = alloc()
%{
for index, node in enumerate(context.whitelist_merkle_proof):
for index, node in enumerate(context.whitelist.merkle_proof):
memory[ids.proof + index] = node
%}
return (proof)
Expand Down
14 changes: 0 additions & 14 deletions tests/integrations/yield/config.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
user:
admin: 1000
anyone: 1001

nft:
name: Project
symbol: NFT

token:
name: Token
symbol: TOK
decimals: 6
initial_supply: 1000000

carbonable_token:
name: Carbonable
symbol: CARBZ
Expand Down
39 changes: 18 additions & 21 deletions tests/integrations/yield/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,50 @@ from starkware.cairo.common.bool import TRUE, FALSE
func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}():
%{
# Load config
import yaml
with open("./tests/integrations/yield/config.yml", 'r') as file_instance:
config = yaml.safe_load(file_instance)
for section, subconfig in config.items():
for key, value in subconfig.items():
name = f"{section.lower()}_{key.lower()}"
setattr(context, name, value)
import sys
sys.path.append('.')
from tests import load
load("./tests/integrations/yield/config.yml", context)
# ERC-721 deployment
context.project_nft_contract = deploy_contract(
"./src/nft/project/CarbonableProjectNFT.cairo",
{
"name": context.nft_name,
"symbol": context.nft_symbol,
"owner": context.user_admin,
"name": context.project.name,
"symbol": context.project.symbol,
"owner": context.signers.admin,
},
).contract_address
# Reward token ERC-20 deployment
context.reward_token_contract = deploy_contract(
"./tests/mocks/token/erc20.cairo",
{
"name": context.token_name,
"symbol": context.token_symbol,
"decimals": context.token_decimals,
"initial_supply": context.token_initial_supply,
"recipient": context.user_anyone
"name": context.token.name,
"symbol": context.token.symbol,
"decimals": context.token.decimals,
"initial_supply": context.token.initial_supply,
"recipient": context.signers.anyone
},
).contract_address
# Carbonable token ERC-20 deployment
context.carbonable_token_contract = deploy_contract(
"./tests/mocks/token/erc20.cairo",
{
"name": context.carbonable_token_name,
"symbol": context.carbonable_token_symbol,
"decimals": context.carbonable_token_decimals,
"initial_supply": context.carbonable_token_initial_supply,
"recipient": context.user_anyone
"name": context.carbonable_token.name,
"symbol": context.carbonable_token.symbol,
"decimals": context.carbonable_token.decimals,
"initial_supply": context.carbonable_token.initial_supply,
"recipient": context.signers.anyone
},
).contract_address
# Yield Manager deployment
context.yield_manager_contract = deploy_contract(
"./src/yield/yield_manager.cairo",
{
"owner": context.user_admin,
"owner": context.signers.admin,
"project_nft_address": context.project_nft_contract,
"carbonable_token_address": context.carbonable_token_contract,
"reward_token_address": context.reward_token_contract,
Expand Down
2 changes: 2 additions & 0 deletions tests/units/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mocks:
project_nft_address: 0x056d4ffea4ca664ffe1256af4b029998014471a87dec8036747a927ab3320b46
5 changes: 0 additions & 5 deletions tests/units/minter/config.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
signers:
admin: 1000
anyone: 1001

mocks:
project_nft_address: 0x056d4ffea4ca664ffe1256af4b029998014471a87dec8036747a927ab3320b46
payment_token_address: 0x073314940630fd6dcda0d772d4c972c4e0a9946bef9dabf4ef84eda8ef542b82

whitelist:
Expand Down
13 changes: 4 additions & 9 deletions tests/units/minter/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ end
func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}():
%{
# Load config
import yaml
from collections import namedtuple
with open("./tests/units/minter/config.yml", 'r') as file_instance:
config = yaml.safe_load(file_instance)
for section, subconfig in config.items():
obj = namedtuple(section, list(subconfig.keys()))
for key, value in subconfig.items():
setattr(obj, key, value)
setattr(context, section, obj)
import sys
sys.path.append('.')
from tests import load
load("./tests/units/minter/config.yml", context)
%}

return ()
Expand Down
5 changes: 0 additions & 5 deletions tests/units/yielder/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
signers:
admin: 1000
anyone: 1001

mocks:
project_nft_address: 0x056d4ffea4ca664ffe1256af4b029998014471a87dec8036747a927ab3320b46
carbonable_token_address: 0x043eB0d3D84CC1f850D4b6dEe9630cAc35Af99980BAA30577A76adEa72475598
reward_token_address: 0x073314940630fd6dcda0d772d4c972c4e0a9946bef9dabf4ef84eda8ef542b82
13 changes: 4 additions & 9 deletions tests/units/yielder/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,10 @@ end
func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}():
%{
# Load config
import yaml
from collections import namedtuple
with open("./tests/units/yielder/config.yml", 'r') as file_instance:
config = yaml.safe_load(file_instance)
for section, subconfig in config.items():
obj = namedtuple(section, list(subconfig.keys()))
for key, value in subconfig.items():
setattr(obj, key, value)
setattr(context, section, obj)
import sys
sys.path.append('.')
from tests import load
load("./tests/units/yielder/config.yml", context)
%}

return ()
Expand Down

0 comments on commit f35de9f

Please sign in to comment.