#![allow(unused)]
extern crate ws;
use ws::{connect, CloseCode};
use std::rc::Rc;
use std::any::Any;
use std::cell::Cell;
use std::cell::RefCell;
use crate::misc::config::*;
use crate::message::common::command_trait::CommandConversion;
use serde_json::json;
use serde::{Deserialize, Serialize};
use serde_json::{Value};
use cast_rs::hex_t;
use crate::{Args, Arg};
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct SolidityInitTxJson {
#[serde(rename="Flags")]
pub flags: i32,
#[serde(rename="Fee")]
pub fee: u64,
#[serde(rename="TransactionType")]
pub transaction_type: String,
#[serde(rename="Account")]
pub account: String,
#[serde(rename="Amount")]
pub amount: u64,
#[serde(rename="Method")]
pub method: i32,
#[serde(rename="Payload")]
pub payload: String, }
impl SolidityInitTxJson {
pub fn new(account: String, payload: String) -> Self {
SolidityInitTxJson {
flags: 0,
fee: 10000,
transaction_type: "AlethContract".to_string(),
account: account,
amount: 100000000,
method: 0,
payload: hex_t::encode(payload),
}
}
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct SolidityInitMessage {
#[serde(rename="id")]
id: u64,
#[serde(rename="command")]
command: String,
#[serde(rename="secret")]
secret: String,
#[serde(rename="tx_json")]
tx_json: SolidityInitTxJson,
}
impl SolidityInitMessage {
pub fn new(secret: String, tx_json: SolidityInitTxJson) -> Self {
SolidityInitMessage {
id: 1,
command: "submit".to_string(),
secret: secret,
tx_json: tx_json,
}
}
pub fn with_params(account: String, secret: String, payload: String) -> Self {
SolidityInitMessage {
id: 1,
command: "submit".to_string(),
secret: secret,
tx_json: SolidityInitTxJson::new(account, payload),
}
}
}
impl CommandConversion for SolidityInitMessage {
type T = SolidityInitMessage;
fn to_string(&self) -> Result<String, serde_json::error::Error> {
let j = serde_json::to_string(&self)?;
Ok(j)
}
fn box_to_raw(&self) -> &dyn Any {
self
}
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct SolidityInvokeTxJson {
#[serde(rename="Flags")]
pub flags: i32,
#[serde(rename="Fee")]
pub fee: u64,
#[serde(rename="TransactionType")]
pub transaction_type: String,
#[serde(rename="Account")]
pub account: String,
#[serde(rename="Amount")]
pub amount: u64,
#[serde(rename="Method")]
pub method: i32,
#[serde(rename="Destination")]
pub destination: String,
#[serde(rename="ContractMethod")]
contract_method: String,
#[serde(rename="Args")]
pub args: Vec<Args>,
}
impl SolidityInvokeTxJson {
pub fn new(account: String, destination: String, contract_method: String, args: Vec<Args>) -> Self {
SolidityInvokeTxJson {
flags: 0,
fee: 10000,
transaction_type: "AlethContract".to_string(),
account: account,
amount: 0,
method: 1,
destination: destination, contract_method: contract_method,
args: args,
}
}
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct SolidityInvokeMessage {
#[serde(rename="id")]
id: u64,
#[serde(rename="command")]
command: String,
#[serde(rename="secret")]
secret: String,
#[serde(rename="tx_json")]
tx_json: SolidityInvokeTxJson,
}
impl SolidityInvokeMessage {
pub fn new(secret: String, tx_json: SolidityInvokeTxJson) -> Self {
SolidityInvokeMessage {
id: 1,
command: "submit".to_string(),
secret: secret,
tx_json: tx_json,
}
}
pub fn with_params(account: String, secret: String, address: String, contract_method: String, args: Vec<Arg>) -> Self {
if account.len() != 34 || secret.len() != 29 || address.len() != 34 || contract_method.len() < 8 {
panic!("Input params Error!");
}
let mut v: Vec<Args> = vec![];
for x in args {
let t = Args::new(x);
v.push(t);
}
let mut hex_method = contract_method;
if hex_method.starts_with("0x") {
hex_method = hex_method.get(2..10).unwrap().to_string();
} else {
hex_method = hex_method.get(0..8).unwrap().to_string();
}
SolidityInvokeMessage {
id: 1,
command: "submit".to_string(),
secret: secret,
tx_json: SolidityInvokeTxJson::new(account, address, hex_t::encode(hex_method), v)
}
}
}
impl CommandConversion for SolidityInvokeMessage {
type T = SolidityInvokeMessage;
fn to_string(&self) -> Result<String, serde_json::error::Error> {
let j = serde_json::to_string(&self)?;
Ok(j)
}
fn box_to_raw(&self) -> &dyn Any {
self
}
}