Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/funding-pool-route.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@parity/truapi": patch
---

Funding deposits in the native token, and in a stablecoin the PSM cannot serve, convert through the asset-conversion pools: one XCM program swaps them into CASH (through the native token for a stablecoin) and teleports it to People, as getcash does. `funding-check` accepts `--asset dot`.
21 changes: 12 additions & 9 deletions rust/crates/truapi-host-cli/src/funding_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const MISSING_POLLS: u32 = 5;
/// The asset a provider pays the deposit in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum FundingAsset {
/// The native token, swapped into CASH through the pools.
Dot,
/// dotUSD, which is CASH already: teleported to People.
Cash,
/// Minted into CASH through the PSM, then teleported.
Expand Down Expand Up @@ -60,12 +62,13 @@ impl FundingAssets {
}
}

/// The asset id and the getcash source id for `asset`.
fn source(&self, asset: FundingAsset) -> (u32, &'static str) {
/// The deposit asset and the getcash source id for `asset`.
fn source(&self, asset: FundingAsset) -> (DepositAsset, &'static str) {
match asset {
FundingAsset::Cash => (self.cash, "dotusd-assethub"),
FundingAsset::Usdt => (self.usdt, "usdt-assethub"),
FundingAsset::Usdc => (self.usdc, "usdc-assethub"),
FundingAsset::Dot => (DepositAsset::Native, "dot-assethub"),
FundingAsset::Cash => (DepositAsset::Asset(self.cash), "dotusd-assethub"),
FundingAsset::Usdt => (DepositAsset::Asset(self.usdt), "usdt-assethub"),
FundingAsset::Usdc => (DepositAsset::Asset(self.usdc), "usdc-assethub"),
}
}
}
Expand Down Expand Up @@ -139,29 +142,29 @@ async fn open_and_assign(
asset: FundingAsset,
amount: u128,
) -> Result<String> {
let (asset_id, source_id) = assets.source(asset);
let (deposit_asset, source_id) = assets.source(asset);
let intent = runtime
.open_funding(FundingDirection::In, Some(amount))
.await
.map_err(|error| anyhow::anyhow!("opening a session failed: {}", error.reason))?
.context("the session was dismissed")?;
let expected = runtime
.quote_funding_deposit(&intent, DepositAsset::Asset(asset_id))
.quote_funding_deposit(&intent, deposit_asset)
.await
.map_err(|error| anyhow::anyhow!("quoting the deposit failed: {}", error.reason))?;
let account = runtime
.assign_funding_deposit(
&intent,
DepositRequest {
source_id: source_id.to_string(),
asset: DepositAsset::Asset(asset_id),
asset: deposit_asset,
expected,
},
)
.await
.map_err(|error| anyhow::anyhow!("assigning a deposit account failed: {}", error.reason))?;
println!("session {intent}, crediting {amount} CASH units");
println!("pay {expected} of asset {asset_id} ({source_id}) on Asset Hub to");
println!("pay {expected} of {deposit_asset:?} ({source_id}) on Asset Hub to");
println!(
" {}",
truapi::host_logic::product_account::product_public_key_to_address(account)
Expand Down
7 changes: 7 additions & 0 deletions rust/crates/truapi/src/host_logic/funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ pub struct FundingDeposit {
pub expected: u128,
/// How the deposit becomes CASH on People.
pub route: ConversionRoute,
/// CASH the session asks to credit, which a swap must not land below.
pub target: Option<u128>,
}

/// How a deposit becomes CASH on People, fixed when its account is assigned
Expand All @@ -111,6 +113,10 @@ pub enum ConversionRoute {
/// Minting fee the route was chosen at, in parts per million.
fee_ppm: u32,
},
/// The deposit is the native token, or a stablecoin the PSM cannot
/// serve: swap it to CASH through the asset-conversion pools, then
/// teleport.
Pool,
}

/// A conversion transaction handed to Asset Hub, with what tells whether it
Expand Down Expand Up @@ -812,6 +818,7 @@ mod tests {
account: [1; 32],
expected: 50,
route: ConversionRoute::Teleport,
target: None,
}),
..session(FundingDirection::In)
}
Expand Down
8 changes: 6 additions & 2 deletions rust/crates/truapi/src/runtime/funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,10 @@ impl FundingRegistry {
plan: DepositPlan,
derive: impl Fn(u32) -> Result<[u8; 32], GenericError>,
) -> Result<[u8; 32], AssignDepositError> {
self.get(intent)
let target = self
.get(intent)
.ok_or(AssignDepositError::NotFound)
.and_then(|session| assignable(&session))?;
.and_then(|session| assignable(&session).map(|()| session.amount))?;
let DepositPlan { request, route } = plan;
for _ in 0..MAX_USED_ACCOUNTS {
let number = self
Expand All @@ -274,6 +275,7 @@ impl FundingRegistry {
account,
expected: request.expected,
route,
target,
};
let intent = intent.to_string();
return self
Expand Down Expand Up @@ -1300,6 +1302,7 @@ mod tests {
account: account(3),
expected: 50,
route: ConversionRoute::Teleport,
target: Some(100),
})
)
);
Expand Down Expand Up @@ -1532,6 +1535,7 @@ mod tests {
account: keypair(1).public.to_bytes(),
expected: 50,
route: ConversionRoute::Teleport,
target: None,
}
}

Expand Down
Loading
Loading