ston-fi / tonlib-rs

Rust SDK for The Open Network
https://github.com/ston-fi/tonlib-rs
MIT License
187 stars 41 forks source link

Example does not compile #75

Open elsvv opened 3 months ago

elsvv commented 3 months ago

README create simple transfer example does not compile.

Code:

use num_bigint::BigUint;
use std::time::SystemTime;

use tonlib::address::TonAddress;
use tonlib::cell::BagOfCells;
use tonlib::message::TransferMessage;
use tonlib::wallet::TonWallet;
use tonlib::client::TonClient;
use tonlib::client::TonClientInterface;
use tonlib::mnemonic::Mnemonic;
use tonlib::wallet::WalletVersion;

async fn create_simple_transfer() -> anyhow::Result<()> {
    let mnemonic = Mnemonic::new(
        vec![
            "dose", "ice", "enrich", "trigger", "test", "dove", "century", "still", "betray",
            "gas", "diet", "dune",
        ],
        &None,
        )?;
    let key_pair = mnemonic.to_key_pair()?;
    let seqno =  30000000;

    let client = TonClient::default().await?;
    let wallet = TonWallet::derive_default(WalletVersion::V4R2, &key_pair)?;
    let dest: TonAddress = "<destination wallet address>".parse()?;
    let value = BigUint::from(10000000u64); // 0.01 TON
    let transfer = TransferMessage::new(&dest, &value).build()?;
    let now = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)?
        .as_secs() as u32;
    let body = wallet.create_external_body(now + 60, seqno, vec![transfer])?;
    let signed = wallet.sign_external_body(&body)?;
    let wrapped = wallet.wrap_signed_body(signed, false)?;
    let boc = BagOfCells::from_root(wrapped);
    let tx = boc.serialize(true)?;
    let hash = client.send_raw_message_return_hash(tx.as_slice()).await?;

    Ok(())
}

fn main() {
    create_simple_transfer();
}

Error:

error[E0277]: the trait bound `Vec<tonlib::cell::Cell>: AsRef<[Arc<tonlib::cell::Cell>]>` is not satisfied
   --> src/main.rs:35:61
    |
35  |     let body = wallet.create_external_body(now + 60, seqno, vec![transfer])?;
    |                       --------------------                  ^^^^^^^^^^^^^^ the trait `AsRef<[Arc<tonlib::cell::Cell>]>` is not implemented for `Vec<tonlib::cell::Cell>`
    |                       |
    |                       required by a bound introduced by this call
    |
    = help: the following other types implement trait `AsRef<T>`:
              <Vec<T, A> as AsRef<Vec<T, A>>>
              <Vec<T, A> as AsRef<[T]>>
note: required by a bound in `TonWallet::create_external_body`
   --> /User/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tonlib-0.15.4/src/wallet.rs:246:36
    |
246 |     pub fn create_external_body<T: AsRef<[ArcCell]>>(
    |                                    ^^^^^^^^^^^^^^^^ required by this bound in `TonWallet::create_external_body`

Rust version:

rustc --version:
rustc 1.79.0 (129f3b996 2024-06-10)

Cargo.toml:

[dependencies]
anyhow = "1.0"
num-bigint = "0.4"
tonlib = "0.15"
tokio = { version = "1", features = ["full"] }
Lawson220 commented 3 months ago

Phoeb

ittechhunter commented 3 months ago

@elsvv, I am getting the same error So I changed like this:

use std::sync::Arc;

async fn create_simple_transfer() -> anyhow::Result<()> {
    ...
    let transfer_arc: Vec<Arc<Cell>> = vec![transfer].into_iter().map(Arc::new).collect();
    let body = wallet.create_external_body(now + 60, seqno, transfer_arc)?;
    ....
}
ittechhunter commented 3 months ago

@elsvv, I am getting the same error So I changed like this:

use std::sync::Arc;

async fn create_simple_transfer() -> anyhow::Result<()> {
    ...
    let transfer_arc: Vec<Arc<Cell>> = vec![transfer].into_iter().map(Arc::new).collect();
    let body = wallet.create_external_body(now + 60, seqno, transfer_arc)?;
    ....
}

But getting this result: image

ittechhunter commented 3 months ago

@elsvv, I've finally found the solution:

use anyhow::Result;
use num_bigint::BigUint;
use tonlib::client::TonClientInterface;
use std::time::SystemTime;
use std::sync::Arc;

use tonlib::address::TonAddress;
use tonlib::cell::Cell;
use tonlib::cell::BagOfCells;
use tonlib::message::TransferMessage;

use tonlib::config::TESTNET_CONFIG;
use tonlib::client::TonClient;
use tonlib::client::TonClientBuilder;
use tonlib::client::TonConnectionParams;
use tonlib::contract::TonContractFactory;

use tonlib::mnemonic::Mnemonic;
use tonlib::mnemonic::KeyPair;

use tonlib::wallet::{TonWallet, WalletVersion};
use tonlib::contract::{TonWalletContract};

async fn create_simple_transfer() -> Result<()> {
    let client = create_client().await?;
    let contract_factory = TonContractFactory::builder(&client).build().await?;
    let key_pair = create_key_pair().await?;
    let wallet = TonWallet::derive_default(WalletVersion::V3R2, &key_pair)?;
    let wallet_contract = contract_factory.get_contract(&wallet.address);
    let seqno =  wallet_contract.seqno().await?;
    let dest: TonAddress = "....".parse()?;
    let value = BigUint::from(10000000u64); // 0.01 TON
    let transfer = TransferMessage::new(&dest, &value).build()?;
    let now = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)?
        .as_secs() as u32;
    let msg_arc: Vec<Arc<Cell>> = vec![transfer].into_iter().map(Arc::new).collect();
    let msg_cell = wallet.create_external_message(now + 60, seqno, msg_arc, false)?;
    let boc = BagOfCells::from_root(msg_cell);
    let tx = boc.serialize(true)?;
    let hash = client.send_raw_message_return_hash(tx.as_slice()).await?;

    Ok(())
}