Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Add account_index parameter to Monero configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
silverpill committed Mar 18, 2023
1 parent 7640598 commit 9a513c9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added

- Added `fep-e232` feature flag (disabled by default).
- Added `account_index` parameter to Monero configuration.

### Fixed

Expand Down
43 changes: 22 additions & 21 deletions config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,28 @@ registration:
type: open

blockchains:
# Parameters for hardhat local node
- chain_id: eip155:31337
chain_metadata:
chain_name: localhost
currency_name: ETH
currency_symbol: ETH
currency_decimals: 18
public_api_url: 'http://127.0.0.1:8546'
explorer_url: null
contract_address: '0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9'
contract_dir: contracts
api_url: 'http://127.0.0.1:8546'
signing_key: 'ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
chain_sync_step: 100
chain_reorg_max_depth: 0
# # Parameters for local Monero node
# - chain_id: monero:regtest
# node_url: 'http://127.0.0.1:58081'
# wallet_url: 'http://127.0.0.1:58083'
# wallet_name: test
# wallet_password: test
# Parameters for local Monero node
- chain_id: monero:regtest
node_url: 'http://127.0.0.1:58081'
wallet_url: 'http://127.0.0.1:58083'
wallet_name: test
wallet_password: test
account_index: 0
# # Parameters for hardhat local node
# - chain_id: eip155:31337
# chain_metadata:
# chain_name: localhost
# currency_name: ETH
# currency_symbol: ETH
# currency_decimals: 18
# public_api_url: 'http://127.0.0.1:8546'
# explorer_url: null
# contract_address: '0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9'
# contract_dir: contracts
# api_url: 'http://127.0.0.1:8546'
# signing_key: 'ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
# chain_sync_step: 100
# chain_reorg_max_depth: 0

ipfs_api_url: 'http://127.0.0.1:5001'
ipfs_gateway_url: 'http://127.0.0.1:8001'
1 change: 1 addition & 0 deletions contrib/mitra_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ retention:
# wallet_url: 'http://127.0.0.1:18083'
# wallet_name: null
# wallet_password: null
# account_index: 0
# - chain_id: eip155:31337
# chain_metadata:
# chain_name: localhost
Expand Down
4 changes: 4 additions & 0 deletions mitra-config/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ impl EthereumConfig {
}
}

fn default_wallet_account_index() -> u32 { 0 }

#[derive(Clone, Deserialize)]
pub struct MoneroConfig {
pub chain_id: ChainId,
Expand All @@ -59,6 +61,8 @@ pub struct MoneroConfig {
// monero-wallet-rpc is running with --wallet-dir option
pub wallet_name: Option<String>,
pub wallet_password: Option<String>,
#[serde(default = "default_wallet_account_index")]
pub account_index: u32,
}

#[derive(Clone, Deserialize)]
Expand Down
3 changes: 1 addition & 2 deletions src/monero/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::models::{
};
use super::wallet::{
open_monero_wallet,
DEFAULT_ACCOUNT,
MoneroError,
};

Expand Down Expand Up @@ -45,7 +44,7 @@ pub async fn check_expired_invoice(
let address_index = wallet_client.get_address_index(address).await?;
let transfers = wallet_client.incoming_transfers(
TransferType::Available,
Some(DEFAULT_ACCOUNT),
Some(config.account_index),
Some(vec![address_index.minor]),
).await?
.transfers
Expand Down
6 changes: 3 additions & 3 deletions src/monero/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use super::wallet::{
get_subaddress_balance,
open_monero_wallet,
send_monero,
DEFAULT_ACCOUNT,
MoneroError,
};

Expand Down Expand Up @@ -68,7 +67,7 @@ pub async fn check_monero_subscriptions(
log::info!("{} invoices are waiting for payment", address_waitlist.len());
let incoming_transfers = wallet_client.incoming_transfers(
TransferType::Available,
Some(DEFAULT_ACCOUNT),
Some(config.account_index),
Some(address_waitlist),
).await?;
incoming_transfers.transfers
Expand All @@ -78,7 +77,7 @@ pub async fn check_monero_subscriptions(
if let Some(transfers) = maybe_incoming_transfers {
for transfer in transfers {
let address_data = wallet_client.get_address(
DEFAULT_ACCOUNT,
config.account_index,
Some(vec![transfer.subaddr_index.minor]),
).await?;
let subaddress_data = get_single_item(address_data.addresses)?;
Expand Down Expand Up @@ -143,6 +142,7 @@ pub async fn check_monero_subscriptions(
let payout_address = Address::from_str(&payment_info.payout_address)?;
let payout_amount = send_monero(
&wallet_client,
config.account_index,
address_index.minor,
payout_address,
).await?;
Expand Down
16 changes: 8 additions & 8 deletions src/monero/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use mitra_config::MoneroConfig;

use crate::database::DatabaseError;

pub const DEFAULT_ACCOUNT: u32 = 0;

#[derive(thiserror::Error, Debug)]
pub enum MoneroError {
#[error(transparent)]
Expand Down Expand Up @@ -71,9 +69,10 @@ pub async fn create_monero_address(
config: &MoneroConfig,
) -> Result<Address, MoneroError> {
let wallet_client = open_monero_wallet(config).await?;
let account_index = config.account_index;
let (address, address_index) =
wallet_client.create_address(DEFAULT_ACCOUNT, None).await?;
log::info!("created monero address {}/{}", DEFAULT_ACCOUNT, address_index);
wallet_client.create_address(account_index, None).await?;
log::info!("created monero address {}/{}", account_index, address_index);
Ok(address)
}

Expand All @@ -100,12 +99,13 @@ pub async fn get_subaddress_balance(
/// https://monerodocs.org/interacting/monero-wallet-rpc-reference/#sweep_all
pub async fn send_monero(
wallet_client: &WalletClient,
from_account: u32,
from_address: u32,
to_address: Address,
) -> Result<Amount, MoneroError> {
let sweep_args = SweepAllArgs {
address: to_address,
account_index: DEFAULT_ACCOUNT,
account_index: from_account,
subaddr_indices: Some(vec![from_address]),
priority: TransferPriority::Default,
mixin: 15,
Expand All @@ -126,7 +126,7 @@ pub async fn send_monero(
// https://github.com/monero-project/monero/issues/8372
let maybe_transfer = wallet_client.get_transfer(
tx_hash,
Some(DEFAULT_ACCOUNT),
Some(from_account),
).await?;
let transfer_status = maybe_transfer
.map(|data| data.transfer_type.into())
Expand All @@ -135,7 +135,7 @@ pub async fn send_monero(
log::error!(
"sent transaction {:x} from {}/{}, {}",
tx_hash,
DEFAULT_ACCOUNT,
from_account,
from_address,
transfer_status,
);
Expand All @@ -145,7 +145,7 @@ pub async fn send_monero(
log::info!(
"sent transaction {:x} from {}/{}, amount {}, fee {}",
tx_hash,
DEFAULT_ACCOUNT,
from_account,
from_address,
amount,
fee,
Expand Down

0 comments on commit 9a513c9

Please sign in to comment.