CosmWasm / cw-plus

Production Quality contracts under open source licenses
Apache License 2.0
508 stars 353 forks source link

Implement atomic swap contract with native tokens #26

Closed ethanfrey closed 4 years ago

ethanfrey commented 4 years ago

This should be a basic contract that implements one half of the IOV atomic swap spec. There is also a Solidity implementation of atomic swap as well as one that supports ERC20 tokens, so creating such a contract allows any CosmWasm-based chain to swap tokens with an Ethereum-based chain, possibly on some off-chain DEX or just "over the counter".

Create one contract that can handle and track many atomic swaps. Each swap has a unique ID and can store some tokens when it is created. Its balance cannot be adjusted after that fact, until it is either claimed or aborted (when the swap is closed and the funds go to a recipient).

For the first step, this should just handle native tokens that are sent along with the "create swap" message (see env.msg.sent_funds), and these can be distributed with BankMsg::Send.

You can start with with cosmwasm-template, but as the various contracts in this package are rather customized, it might make sense to copy eg. cw20-escrow as a start and then modify it heavily.

Please cover it with reasonable unit tests. You can look at this integration test to get an idea of a successful end-to-end flow. For this contract, it would be nice to break it up into smaller pieces. Note it should be able to create both sides of the swap on the same contract and resolve them.

ethanfrey commented 4 years ago

@maurolacy I was doing cleanup and stubled upon https://github.com/CosmWasm/cosmwasm-examples/pull/2 which is when I started an atomic swap contract in Jan/Feb. The code uses a much older version of CosmWasm, but the key logic is the same. Maybe you can copy some stuff from that (I'm closing that PR)

maurolacy commented 4 years ago

OK, I've done an initial implementation. Basically copying cw20-escrow as you suggested, and adapting from your cosmwasm-examples atomic-swap branch.

I guess you'll want / prefer to have HandleMsg::Create functionality, a la cw20-escrow, to support multiple swaps in the contract. I'll do that next.

In case you want to take a look, code is pushed under https://github.com/CosmWasm/cosmwasm-plus/tree/atomic-swap.

Didn't make a pull request as code is still pretty basic, no unit tests, etc.

maurolacy commented 4 years ago

PR: https://github.com/CosmWasm/cosmwasm-plus/pull/52