Skip to content

Commit

Permalink
Merge pull request keep-starknet-strange#21 from lana-shanghai/li/fea…
Browse files Browse the repository at this point in the history
…t/add-relay

contract checking tx inclusion in block
  • Loading branch information
lana-shanghai authored Dec 18, 2024
2 parents 3a90238 + 5e009be commit 619ff15
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 111 deletions.
52 changes: 52 additions & 0 deletions packages/onchain/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@ name = "alexandria_math"
version = "0.2.1"
source = "git+https://github.com/keep-starknet-strange/alexandria#95d98a5182001d07673b856a356eff0e6bd05354"

[[package]]
name = "consensus"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/raito.git?rev=02a13045b7074ae2b3247431cd91f1ad76263fb2#02a13045b7074ae2b3247431cd91f1ad76263fb2"
dependencies = [
"shinigami_engine",
"utils",
]

[[package]]
name = "onchain"
version = "0.1.0"
dependencies = [
"alexandria_math",
"consensus",
"openzeppelin",
"openzeppelin_token",
"openzeppelin_utils",
"snforge_std",
"utils",
"utu_relay",
]

[[package]]
Expand Down Expand Up @@ -123,6 +135,31 @@ name = "openzeppelin_utils"
version = "0.19.0"
source = "git+https://github.com/openzeppelin/cairo-contracts?tag=v0.19.0#8d49e8c445efd9bdc99b050c8b7d11ae5ad19628"

[[package]]
name = "ripemd160"
version = "0.1.0"
source = "git+https://github.com/j1mbo64/ripemd160_cairo.git#833e07d7d074d4ee51ceb40a5bcb4af2fe6898f3"

[[package]]
name = "sha1"
version = "0.1.0"
source = "git+https://github.com/j1mbo64/sha1_cairo.git#2b65bc00a829bdcc244c140d0f31feda32f8d2c4"

[[package]]
name = "shinigami_engine"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/shinigami.git?rev=3415ed6#3415ed6331d3ea2dc2de6f9ab8e0be6562585f2d"
dependencies = [
"ripemd160",
"sha1",
"shinigami_utils",
]

[[package]]
name = "shinigami_utils"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/shinigami.git?rev=3415ed6#3415ed6331d3ea2dc2de6f9ab8e0be6562585f2d"

[[package]]
name = "snforge_scarb_plugin"
version = "0.33.0"
Expand All @@ -135,3 +172,18 @@ source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.33.0#221b1db
dependencies = [
"snforge_scarb_plugin",
]

[[package]]
name = "utils"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/raito.git?rev=02a13045b7074ae2b3247431cd91f1ad76263fb2#02a13045b7074ae2b3247431cd91f1ad76263fb2"

[[package]]
name = "utu_relay"
version = "0.1.0"
source = "git+https://github.com/lana-shanghai/utu_relay.git#25f8d9799df04465c716155e9ece61d9145b0f8c"
dependencies = [
"openzeppelin",
"openzeppelin_upgrades",
"utils",
]
13 changes: 12 additions & 1 deletion packages/onchain/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ edition = "2024_07"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[patch.crates-io]
openzeppelin = "0.19.0"

[dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.33.0" }
openzeppelin = { git = "https://github.com/openzeppelin/cairo-contracts", tag = "v0.19.0" }
starknet = "2.9.1"
alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria" }
openzeppelin_token = { git = "https://github.com/openzeppelin/cairo-contracts", tag = "v0.19.0" }
openzeppelin_utils = { git = "https://github.com/openzeppelin/cairo-contracts", tag = "v0.19.0" }
utils = { git = "https://github.com/keep-starknet-strange/raito.git", rev = "02a13045b7074ae2b3247431cd91f1ad76263fb2" }
consensus = { git = "https://github.com/keep-starknet-strange/raito.git", rev = "02a13045b7074ae2b3247431cd91f1ad76263fb2" }
utu_relay = { git = "https://github.com/lana-shanghai/utu_relay.git" }

[[target.starknet-contract]]
casm = true
sierra = true
build-external-contracts = ["openzeppelin_presets::erc20::ERC20Upgradeable"]
build-external-contracts = [
"openzeppelin_presets::erc20::ERC20Upgradeable",
"utu_relay::utu_relay::UtuRelay"
]
allowed-libfuncs-list.name = "experimental"
casm-add-pythonic-hints = true

[dev-dependencies]
assert_macros = "2.9.1"
Expand Down
1 change: 1 addition & 0 deletions packages/onchain/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod escrow;
mod orderbook;
mod utils;
mod relay;
14 changes: 6 additions & 8 deletions packages/onchain/src/orderbook/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ pub trait IOrderbook<TContractState> {
ref self: TContractState,
inscription_data: ByteArray,
receiving_address: ByteArray,
satoshi: felt252,
currency_fee: felt252,
submitter_fee: u256,
) -> u32;
fn cancel_inscription(ref self: TContractState, inscription_id: u32, currency_fee: felt252);
fn lock_inscription(ref self: TContractState, inscription_id: u32, tx_hash: ByteArray);
fn submit_inscription(ref self: TContractState, inscription_id: u32, tx_hash: ByteArray);
fn query_inscription(self: @TContractState, inscription_id: u32) -> (ByteArray, u256);
fn is_valid_bitcoin_address(self: @TContractState, receiving_address: ByteArray) -> bool;
fn is_locked(self: @TContractState, tx_hash: ByteArray) -> (bool, ContractAddress);
fn query_inscription(
self: @TContractState, inscription_id: u32,
) -> (ContractAddress, ByteArray, u256);
}

#[starknet::interface]
Expand All @@ -34,16 +33,15 @@ pub trait OrderbookABI<TContractState> {
ref self: TContractState,
inscription_data: ByteArray,
receiving_address: ByteArray,
satoshi: felt252,
currency_fee: felt252,
submitter_fee: u256,
) -> u32;
fn cancel_inscription(ref self: TContractState, inscription_id: u32, currency_fee: felt252);
fn lock_inscription(ref self: TContractState, inscription_id: u32, tx_hash: ByteArray);
fn submit_inscription(ref self: TContractState, inscription_id: u32, tx_hash: ByteArray);
fn query_inscription(self: @TContractState, inscription_id: u32) -> (ByteArray, u256);
fn is_valid_bitcoin_address(self: @TContractState, receiving_address: ByteArray) -> bool;
fn is_locked(self: @TContractState, tx_hash: ByteArray) -> (bool, ContractAddress);
fn query_inscription(
self: @TContractState, inscription_id: u32,
) -> (ContractAddress, ByteArray, u256);

// ERC20 functions
fn balance_of(self: @TContractState, account: ContractAddress) -> felt252;
Expand Down
44 changes: 26 additions & 18 deletions packages/onchain/src/orderbook/mock.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod OrderbookMock {
new_inscription_id: u32,
// A map from the inscription ID to a tuple with the inscribed
// data and submitter fee.
inscriptions: Map<u32, (ByteArray, u256)>,
inscriptions: Map<u32, (ContractAddress, ByteArray, u256)>,
// A map from the inscription ID to status. Possible values:
// 'Open', 'Locked', 'Canceled', 'Closed'.
inscription_statuses: Map<u32, Status>,
Expand Down Expand Up @@ -47,7 +47,6 @@ mod OrderbookMock {
caller: ContractAddress,
inscription_data: ByteArray,
receiving_address: ByteArray,
satoshi: felt252,
currency_fee: felt252,
submitter_fee: u256,
}
Expand Down Expand Up @@ -85,7 +84,6 @@ mod OrderbookMock {
/// Inputs:
/// - `inscription_data: ByteArray`, the data to be inscribed on Bitcoin.
/// - `receiving_address: ByteArray`, the taproot address that will own the inscription.
/// - `satoshi: felt252`, the Sat where the user wants to inscribe data.
/// - `currency_fee: felt252`, 'STRK' tokens.
/// - `submitter_fee: u256`, fee to be paid to the submitter for the inscription.
/// Returns:
Expand All @@ -94,12 +92,13 @@ mod OrderbookMock {
ref self: ContractState,
inscription_data: ByteArray,
receiving_address: ByteArray,
satoshi: felt252,
currency_fee: felt252,
submitter_fee: u256,
) -> u32 {
assert(currency_fee == 'STRK'.into(), 'The currency is not supported');
let caller = get_caller_address();
let id = self.new_inscription_id.read();
self.inscriptions.write(id, (inscription_data.clone(), submitter_fee));
self.inscriptions.write(id, (caller, inscription_data.clone(), submitter_fee));
self.inscription_statuses.write(id, Status::Open);
self.new_inscription_id.write(id + 1);
self
Expand All @@ -109,7 +108,6 @@ mod OrderbookMock {
caller: get_caller_address(),
inscription_data: inscription_data,
receiving_address: receiving_address,
satoshi: satoshi,
currency_fee: currency_fee,
submitter_fee: submitter_fee,
},
Expand All @@ -123,8 +121,25 @@ mod OrderbookMock {
/// cancel.
/// - `currency_fee: felt252`, the token that the user paid the submitter fee in.
fn cancel_inscription(ref self: ContractState, inscription_id: u32, currency_fee: felt252) {
let (inscription_data, _) = self.inscriptions.read(inscription_id);
self.inscriptions.write(inscription_id, (inscription_data, 0));
let caller = get_caller_address();
let (request_creator, inscription_data, amount) = self
.inscriptions
.read(inscription_id);
assert(caller == request_creator, 'Caller cannot cancel this id');

let status = self.inscription_statuses.read(inscription_id);
assert(status != Status::Undefined, 'Inscription does not exist');
assert(status != Status::Locked, 'The inscription is locked');
assert(status != Status::Canceled, 'The inscription is canceled');
assert(status != Status::Closed, 'The inscription has been closed');

// TODO: change the address to the actual escrow contract once it's implemented.
let escrow_address = get_contract_address();
if (currency_fee == 'STRK'.into()) {
let strk_token = self.strk_token.read();
strk_token.transfer_from(sender: escrow_address, recipient: caller, amount: amount);
}
self.inscriptions.write(inscription_id, (caller, inscription_data, 0));
self.inscription_statuses.write(inscription_id, Status::Canceled);
self
.emit(
Expand Down Expand Up @@ -165,18 +180,11 @@ mod OrderbookMock {
self.emit(RequestCompleted { inscription_id: inscription_id, tx_hash: tx_hash });
}

fn query_inscription(self: @ContractState, inscription_id: u32) -> (ByteArray, u256) {
fn query_inscription(
self: @ContractState, inscription_id: u32,
) -> (ContractAddress, ByteArray, u256) {
return self.inscriptions.read(inscription_id);
}


fn is_valid_bitcoin_address(self: @ContractState, receiving_address: ByteArray) -> bool {
return true;
}

fn is_locked(self: @ContractState, tx_hash: ByteArray) -> (bool, ContractAddress) {
return (true, get_contract_address());
}
}

#[generate_trait]
Expand Down
Loading

0 comments on commit 619ff15

Please sign in to comment.