Skip to content

Commit

Permalink
ruff lint & fix
Browse files Browse the repository at this point in the history
  • Loading branch information
z80dev committed Nov 27, 2023
1 parent 95c3541 commit 11b7939
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 38 deletions.
2 changes: 1 addition & 1 deletion dasy/builtin/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from dasy import parser

from hy import repr, read_many
from hy import repr


def parse_venom(expr):
Expand Down
1 change: 0 additions & 1 deletion dasy/compiler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from vyper.compiler import compile_code
from vyper.compiler.phases import CompilerData as VyperCompilerData
from pathlib import Path
from vyper.compiler.output import (
Expand Down
3 changes: 1 addition & 2 deletions dasy/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from dasy import compiler, parser
from dasy import compiler
from vyper.compiler import OUTPUT_FORMATS as VYPER_OUTPUT_FORMATS
from vyper import compiler as vyper_compiler
import argparse
import sys

Expand Down
2 changes: 1 addition & 1 deletion dasy/parser/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def parse_declaration(var, typ, value=None):
raise Exception(f"Invalid declaration type {typ}")

if annotation is None:
raise Exception(f"No valid annotation was found")
raise Exception("No valid annotation was found")

vdecl_node = build_node(
vy_nodes.VariableDecl,
Expand Down
2 changes: 1 addition & 1 deletion dasy/parser/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
from hy import models
from dasy import parser
from .utils import process_body, build_node, set_parent_children
from .utils import process_body, build_node


def parse_for(expr):
Expand Down
2 changes: 1 addition & 1 deletion dasy/parser/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def parse_node(
models.Keyword,
models.Bytes,
models.List,
]
],
):
"""
This function converts a node into its corresponding AST node based on its type.
Expand Down
6 changes: 6 additions & 0 deletions dasy/parser/utils.hy
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
(isinstance tree Sequence) (for [el tree] (when (has-return el) (return True)))
True (return False)))

(defn is-venom [tree]
(cond
(isinstance tree Symbol) (= (str tree) "venom")
(isinstance tree Sequence) (for [el tree] (when (is-venom el) (return True)))
True (return False)))

(defn filename-to-contract-name [fname]
;; converts a filename to a contract name
;; e.g. "contracts/my_contract.vy" -> "MyContract"
Expand Down
11 changes: 8 additions & 3 deletions examples/venom.dasy
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
(defn retOne [] :uint256 :external
(defvar x :uint256 0)
(venom "(mstore 64 1)")
(venom (mstore 64 1))
x)


;; 41 gas
;; this works as-is but unecessary variable costs gas
(defn addTwoNums [:uint256 x y] :uint256 :external
(defvar z :uint256 0) ;; first variable is at offset 64
(venom "(mstore 64 (add (calldataload 4) (calldataload 36)))")
(venom (mstore 64 (add (calldataload 4) (calldataload 36))))
z)

;; ;; this might be ideal, removing implicit returns around venom blocks
;; (defn addTwoNums2 [:uint256 x y] :uint256 :external
;; (venom (seq
;; (mstore 64 (add (calldataload 4) (calldataload 36))))))
2 changes: 1 addition & 1 deletion examples/venom_comp.vy
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ def retOne() -> uint256:
# 71 gas
@external
def addTwoNums(a: uint256, b: uint256) -> uint256:
return a + b
return unsafe_add(a, b)
15 changes: 7 additions & 8 deletions tests/parser/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from dasy.parser.utils import *
from dasy.parser import reset_nodeid_counter
from vyper.ast.nodes import *
from dasy.parser.utils import process_body, add_src_map, set_parent_children, build_node, next_node_id_maker, pairwise, filename_to_contract_name, has_return
from vyper.ast.nodes import Expr
import dasy


Expand All @@ -24,11 +23,11 @@ def test_pairwise():


def test_has_return():
assert has_return(dasy.read("(return 1)")) == True
assert has_return(dasy.read("(return 1 2)")) == True
assert has_return(dasy.read("(return)")) == True
assert has_return(dasy.read("(return (return 1))")) == True
assert not has_return(dasy.read("(1 2 3)")) == False
assert has_return(dasy.read("(return 1)"))
assert has_return(dasy.read("(return 1 2)"))
assert has_return(dasy.read("(return)"))
assert has_return(dasy.read("(return (return 1))"))
assert not has_return(dasy.read("(1 2 3)"))


def test_build_node():
Expand Down
24 changes: 5 additions & 19 deletions tests/test_dasy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import dasy
from boa.vyper.contract import VyperContract
import boa
import pytest


def test_compare_venom_vyper():
Expand All @@ -12,6 +11,7 @@ def test_compare_venom_vyper():
assert contract.retOne() == 1
assert contract.addTwoNums(1, 2) == 3


def compile_src(src: str, *args) -> VyperContract:
ast = dasy.compile(src, include_abi=True)
return VyperContract(ast, *args)
Expand Down Expand Up @@ -166,19 +166,6 @@ def test_map():
assert c.getOwnerNum() == 10


def test_dynarrays():
c = compile_src(
"""
(defvar nums (public (dyn-array :uint256 3)))
(defn __init__ [] :external
(.append self/nums 11)
(.append self/nums 12))
"""
)
assert c.nums(0) == 11
assert c.nums(1) == 12


def test_reference_types():
c = compile_src(
"""
Expand Down Expand Up @@ -229,15 +216,15 @@ def test_funtions():

def test_visibility():
c = compile("examples/function_visibility.dasy")
assert c.extFunc() == True
assert c.extFunc()
assert c.sumOfSquares(2, 5) == 29
assert c.avg(20, 80) == 50


def test_view_pure():
c = compile("examples/view_pure.dasy")
assert c.pureFunc(5) == 5
assert c.viewFunc(1) == True
assert c.viewFunc(1)
assert c.sum(4, 5, 6) == 15
assert c.addNum(10) == 10

Expand Down Expand Up @@ -325,20 +312,19 @@ def testInterface():
b = compile("examples/test_interface.dasy")
c = compile("examples/interface.dasy", b.address)
addr1 = boa.env.generate_address()
addr2 = boa.env.generate_address()
assert b.owner() == c.getOwner()
c.setOwner(addr1)
# convert addr1 to 0x formatted hex string
assert b.owner() == addr1


def test_reentrancy():
c = compile("examples/nonreentrant.dasy")
c = compile("examples/nonreentrant.dasy") # noqa: F841


def test_auction():
a = boa.env.generate_address()
c = compile("examples/simple_auction.dasy", a, 100, 10000000)
c = compile("examples/simple_auction.dasy", a, 100, 10000000) # noqa: F841


def test_token():
Expand Down

0 comments on commit 11b7939

Please sign in to comment.