Skip to content
Open
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
9 changes: 9 additions & 0 deletions crates/contracts/simf/fee_collector.simf

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a spending path that allows us to add an asset to the UTXO without changing the script_pubkey

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn checksig(pubkey: Pubkey, sig: Signature) {
let sighash: u256 = jet::sig_all_hash();

jet::bip_0340_verify((pubkey, sighash), sig);
}

fn main() {
checksig(param::WITHDRAWAL_PUBKEY, witness::SIGNATURE);
}
71 changes: 71 additions & 0 deletions crates/contracts/src/artifacts/fee_collector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// This file is @generated by Simplex. Do not edit manually.

use simplex::include_simf;
use simplex::program::{ArgumentsTrait, Program};
use simplex::provider::SimplicityNetwork;
use simplex::simplicityhl::elements::Script;
use simplex::simplicityhl::elements::secp256k1_zkp::XOnlyPublicKey;
pub struct FeeCollectorProgram {
program: Program,
}
impl FeeCollectorProgram {
pub const SOURCE: &'static str = derived_fee_collector::FEE_COLLECTOR_CONTRACT_SOURCE;
#[must_use]
pub fn new(arguments: &impl ArgumentsTrait) -> Self {
Self {
program: Program::new(Self::SOURCE, arguments),
}
}
#[must_use]
pub fn with_taproot_pubkey(mut self, pub_key: XOnlyPublicKey) -> Self {
self.program = self.program.with_taproot_pubkey(pub_key);
self
}
#[must_use]
pub fn with_storage_capacity(mut self, capacity: usize) -> Self {
self.program = self.program.with_storage_capacity(capacity);
self
}
pub fn set_storage_at(&mut self, index: usize, new_value: impl Into<Vec<u8>>) {
self.program.set_storage_at(index, new_value);
}
#[must_use]
pub fn get_storage_len(&self) -> usize {
self.program.get_storage_len()
}
#[must_use]
pub fn get_storage(&self) -> &[Vec<u8>] {
self.program.get_storage()
}
#[must_use]
pub fn get_storage_at(&self, index: usize) -> Vec<u8> {
self.program.get_storage_at(index)
}
#[must_use]
pub fn get_script_pubkey(&self, network: &SimplicityNetwork) -> Script {
self.program.get_script_pubkey(network)
}
#[must_use]
pub fn get_script_hash(&self, network: &SimplicityNetwork) -> [u8; 32] {
self.program.get_script_hash(network)
}
#[must_use]
pub fn get_cmr(&self) -> [u8; 32] {
self.program.get_cmr()
}
#[must_use]
pub fn get_tapleaf_hash(&self) -> [u8; 32] {
self.program.get_tapleaf_hash()
}
}
impl AsRef<Program> for FeeCollectorProgram {
fn as_ref(&self) -> &Program {
&self.program
}
}
impl AsMut<Program> for FeeCollectorProgram {
fn as_mut(&mut self) -> &mut Program {
&mut self.program
}
}
include_simf!("src/artifacts/simf/fee_collector.simf");
4 changes: 4 additions & 0 deletions crates/contracts/src/artifacts/metadata.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions crates/contracts/src/artifacts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#![allow(clippy::all)]
#[rustfmt::skip]
pub mod fee_collector;
#[rustfmt::skip]
pub mod asset_auth;
#[rustfmt::skip]
pub mod issuance_factory;
Expand Down
56 changes: 56 additions & 0 deletions crates/contracts/src/programs/fee_collector/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use simplex::{
program::Program,
provider::SimplicityNetwork,
simplicityhl::elements::AssetId,
transaction::{FinalTransaction, UTXO},
};

use crate::artifacts::fee_collector::FeeCollectorProgram;

use crate::programs::fee_collector::{FeeCollectorParameters, FeeCollectorWitnessParams};
use crate::programs::program::SimplexProgram;

pub struct FeeCollector {
program: FeeCollectorProgram,
parameters: FeeCollectorParameters,
}

impl FeeCollector {
pub fn new(parameters: FeeCollectorParameters) -> Self {
Self {
program: FeeCollectorProgram::new(&parameters.build_arguments()),
parameters,
}
}

pub fn get_parameters(&self) -> &FeeCollectorParameters {
&self.parameters
}

pub fn attach_deposit(&self, ft: &mut FinalTransaction, asset_id: AssetId, amount: u64) {
self.add_program_output(ft, asset_id, amount);
}

pub fn attach_withdrawal(
&self,
ft: &mut FinalTransaction,
program_utxo: UTXO,
witness_params: FeeCollectorWitnessParams,
) {
self.add_program_input(ft, program_utxo, witness_params.build_witness());
}
}

impl SimplexProgram for FeeCollector {
fn get_program_source_code() -> &'static str {
FeeCollectorProgram::SOURCE
}

fn get_program(&self) -> &Program {
self.program.as_ref()
}

fn get_network(&self) -> &SimplicityNetwork {
&self.parameters.network
}
}
7 changes: 7 additions & 0 deletions crates/contracts/src/programs/fee_collector/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod core;
mod params;
mod witness;

pub use core::FeeCollector;
pub use params::FeeCollectorParameters;
pub use witness::FeeCollectorWitnessParams;
17 changes: 17 additions & 0 deletions crates/contracts/src/programs/fee_collector/params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use simplex::{provider::SimplicityNetwork, simplicityhl::elements::secp256k1_zkp::XOnlyPublicKey};

use crate::artifacts::fee_collector::derived_fee_collector::FeeCollectorArguments;

#[derive(Debug, Clone, Copy)]
pub struct FeeCollectorParameters {
pub withdrawal_pubkey: XOnlyPublicKey,
pub network: SimplicityNetwork,
}

impl FeeCollectorParameters {
pub fn build_arguments(&self) -> FeeCollectorArguments {
FeeCollectorArguments {
withdrawal_pubkey: self.withdrawal_pubkey.serialize(),
}
}
}
18 changes: 18 additions & 0 deletions crates/contracts/src/programs/fee_collector/witness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::artifacts::fee_collector::derived_fee_collector::FeeCollectorWitness;

#[derive(Debug, Clone, Copy)]
pub struct FeeCollectorWitnessParams {
pub signature: [u8; 64],
}

impl FeeCollectorWitnessParams {
pub fn new(signature: [u8; 64]) -> Self {
Self { signature }
}

pub fn build_witness(&self) -> Box<FeeCollectorWitness> {
Box::new(FeeCollectorWitness {
signature: self.signature,
})
}
}
6 changes: 2 additions & 4 deletions crates/contracts/src/programs/lending/scanners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,10 +1052,8 @@ mod tests {
let protocol_after_supplied = 2_u64;
let lender_after_supplied = lender_before + (amount_to_repay - 1);

let lender_after = AssetAuthVault::new_active(
params.get_lender_vault_parameters(),
lender_after_supplied,
);
let lender_after =
AssetAuthVault::new_active(params.get_lender_vault_parameters(), lender_after_supplied);
let protocol_after = AssetAuthVault::new_active(
params.get_protocol_fee_vault_parameters(),
protocol_after_supplied,
Expand Down
1 change: 1 addition & 0 deletions crates/contracts/src/programs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod asset_auth;
pub mod asset_auth_vault;
pub mod fee_collector;
pub mod issuance_factory;
pub mod lending;
pub mod program;
Expand Down
Loading