Skip to content

Commit

Permalink
rabbit fixes and tx_from_json
Browse files Browse the repository at this point in the history
  • Loading branch information
MCysneiros committed Oct 21, 2024
1 parent 65e6dc9 commit 39190da
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
73 changes: 71 additions & 2 deletions packages/kos-sdk/src/chains/moonbeam/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use kos_types::number::BigNumber;

use serde::Serialize;
use wasm_bindgen::prelude::*;
use kos_types::hash::Hash;
use crate::chain;
use crate::chains::ethereum::transaction;

#[derive(Debug, Copy, Clone)]
#[wasm_bindgen]
Expand All @@ -19,6 +22,12 @@ pub struct GLMR {}

pub const CHAIN_ID: u64 = 1284;

pub fn hash_transaction(moonbeam_tx: &transaction::Transaction) -> Result<Vec<u8>, Error> {
let bytes = moonbeam_tx.encode()?;
let digest = GLMR::hash(&bytes)?;
Ok(digest)
}

pub const BASE_CHAIN: BaseChain = BaseChain {
name: "Moonbeam",
symbol: "GLMR",
Expand Down Expand Up @@ -162,7 +171,7 @@ impl GLMR {
let node = node_url.unwrap_or_else(|| crate::utils::get_node_url("GLMR"));
ETH::gas_price(Some(node)).await
}

#[wasm_bindgen(js_name="send")]
pub async fn send(
sender: String,
receiver: String,
Expand Down Expand Up @@ -199,11 +208,36 @@ impl GLMR {
) -> Result<bool, Error> {
ETH::validate_address(address, option)
}

#[wasm_bindgen(js_name = "txFromJson")]
pub fn tx_from_json(raw: &str) -> Result<crate::models::Transaction, Error> {
let tx: transaction::Transaction = serde_json::from_str(raw)?;
let digest = hash_transaction(&tx)?;

let sender = match tx.from {
Some(addr) => addr.to_string(),
None => "".to_string(),
};

let signature = tx.signature.map(|sig| sig.to_standard().to_string());

Ok(crate::models::Transaction {
chain: chain::Chain::GLMR,
sender,
hash: Hash::from_vec(digest)?,
data: Some(TransactionRaw::Ethereum(tx)),
signature,
})
}
}



#[cfg(test)]
mod tests {
use super::*;
use hex::FromHex;
use web3::types::U256;
use kos_crypto::secp256k1::Secp256k1KeyPair;
use kos_types::Bytes32;

Expand Down Expand Up @@ -251,6 +285,7 @@ mod tests {

#[test]
fn test_send_and_sign() {
init();
let options = models::SendOptions {
data: Some(models::Options::Moonbeam(kos_proto::options::GLMROptions {
eth: kos_proto::options::ETHOptions {
Expand Down Expand Up @@ -289,7 +324,7 @@ mod tests {

#[test]
fn test_send_erc20() {
std::env::set_var("NODE_GLMR", "https://moonbeam.node.klever.io");
init();
let options = models::SendOptions {
data: Some(models::Options::Moonbeam(kos_proto::options::GLMROptions {
eth: kos_proto::options::ETHOptions {
Expand Down Expand Up @@ -355,4 +390,38 @@ mod tests {

assert_eq!(balance.unwrap().to_string(), "0");
}

#[test]
fn test_decode_json() {
let json = r#"{
"from":"0x4cbeee256240c92a9ad920ea6f4d7df6466d2cdc",
"maxPriorityFeePerGas":null,"maxFeePerGas":null,
"gas": "0x00",
"value": "0x00",
"data":"0xa9059cbb000000000000000000000000ac4145fef6c828e8ae017207ad944c988ccb2cf700000000000000000000000000000000000000000000000000000000000f4240",
"to":"0xdac17f958d2ee523a2206206994597c13d831ec7",
"nonce":"0x00"}"#;
let tx = GLMR::tx_from_json(json).unwrap();

assert_eq!(tx.chain, chain::Chain::GLMR);

let moonbeam_tx = match tx.data {
Some(TransactionRaw::Ethereum(tx)) => tx,
_ => panic!("invalid tx"),
};

assert_eq!(moonbeam_tx.chain_id, None);
assert_eq!(moonbeam_tx.nonce, U256::from_dec_str("0").unwrap());
assert_eq!(
moonbeam_tx.from.unwrap().to_string(),
"0x4cBeee256240c92A9ad920ea6f4d7Df6466D2Cdc"
);
assert_eq!(
moonbeam_tx.to.unwrap().to_string(),
"0xdAC17F958D2ee523a2206206994597C13D831ec7"
);
assert_eq!(moonbeam_tx.gas, U256::from(0));
assert_eq!(moonbeam_tx.value, U256::from(0));
assert_eq!(moonbeam_tx.signature, None);
}
}
6 changes: 2 additions & 4 deletions packages/kos-sdk/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::chain::Chain;
use crate::chains::{ETH, KLV, TRX};
use crate::chains::{ETH, GLMR, KLV, TRX};
use kos_types::{error::Error, hash::Hash};
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -214,12 +214,10 @@ impl Transaction {
Chain::KLV => KLV::tx_from_raw(data),
Chain::TRX => TRX::tx_from_raw(data),
Chain::ETH => ETH::tx_from_json(data),
Chain::GLMR => GLMR::tx_from_json(data),
Chain::MATIC => Err(Error::InvalidTransaction(
"MATIC chain not implemented".to_string(),
)),
Chain::GLMR => Err(Error::InvalidTransaction(
"GLMR chain not implemented".to_string(),
)),
Chain::BTC => Err(Error::InvalidTransaction(
"BTC chain not implemented".to_string(),
)),
Expand Down

0 comments on commit 39190da

Please sign in to comment.