1+ use defi_wallet_connect:: v2:: WCMiddleware ;
12use ethers:: abi:: Address ;
23use ethers:: core:: types:: transaction:: eip2718:: TypedTransaction ;
4+ use ethers:: prelude:: * ;
35use eyre:: Result ;
46use image:: Luma ;
57use qrcode:: QrCode ;
68use std:: fs;
79use std:: io;
810use std:: io:: Write ;
9- //use ethers::ethers_providers::Middleware;
10- use defi_wallet_connect:: v2:: WCMiddleware ;
11- use ethers:: prelude:: * ;
1211
1312use std:: str:: FromStr ;
1413
1514use defi_wallet_connect:: v2:: { Client , ClientOptions , Metadata , RequiredNamespaces , SessionInfo } ;
1615use std:: error:: Error ;
1716use std:: io:: BufRead ;
1817
19- const RPC_URL : & str = env ! ( "MY_RPC_URL" ) ;
20- const CHAIN_ID : & str = env ! ( "MY_CHAIN_ID" ) ;
21- const FROM_ADDRESS : & str = env ! ( "MY_FROM_ADDRESS" ) ;
22- const TO_ADDRESS : & str = env ! ( "MY_TO_ADDRESS" ) ;
23-
2418#[ derive( Debug , Default ) ]
2519pub struct WalletConnectTxCommon {
2620 pub gas_limit : String , // decimal string, "1"
@@ -43,7 +37,9 @@ pub struct WalletConnectTxEip155 {
4337async fn make_client (
4438 callback_sender : Option < tokio:: sync:: mpsc:: UnboundedSender < String > > ,
4539) -> Result < Client , relay_client:: Error > {
46- let mychain = format ! ( "eip155:{}" , CHAIN_ID ) ;
40+ let chain_id = std:: env:: var ( "MY_CHAIN_ID" ) . expect ( "MY_CHAIN_ID not set" ) ;
41+ let mychain = format ! ( "eip155:{}" , chain_id) ;
42+
4743 let opts = ClientOptions {
4844 relay_server : "wss://relay.walletconnect.com" . parse ( ) . expect ( "url" ) ,
4945 project_id : std:: env:: args ( ) . skip ( 1 ) . next ( ) . expect ( "project_id" ) ,
@@ -200,7 +196,6 @@ pub async fn send_eip155_transaction_blocking(
200196 . await
201197 . map_err ( |e| eyre:: eyre!( "send_typed_transaction error {}" , e. to_string( ) ) ) ?;
202198
203- //Ok(tx_bytes.0.to_vec())
204199 Ok ( tx_bytes. 0 . to_vec ( ) )
205200}
206201
@@ -225,7 +220,8 @@ async fn send_ethereum_transaction(
225220 let from_address = Address :: from_str ( from) ?;
226221 let to_address = Address :: from_str ( to) ?;
227222
228- let provider = Provider :: < Http > :: try_from ( RPC_URL ) ?;
223+ let rpc_url = std:: env:: var ( "MY_RPC_URL" ) . expect ( "MY_RPC_URL not set" ) ;
224+ let provider = Provider :: < Http > :: try_from ( rpc_url. as_str ( ) ) ?;
229225 let chain_id = provider. get_chainid ( ) . await ?;
230226 let nonce = provider. get_transaction_count ( from_address, None ) . await ?;
231227 // Construct the transaction
@@ -296,18 +292,23 @@ async fn main() -> Result<(), Box<dyn Error>> {
296292 }
297293
298294 if test_sign_tx {
295+ let from_address = std:: env:: var ( "MY_FROM_ADDRESS" ) . expect ( "MY_FROM_ADDRESS not set" ) ;
296+ let to_address = std:: env:: var ( "MY_TO_ADDRESS" ) . expect ( "MY_TO_ADDRESS not set" ) ;
297+
299298 // convert fromaddress to 20 fixed byte array
300- let fromaddress = Address :: from_str ( & FROM_ADDRESS ) ?;
299+ let fromaddress = Address :: from_str ( & from_address ) ?;
301300
302301 let txinfo = WalletConnectTxEip155 {
303302 common : WalletConnectTxCommon {
304- chainid : CHAIN_ID . parse :: < u64 > ( ) ?,
303+ chainid : std:: env:: var ( "MY_CHAIN_ID" )
304+ . expect ( "MY_CHAIN_ID not set" )
305+ . parse :: < u64 > ( ) ?,
305306 gas_limit : "21000" . into ( ) ,
306307 gas_price : "1000000000" . into ( ) ,
307- nonce : "0 " . into ( ) ,
308+ nonce : "" . into ( ) ,
308309 web3api_url : "" . into ( ) ,
309310 } ,
310- to : TO_ADDRESS . into ( ) ,
311+ to : to_address . into ( ) ,
311312 data : vec ! [ ] ,
312313 value : "1000" . into ( ) ,
313314 } ;
@@ -331,10 +332,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
331332 }
332333
333334 if test_send_tx {
335+ let from_address = std:: env:: var ( "MY_FROM_ADDRESS" ) . expect ( "MY_FROM_ADDRESS not set" ) ;
336+ let to_address = std:: env:: var ( "MY_TO_ADDRESS" ) . expect ( "MY_TO_ADDRESS not set" ) ;
337+
334338 let amount = U256 :: from_dec_str ( "1000012345678912" ) ?; // 1 ETH for example
335339
336340 let tx_hash =
337- send_ethereum_transaction ( & mut client, FROM_ADDRESS , TO_ADDRESS , amount) . await ?;
341+ send_ethereum_transaction ( & mut client, & from_address , & to_address , amount) . await ?;
338342
339343 println ! ( "Transaction sent with hash: {:?}" , tx_hash) ;
340344 }
0 commit comments