-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: withdraw payments Signed-off-by: David Dal Busco <[email protected]> * feat: generate did Signed-off-by: David Dal Busco <[email protected]> * docs: withdraw balance Signed-off-by: David Dal Busco <[email protected]> * feat: script to widthdraw payments Signed-off-by: David Dal Busco <[email protected]> * test: should throw Signed-off-by: David Dal Busco <[email protected]> --------- Signed-off-by: David Dal Busco <[email protected]>
- Loading branch information
1 parent
10835e5
commit edd0eec
Showing
10 changed files
with
163 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env node | ||
|
||
import { consoleActorLocal } from './actor.mjs'; | ||
|
||
try { | ||
const { withdraw_payments } = await consoleActorLocal(); | ||
|
||
await withdraw_payments(); | ||
|
||
console.log('✅ Payments successfully withdrawn.'); | ||
} catch (error) { | ||
console.error('❌ Payments cannot be withdrawn', error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod payments; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use candid::Principal; | ||
use ic_cdk::id; | ||
use ic_ledger_types::{ | ||
account_balance, AccountBalanceArgs, AccountIdentifier, BlockIndex, Memo, Tokens, | ||
}; | ||
use junobuild_shared::constants::IC_TRANSACTION_FEE_ICP; | ||
use junobuild_shared::env::LEDGER; | ||
use junobuild_shared::ledger::{principal_to_account_identifier, transfer_token, SUB_ACCOUNT}; | ||
|
||
/// Withdraws the entire balance of the Console — i.e., withdraws the payments for the additional | ||
/// Satellites and Orbiters that have been made. | ||
/// | ||
/// The destination account for the withdrawal is one of mine (David here). | ||
/// | ||
/// # Returns | ||
/// - `Ok(BlockIndex)`: If the transfer was successful, it returns the block index of the transaction. | ||
/// - `Err(String)`: If an error occurs during the process, it returns a descriptive error message. | ||
/// | ||
/// # Errors | ||
/// This function can return errors in the following cases: | ||
/// - If the account balance retrieval fails. | ||
/// - If the transfer to the ledger fails due to insufficient balance or other issues. | ||
/// | ||
/// # Example | ||
/// ```rust | ||
/// let result = withdraw_balance().await; | ||
/// match result { | ||
/// Ok(block_index) => println!("Withdrawal successful! Block index: {}", block_index), | ||
/// Err(e) => println!("Error during withdrawal: {}", e), | ||
/// } | ||
/// ``` | ||
pub async fn withdraw_balance() -> Result<BlockIndex, String> { | ||
let account_identifier: AccountIdentifier = AccountIdentifier::from_hex( | ||
"e4aaed31b1cbf2dfaaca8ef9862a51b04fc4a314e2c054bae8f28d501c57068b", | ||
)?; | ||
|
||
let balance = console_balance().await?; | ||
|
||
let block_index = transfer_token( | ||
account_identifier, | ||
Memo(0), | ||
balance - IC_TRANSACTION_FEE_ICP, | ||
IC_TRANSACTION_FEE_ICP, | ||
) | ||
.await | ||
.map_err(|e| format!("failed to call ledger: {:?}", e))? | ||
.map_err(|e| format!("ledger transfer error {:?}", e))?; | ||
|
||
Ok(block_index) | ||
} | ||
|
||
async fn console_balance() -> Result<Tokens, String> { | ||
let ledger = Principal::from_text(LEDGER).unwrap(); | ||
|
||
let console_account_identifier: AccountIdentifier = | ||
principal_to_account_identifier(&id(), &SUB_ACCOUNT); | ||
|
||
let args: AccountBalanceArgs = AccountBalanceArgs { | ||
account: console_account_identifier, | ||
}; | ||
|
||
let tokens = account_balance(ledger, args) | ||
.await | ||
.map_err(|e| format!("failed to call ledger balance: {:?}", e))?; | ||
|
||
Ok(tokens) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters