I made a few hundred dollars with this trading bot when Friend.Tech launched. In this tutorial I’m going to show you how to take advantage of system trading web3 opportunities.
tl;dr Takeaways
- Arbitrage and trading opportunities follow real users.
- When degens are clicking buttons there’s often potential to profit with trading bots, smart contracts and system trading strategies
- No real money has poured into social trading yet so it doesn’t scale
- Simple JS scripts wont compete on speed with people monitoring the mempool and executing more efficiently long-term
- Bot on bot battles are fun when the stakes are low
Trading On Friend.Tech
Friend.Tech is a web3 social trading game. You can buy shares in your friends and favorite influencers. It launched on Coinbase’s Base EVM blockchain in August 2023 and gained some early traction.
The idea is that the more people buy shares in your profile the higher price goes up.
The backend is based on a simple smart contract at: https://basescan.org/address/0xcf205808ed36593aa40a44f10c7f7c2f67d4a4d4#code
Which means we can trade this without ever touching the frontend UI, which is good because the UX was quirky to say the least.
When you go to the site at https://friend.tech you get prompted to “add app to homescreen”, this is a web based app which installs it’s homepage as an app on your device. Then you need to link it to Twitter where it get’s your name and profile pic. Before continuing you need to bridge funds in to a generated wallet address on Base. Then finally once that is all complete you get to buy the first share in yourself for free.
The first thing that most users then done was poke around the site and buy a few shares in their own profile. Other people would also buy shares in newly created profiles due to a list of newly created accounts on the first view. This meant there was a frontrunning opportunity if you could execute programmatically on that first transaction and get a buy order in for next to nothing before everyone clicking buttons.
Open Sourcing The Code
The code is available on Github at: https://github.com/jamesbachini/FriendTech-Trading-Bot
It’s written in Javascript using NodeJS and Ethers.js
Fork or download the repository, make sure you have Node installed and then open up a terminal and install dependencies with:
npm install
You can then export the private key for the platform generated wallet from the settings in the Friend.Tech app. Add this to .env-sample and rename it .env
You can then run any of the scripts using a command like
node friendsTechBot.js
node sellOut.js
It’s not pretty and I was getting a lot of @TODO errors about “filter not found” (if anyone knows how to mute these could you put in a PR please).
The scripts available are:
- friendsTechBot.js – main trading bot for frontrunning newly created accounts
- sellOut.js – the bot above only purchases shares, when you are ready to sell your positions provide a list of user addresses and it will sell all shares.
- agroTechBot.js – adversarial trading bot to try and prevent frontrunning competition.
- checkWallet.js – give it a list of transaction hashes and it will decode the data to get the user addresses of shares you’ve purchased (not needed anymore as it’s logged to buys.txt)
- collectDust.js – Go through a list of private keys and accumulate any left over funds into a single wallet
- transfer.js – Just to do a single transfer of eth from one account to another
Buying New Users
The first thing I noticed was that new users get bought up pretty early on. This was profitable because if you got in early you were paying fractions of a cent for the second share.
I optimized this by doing a balance check and only bidding on users that had more than x in their wallets. Users with larger balances would tend to buy more shares in themselves increasing margins. For whales with greater than 1 ETH bridged in I would buy up to 3 shares straight away in a single transaction.
After connecting up to a JSON RPC Provider which I found at chainlist, the next step was to export my platform wallet address and use a block explorer to find the FriendTechV1 smart contract. From there I added the contract address and created a simple human readable interface which is just the first line of each function without curly brackets.
const friends = new ethers.Contract(
friendsAddress,
[
'function buyShares(address arg0, uint256 arg1)',
'function getBuyPriceAfterFee(address sharesSubject, uint256 amount) public view returns (uint256)',
'event Trade(address trader, address subject, bool isBuy, uint256 shareAmount, uint256 ethAmount, uint256 protocolEthAmount, uint256 subjectEthAmount, uint256 supply)',
],
account
);
From there I could create a filter for the Trade event and run some logic on every incoming trade to the contract.
I would check the user being traded was new by the supply qty and then I’d use the internal function to get a price and push through a purchase.
const buyPrice = await friends.getBuyPriceAfterFee(amigo, qty);
await friends.buyShares(amigo, qty, {value: buyPrice, gasPrice});
This was pretty much profitable from the start and with 0.01 ETH I started building up a nice portfolio of social accounts. Which to start with I was selling manually on the app as and when my balance got low.
Bot On Bot Games
When I first started trading there was one other bot doing the same thing and it was executing much faster than I was. I’m not sure if this was because I was doing balance checks and JS isn’t known for it’s speed of execution or they were monitoring the mempool and executing on pending transactions. Either way “NoName” was getting his transaction through a couple of blocks before I was.
This wasn’t a major issue I was just paying a higher price which was still very profitable. Then disaster struck as someone launched a anti-sniper bot which would mimick new users and automate the process of cleaning out the frontrunners.
The anti-sniper bot would transfer in funds buy the first share for free, wait for everyone to buy in, then sell their share and transfer to the next wallet. Whoever ends up with the final share can’t sell it so you ended up being a bag holder for a tiny amount which was annoying.
To combat this I created an array of wallet balances from previous users and then checked new users to see if they were within a tight range of previously seen balances.
This meant when they transferred funds from one wallet to the next I could tell and not bid on the next user.
I also put together my own agroTradingBot.js which initially done the same thing and then eventually evolved into a script which does a couple of transfers to obfuscate funds then buys itself 3 times over a one minute period before selling out the entire position and returning funds to the original wallet.
Whenever I ran the agroTradingBot it would work for a bit and then whoever I was trading against would catch on, iterate and block it.
These bot on bot games are fun and the competitive nature of trading on-chain means there is always competition for profitable strategies. When I first started trading there was one other bot, when I open-sourced the code there was at least 5 or 6 different automated applications with different logic patterns operating on the protocol.
Finishing Up
After a few days I had resigned myself to it not being scalable and the trading bot margins started to dwindle as I was getting frontrun more often and the platform’s early traction was starting to ware thin.
I ended up with profits of around 0.2 ETH across the different strategies which is a great return on a 0.01 ETH investment, shame it didn’t scale. The scripts were responsible for a few thousand transactions and I recorded hundreds of private keys with tiny amounts of dust in the wallets, hopefully there’s an airdrop at some point 🚀
Please be careful with running arbitrage and trading bots and never send funds to something that you don’t know how it works. There are a lot of scams out there where they’ll promise “$800/day” and the truth is no one is going to give away code to profitable strategies that even make a $100 a day.
That being said I hope you find this code useful and it can act as a starting point for finding your own little areas of alpha in web3 and DeFi markets.