Skip to content
This repository has been archived by the owner on May 31, 2022. It is now read-only.

Commit

Permalink
add declare section for arbitrary values
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo committed Feb 24, 2022
1 parent 4cda12b commit 0b36119
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
16 changes: 14 additions & 2 deletions example/deploy.fd
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
.id whatever

.declare
random_param 99
callme functionName(uint256)

.contracts
LABEL1 "src/Contract1.sol:ContractName1" 0x1111111111111111111111111111111111111111
LABEL2 "src/Contract2.sol:ContractName2"
LABEL3 0x2222222222222222222222222222222222222222
LABEL3 0xeea2fc1d255fd28aa15c6c2324ad40b03267f9c5

.signer mySigner1
private 4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d
Expand All @@ -20,7 +24,7 @@

.deployer myDeployer2
legacy
debug
# debug
# no_cache

network local
Expand Down Expand Up @@ -48,6 +52,14 @@
.use myDeployer2
send LABEL2 functionName (99999)
send LABEL2 functionName(uint256) (99999)
send LABEL2 @callme (@random_param)

# seeded before
send LABEL3 functionName(uint256) (@random_param)
send LABEL3 @callme (@random_param)

# Address-only variables need to have full function signature
# send LABEL3 functionName(uint256) (@random_param)

# Invalid
# send LABEL2 functionName(uint256)(99999)
1 change: 1 addition & 0 deletions foundrydeploy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
parse(f.read())
except ValueError as e:
_error(e)
raise e
2 changes: 2 additions & 0 deletions foundrydeploy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
SECTION_SIGNER = ".signer"
SECTION_DEPLOYER = ".deployer"
SECTION_PATH = ".use"
SECTION_DECLARATIONS = ".declare"

SECTIONS = [
SECTION_ID,
SECTION_CONTRACTS,
SECTION_SIGNER,
SECTION_DEPLOYER,
SECTION_PATH,
SECTION_DECLARATIONS,
]

#####################
Expand Down
12 changes: 9 additions & 3 deletions foundrydeploy/deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
self.addresses = {}
self.contract_signatures = {}
self.transactions = []
self.context = {}

# Load from cache if it exists
self.cache_path = (
Expand Down Expand Up @@ -95,7 +96,6 @@ def load_from_cache(self, cache_path):
self.contracts = deployer.contracts
self.addresses = deployer.addresses
self.contract_signatures = deployer.contract_signatures
self.transactions = self.transactions

except FileNotFoundError:
_info(f"# Starting cache at `{cache_path}`")
Expand Down Expand Up @@ -243,12 +243,18 @@ def send(self, contract_label: str, address: str, _args: str) -> str:
Calls `$ cast send`
"""

contract_path = self.contracts[contract_label]

function_name = _args[0]

# Get function signature if not given
if "(" not in function_name:

if contract_label not in self.contracts:
raise ValueError(
f"{contract_label} has no contract specified, so you need to specify the function signature"
)

contract_path = self.contracts[contract_label]

if function_name not in self.contract_signatures[contract_path]:
raise ValueError(f"{function_name} does not exist in {contract_path}")

Expand Down
38 changes: 34 additions & 4 deletions foundrydeploy/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ def _sharing_is_caring():
DEPLOYERS[deployer].addresses.update(addresses)


def _load_arguments(is_send: bool, arguments: list):
def _is_declaration(arg: str, declarations: dict):
if arg.startswith("@"):
return declarations[arg[1:]]
else:
return arg


def _load_arguments(is_send: bool, arguments: list, declarations: dict):
args = []
if is_send:

Expand All @@ -40,6 +47,7 @@ def _load_arguments(is_send: bool, arguments: list):
function_name = arguments[0]
arguments = arguments[1]

function_name = _is_declaration(function_name, declarations)
args.append(function_name)

if not arguments.startswith("(") or not arguments.endswith(")"):
Expand All @@ -50,6 +58,8 @@ def _load_arguments(is_send: bool, arguments: list):
for arg in arguments:
arg = arg.strip()

arg = _is_declaration(arg, declarations)

# check if * is present
# 00*2 -> 0000
str_repetition = arg.split("*")
Expand Down Expand Up @@ -164,7 +174,7 @@ def parse(script: str):

next_section = 0

context = {}
context = {SECTION_DECLARATIONS: {}}
paths = []
missing_fields = []

Expand Down Expand Up @@ -356,6 +366,22 @@ def parse(script: str):
f"error at line({linenu}) | section: {current_section} "
)

#####################
# DECLARATIONS
#####################

elif current_section == SECTION_DECLARATIONS:
if line.startswith(SECTION_DECLARATIONS):
if SECTION_DECLARATIONS not in context:
context[SECTION_DECLARATIONS] = {}
else:
if len(tokens) > 0 and len(tokens) != 2:
raise ValueError(
f"line({linenu} variable values should have no spaces. `{variable_name} {variable_value_no_space}`"
)
else:
context[SECTION_DECLARATIONS][tokens[0]] = " ".join(tokens[1:])

#####################
# PATH / USE
#####################
Expand Down Expand Up @@ -385,15 +411,19 @@ def parse(script: str):
contract_label = tokens[1]

if line.startswith(SECTION_PATH_DEPLOY):
arguments = _load_arguments(False, tokens[2])
arguments = _load_arguments(
False, tokens[2], context[SECTION_DECLARATIONS]
)
current_path.append(
(Deployer.DEPLOY, contract_label, arguments)
)
elif line.startswith(SECTION_PATH_SEND):
arguments = tokenize(
line.split(tokens[0] + " " + tokens[1])[1].strip()
)
arguments = _load_arguments(True, arguments)
arguments = _load_arguments(
True, arguments, context[SECTION_DECLARATIONS]
)
current_path.append((Deployer.SEND, contract_label, arguments))
else:
raise ValueError(
Expand Down

0 comments on commit 0b36119

Please sign in to comment.