Skip to content

Commit

Permalink
"Fix macros, fix tests, fix examples, fix clippy lints, new and impro…
Browse files Browse the repository at this point in the history
…ved error handling, reduce allocations"
  • Loading branch information
crypdoughdoteth committed Feb 26, 2024
1 parent 496a7a3 commit fd47c36
Show file tree
Hide file tree
Showing 14 changed files with 570 additions and 607 deletions.
27 changes: 27 additions & 0 deletions .vimspector.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"configurations": {
"<name>: Launch": {
"adapter": "CodeLLDB",
"configuration": {
"name": "<name>",
"type": "lldb",
"request": "launch",
"program": "${workspaceRoot}/target/debug/deps/vyper_rs-686403a730734cd8",
"cwd": "${workspaceRoot}",
"externalConsole": true,
"stopAtEntry": true,
"MIMode": "gdb"
}
},
"<name>: Attach": {
"adapter": "CodeLLDB",
"configuration": {
"name": "<name>: Attach",
"type": "lldb",
"request": "attach",
"program": "${workspaceRoot}/target/debug/deps/vyper_rs-686403a730734cd8",
"MIMode": "gdb"
}
}
}
}
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ license = "MIT"
keywords = ["Crypto", "Vyper", "EVM", "Compilers"]
description = "A Rust library to interact with the Vyper compiler!"
repository = "https://github.com/crypdoughdoteth/vyper-rs/"
exclude = [
"examples/contracts"
]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.72"
hex = "0.4.3"
serde = {version = "1.0.171", features = ["derive"]}
serde_json = "1.0.102"
tokio = { version = "1.29.1", features = ["rt"] }
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
[dev-dependencies]
tokio-test = "0.4.2"

3 changes: 1 addition & 2 deletions examples/contracts/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use vyper_rs::vyper::Vyper;

pub fn compile_and_generate_bindings() -> Result<(), Box<dyn Error>> {
let cpath: PathBuf = PathBuf::from("../../multisig.vy");
let abi: PathBuf = PathBuf::from("./my_abi.json");
let contract = Vyper::new(cpath, abi);
let contract = Vyper::new(&cpath);
contract.gen_abi()?;
println!("Generating bindings for {contract}\n");

Expand Down
3 changes: 1 addition & 2 deletions examples/contracts/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use vyper_rs::vyper::Vyper;

pub async fn deploy() -> Result<(), Box<dyn Error>> {
let cpath: PathBuf = PathBuf::from("../../multisig.vy");
let abi: PathBuf = PathBuf::from("./my_abi.json");
let mut contract = Vyper::new(cpath, abi);
let mut contract = Vyper::new(&cpath);
contract.compile()?;
contract.gen_abi()?;
let anvil = Anvil::new().spawn();
Expand Down
14 changes: 7 additions & 7 deletions examples/contracts/src/venv.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{error::Error, path::PathBuf};
use vyper_rs::{venv::Venv, vyper::Vyper};
use std::{error::Error, path::Path};
use vyper_rs::venv::Venv;

pub fn venv_example() -> Result<(), Box<dyn Error>> {
let cpath: PathBuf = PathBuf::from("../../multisig.vy");
let abi: PathBuf = PathBuf::from("./my_abi.json");
let venv = Venv::new().init()?.ivyper_venv(None)?;
let mut contract = Vyper::new(cpath, abi);
venv.compile(&mut contract)?;
let mut contract = Venv::default()
.init()?
.ivyper_venv(None)?
.vyper(Path::new("../../multisig.vy"));
contract.compile()?;
Ok(())
}
125 changes: 125 additions & 0 deletions multisig.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# @version ^0.3.3
# Multiple Signature Contract

event Deposit:
sender: indexed(address)
amount: uint256
bal: uint256
event SubmitTransaction:
owner: indexed(address)
txIndex: indexed(uint256)
to: indexed(address)
value: uint256
data: bytes32
event ConfirmTransaction:
owner: indexed(address)
txIndex: indexed(uint256)
event RevokeConfirmation:
owner: indexed(address)
txIndex: indexed(uint256)
event ExecuteTransaction:
owner: indexed(address)
txIndex: indexed(uint256)

owners: DynArray[address, 10]
isOwner: HashMap[address, bool]
numConfirmationsRequired: constant(uint256) = 1


struct Transaction:
to: address
val: uint256
data: bytes32
fnSel: String[32]
executed: bool
numConfirmations: uint256


isConfirmed: public(HashMap[uint256, HashMap[address, bool]])

transactions: DynArray[Transaction, 100000]


@external
def __init__ (_owners: DynArray[address,10]):
assert len(_owners) > 0, "Error: No Owners"
assert numConfirmationsRequired > 0 and numConfirmationsRequired <= len(_owners)
for o in _owners:
assert o != empty(address)
assert self.isOwner[o] == False, "Owner Not Unique"
self.isOwner[o] = True
self.owners.append(o)

@external
@payable
def __default__():
log Deposit(msg.sender, msg.value, self.balance)

@external
def submitTransaction( _to: address, _val: uint256, _data: bytes32, _fnSel: String[32]):
assert self.isOwner[msg.sender] == True, "Not an Owner"
txIndex: uint256 = len(self.transactions)
self.transactions.append(Transaction({to:_to, val:_val, data: _data, fnSel: _fnSel, executed: False, numConfirmations: 0}))

@external
def confirmTransaction(txIndex: uint256):
assert self.isOwner[msg.sender]==True, "Not an Owner"
assert txIndex < len(self.transactions), "Does Not Exist"
assert self.transactions[txIndex].executed == False, "Transaction Already Executed"
assert self.isConfirmed[txIndex][msg.sender], "Transactions Already Confirmed"
self.transactions[txIndex].numConfirmations += 1
self.isConfirmed[txIndex][msg.sender] = True
log ConfirmTransaction(msg.sender, txIndex)

@payable
@external
def executeTransaction(txIndex: uint256) -> Bytes[32]:
assert self.isOwner[msg.sender]==True, "Not an Owner"
assert txIndex < len(self.transactions), "Does Not Exist"
assert self.transactions[txIndex].executed == False, "Transaction Already Executed"
assert self.transactions[txIndex].numConfirmations >= numConfirmationsRequired, "Insufficent Confirmations"
self.transactions[txIndex].executed = True
success: bool = False
response: Bytes[32] = b""
functionSelector: Bytes[32] = convert(self.transactions[txIndex].fnSel, Bytes[32])
success, response = raw_call(
self.transactions[txIndex].to,
#add field for function selector, seperate from data
_abi_encode(self.transactions[txIndex].data, (convert(keccak256(self.transactions[txIndex].fnSel), bytes4))),
max_outsize=32,
value = self.transactions[txIndex].val,
revert_on_failure = False
)
assert success
return response

@nonpayable
@external
def revokeConfirmation(_txIndex: uint256):
assert _txIndex < len(self.transactions), "Does Not Exist"
assert self.transactions[_txIndex].executed == False, "Transaction Already Executed"
self.transactions[_txIndex].numConfirmations -= 1
self.isConfirmed[_txIndex][msg.sender] = False
log RevokeConfirmation(msg.sender, _txIndex)

@view
@external
def getOwners() -> DynArray[address, 10]:
return self.owners

@view
@external
def getTransactionCount() -> uint256:
return len(self.transactions)

@view
@external
def getTransaction(_txIndex: uint256) -> (address, uint256, bytes32, String[32], bool, uint256):
return(
self.transactions[_txIndex].to,
self.transactions[_txIndex].val,
self.transactions[_txIndex].data,
self.transactions[_txIndex].fnSel,
self.transactions[_txIndex].executed,
self.transactions[_txIndex].numConfirmations
)
Loading

0 comments on commit fd47c36

Please sign in to comment.