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:
- deploy a token swap smart-contract
- alice deposits tokenA in the smart-contract
- 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?