Skip to content

Instantly share code, notes, and snippets.

@souradeep-das
souradeep-das / AtomicSwap.sol
Last active July 1, 2020 23:34
Atomic Swap for Fast Exits
pragma solidity 0.5.11;
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import "openzeppelin-solidity/contracts/token/ERC20/SafeERC20.sol";
import "openzeppelin-solidity/contracts/token/ERC721/IERC721.sol";
contract AtomicSwap {
using SafeERC20 for IERC20;
struct Swap {
{
calls: [{
from: "0x895cc6f20d386f5c0deae08b08ccfec9f821e7d9",
gas: "0x19304",
gasUsed: "0x6da2",
input: "0xaee150c700000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000214e8348c4f0000000000000000000000000000304db280
88e12ddcbec79c7e029e484eaa9a8b0e000000000000000000000000000000000000000000000000000000000000005df85b01c0f6f501f394304db28088e12ddcbec79c7e029e484eaa9a8b0e940000000000000000000000000000
000000000000880214e8348c4f000080a00000000000000000000000000000000000000000000000000000000000000000000000",
output: "0x",
to: "0x691a17e2d1ec316939bc9e9e7b6979c8fd17066c",
gasUsed: "0x223c",
input: "0xff27b189",
output: "0x",
to: "0x96d5d8bc539694e5fa1ec0dab0e6327ca9e680f9",
type: "CALL",
value: "0x0"
}, {
calls: [{...}, {...}, {...}, {...}, {...}, {...}],
from: "0x08c569c5774110eb84a80b292e6d6f039e18915a",
gas: "0x4660a",
{
calls: [{
from: "0xa972fef246494b13a2175dc33344e137ddc5d604",
gas: "0x834a9",
gasUsed: "0x93a",
input: "0xf25b3f9900000000000000000000000000000000000000000000000000000000003709d9",
output: "0x6412521dbf79bc245a0ddff0db561b07c33e1614927f2bb7d94d0f272eddef1c000000000000000000000000000000000000000000000000000000005f524da6",
to: "0x96d5d8bc539694e5fa1ec0dab0e6327ca9e680f9",
type: "STATICCALL"
}, {

Design: https://github.com/omgnetwork/plasma-contracts/blob/master/plasma_framework/docs/quasar/quasar-fast-withdrawals.md
Implementation: https://github.com/omgnetwork/plasma-contracts/tree/master/plasma_framework/contracts/quasar

Quasar Fast Withdrawals

This is an attempt to reduce the time needed to move funds from Layer 2 to Layer 1 to <= 24 hrs. It is a trustless way to exchange funds using a Layer 1 smart contract, which would enable almost anyone (i.e. not only limited to token-issuers or partners) to provide the service for incentives. This approach also pushes for bringing the liquidity providers to the exiters, hence eliminating the requirement of a token market.

Overview

Exchanging/Swapping assets is done with the help of transaction inclusion proofs

To put it briefly, the UX for Alice, who has PETH (ETH on Plasma Layer 2) and wants Layer 1 ETH is:

const { providers, Wallet} = require('ethers')
const { hexConcat } = require('ethers/lib/utils')
require('dotenv').config()
const l2Provider = new providers.JsonRpcProvider('<rpc-here>')
async function main() {
const saltEncoded = '0x0000000000000000000000000000000000000000d3af2663da51c10215000000'
const ctrCode = "0x60c0346100bb574660a052602081017f8cad95687ba82c2ce50e74f7b754645e5117c3a5bec8151c0726d5857980a86681527f9ac997416e8ff9d2ff6bebeb7149f65cdae5e32e2b90440b566bb3044041d36a60408301524660608301523060808301526080825260a082019180831060018060401b038411176100a557826040525190206080526123c090816100c1823960805181611b47015260a05181611b210152f35b634e487b7160e01b600052604160045260246000fd5b600080fdfe6040608081526004908136101561001557600080fd5b600090813560e01c80630d58b1db1461126c578063137c29fe146110755780632a2d80d114610db75780632b67b57014610bde57806330f28b7a14610ade5780633644e51514610a9d57806336c7851614610a285780633ff9dcb1146109a85780634fe02b441461093f57806365d9723c146107ac57806387517c451461067a578063927