approveon the token to allow the
TokenSpendercontract to transfer funds. It is safe to approve more than what is needed for the desired swap-and-call transaction. This step is not necessary if the user is spending ETH.
TokenSpender. If the user has already interacted with swap-and-call then we may be able to leverage a previous approval. And if the user is spending ETH we can skip this step all together.
_sourceTokenis the address of the ERC-20 token the user is spending. If the user is spending ETH than this should be set to 0.
_sourceAmountis the number of tokens the user is making available for this transaction. The user must have at least this amount in their wallet and have approved
TokenSpenderto transfer these funds. If this value is higher than what is required, the remaining tokens will be refunded. If the user is spending ETH than this should be set to 0.
_contractsis an array of contract addresses to call after retrieving the tokens or ETH from the end user. There can be any number of calls and if you are making several calls to the same contract just include the same address multiple times in this array.
_callDataConcatis the ABI for each contract call to make. You can get the bytes for each call using web3's
encodeABI(). This allows swap-and-call to call any function on each of the contracts. The data is appended together into a single blob of bytes.
_startPositionsis an array with an entry for each contract call to make. It defines where the appropriate call data is in the
_callDataConcatfor each individual call (since depending on the parameters call data varies in length). The first call is not included in this array as we can assume it starts at position 0.
_valuesis an array defining the call
valueto include with each contract call. The
valueis used for transferring ETH and not applicable to ERC-20 tokens. Use MAX_UINT to include the contract's entire balance at the time of the call.
_tokenToRefundis an additional ERC-20 token to refund after all the calls complete if any balance remains. ETH and
_sourceTokenare always refunded by default. This may be useful for scenarios where we the purchase call we are making may not have a known fixed price when the transaction occurs.
getTokenToEthOutputPriceare used to calculate the conversion rate. Add a bit more so that there is room for slippage in case the price goes up before the user's transaction is mined.
approveis called for Uniswap (if spending tokens instead of ETH) and for Unlock (unless the lock is priced in ETH).
tokenToTokenSwapOutputare used to do the exchange itself, getting exactly the number of tokens requested (which can be set to the lock's
purchaseis called with the purchase recipient set to the end user so that the key is delivered to them directly.
approvethe swap-and-call contract's address. This is very important as if you do anyone will be able to steal your funds.
approvestep is only for the
TokenSpenderaddress. This is a separate contract allowing us to add a layer of security ensuring that the arbitrary calls people are making with swap-and-call are not abusing approvals.
TokenSpenderallows just one contract, the swap-and-call implementation to transfer funds from a user's wallet. When calling swap-and-call, the
_sourceAmountis the only way that the
TokenSpendercan perform a transfer.