Skip to content
This repository has been archived by the owner on Mar 30, 2021. It is now read-only.

Added Contract Functionality & Upgraded To Match Latest Rust #32

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
668 changes: 443 additions & 225 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
[package]
name = "hedera"
publish = false
version = "0.0.0"
version = "0.1.0-alpha.0"
edition = "2018"
authors = [
"LaunchBadge <[email protected]>"
"LaunchBadge <[email protected]> (PRIMARY)",
"Lonestar Data Technologies <[email protected]>"
]

[lib]
crate-type = ["cdylib"]

[dependencies]
hedera = "0.4.1"
hedera = { git = "https://github.com/launchbadge/hedera-sdk-rust" }
pyo3 = { version = "0.6.0-alpha.1", features = [ "extension-module" ] }
chrono = "0.4.6"
itertools = "0.8.0"
derive_more = "0.13.0"
mashup = "0.1.9"
try_from = "0.3.2"
failure = "0.1.5"
sha3 = "0.8.2"

[profile.release]
lto = true
Expand Down
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM rustlang/rust:nightly

ENV PYTHONUNBUFFERED=1 \
DEBIAN_FRONTEND=noninteractive \
PROTOC_ZIP=protoc-3.3.0-linux-x86_64.zip

RUN apt-get update \
&& apt-get install -yq --no-install-recommends \
python-setuptools

# install pyo3-pack
RUN easy_install pip && pip install pyo3-pack

# install protoc required for library
RUN wget -q https://github.com/google/protobuf/releases/download/v3.3.0/$PROTOC_ZIP && \
unzip -o $PROTOC_ZIP -d /protoc && \
ln -s /protoc/bin/protoc /usr/local/bin/protoc && \
rm -f $PROTOC_ZIP

# pyo3-pack requires newer version of rust
RUN rustup toolchain add nightly-2019-02-07 && rustup default nightly-2019-02-07

ADD . /io/

WORKDIR /io

ENTRYPOINT ["pyo3-pack"]
46 changes: 46 additions & 0 deletions build
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

if ! type cargo > /dev/null; then
read -p "Cargo/Rust is not found on this system. Would you like to install? [Y/n] " install_rust
if [[ "${install_rust}" == "Y" ]]; then
curl https://sh.rustup.rs -sSf | sh
source $HOME/.cargo/env
fi
fi

pck=$(command -v pyo3-pack)

if [[ "${pck}" == "" ]]; then
read -p "PY03-Pack not found. Install PY03-Pack? [Y/n] " pyo
if [[ "${pyo}" == "Y" ]]; then
pip install pyo3-pack
fi
fi

pyo3-pack build

echo -e "\n"
read -p "Hedera SDK Built Successfully. Install Hedera SDK? [Y/n] " install

if [[ "${install}" != "Y" ]]; then
exit
fi

declare -a build_files

count=0
echo -e "\nBuild Files\n"
for entry in ./target/wheels/*
do
echo "[${count}] $entry"
build_files[count]=$entry
count=$((count+1))
done

read -p "Select Build File To Install: " index
echo -e "\n"

echo "Selected: ${build_files[$index]}"

echo "Installing..."
pip install --force-reinstall ${build_files[$index]}
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2019-01-29
nightly-2019-09-06
9 changes: 2 additions & 7 deletions src/account_info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{PyAccountId, PyClaim, PyDateTime, PyDuration, PyPublicKey};
use crate::{PyAccountId, PyClaim, PyTimestamp, PyDuration, PyPublicKey};
use derive_more::From;
use hedera::AccountInfo;
use itertools::Itertools;
Expand Down Expand Up @@ -33,11 +33,6 @@ impl PyAccountInfo {
Ok(self.inner.proxy_account_id.map(Into::into))
}

#[getter]
pub fn proxy_fraction(&self) -> PyResult<i32> {
Ok(self.inner.proxy_fraction as i32)
}

#[getter]
pub fn proxy_received(&self) -> PyResult<i64> {
Ok(self.inner.proxy_received as i64)
Expand Down Expand Up @@ -69,7 +64,7 @@ impl PyAccountInfo {
}

#[getter]
pub fn expiration_time(&self) -> PyResult<PyDateTime> {
pub fn expiration_time(&self) -> PyResult<PyTimestamp> {
self.inner.expiration_time.try_into()
}

Expand Down
176 changes: 176 additions & 0 deletions src/call_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
use hedera::call_params::CallParams;
use derive_more::From;
use pyo3::{prelude::*, exceptions};

#[pyclass(name = CallParams)]
#[derive(From)]
pub struct PyCallParams {
pub(crate) inner: CallParams,
}

#[pymethods]
impl PyCallParams {
#[new]
pub fn __new__(obj: &PyRawObject, func: Option<String>) -> PyResult<()> {
let cp = match func {
Some(name) => {
CallParams::new(Some(name))
},
None => CallParams::new(None)
};
obj.init(move || Self {
inner: cp
})
}

fn prep_fixed_bytes(&self, mut param: Vec<u8>, fixed_len: usize) -> PyResult<Vec<u8>> {
if fixed_len < param.len() {
return Err(exceptions::ValueError::py_err("ILLEGAL ARGUMENT ERROR: Fixed byte len is \
less than byte length. Fixed byte length must be greater than the byte length and less \
than or equal to 32."))
} else if fixed_len > 32 {
return Err(exceptions::ValueError::py_err("ILLEGAL ARGUMENT ERROR: Fixed byte length \
cannot be greater than 32."))
} else if param.len() < fixed_len {
for _ in 0..(fixed_len - param.len()) { param.push(0); }
}
Ok(param)
}

pub fn add_string(&mut self, param: String) -> PyResult<()> {
self.inner.add_string(param);
Ok(())
}

pub fn add_string_array(&mut self, param: Vec<String>) -> PyResult<()> {
self.inner.add_string_array(param);
Ok(())
}

pub fn add_fixed_string_array(&mut self, param: Vec<String>, fixed_len: usize) -> PyResult<()> {
self.inner.add_fixed_string_array(param, fixed_len);
Ok(())
}

pub fn add_bytes(&mut self, param: Vec<u8>) -> PyResult<()> {
self.inner.add_bytes(param);
Ok(())
}

pub fn add_fixed_bytes(&mut self, mut param: Vec<u8>, fixed_len: usize) -> PyResult<()> {
param = self.prep_fixed_bytes(param, fixed_len)?;
self.inner.add_fixed_bytes(param, fixed_len);
Ok(())
}

pub fn add_byte_array(&mut self, param: Vec<Vec<u8>>) -> PyResult<()> {
self.inner.add_byte_array(param);
Ok(())
}

pub fn add_fixed_byte_array(&mut self, mut param: Vec<Vec<u8>>, fixed_byte_len: usize) -> PyResult<()> {
for i in 0..param.len() {
param[i] = self.prep_fixed_bytes(param[i].clone(), fixed_byte_len)?;
}
self.inner.add_fixed_byte_array(param, fixed_byte_len);
Ok(())
}

pub fn add_byte_fixed_array(&mut self, param: Vec<Vec<u8>>, fixed_len: usize) -> PyResult<()> {
self.inner.add_byte_fixed_array(param, fixed_len);
Ok(())
}

pub fn add_fixed_byte_fixed_array(&mut self, mut param: Vec<Vec<u8>>, fixed_byte_len: usize,
fixed_len: usize) -> PyResult<()> {
for i in 0..param.len() {
param[i] = self.prep_fixed_bytes(param[i].clone(), fixed_byte_len)?;
}
self.inner.add_fixed_byte_fixed_array(param, fixed_byte_len, fixed_len);
Ok(())
}

pub fn add_bool(&mut self, param: bool) -> PyResult<()> {
self.inner.add_bool(param);
Ok(())
}

pub fn add_int(&mut self, param: isize, width: usize) -> PyResult<()> {
self.inner.add_int(param, width);
Ok(())
}

pub fn add_int_array(&mut self, param: Vec<isize>, width: usize) -> PyResult<()> {
self.inner.add_int_array(param, width);
Ok(())
}

pub fn add_fixed_int_array(&mut self, param: Vec<isize>, width: usize,
fixed_len: usize) -> PyResult<()> {
self.inner.add_fixed_int_array(param, width, fixed_len);
Ok(())
}

pub fn add_uint(&mut self, param: usize, width: usize) -> PyResult<()> {
self.inner.add_uint(param, width);
Ok(())
}

pub fn add_uint_array(&mut self, param: Vec<usize>, width: usize) -> PyResult<()> {
self.inner.add_uint_array(param, width);
Ok(())
}

pub fn add_fixed_uint_array(&mut self, param: Vec<usize>, width: usize,
fixed_len: usize) -> PyResult<()> {
self.inner.add_fixed_uint_array(param, width, fixed_len);
Ok(())
}

pub fn add_address(&mut self, param: Vec<u8>) -> PyResult<()> {
self.inner.add_address(param);
Ok(())
}

pub fn add_address_string(&mut self, param: String) -> PyResult<()> {
self.inner.add_address_string(param);
Ok(())
}

pub fn add_address_array(&mut self, param: Vec<Vec<u8>>) -> PyResult<()> {
self.inner.add_address_array(param);
Ok(())
}

pub fn add_fixed_address_array(&mut self, param: Vec<Vec<u8>>,
fixed_len: usize) -> PyResult<()> {
self.inner.add_fixed_address_array(param, fixed_len);
Ok(())
}

pub fn add_address_string_array(&mut self, param: Vec<String>) -> PyResult<()> {
self.inner.add_address_string_array(param);
Ok(())
}

pub fn add_fixed_address_string_array(&mut self, param: Vec<String>,
fixed_len: usize) -> PyResult<()> {
self.inner.add_fixed_address_string_array(param, fixed_len);
Ok(())
}

pub fn add_function(&mut self, addr: Vec<u8>, selector: Vec<u8>) -> PyResult<()> {
self.inner.add_function(addr, selector);
Ok(())
}

pub fn add_function_string(&mut self, addr: String, selector: String) -> PyResult<()> {
self.inner.add_function_string(addr, selector);
Ok(())
}

pub fn assemble(&self) -> PyResult<Vec<u8>> {
let out = self.inner.assemble();
Ok(out)
}
}
37 changes: 33 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use super::{
errors::PyValueError, query_crypto_get_account_balance::*, query_crypto_get_info::*,
query_file_get_contents::*, query_transaction_get_receipt::*,
query_file_get_contents::*, query_transaction_get_receipt::*
};
use crate::{
either::Either,
id::{PyAccountId, PyContractId, PyFileId},
transaction_id::PyTransactionId,
transaction_id::PyTransactionId, PyQueryContractCall,
PyQueryCryptoGetClaim, PyQueryFileGetInfo, PyQueryTransactionGetRecord,
PyTransactionContractCall, PyTransactionContractCreate, PyTransactionContractUpdate,
PyTransactionCryptoCreate, PyTransactionCryptoDelete, PyTransactionCryptoDeleteClaim,
PyTransactionCryptoTransfer, PyTransactionCryptoUpdate, PyTransactionFileAppend,
PyTransactionFileCreate, PyTransactionFileDelete,
PyTransactionFileCreate, PyTransactionFileDelete, PySecretKey
};
use hedera::{AccountId, Client, ContractId, FileId, TransactionId};
use pyo3::{prelude::*, types::PyObjectRef};
Expand All @@ -19,7 +19,7 @@ use try_from::TryInto;

#[pyclass(name = Client)]
pub struct PyClient {
inner: Rc<Client>,
pub inner: Rc<Client>,
}

#[pymethods]
Expand All @@ -32,6 +32,31 @@ impl PyClient {
})
}

pub fn set_node(&mut self, node: &PyObjectRef) -> PyResult<()> {
let n = (FromPyObject::extract(node)?: Either<&str, &PyAccountId>).try_into()?;
match Rc::get_mut(&mut self.inner) {
Some(c) => c.set_node(n),
None => ()
};
Ok(())
}

pub fn set_operator(&mut self, operator: &PyObjectRef,
secret: &'static PyObjectRef) -> PyResult<()> {
let op = (FromPyObject::extract(operator)?: Either<&str, &PyAccountId>).try_into()?;
let sk = FromPyObject::extract(secret)?: &PySecretKey;

let s = move || {
return sk.inner.clone()
};

match Rc::get_mut(&mut self.inner) {
Some(c) => c.set_operator(op, s),
None => ()
}
Ok(())
}

/// transfer_crypto(self) TransactionCryptoTransfer
/// --
///
Expand Down Expand Up @@ -292,6 +317,10 @@ impl PyPartialContractMessage {
Ok(PyTransactionContractCall::new(&self.client, self.contract))
}

pub fn query(&self, gas: i64, params: Vec<u8>, max_result_size: i64) -> PyResult<PyQueryContractCall> {
Ok(PyQueryContractCall::new(&self.client, self.contract, gas, params, max_result_size))
}

/// update(self) -> TransactionContractUpdate
/// --
///
Expand Down
Loading