Falling Crypto Vending Machines

Alchemy
2 min readJun 29, 2021

Using Smart Contract Fallbacks with Custom Calldata to route automated, interfaceless trades

There’s a very useful feature that smart contracts have known as a fallback() function. These functions allow for contracts to execute a set function whenever a transaction which doesnt specify any specific functions sent to the contract. This allows simple sends to the contracts address from user wallets to execute the contract, however you cannot specify any parameters or extra information with these fallbacks, so you need to be creative.

tl;dr 4 Apes

Alchemy now allows you to send ETH (only ETH, not tokens) to specific ENS addresses (like USDC.alch.eth) and the contract will take the ETH and swap it through multiple Uniswap pools for the token, sending the swapped tokens back to your wallet. You don’t need to use any dapp, theres no websites, just send and receive. Note, make sure to manually set gas to about 250k (will only use what it needs to refund the rest) if your wallet shows 21k gas.

Crypto Vending Machines

Uniswap allows for instant execution trades for arbitrary amounts through its platform, allowing you to swap Token A for Token B, and receiving the Token B instantly once Token As sent. Further Uniswap has a Router which allows multiple trades between different assets to happen depending on liquidity, so using a router’s almost needed for any decent trade, this causes complex routes with transaction deadlines to ensure that transactions execute while they’re fresh. So how do you take this and allow any trade to happen for Token B?

If you haven’t crafted custom calldata you ain’t a dev

By taking the standard callData provided by the V2 Router for an end user, which provides the best route between multiple pools to get the best rate, ripping it apart, swapping the recipient address with the contract, removing the deadline and setting minOut to 0, you can now have a generic call which will always route trades along the path, no matter when they are submitted, whos the receiver or how much they are trading. We then take the custom callData and use that with a call to the V2 Router inside our own contract, sending the ETH to the Router as any user would and letting the router manage the hops. We finally take the received tokens and the user, transfer the tokens and boom we are all done! You then simply map the contract to an ENS domain for usability and you have a fully autonomous Crypto Vending Machines.

Decentralized Autonomous Wallet Native apps
DAWN apps are a new way to interact with the Ethereum ecosystem, and these routers for autonomous trades are just the first application. By allowing for users to interact with decentralized systems without centralized and censorable front end interfaces, and with the speed just sending a transaction to usdc.alch.eth rather than going to Uniswap, choosing the tokens, putting the amounts and confirming, the DAWN design allows for much faster and harder systems. Purchase tickets to events, trade, mint NFTs, open loans, all right from your wallet.

--

--

Alchemy

Turning NFTs into liquid, DAO based communities