0

I want to perform an atomic token swap without a pre-deployed smart-contract and I am wondering how to do that in Solidity.

The most common approach to do atomic swaps between tokenA and tokenB is the P2-SC-2P (Peer-to-Smart-contract-to-Peer) approach:

  1. deploy a token swap smart-contract
  2. alice deposits tokenA in the smart-contract
  3. bob deposits tokenB in the smart-contract and receives tokenA in exchange

I want to get rid of step 1 to have a truly P2P mechanism. The main idea would be that Alice signs a transactions that transfers X tokenA to the person propagating the transaction, only if that person transfers Y tokenB to Alice in return.

Is it even possible to implement something like this?

Technically speaking, my first thought was to have Alice sign a transaction that deploys a smart-contract would take care of making the atomic swap between the signer and the caller before self-destructing.

I am not a good Solidity dev (to say the least) so here is the pseudo Solidity code I had in mind that could perform the atomic swap without requiring a pre-deployed smart-contract:

contract AtomicSwap {
   sellerSignedApprovalTx = "<RawSignedTxData>"

   constructor() public {
      buyer = caller
      seller = verifySignature(sellerSignedApprovalTx)
      TokenB.approve(buyer,sellerSignedApprovalTx.askPrice)
      delegateCall(sellerSignedApprovalTx.data)
      safeTransferFrom(TokenB,buyer,seller,sellerSignerApprovalTx.askPrice)
      safeTransferFrom(TokenA,seller,buyer,sellerSignerApprovalTx.tokenAmount)
      selfdestruct()
   }
}

As you can see, the idea is that the smart-contract has only 1 use: it performs the swap and then self-destructs (hence the notion of "disposable" smart-contract).

What do you think? Is this even technically possible? If yes, would it work a bit like my pseudo code or am I completely off?

Thibauld
  • 183
  • 1
  • 9
  • It does not make sense; for atomic swaps you can create a smart contract that serves any audience, not just Alice and Bob. One smart contract can handle any multiple parties over its life time. There is no benefit of having self desctructable smart contract for this. – Mikko Ohtamaa Jan 31 '23 at 10:46
  • the benefit is a legal one, not a technical one. There is a notable difference legally if a transaction involves a 3rd party (a smart-contract being considered a 3rd party legally speaking) vs if it is purely peer-to-peer. – Thibauld Jan 31 '23 at 17:38
  • I don't think is true. I have been in this space for a decade and never heard such a theory. Because even if smart contract is self destructed, its code will stay in the blockchain. – Mikko Ohtamaa Feb 01 '23 at 08:52

1 Answers1

0

I got the answer to my question via another channel: it's not recommendable to use this technique because the seller needs to sign the transaction for execution without knowing the contract address which will be executing the trade. So it would be like writing a blank check and comes with many security issues.

Thibauld
  • 183
  • 1
  • 9