Skip to content

Commit 1912437

Browse files
committed
feat: add pegin contract initial version
1 parent f1fd36f commit 1912437

File tree

8 files changed

+530
-20
lines changed

8 files changed

+530
-20
lines changed

contracts/PegInContract.sol

Lines changed: 453 additions & 0 deletions
Large diffs are not rendered by default.

contracts/PegOutContract.sol

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ contract PegOutContract is
5656
/// @param newTime the new Bitcoin block time
5757
event BtcBlockTimeSet(uint256 indexed oldTime, uint256 indexed newTime);
5858

59+
// solhint-disable-next-line comprehensive-interface
60+
receive() external payable {
61+
revert Flyover.PaymentNotAllowed();
62+
}
63+
5964
/// @inheritdoc IPegOut
6065
function depositPegOut(
6166
Quotes.PegOutQuote calldata quote,
@@ -66,7 +71,7 @@ contract PegOutContract is
6671
}
6772
uint256 requiredAmount = quote.value + quote.callFee + quote.productFeeAmount + quote.gasFee;
6873
if (msg.value < requiredAmount) {
69-
revert InsufficientAmount(msg.value, requiredAmount);
74+
revert Flyover.InsufficientAmount(msg.value, requiredAmount);
7075
}
7176
if (quote.depositDateLimit < block.timestamp || quote.expireDate < block.timestamp) {
7277
revert QuoteExpiredByTime(quote.depositDateLimit, quote.expireDate);
@@ -179,7 +184,7 @@ contract PegOutContract is
179184

180185
Quotes.PegOutQuote memory quote = _pegOutQuotes[quoteHash];
181186
if (quote.lbcAddress == address(0)) revert Flyover.QuoteNotFound(quoteHash);
182-
if (quote.lpRskAddress != msg.sender) revert InvalidSender(quote.lpRskAddress, msg.sender);
187+
if (quote.lpRskAddress != msg.sender) revert Flyover.InvalidSender(quote.lpRskAddress, msg.sender);
183188

184189
BtcUtils.TxRawOutput[] memory outputs = BtcUtils.getOutputs(btcTx);
185190
_validateBtcTxNullData(outputs, quoteHash);
@@ -348,7 +353,7 @@ contract PegOutContract is
348353
requiredAmount = quote.value - (quote.value % _SAT_TO_WEI_CONVERSION);
349354
}
350355
uint256 paidAmount = outputs[_PAY_TO_ADDRESS_OUTPUT].value * _SAT_TO_WEI_CONVERSION;
351-
if (paidAmount < requiredAmount) revert InsufficientAmount(paidAmount, requiredAmount);
356+
if (paidAmount < requiredAmount) revert Flyover.InsufficientAmount(paidAmount, requiredAmount);
352357
}
353358

354359
/// @notice This function is used to validate the null data of the Bitcoin transaction. The null data

contracts/interfaces/ICollateralManagement.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ interface ICollateralManagement {
2525

2626
function addPegInCollateralTo(address addr) external payable;
2727
function addPegInCollateral() external payable;
28-
function slashPegInCollateral(Quotes.PegInQuote calldata quote, bytes32 quoteHash) external;
28+
function slashPegInCollateral(Quotes.PegInQuote calldata quote, bytes32 quoteHash) external returns (uint256);
2929
function addPegOutCollateralTo(address addr) external payable;
3030
function addPegOutCollateral() external payable;
31-
function slashPegOutCollateral(Quotes.PegOutQuote calldata quote, bytes32 quoteHash) external;
31+
function slashPegOutCollateral(Quotes.PegOutQuote calldata quote, bytes32 quoteHash) external returns (uint256);
3232

3333
function getPegInCollateral(address addr) external view returns (uint256);
3434
function getPegOutCollateral(address addr) external view returns (uint256);

contracts/interfaces/IPegIn.sol

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.25;
3+
4+
import {Quotes} from "../libraries/Quotes.sol";
5+
6+
interface IPegIn {
7+
enum PegInStates { UNPROCESSED_QUOTE, CALL_DONE, PROCESSED_QUOTE }
8+
9+
event BalanceIncrease(address indexed dest, uint256 indexed amount);
10+
event BalanceDecrease(address indexed dest, uint256 indexed amount);
11+
event Withdrawal(address indexed from, uint256 indexed amount);
12+
event BridgeCapExceeded(bytes32 indexed quoteHash, int256 indexed errorCode);
13+
event PegInRegistered(bytes32 indexed quoteHash, uint256 indexed transferredAmount);
14+
event Refund(address indexed dest, bytes32 indexed quoteHash, uint indexed amount, bool success);
15+
event CallForUser(
16+
address indexed from,
17+
address indexed dest,
18+
bytes32 indexed quoteHash,
19+
uint gasLimit,
20+
uint value,
21+
bytes data,
22+
bool success
23+
);
24+
25+
error InvalidRefundAddress(bytes refundAddress);
26+
error AmountUnderMinimum(uint256 amount);
27+
error QuoteAlreadyProcessed(bytes32 quoteHash);
28+
error InsufficientGas(uint256 gasLeft, uint256 gasRequired);
29+
error NotEnoughConfirmations();
30+
error UnexpectedBridgeError(int256 errorCode);
31+
32+
function deposit() external payable;
33+
function callForUser(Quotes.PegInQuote calldata quote) external payable returns (bool);
34+
function withdraw(uint256 amount) external;
35+
function registerPegIn(
36+
Quotes.PegInQuote calldata quote,
37+
bytes calldata signature,
38+
bytes calldata btcRawTransaction,
39+
bytes calldata partialMerkleTree,
40+
uint256 height
41+
) external returns (int256);
42+
function getBalance(address addr) external view returns (uint256);
43+
function validatePegInDepositAddress(
44+
Quotes.PegInQuote calldata quote,
45+
bytes calldata depositAddress
46+
) external view returns (bool);
47+
function hashPegInQuote(Quotes.PegInQuote calldata quote) external view returns (bytes32);
48+
}

contracts/interfaces/IPegOut.sol

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ interface IPegOut {
4646
uint256 amount
4747
);
4848

49-
/// @notice This error is emitted when the amount sent is less than the amount required to pay for the quote
50-
/// @param amount the amount sent
51-
/// @param target the amount required to pay for the quote
52-
error InsufficientAmount(uint256 amount, uint256 target);
53-
5449
/// @notice This error is emitted when the quote has expired by the number of blocks
5550
/// @param expireBlock the number of blocks the quote has expired
5651
error QuoteExpiredByBlocks(uint32 expireBlock);
@@ -83,11 +78,6 @@ interface IPegOut {
8378
/// @param actual the actual quote hash
8479
error InvalidQuoteHash(bytes32 expected, bytes32 actual);
8580

86-
/// @notice This error is emitted when the sender is not allowed to perform a specific operation
87-
/// @param expected the expected sender
88-
/// @param actual the actual sender
89-
error InvalidSender(address expected, address actual);
90-
9181
/// @notice This error is emitted when the get confirmations from the rootstock bridge fails
9282
/// @param errorCode The error code returned by the rootstock bridge
9383
error UnableToGetConfirmations(int errorCode);

contracts/libraries/Flyover.sol

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@ library Flyover {
1717
error IncorrectContract(address expected, address actual);
1818
error QuoteNotFound(bytes32 quoteHash);
1919
error PaymentFailed(address addr, uint amount, bytes reason);
20-
error EmptyBlockHeader(bytes32 blockHash);
20+
error EmptyBlockHeader(bytes32 heightOrHash);
2121
error NoBalance(uint256 wanted, uint256 actual);
2222
error NoContract(address addr);
23+
error PaymentNotAllowed();
24+
/// @notice This error is emitted when the sender is not allowed to perform a specific operation
25+
/// @param expected the expected sender
26+
/// @param actual the actual sender
27+
error InvalidSender(address expected, address actual);
28+
/// @notice This error is emitted when the amount sent is less than the amount required to pay for the quote
29+
/// @param amount the amount sent
30+
/// @param target the amount required to pay for the quote
31+
error InsufficientAmount(uint256 amount, uint256 target);
32+
error Overflow(uint256 passedAmount);
2333
}

contracts/split/CollateralManagement.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ contract CollateralManagementContract is
8383
function slashPegInCollateral(
8484
Quotes.PegInQuote calldata quote,
8585
bytes32 quoteHash
86-
) external onlyRole(COLLATERAL_SLASHER) {
86+
) external onlyRole(COLLATERAL_SLASHER) returns (uint256) {
8787
uint penalty = _min(
8888
quote.penaltyFee,
8989
_pegInCollateral[quote.liquidityProviderRskAddress]
9090
);
9191
_pegInCollateral[quote.liquidityProviderRskAddress] -= penalty;
9292
emit Penalized(quote.liquidityProviderRskAddress, quoteHash, Flyover.ProviderType.PegIn, penalty);
93+
return penalty;
9394
}
9495

9596
function addPegOutCollateralTo(address addr) external onlyRole(COLLATERAL_ADDER) payable {
@@ -103,13 +104,14 @@ contract CollateralManagementContract is
103104
function slashPegOutCollateral(
104105
Quotes.PegOutQuote calldata quote,
105106
bytes32 quoteHash
106-
) external onlyRole(COLLATERAL_SLASHER) {
107+
) external onlyRole(COLLATERAL_SLASHER) returns (uint256) {
107108
uint penalty = _min(
108109
quote.penaltyFee,
109110
_pegOutCollateral[quote.lpRskAddress]
110111
);
111112
_pegOutCollateral[quote.lpRskAddress] -= penalty;
112113
emit Penalized(quote.lpRskAddress, quoteHash, Flyover.ProviderType.PegOut, penalty);
114+
return penalty;
113115
}
114116

115117
function getMinCollateral() external view returns (uint) {

contracts/split/FlyoverDiscoveryFull.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,25 +333,27 @@ contract FlyoverDiscoveryFull is
333333
function slashPegInCollateral(
334334
Quotes.PegInQuote calldata quote,
335335
bytes32 quoteHash
336-
) external onlyRole(COLLATERAL_SLASHER) {
336+
) external onlyRole(COLLATERAL_SLASHER) returns(uint256) {
337337
uint penalty = _min(
338338
quote.penaltyFee,
339339
_pegInCollateral[quote.liquidityProviderRskAddress]
340340
);
341341
_pegInCollateral[quote.liquidityProviderRskAddress] -= penalty;
342342
emit Penalized(quote.liquidityProviderRskAddress, quoteHash, Flyover.ProviderType.PegIn, penalty);
343+
return penalty;
343344
}
344345

345346
function slashPegOutCollateral(
346347
Quotes.PegOutQuote calldata quote,
347348
bytes32 quoteHash
348-
) external onlyRole(COLLATERAL_SLASHER) {
349+
) external onlyRole(COLLATERAL_SLASHER) returns (uint256) {
349350
uint penalty = _min(
350351
quote.penaltyFee,
351352
_pegOutCollateral[quote.lpRskAddress]
352353
);
353354
_pegOutCollateral[quote.lpRskAddress] -= penalty;
354355
emit Penalized(quote.lpRskAddress, quoteHash, Flyover.ProviderType.PegOut, penalty);
356+
return penalty;
355357
}
356358

357359
function _min(uint a, uint b) private pure returns (uint) {

0 commit comments

Comments
 (0)