Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R4R]BEP3: HTLC and Atomic Peg #30

Merged
merged 3 commits into from
Jul 30, 2019
Merged

[R4R]BEP3: HTLC and Atomic Peg #30

merged 3 commits into from
Jul 30, 2019

Conversation

abelliumnt
Copy link
Contributor

@abelliumnt abelliumnt commented Jul 9, 2019

This BEP is about introducing Hash Timer Locked Contract functions and further mechanism to handle inter-blockchain token peg.

HTLC has been used for Atomic Swap and cross payment channel for a few years on Bitcoin and its variant blockchains, and also Ethereum. This BEP defines native transactions to support HTLC on Binance Chain, and also proposes the standard infrastructure and procedure to use HTLC for inter-chain atomic swap to easily create and use pegged token, which is called Atomic Peg.

Related BEP: #3

@abelliumnt abelliumnt force-pushed the atomic-swap branch 3 times, most recently from 8e8305b to 3f09938 Compare July 9, 2019 10:53
@abelliumnt abelliumnt changed the title [WIP]BEP3: Atomic swap [R4R]BEP3: Atomic swap Jul 9, 2019
@abelliumnt abelliumnt changed the title [R4R]BEP3: Atomic swap [R4R]BEP3: Atomic swap HTLC Jul 9, 2019
@abelliumnt
Copy link
Contributor Author

@caffeinum Could you help to review this BEP?

@abelliumnt abelliumnt changed the title [R4R]BEP3: Atomic swap HTLC [R4R]BEP3: HTLC and Atomic Peg Jul 9, 2019
BEP3.md Outdated Show resolved Hide resolved
BEP3.md Outdated Show resolved Hide resolved
BEP3.md Outdated Show resolved Hide resolved
BEP3.md Outdated Show resolved Hide resolved
@drunken005
Copy link

When will this feature come online

#### Preparation Stage

1. Owner should issue proper number of tokens on Binance Chain as the Pegged
2. Owner should create one address on Binance Chain (as OB in the below diagram), and transfer in enough number of tokens for swap
Copy link
Contributor

Choose a reason for hiding this comment

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

where is the diagram?

#### Client Swap Tokens from Ethereum to Binance Chain

1. Client calls APS contract, with the hash of a secret, X tokens and a time span T parameters, to express his/her interest to swap X tokens. If the parameters are good (e.g. T>MinLockTime, enough tokens to swap), the call transaction is recorded on the blockchain.
2. Deputy process monitors the events of the APS contract on Ethereum. If it detects the client’s call and verify good, it will sign and broadcast the HTLT transaction on Binance Chain. This will lock X (or more as bonus) number of pegged tokens on Binance Chain. Please note the time span used in this HTLT transaction should be smaller enough than T in Client’s APS call.
Copy link
Contributor

Choose a reason for hiding this comment

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

we need to check if the contract has enough deposit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no need to check. The contract will ensure there has been enough deposit on the contract address.

@wjmelements
Copy link
Contributor

Will it be possible to lock the operator's balance so they can only send using HTLC?


### Claim Hash Timer Locked

Claim Hash Timer Locked (CHTL) is to claim the locked asset by showing the Random Number value that matches the hash. Each HTLT locked asset is guaranteed to be release once.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be CHLT? This is the only place it is called CHTL.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CHTL is right.


## Atomic Peg Swap

The primary purpose of HTLC in Binance Chain is to support Atomic Peg Swap (APS). APS is proposed here as the standard way for asset/token issuers to peg or migrate part of the asset/token onto Binance Chain, so that users of the tokens can enjoy the fast transactions and pleasant trading experience.

Choose a reason for hiding this comment

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

I believe Atomic Swap and Peg should be a totally separate concerns. Atomic Swap is an option, and cannot be enforced without intermediary and complicated mechanisms like DAI (which isn't safe, too).

It's easy to implement an Atomic Swap using HTLC first, so I think we should focus on that and move Peg discussion to separate BEP.

Choose a reason for hiding this comment

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

#### Preparation Stage

1. Owner should issue proper number of tokens on Binance Chain as the Pegged
2. Owner should create one address on Binance Chain (as OB in the below diagram), and transfer in enough number of tokens for swap

Choose a reason for hiding this comment

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

Is "OB" an ordinary address controlled by the owner's private key? If so, isn't the owner free to move tokens from this address?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. It is an ordinary address. But once HTLT transaction is sent, a certain number of coins will be transferred to a temporary address. There is no risk for swap.

Copy link

@Dominator008 Dominator008 Jul 19, 2019

Choose a reason for hiding this comment

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

Understood the swap process works. My question is geared more towards ensuring the total supply of tokens is still fixed. It's unclear to me how the current proposal guarantees at any given time, circulatingSupply(BEP-2) + circulatingSupply(ERC-20) == originalCirculatingSupply(ERC-20)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Taking ERC20 token for instance, the total supply of token A is 10000. If 5000 the BEP2 token is minted, then 5000 ERC20 token should be burned. Currently, there are 5000 BEP2 token and 5000 ERC20 token. Then the swap service provider use 1000 BEP2 token and 1000 ERC20 token for atomic swap service. Then total supply is still 10000.

Choose a reason for hiding this comment

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

But there's no prevention against the token owner minting more BEP-2, besides community monitoring?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. The community monitor is the main insurance for the unexpected minting on Binance Chain.

Choose a reason for hiding this comment

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

@HaoyangLiu I believe it cannot be called a peg then.

I will state again that I think it's needed to separate Atomic swap BEP and Peg BEP.

@abelliumnt abelliumnt force-pushed the atomic-swap branch 2 times, most recently from 22389ff to feca1f0 Compare July 19, 2019 09:36
@abelliumnt
Copy link
Contributor Author

abelliumnt commented Jul 22, 2019

Changes on Binance Chain implementation:

  1. Add swap in amount and swap out amount.
  2. Add refund msg type on Binance Chain
  3. Once a CHTL transaction or a refund transaction are sent, the corresponding hash lock record will be deleted.
  4. Change Coins to Coin
  5. secetHashLock = hash(secretKey+timestamp), The gap between timestamp and block time should be less than one day.

Changes on swap contract interface:

  1. Add swap amount in and swap amount out.
  2. _timelock should be block height instead of timestamp.
  3. Add swap query interface to get all opened swap

I have add the above changes. Please review it again.
@darren-liu @notatestuser @rickyyangz @yutianwu @guagualvcha @ackratos

Copy link

@caffeinum caffeinum left a comment

Choose a reason for hiding this comment

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

I think pegging mechanism needs much improvement, while Atomic swap is OK.

It's great we're having progress here, but I am for the separation.

@abelliumnt
Copy link
Contributor Author

@caffeinum
Could you clarify how to improve pegging mechanism?

@Dominator008
Copy link

Dominator008 commented Jul 22, 2019

Without loss of generality, let's design a pegging scheme between ERC-20 and BEP-2. IMO a proper mechanism should be something as follows:

Create pegged BEP-20 tokens (convert a part of the supply from ERC-20 to BEP-2):

  1. On Ethereum, the token owner sends a certain amount of ERC-20 tokens to a bridge contract where the tokens are locked until unpegged.
  2. On Binance Chain, the token owner broadcasts a peg transaction with the requested pegging amount.
  3. Binance Chain validators check on Ethereum that the locked amount is enough for the pegging and approves the peg transaction. Note that they will need to wait for a certain number of Ethereum blocks to establish finality.
  4. Token owner is now free to transfer the newly minted pegged tokens on Binance Chain.

Destroy pegged BEP-20 tokens (convert a part of they supply from BEP-2 to ERC-20):

  1. On Binance Chain, token owner broadcasts a regular burn transaction on the Binance chain, which burns certain number of pegged tokens.
  2. On Binance Chain, token owner broadcasts a requestProofOfBurn transaction, for which the validators sign-off the burnt amount and a nonce with secp256k1.
  3. On Ethereum, token owner sends the proof of burn to the bridge contract. The contract checks the signatures of the validators and unlocks the signed-off amount of tokens. The nonce is recorded as used to prevent replay attacks.

Note that this requires an extension to the semantics of mintable on Binance chain -- pegged assets should only be mintable with the special peg transaction.

This mechanism guarantees that the total supply of the token remains constant.

@abelliumnt abelliumnt force-pushed the atomic-swap branch 5 times, most recently from b9573e5 to 235f94b Compare July 25, 2019 12:29
@Dominator008
Copy link

Dominator008 commented Jul 26, 2019

https://github.com/swishlabsco/cosmos-ethereum-bridge is an example of a unidirectional pegging implementation.

@karzak
Copy link

karzak commented Jul 26, 2019

https://github.com/swishlabsco/cosmos-ethereum-bridge is an example of a unidirectional pegging implementation.

FYI The latest version of the ETH bridge code is https://github.com/musnit/peggy/tree/unidirectional-complete

@darren-liu
Copy link
Contributor

darren-liu commented Jul 30, 2019

Without loss of generality, let's design a pegging scheme between ERC-20 and BEP-2. IMO a proper mechanism should be something as follows:

Create pegged BEP-20 tokens (convert a part of the supply from ERC-20 to BEP-2):

  1. On Ethereum, the token owner sends a certain amount of ERC-20 tokens to a bridge contract where the tokens are locked until unpegged.
  2. On Binance Chain, the token owner broadcasts a peg transaction with the requested pegging amount.
  3. Binance Chain validators check on Ethereum that the locked amount is enough for the pegging and approves the peg transaction. Note that they will need to wait for a certain number of Ethereum blocks to establish finality.
  4. Token owner is now free to transfer the newly minted pegged tokens on Binance Chain.

Destroy pegged BEP-20 tokens (convert a part of they supply from BEP-2 to ERC-20):

  1. On Binance Chain, token owner broadcasts a regular burn transaction on the Binance chain, which burns certain number of pegged tokens.
  2. On Binance Chain, token owner broadcasts a requestProofOfBurn transaction, for which the validators sign-off the burnt amount and a nonce with secp256k1.
  3. On Ethereum, token owner sends the proof of burn to the bridge contract. The contract checks the signatures of the validators and unlocks the signed-off amount of tokens. The nonce is recorded as used to prevent replay attacks.

Note that this requires an extension to the semantics of mintable on Binance chain -- pegged assets should only be mintable with the special peg transaction.

This mechanism guarantees that the total supply of the token remains constant.

It is nice to have it trustless, but it is heavy-weight-lifting and I don't think it is realistically achievable to implement this feature for multiple chains.

I agree it is not a perfect solution for Peg here. The progress is to make at least a better way than current situation of how peg is performed on Binance Chain.

@darren-liu darren-liu closed this Jul 30, 2019
@darren-liu darren-liu reopened this Jul 30, 2019
@darren-liu darren-liu merged commit 2066685 into master Jul 30, 2019
@Dominator008
Copy link

Without loss of generality, let's design a pegging scheme between ERC-20 and BEP-2. IMO a proper mechanism should be something as follows:
Create pegged BEP-20 tokens (convert a part of the supply from ERC-20 to BEP-2):

  1. On Ethereum, the token owner sends a certain amount of ERC-20 tokens to a bridge contract where the tokens are locked until unpegged.
  2. On Binance Chain, the token owner broadcasts a peg transaction with the requested pegging amount.
  3. Binance Chain validators check on Ethereum that the locked amount is enough for the pegging and approves the peg transaction. Note that they will need to wait for a certain number of Ethereum blocks to establish finality.
  4. Token owner is now free to transfer the newly minted pegged tokens on Binance Chain.

Destroy pegged BEP-20 tokens (convert a part of they supply from BEP-2 to ERC-20):

  1. On Binance Chain, token owner broadcasts a regular burn transaction on the Binance chain, which burns certain number of pegged tokens.
  2. On Binance Chain, token owner broadcasts a requestProofOfBurn transaction, for which the validators sign-off the burnt amount and a nonce with secp256k1.
  3. On Ethereum, token owner sends the proof of burn to the bridge contract. The contract checks the signatures of the validators and unlocks the signed-off amount of tokens. The nonce is recorded as used to prevent replay attacks.

Note that this requires an extension to the semantics of mintable on Binance chain -- pegged assets should only be mintable with the special peg transaction.
This mechanism guarantees that the total supply of the token remains constant.

It is nice to have it trustless, but it is heavy-weight-lifting and I don't think it is realistically achievable to implement this feature for multiple chains.

I agree it is not a perfect solution for Peg here. The progress is to make at least a better way than current situation of how peg is performed on Binance Chain.

Understood. We can revisit once projects like ThorChain have made more progress with generic peg zone implementations.

@unclezoro unclezoro deleted the atomic-swap branch November 28, 2023 04:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants