eth.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use num_bigint::BigUint;
  19. use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
  20. use darkfi::{
  21. service::eth::{erc20_transfer_data, EthClient, EthTx},
  22. util::{decode_base10, encode_base10},
  23. Result,
  24. };
  25. #[async_std::main]
  26. async fn main() -> Result<()> {
  27. TermLogger::init(
  28. LevelFilter::Trace,
  29. simplelog::Config::default(),
  30. TerminalMode::Mixed,
  31. ColorChoice::Auto,
  32. )?;
  33. let acc = "0x113b6648f34f4d0340d04ff171cbcf0b49d47827".to_string();
  34. let key = "67cbb73cb293eea5fa2a7025d5479dbd50319010c03fd8821917ad0d9d53276c".to_string();
  35. let mut eth = EthClient::new("", "/home/parazyd/.ethereum/ropsten/geth.ipc", "foobar");
  36. eth.main_keypair.private_key = key;
  37. eth.main_keypair.public_key = acc.clone();
  38. //let key = generate_privkey();
  39. //let passphrase = "foobar".to_string();
  40. //let rep = eth.import_privkey(&key, &passphrase).await?;
  41. //println!("{:#?}", rep);
  42. let passphrase = "foobar".to_string();
  43. // Recipient address
  44. let dest = "0xcD640A363305c21255c58Ba9C8c1C508e6997a12".to_string();
  45. // Latest known block, used to calculate present balance.
  46. let block = eth.block_number().await?;
  47. let block = block.as_str().unwrap();
  48. // Native ETH balance
  49. let hexbalance = eth.get_eth_balance(&acc, block).await?;
  50. let hexbalance = hexbalance.as_str().unwrap().trim_start_matches("0x");
  51. let balance = BigUint::parse_bytes(hexbalance.as_bytes(), 16).unwrap();
  52. println!("{}", encode_base10(balance, 18));
  53. /*
  54. // Transfer native ETH
  55. let tx = EthTx::new(
  56. &acc,
  57. &dest,
  58. None,
  59. None,
  60. Some(decode_base10("0.051", 18, true)?),
  61. None,
  62. None,
  63. );
  64. let rep = eth.send_transaction(&tx, &passphrase).await?;
  65. println!("TXID: {}", rep.as_str().unwrap());
  66. */
  67. // ERC20 Token balance
  68. let mint = "0xad6d458402f60fd3bd25163575031acdce07538d"; // Ropsten DAI (get on Uniswap)
  69. let hexbalance = eth.get_erc20_balance(&acc, mint).await?;
  70. let hexbalance = hexbalance.as_str().unwrap().trim_start_matches("0x");
  71. let balance = BigUint::parse_bytes(hexbalance.as_bytes(), 16).unwrap();
  72. println!("{}", encode_base10(balance, 18));
  73. // Transfer ERC20 token
  74. let tx = EthTx::new(
  75. &acc,
  76. mint,
  77. None,
  78. None,
  79. None,
  80. Some(erc20_transfer_data(&dest, decode_base10("1", 18, true)?)),
  81. None,
  82. );
  83. let rep = eth.send_transaction(&tx, &passphrase).await?;
  84. println!("TXID: {}", rep.as_str().unwrap());
  85. Ok(())
  86. }