A Rust library for interacting with the Waves blockchain.
Supports node interaction, offline transaction signing and creating addresses and keys.
Use the code below to add waves-rust as a dependency for your project.
cargo
waves-rust = "0.1.0"
Create an account from a private key ('T' for testnet) from random seed phrase: ```rust use wavesrust::model::{ChainId, PrivateKey}; use wavesrust::util::Crypto;
let seedphrase = Crypto::getrandomseedphrase(12); let privatekey = PrivateKey::fromseed(&seedphrase, 0).unwrap(); let publickey = privatekey.publickey(); let address = public_key.address(ChainId::TESTNET.byte()).unwrap(); ```
Create a Node and learn a few things about blockchain: ```rust use wavesrust::api::{Node, Profile}; use wavesrust::model::Address;
async fn getnodeinfo() { let node = Node::fromprofile(Profile::TESTNET); println!("Current height is {}", node.getheight().await.unwrap()); println!("My balance is {}", node.getbalance(&address).await.unwrap()); println!("With 100 confirmations: {}", node.getbalancewithconfirmations(&address, 100).await.unwrap()); } ```
Send some money to a buddy: ```rust use wavesrust::api::{Node, Profile}; use wavesrust::model::{Address, Amount, Base58String, ChainId, PrivateKey, Transaction, TransactionData, TransferTransaction}; use wavesrust::util::getcurrentepochmillis;
let buddy = Address::from_string("3N2yqTEKArWS3ySs2f6t8fpXdjX6cpPuhG8").unwrap();
let transactiondata = TransactionData::Transfer(TransferTransaction::new( buddy, Amount::new(100000000, None), // None is WAVES asset Base58String::from_string("thisisattachment").unwrap(), ));
let timestamp = getcurrentepochmillis(); let signedtx = Transaction::new( transactiondata, Amount::new(100000, None), timestamp, privatekey.publickey(), 3, ChainId::TESTNET.byte(), ) .sign(&privatekey) .unwrap();
node.broadcast(&signed_tx).await.unwrap(); ```
Set a script on an account. Be careful with the script you pass here, as it may lock the account forever! ```rust use wavesrust::api::{Node, Profile}; use wavesrust::model::{Address, Amount, ChainId, PrivateKey, SetScriptTransaction, Transaction, TransactionData}; use wavesrust::util::getcurrentepochmillis;
let script = "{-# CONTENT_TYPE EXPRESSION #-} sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)";
let compiledscript = node.compilescript(script, true).await.unwrap(); let transactiondata = TransactionData::SetScript(SetScriptTransaction::new(compiledscript.script()));
let timestamp = getcurrentepochmillis(); let signedtx = Transaction::new( transactiondata, Amount::new(100000, None), timestamp, privatekey.publickey(), 3, ChainId::TESTNET.byte(), ) .sign(&privatekey) .unwrap();
node.broadcast(&signed_tx).await.unwrap(); ```
Same transaction from REST API
```rust use wavesrust::api::{Node, Profile}; use wavesrust::model::{Address, ByteString, ChainId, Id, TransactionDataInfo};
let node = Node::from_profile(Profile::STAGENET);
let id = Id::fromstring("CWuFY42te67sLmc5gwt4NxwHmFjVfJdHkKuLyshTwEct").unwrap(); let txinfo = node.gettransactioninfo(&id).await.unwrap();
println!("type: {:?}", txinfo.txtype()); println!("id: {:?}", txinfo.id()); println!("fee: {:?}", txinfo.fee().value()); println!("feeAssetId: {:?}", txinfo.fee().assetid()); println!("timestamp: {:?}", txinfo.timestamp()); println!("version: {:?}", txinfo.version()); println!("chainId: {:?}", txinfo.chainid()); println!("sender: {:?}",txinfo.publickey().address(ChainId::STAGENET.byte()).unwrap().encoded()); println!("senderPublicKey: {:?}", txinfo.publickey().encoded()); println!("height: {:?}", txinfo.height()); println!("applicationStatus: {:?}", txinfo.status());
let ethtx = match txinfo.data() { TransactionDataInfo::Ethereum(ethtx) => ethtx, _ => panic!("expected ethereum transaction"), };
println!("bytes: {}", ethtx.bytes().encoded()); println!("{:?}", ethtx.payload()); ```
```rust use wavesrust::model::PrivateKey; use wavesrust::util::Crypto;
let bob = PrivateKey::fromseed(&Crypto::getrandomseedphrase(12), 0).unwrap(); let alice = PrivateKey::fromseed(&Crypto::getrandomseedphrase(12), 0).unwrap(); ```
```rust use wavesrust::api::{Node, Profile}; use wavesrust::model::{Amount, AssetId, ChainId, ExchangeTransaction, Order, OrderType, PriceMode, PrivateKey, Transaction, TransactionData}; use wavesrust::util::{getcurrentepochmillis, Crypto};
let price = Amount::new(1000, None); let amount = Amount::new( 100, Some(AssetId::from_string("8bt2MZjuUCJPmfucPfaZPTXqrxmoCHCC8gVnbjZ7bhH6").unwrap()), );
let matcher_fee = 300000;
let buy = Order::v4( ChainId::TESTNET.byte(), getcurrentepochmillis(), alice.publickey(), Amount::new(300000, None), OrderType::Buy, amount.clone(), price.clone(), bob.publickey(), Order::defaultexpiration(getcurrentepoch_millis()), PriceMode::AssetDecimals, ) .sign(&alice) .unwrap();
let sell = Order::v3( ChainId::TESTNET.byte(), getcurrentepochmillis(), bob.publickey(), Amount::new(300000, None), OrderType::Sell, amount.clone(), price.clone(), bob.publickey(), Order::defaultexpiration(getcurrentepoch_millis()), ) .sign(&bob) .unwrap();
let transactiondata = TransactionData::Exchange(ExchangeTransaction::new( buy.clone(), sell.clone(), amount.value(), price.value(), matcherfee, matcher_fee, ));
let timestamp = getcurrentepochmillis(); let signedtx = Transaction::new( transactiondata, Amount::new(300000, None), timestamp, bob.publickey(), 4, ChainId::TESTNET.byte(), ) .sign(&bob) .unwrap();
let txinfo = node.broadcast(&signedtx).await.unwrap(); ```
```rust use wavesrust::model::PrivateKey; use wavesrust::util::Crypto;
let bob = PrivateKey::fromseed(&Crypto::getrandomseedphrase(12), 0); let alice = PrivateKey::fromseed(&Crypto::getrandomseedphrase(12), 0); ```
```rust use wavesrust::api::{Node, Profile}; use wavesrust::model::{Amount, ChainId, IssueTransaction, PrivateKey, Transaction, TransactionData}; use wavesrust::util::{Crypto, getcurrentepochmillis};
let transactiondata = TransactionData::Issue(IssueTransaction::new( "Asset".toowned(), "this is test asset".to_owned(), 1000, 2, false, None, ));
let timestamp = getcurrentepochmillis(); let signedtx = Transaction::new( transactiondata, Amount::new(100400000, None), timestamp, alice.publickey(), 3, ChainId::TESTNET.byte(), ) .sign(&alice) .unwrap();
node.broadcast(&signed_tx).await.unwrap(); ```
```rust use wavesrust::api::{Node, Profile}; use wavesrust::model::{Amount, ChainId, PrivateKey, SetScriptTransaction, Transaction, TransactionData}; use wavesrust::util::{getcurrentepochmillis, Crypto};
let script = r#" {-# STDLIBVERSION 5 #-} {-# CONTENTTYPE DAPP #-} {-# SCRIPT_TYPE ACCOUNT #-}
@Callable(inv)
func call(bv: ByteVector, b: Boolean, int: Int, str: String, list: List[Int]) = {
let asset = Issue("Asset", "", 1, 0, true)
let assetId = asset.calculateAssetId()
let lease = Lease(inv.caller, 7)
let leaseId = lease.calculateLeaseId()
[
BinaryEntry("bin", assetId),
BooleanEntry("bool", true),
IntegerEntry("int", 100500),
StringEntry("assetId", assetId.toBase58String()),
StringEntry("leaseId", leaseId.toBase58String()),
StringEntry("del", ""),
DeleteEntry("del"),
asset,
SponsorFee(assetId, 1),
Reissue(assetId, 4, false),
Burn(assetId, 3),
ScriptTransfer(inv.caller, 2, assetId),
lease,
LeaseCancel(lease.calculateLeaseId())
]
}
"#;
let compiledscript = node.compilescript(script, true).await.unwrap();
let transactiondata = TransactionData::SetScript(SetScriptTransaction::new(compiledscript.script()));
let timestamp = getcurrentepochmillis(); let signedtx = Transaction::new( transactiondata, Amount::new(500000, None), timestamp, alice.publickey(), 3, ChainId::TESTNET.byte(), ) .sign(&alice) .unwrap();
node.broadcast(&signed_tx).await.unwrap(); ```
```rust use wavesrust::api::{Node, Profile}; use wavesrust::model::{Address, Amount, Base64String, ByteString, ChainId, Function, InvokeScriptTransaction, PrivateKey, Transaction, TransactionData}; use wavesrust::model::Arg::{Binary, Boolean, Integer, List, String}; use wavesrust::util::{getcurrentepoch_millis, Crypto};
let aliceaddress = Address::frompublickey(ChainId::TESTNET.byte(), &alice.publickey()).unwrap(); let transactiondata = TransactionData::InvokeScript(InvokeScriptTransaction::new( aliceaddress.clone(), Function::new( "call".toowned(), vec![ Binary(Base64String::frombytes(vec![1, 2, 3])), Boolean(true), Integer(100500), String(alice_address.encoded()), List(vec![Integer(100500)]), ], ), vec![ Amount::new(1, None), Amount::new(2, None), Amount::new(3, None), Amount::new(4, None), ], ));
let timestamp = getcurrentepochmillis(); let signedtx = Transaction::new( transactiondata, Amount::new(100500000, None), timestamp, bob.publickey(), 3, ChainId::TESTNET.byte(), ) .sign(&bob) .unwrap();
node.broadcast(&signed_tx).await.unwrap(); ```