Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.75.0
toolchain: stable
target: wasm32-unknown-unknown
override: true

Expand Down
2 changes: 1 addition & 1 deletion contracts/ephemeral_account/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ pub enum Error {
NoPaymentReceived = 10,
AccountExpired = 11,
InvalidStatus = 12,
}
}
2 changes: 1 addition & 1 deletion contracts/ephemeral_account/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ pub fn emit_account_expired(env: &Env, recovery_address: Address, amount_returne
amount_returned,
};
env.events().publish((symbol_short!("expired"),), event);
}
}
38 changes: 15 additions & 23 deletions contracts/ephemeral_account/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![no_std]

mod storage;
mod events;
mod errors;
mod events;
mod storage;

use soroban_sdk::{contract, contractimpl, contracttype, Address, Env, BytesN};
use soroban_sdk::{contract, contractimpl, contracttype, Address, BytesN, Env};

pub use errors::Error;
pub use events::{AccountCreated, PaymentReceived, SweepExecuted, AccountExpired};
pub use events::{AccountCreated, AccountExpired, PaymentReceived, SweepExecuted};
pub use storage::{AccountStatus, DataKey};

#[contract]
Expand All @@ -16,12 +16,12 @@ pub struct EphemeralAccountContract;
#[contractimpl]
impl EphemeralAccountContract {
/// Initialize the ephemeral account with restrictions
///
///
/// # Arguments
/// * `creator` - Address that created this account
/// * `expiry_ledger` - Ledger number when account expires
/// * `recovery_address` - Address to return funds if expired
///
///
/// # Errors
/// Returns Error::AlreadyInitialized if called more than once
pub fn initialize(
Expand Down Expand Up @@ -59,18 +59,14 @@ impl EphemeralAccountContract {

/// Record an inbound payment to this ephemeral account
/// Only the first payment is accepted
///
///
/// # Arguments
/// * `amount` - Payment amount
/// * `asset` - Asset address
///
///
/// # Errors
/// Returns Error::PaymentAlreadyReceived if payment already recorded
pub fn record_payment(
env: Env,
amount: i128,
asset: Address,
) -> Result<(), Error> {
pub fn record_payment(env: Env, amount: i128, asset: Address) -> Result<(), Error> {
// Check initialized
if !storage::is_initialized(&env) {
return Err(Error::NotInitialized);
Expand Down Expand Up @@ -100,19 +96,15 @@ impl EphemeralAccountContract {

/// Execute sweep to destination wallet
/// Transfers all funds to the specified destination
///
///
/// # Arguments
/// * `destination` - Recipient wallet address
/// * `auth_signature` - Authorization signature from off-chain system
///
///
/// # Errors
/// Returns Error::Unauthorized if authorization fails
/// Returns Error::AlreadySwept if sweep already executed
pub fn sweep(
env: Env,
destination: Address,
auth_signature: BytesN<64>,
) -> Result<(), Error> {
pub fn sweep(env: Env, destination: Address, auth_signature: BytesN<64>) -> Result<(), Error> {
// Check initialized
if !storage::is_initialized(&env) {
return Err(Error::NotInitialized);
Expand Down Expand Up @@ -164,7 +156,7 @@ impl EphemeralAccountContract {

let expiry_ledger = storage::get_expiry_ledger(&env);
let current_ledger = env.ledger().sequence();

current_ledger >= expiry_ledger
}

Expand All @@ -179,7 +171,7 @@ impl EphemeralAccountContract {

/// Expire the account and return funds to recovery address
/// Can only be called after expiry ledger is reached
///
///
/// # Errors
/// Returns Error::NotExpired if called before expiry ledger
pub fn expire(env: Env) -> Result<(), Error> {
Expand Down Expand Up @@ -265,4 +257,4 @@ pub struct AccountInfo {
pub payment_received: bool,
pub payment_amount: Option<i128>,
pub swept_to: Option<Address>,
}
}
38 changes: 29 additions & 9 deletions contracts/ephemeral_account/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,30 @@ pub fn get_creator(env: &Env) -> Address {

// Expiry ledger
pub fn set_expiry_ledger(env: &Env, ledger: u32) {
env.storage().instance().set(&DataKey::ExpiryLedger, &ledger);
env.storage()
.instance()
.set(&DataKey::ExpiryLedger, &ledger);
}

pub fn get_expiry_ledger(env: &Env) -> u32 {
env.storage().instance().get(&DataKey::ExpiryLedger).unwrap()
env.storage()
.instance()
.get(&DataKey::ExpiryLedger)
.unwrap()
}

// Recovery address
pub fn set_recovery_address(env: &Env, address: &Address) {
env.storage().instance().set(&DataKey::RecoveryAddress, address);
env.storage()
.instance()
.set(&DataKey::RecoveryAddress, address);
}

pub fn get_recovery_address(env: &Env) -> Address {
env.storage().instance().get(&DataKey::RecoveryAddress).unwrap()
env.storage()
.instance()
.get(&DataKey::RecoveryAddress)
.unwrap()
}

// Payment
Expand All @@ -65,23 +75,33 @@ pub fn has_payment_received(env: &Env) -> bool {
}

pub fn set_payment_received(env: &Env, value: bool) {
env.storage().instance().set(&DataKey::PaymentReceived, &value);
env.storage()
.instance()
.set(&DataKey::PaymentReceived, &value);
}

pub fn set_payment_amount(env: &Env, amount: i128) {
env.storage().instance().set(&DataKey::PaymentAmount, &amount);
env.storage()
.instance()
.set(&DataKey::PaymentAmount, &amount);
}

pub fn get_payment_amount(env: &Env) -> i128 {
env.storage().instance().get(&DataKey::PaymentAmount).unwrap_or(0)
env.storage()
.instance()
.get(&DataKey::PaymentAmount)
.unwrap_or(0)
}

pub fn set_payment_asset(env: &Env, asset: &Address) {
env.storage().instance().set(&DataKey::PaymentAsset, asset);
}

pub fn get_payment_asset(env: &Env) -> Address {
env.storage().instance().get(&DataKey::PaymentAsset).unwrap()
env.storage()
.instance()
.get(&DataKey::PaymentAsset)
.unwrap()
}

// Status
Expand All @@ -103,4 +123,4 @@ pub fn set_swept_to(env: &Env, address: &Address) {

pub fn get_swept_to(env: &Env) -> Option<Address> {
env.storage().instance().get(&DataKey::SweptTo)
}
}
Loading