Skip to content

Starknet: Implement Cairo HTLC Contract #114

@0xdevcollins

Description

@0xdevcollins

Severity: MEDIUM — Starknet Chain Support Is Non-Functional

Problem

contract/starknet/src/htlc.cairo is completely empty (0 bytes). The bridge router routes Starknet payments via Layerswap, which handles bridging, but the on-chain HTLC on Starknet side for locking payer funds does not exist.

Implementation Required

```cairo
use starknet::ContractAddress;
use starknet::get_caller_address;
use starknet::get_block_timestamp;
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};

#[derive(Drop, Serde, starknet::Store)]
struct LockEntry {
sender: ContractAddress,
receiver: ContractAddress,
token: ContractAddress,
amount: u256,
hashlock: felt252, // sha256 of secret
timelock: u64,
withdrawn: bool,
refunded: bool,
}

#[starknet::contract]
mod HTLCStarknet {
use super::*;
use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess};

#[storage]
struct Storage {
    locks: Map<felt252, LockEntry>,  // lockId => LockEntry
    paused: bool,
    admin: ContractAddress,
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
    Locked: LockedEvent,
    Withdrawn: WithdrawnEvent,
    Refunded: RefundedEvent,
}

#[abi(embed_v0)]
impl HTLCImpl of super::IHTLC<ContractState> {
    fn lock(ref self, receiver, token, amount, hashlock, timelock) -> felt252
    fn withdraw(ref self, lock_id: felt252, preimage: felt252) -> bool
    fn refund(ref self, lock_id: felt252) -> bool
    fn get_lock(self: @ContractState, lock_id: felt252) -> LockEntry
    fn pause(ref self)
    fn unpause(ref self)
}

}
```

Also Required

  • Scarb.toml build config
  • Deploy script using starkli
  • Tests using snforge (Starknet Foundry)
  • Add STARKNET_HTLC_ADDRESS and STARKNET_RELAY_PRIVATE_KEY to .env.example

Note on Layerswap Integration

For the beta, Layerswap handles Starknet bridging at the protocol level. The Cairo HTLC is needed for a fully non-custodial Starknet integration in the future. This can be deprioritized if beta scope excludes Starknet.

Acceptance Criteria

  • Cairo HTLC contract compiles with scarb build
  • lock(), withdraw(), refund() match EVM/Soroban semantics
  • Emergency pause implemented
  • Tests with snforge: min 8 test cases
  • Deploy script using starkli
  • Deployed to Starknet testnet (Sepolia)

Metadata

Metadata

Assignees

No one assigned

    Labels

    blockchainSmart contracts and chain integrationsfeatureNew feature

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions