Skip to content
Merged
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
34 changes: 31 additions & 3 deletions proto/side/btcbridge/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package side.btcbridge;

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/sideprotocol/side/x/btcbridge/types";

Expand All @@ -13,9 +14,16 @@ message Params {
int32 confirmations = 2;
// Indicates the maximum depth or distance from the latest block up to which transactions are considered for acceptance.
uint64 max_acceptable_block_depth = 3;
// the denomanation of the voucher
// The denomination of the voucher
string btc_voucher_denom = 4;
// Vault list
repeated Vault vaults = 5;
// Protocol limitations
ProtocolLimits protocol_limits = 6;
// Protocol fees
ProtocolFees protocol_fees = 7;
// Network fee for withdrawal to bitcoin
int64 NetworkFee = 8;
}

// AssetType defines the type of asset
Expand All @@ -26,11 +34,11 @@ enum AssetType {
ASSET_TYPE_BTC = 1;
// BRC20: ordi, sats
ASSET_TYPE_BRC20 = 2;
// RUNE: dog*go*to*the*moon
// RUNE, dog*go*to*the*moon
ASSET_TYPE_RUNE = 3;
}

// Vault defines the vault.
// Vault defines the asset vault
message Vault {
// the depositor should send their btc to this address
string address = 1;
Expand All @@ -39,3 +47,23 @@ message Vault {
// the address to which the voucher is sent
AssetType asset_type = 3;
}

// ProtocolLimits defines the params related to the the protocol limitations
message ProtocolLimits {
// The minimum deposit amount for btc
int64 btc_min_deposit = 1;
// The minimum withdrawal amount for btc
int64 btc_min_withdraw = 2;
// The maximum withdrawal amount for btc
int64 btc_max_withdraw = 3;
}

// ProtocolFees defines the params related to the protocol fees
message ProtocolFees {
// Protocol fee amount for deposit
int64 deposit_fee = 1;
// Protocol fee amount for withdrawal
int64 withdraw_fee = 2;
// Protocol fee collector
string collector = 3;
}
22 changes: 16 additions & 6 deletions x/btcbridge/keeper/keeper_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,32 @@ func (k Keeper) mintBTC(ctx sdk.Context, tx *btcutil.Tx, height uint64, sender s
}
k.addToMintHistory(ctx, hash)

// mint the voucher token
if len(denom) == 0 {
denom = "sat"
params := k.GetParams(ctx)

protocolFee := sdk.NewInt64Coin(denom, params.ProtocolFees.DepositFee)
protocolFeeCollector := sdk.MustAccAddressFromBech32(params.ProtocolFees.Collector)

amount := sdk.NewInt64Coin(denom, out.Value)

depositAmount := amount.Sub(protocolFee)
if depositAmount.Amount.Int64() < params.ProtocolLimits.BtcMinDeposit {
return types.ErrInvalidDepositAmount
}
coins := sdk.NewCoins(sdk.NewCoin(denom, sdk.NewInt(out.Value)))

receipient, err := sdk.AccAddressFromBech32(sender)
if err != nil {
return err
}

if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, coins); err != nil {
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(amount)); err != nil {
return err
}

if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, receipient, sdk.NewCoins(depositAmount)); err != nil {
return err
}

if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, receipient, coins); err != nil {
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, protocolFeeCollector, sdk.NewCoins(protocolFee)); err != nil {
return err
}

Expand Down
29 changes: 24 additions & 5 deletions x/btcbridge/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,44 @@ func (m msgServer) WithdrawBitcoin(goCtx context.Context, msg *types.MsgWithdraw

ctx := sdk.UnwrapSDKContext(goCtx)

params := m.GetParams(ctx)

sender := sdk.MustAccAddressFromBech32(msg.Sender)

coin, err := sdk.ParseCoinNormalized(msg.Amount)
amount, err := sdk.ParseCoinNormalized(msg.Amount)
if err != nil {
return nil, err
}

if coin.Denom == m.GetParams(ctx).BtcVoucherDenom {
if err := types.CheckOutputAmount(msg.Sender, coin.Amount.Int64()); err != nil {
networkFee := sdk.NewInt64Coin(params.BtcVoucherDenom, params.NetworkFee)

if amount.Denom == params.BtcVoucherDenom {
protocolFee := sdk.NewInt64Coin(params.BtcVoucherDenom, params.ProtocolFees.WithdrawFee)

amount = amount.Sub(networkFee).Sub(protocolFee)
if amount.Amount.Int64() < params.ProtocolLimits.BtcMinWithdraw || amount.Amount.Int64() > params.ProtocolLimits.BtcMaxWithdraw {
return nil, types.ErrInvalidWithdrawAmount
}

if err := types.CheckOutputAmount(msg.Sender, amount.Amount.Int64()); err != nil {
return nil, types.ErrInvalidWithdrawAmount
}

if err := m.bankKeeper.SendCoins(ctx, sender, sdk.MustAccAddressFromBech32(params.ProtocolFees.Collector), sdk.NewCoins(protocolFee)); err != nil {
return nil, err
}
}

req, err := m.Keeper.NewWithdrawRequest(ctx, msg.Sender, coin)
req, err := m.Keeper.NewWithdrawRequest(ctx, msg.Sender, amount)
if err != nil {
return nil, err
}

if err = m.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, sdk.NewCoins(coin)); err != nil {
if err := m.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, sdk.NewCoins(amount, networkFee)); err != nil {
return nil, err
}

if err := m.bankKeeper.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(amount, networkFee)); err != nil {
return nil, err
}

Expand Down
3 changes: 3 additions & 0 deletions x/btcbridge/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ var (
ErrInvalidRuneId = errorsmod.Register(ModuleName, 6101, "invalid rune id")

ErrInvalidParams = errorsmod.Register(ModuleName, 7100, "invalid module params")

ErrInvalidDepositAmount = errorsmod.Register(ModuleName, 8100, "invalid deposit amount")
ErrInvalidWithdrawAmount = errorsmod.Register(ModuleName, 8101, "invalid withdrawal amount")
)
25 changes: 25 additions & 0 deletions x/btcbridge/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ func NewParams(relayers []string) Params {
PubKey: "",
AssetType: AssetType_ASSET_TYPE_RUNE,
}},
ProtocolLimits: &ProtocolLimits{
BtcMinDeposit: 50000, // 0.0005BTC
BtcMinWithdraw: 30000, // 0.0003BTC
BtcMaxWithdraw: 500000000, // 5BTC
},
ProtocolFees: &ProtocolFees{
DepositFee: 8000, // 0.00008BTC
WithdrawFee: 12000, // 0.00012BTC
Collector: "",
},
}
}

Expand Down Expand Up @@ -78,6 +88,21 @@ func (p Params) Validate() error {
}
}

if p.ProtocolLimits != nil {
if p.ProtocolLimits.BtcMinWithdraw > p.ProtocolLimits.BtcMaxWithdraw {
return ErrInvalidParams
}
}

if p.ProtocolFees != nil {
if len(p.ProtocolFees.Collector) != 0 {
_, err := sdk.AccAddressFromBech32(p.ProtocolFees.Collector)
if err != nil {
return ErrInvalidParams
}
}
}

return nil
}

Expand Down
Loading