2021-08-23 09:56:44 +00:00
|
|
|
#![cfg(not(target_arch = "wasm32"))]
|
2020-09-24 21:33:09 +00:00
|
|
|
use ethers_providers::{Http, Middleware, Provider};
|
|
|
|
|
|
|
|
use ethers_core::types::TransactionRequest;
|
2020-10-08 15:56:36 +00:00
|
|
|
use ethers_middleware::signer::SignerMiddleware;
|
2021-07-29 20:22:25 +00:00
|
|
|
use ethers_signers::{LocalWallet, Signer};
|
2020-09-24 21:33:09 +00:00
|
|
|
use std::{convert::TryFrom, time::Duration};
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(not(feature = "celo"))]
|
|
|
|
async fn send_eth() {
|
|
|
|
use ethers_core::utils::Ganache;
|
|
|
|
|
|
|
|
let ganache = Ganache::new().spawn();
|
|
|
|
|
|
|
|
// this private key belongs to the above mnemonic
|
2020-10-02 08:41:16 +00:00
|
|
|
let wallet: LocalWallet = ganache.keys()[0].clone().into();
|
|
|
|
let wallet2: LocalWallet = ganache.keys()[1].clone().into();
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
// connect to the network
|
|
|
|
let provider = Provider::<Http>::try_from(ganache.endpoint())
|
|
|
|
.unwrap()
|
|
|
|
.interval(Duration::from_millis(10u64));
|
2021-07-29 20:22:25 +00:00
|
|
|
let chain_id = provider.get_chainid().await.unwrap().as_u64();
|
|
|
|
let wallet = wallet.with_chain_id(chain_id);
|
2020-10-08 15:56:36 +00:00
|
|
|
let provider = SignerMiddleware::new(provider, wallet);
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
// craft the transaction
|
|
|
|
let tx = TransactionRequest::new().to(wallet2.address()).value(10000);
|
|
|
|
|
|
|
|
let balance_before = provider
|
|
|
|
.get_balance(provider.address(), None)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// send it!
|
|
|
|
provider.send_transaction(tx, None).await.unwrap();
|
|
|
|
|
|
|
|
let balance_after = provider
|
|
|
|
.get_balance(provider.address(), None)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(balance_before > balance_after);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "celo")]
|
|
|
|
async fn test_send_transaction() {
|
|
|
|
// Celo testnet
|
|
|
|
let provider = Provider::<Http>::try_from("https://alfajores-forno.celo-testnet.org")
|
|
|
|
.unwrap()
|
|
|
|
.interval(Duration::from_millis(3000u64));
|
2021-07-29 20:22:25 +00:00
|
|
|
let chain_id = provider.get_chainid().await.unwrap().as_u64();
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
// Funded with https://celo.org/developers/faucet
|
|
|
|
// Please do not drain this account :)
|
|
|
|
let wallet = "d652abb81e8c686edba621a895531b1f291289b63b5ef09a94f686a5ecdd5db1"
|
2020-10-02 08:41:16 +00:00
|
|
|
.parse::<LocalWallet>()
|
2021-05-09 09:47:21 +00:00
|
|
|
.unwrap()
|
2021-07-29 20:22:25 +00:00
|
|
|
.with_chain_id(chain_id);
|
2020-10-08 15:56:36 +00:00
|
|
|
let client = SignerMiddleware::new(provider, wallet);
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
let balance_before = client.get_balance(client.address(), None).await.unwrap();
|
|
|
|
let tx = TransactionRequest::pay(client.address(), 100);
|
|
|
|
let _receipt = client
|
2020-12-17 11:26:01 +00:00
|
|
|
.send_transaction(tx, None)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2020-09-24 21:33:09 +00:00
|
|
|
.confirmations(3)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let balance_after = client.get_balance(client.address(), None).await.unwrap();
|
|
|
|
assert!(balance_before > balance_after);
|
|
|
|
}
|
2021-07-27 21:23:25 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
2021-07-29 20:22:25 +00:00
|
|
|
#[cfg(not(feature = "celo"))]
|
2021-07-27 21:23:25 +00:00
|
|
|
async fn send_transaction_handles_tx_from_field() {
|
|
|
|
use ethers_core::utils::Ganache;
|
|
|
|
|
|
|
|
// launch ganache
|
|
|
|
let ganache = Ganache::new().spawn();
|
|
|
|
|
|
|
|
// grab 2 wallets
|
|
|
|
let signer: LocalWallet = ganache.keys()[0].clone().into();
|
|
|
|
let other: LocalWallet = ganache.keys()[1].clone().into();
|
|
|
|
|
|
|
|
// connect to the network
|
|
|
|
let provider = Provider::try_from(ganache.endpoint()).unwrap();
|
|
|
|
let provider = SignerMiddleware::new(provider, signer.clone());
|
|
|
|
|
|
|
|
// sending a TransactionRequest with a from field of None should result
|
|
|
|
// in a transaction from the signer address
|
|
|
|
let request_from_none = TransactionRequest::new();
|
|
|
|
let receipt = provider
|
|
|
|
.send_transaction(request_from_none, None)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
let sent_tx = provider
|
|
|
|
.get_transaction(receipt.transaction_hash)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(sent_tx.from, signer.address());
|
|
|
|
|
|
|
|
// sending a TransactionRequest with the signer as the from address should
|
|
|
|
// result in a transaction from the signer address
|
|
|
|
let request_from_signer = TransactionRequest::new().from(signer.address());
|
|
|
|
let receipt = provider
|
|
|
|
.send_transaction(request_from_signer, None)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
let sent_tx = provider
|
|
|
|
.get_transaction(receipt.transaction_hash)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(sent_tx.from, signer.address());
|
|
|
|
|
|
|
|
// sending a TransactionRequest with a from address that is not the signer
|
|
|
|
// should result in a transaction from the specified address
|
|
|
|
let request_from_other = TransactionRequest::new().from(other.address());
|
|
|
|
let receipt = provider
|
|
|
|
.send_transaction(request_from_other, None)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
let sent_tx = provider
|
|
|
|
.get_transaction(receipt.transaction_hash)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(sent_tx.from, other.address());
|
|
|
|
}
|