Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support contracts sending funds to a Moonlight Account #2284

Merged
merged 3 commits into from
Sep 6, 2024
Merged
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 contracts/alice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ mod wasm {
unsafe fn transfer_to_contract(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |arg| STATE.transfer_to_contract(arg))
}

#[no_mangle]
unsafe fn transfer_to_account(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |arg| STATE.transfer_to_account(arg))
}
}
12 changes: 11 additions & 1 deletion contracts/alice/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use execution_core::transfer::{
withdraw::Withdraw, TransferToContract, TRANSFER_CONTRACT,
withdraw::Withdraw, TransferToAccount, TransferToContract,
TRANSFER_CONTRACT,
};

/// Alice contract.
Expand Down Expand Up @@ -35,4 +36,13 @@ impl Alice {
)
.expect("Transferring to contract should succeed");
}

pub fn transfer_to_account(&mut self, transfer: TransferToAccount) {
rusk_abi::call::<_, ()>(
TRANSFER_CONTRACT,
"transfer_to_account",
&transfer,
)
.expect("Transferring to account should succeed");
}
}
5 changes: 5 additions & 0 deletions contracts/transfer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ unsafe fn transfer_to_contract(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |arg| STATE.transfer_to_contract(arg))
}

#[no_mangle]
unsafe fn transfer_to_account(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |arg| STATE.transfer_to_account(arg))
}

// Queries

#[no_mangle]
Expand Down
39 changes: 37 additions & 2 deletions contracts/transfer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use execution_core::{
withdraw::{
Withdraw, WithdrawReceiver, WithdrawReplayToken, WithdrawSignature,
},
ReceiveFromContract, Transaction, TransferToContract,
PANIC_NONCE_NOT_READY, TRANSFER_CONTRACT,
ReceiveFromContract, Transaction, TransferToAccount,
TransferToContract, PANIC_NONCE_NOT_READY, TRANSFER_CONTRACT,
},
BlsScalar, ContractError, ContractId,
};
Expand Down Expand Up @@ -352,6 +352,41 @@ impl TransferState {
.expect("Calling receiver should succeed")
}

/// Transfer funds from a contract balance to a Moonlight account.
///
/// Contracts can call the function and expect that if it succeeds the funds
/// are successfully transferred to the account they specify.
///
/// # Panics
/// The function will panic if it is not being called by a contract, if it
/// is called by the transfer contract itself, or if the calling contract
/// doesn't have enough funds.
pub fn transfer_to_account(&mut self, transfer: TransferToAccount) {
let from = rusk_abi::caller()
.expect("A transfer to an account must happen in the context of a transaction");

if from == TRANSFER_CONTRACT {
panic!("Cannot be called directly by the transfer contract");
}

let from_balance = self
.contract_balances
.get_mut(&from)
.expect("Caller must have a balance");

if *from_balance < transfer.value {
panic!("Caller must have enough balance");
}

let account = self
.accounts
.entry(transfer.account.to_bytes())
.or_insert(EMPTY_ACCOUNT);

*from_balance -= transfer.value;
account.balance += transfer.value;
}

/// The top level transaction execution function.
///
/// This will emplace the deposit in the state, if it exists - making it
Expand Down
Loading
Loading