Skip to content

Commit

Permalink
chore: add tests for jolt request functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Darlington02 committed Oct 4, 2024
1 parent 200f2b0 commit 804ca37
Show file tree
Hide file tree
Showing 6 changed files with 480 additions and 46 deletions.
1 change: 1 addition & 0 deletions src/base/constants/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod Errors {
pub const SELF_TIPPING: felt252 = 'Karst: self-tip forbidden!';
pub const SELF_TRANSFER: felt252 = 'Karst: self-transfer forbidden!';
pub const SELF_REQUEST: felt252 = 'Karst: self-request forbidden!';
pub const INVALID_EXPIRATION_STAMP: felt252 = 'Karst: invalid expiration stamp';
pub const INSUFFICIENT_ALLOWANCE: felt252 = 'Karst: insufficient allowance!';
pub const AUTO_RENEW_DURATION_ENDED: felt252 = 'Karst: auto renew ended!';
pub const INVALID_JOLT: felt252 = 'Karst: invalid jolt!';
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IJolt.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub trait IJolt<TState> {
fn jolt(ref self: TState, jolt_params: JoltParams) -> u256;
fn set_fee_address(ref self: TState, _fee_address: ContractAddress);
fn auto_renew(ref self: TState, profile: ContractAddress, renewal_id: u256) -> bool;
fn fullfill_request(ref self: TState, jolt_id: u256, sender: ContractAddress) -> bool;
fn fulfill_request(ref self: TState, jolt_id: u256) -> bool;
// *************************************************************************
// GETTERS
// *************************************************************************
Expand Down
43 changes: 22 additions & 21 deletions src/jolt/jolt.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub mod Jolt {
let jolt_type = @jolt_params.jolt_type;
match jolt_type {
JoltType::Tip => {
let _jolt_status = self
let _jolt_status = self
._tip(
jolt_id,
sender,
Expand Down Expand Up @@ -203,13 +203,13 @@ pub mod Jolt {
self.fee_address.write(_fee_address);
}

fn fullfill_request(
ref self: ContractState, jolt_id: u256, sender: ContractAddress
) -> bool {
fn fulfill_request(ref self: ContractState, jolt_id: u256) -> bool {
// get jolt details
let mut jolt_details = self.jolt.read(jolt_id);
let sender = get_caller_address();

// validate request
assert(jolt_details.jolt_type == JoltType::Request, Errors::INVALID_JOLT);
assert(jolt_details.status == JoltStatus::PENDING, Errors::INVALID_JOLT);
assert(sender == jolt_details.recipient, Errors::INVALID_JOLT_RECIPIENT);

Expand Down Expand Up @@ -334,9 +334,7 @@ pub mod Jolt {
if (renewal_status == true) {
// check allowances match auto-renew duration
let allowance = dispatcher.allowance(sender, this_contract);
assert(
allowance >= renewal_duration * amount, Errors::INSUFFICIENT_ALLOWANCE
);
assert(allowance >= renewal_duration * amount, Errors::INSUFFICIENT_ALLOWANCE);

// generate renewal ID
let renewal_hash = PedersenTrait::new(0)
Expand Down Expand Up @@ -387,9 +385,11 @@ pub mod Jolt {
expiration_timestamp: u64,
erc20_contract_address: ContractAddress
) -> JoltStatus {
// check that user is not requesting to self or to a non-existent address
// check that user is not requesting to self or to a non-existent address and expiration
// time exceeds current time
assert(sender != recipient, Errors::SELF_REQUEST);
assert(recipient.is_non_zero(), Errors::INVALID_PROFILE_ADDRESS);
assert(expiration_timestamp > get_block_timestamp(), Errors::INVALID_EXPIRATION_STAMP);

// emit event
self
Expand All @@ -412,12 +412,13 @@ pub mod Jolt {
ref self: ContractState, jolt_id: u256, sender: ContractAddress, jolt_details: JoltData
) -> bool {
// transfer request amount
self._transfer_helper(
jolt_details.erc20_contract_address,
jolt_details.sender,
jolt_details.recipient,
jolt_details.amount
);
self
._transfer_helper(
jolt_details.erc20_contract_address,
sender,
jolt_details.sender,
jolt_details.amount
);

// update jolt details
let jolt_data = JoltData { status: JoltStatus::SUCCESSFUL, ..jolt_details };
Expand All @@ -428,8 +429,8 @@ pub mod Jolt {
.emit(
JoltRequestFullfilled {
jolt_id,
jolt_type: 'REQUEST',
sender,
jolt_type: 'REQUEST FULFILLMENT',
sender: jolt_details.recipient,
recipient: jolt_details.sender,
expiration_timestamp: jolt_details.expiration_stamp,
block_timestamp: get_block_timestamp(),
Expand Down Expand Up @@ -506,10 +507,10 @@ pub mod Jolt {
}

fn _transfer_helper(
ref self: ContractState,
erc20_contract_address: ContractAddress,
sender: ContractAddress,
recipient: ContractAddress,
ref self: ContractState,
erc20_contract_address: ContractAddress,
sender: ContractAddress,
recipient: ContractAddress,
amount: u256
) {
let dispatcher = IERC20Dispatcher { contract_address: erc20_contract_address };
Expand All @@ -522,4 +523,4 @@ pub mod Jolt {
dispatcher.transfer_from(sender, recipient, amount);
}
}
}
}
2 changes: 1 addition & 1 deletion src/mocks.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod registry;
pub mod interfaces;
pub mod ERC20;
pub mod ERC20;
8 changes: 2 additions & 6 deletions src/mocks/ERC20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ mod USDT {
}

#[constructor]
fn constructor(
ref self: ContractState,
initial_supply: u256,
recipient: ContractAddress
) {
fn constructor(ref self: ContractState, initial_supply: u256, recipient: ContractAddress) {
let name = "USDT";
let symbol = "USDT";

self.erc20.initializer(name, symbol);
self.erc20.mint(recipient, initial_supply);
}
}
}
Loading

0 comments on commit 804ca37

Please sign in to comment.