Smart Contracts are now Buyable!
Buyable.sol is a new way for developers to earn for life off of their ideas.
Blockchain has revolutionized how people make a living. Artists, marketers, developers, and speculators, can all monetize their skills and earn years worth of income off of just one successful project. Take NFTs, for example: a successful NFT project with significant secondary trading volume can earn the creating team consistent royalty payments — even though their work is done! Active income becomes passive. Now there is yet another way to monetize your skills and potentially make continuous passive income: Buyable.sol.
In this piece, I will briefly go over what Buyable.sol is, why it is so important, and how you can use it, if you wish. But first, a very important disclaimer.
***USE THIS CODE AT YOUR OWN RISK. I AM NOT RESPONSIBLE FOR ANY LOSS OF FUNDS AS A RESULT OF USING THIS CODE. DO YOUR OWN SECURITY TESTING BEFORE USING.***
Before we comb through the code, you may be asking “why is any of this useful?”
Let’s consider a hypothetical.
You are a talented Solidity developer, but you can’t or don’t want to find a steady job. You can do freelance work, but that can be unreliable and sometimes shady. I speak from personal experience when I say freelance hirers don’t always pay. But if you develop the smart contract someone needs, you can now sell it to them in a trustless, peer-to-peer way. They cannot reap the benefits of your work until they pay you for ownership of that contract.
From the hirer’s perspective this is a beneficial setup as well. The hirer has the opportunity to look over the smart contract and verify that it meets their requirements before paying for the code.
This is just one hypothetical example, but there are more potential situations in which Buyable.sol would be beneficial. Imagine being able to sell/buy an NFT collection, or even an entire marketplace smart contract. The potential is endless.
Now, let’s dive into the code!
As we can see above, the Buyable contract inherits from Ownable. Ownable is what handles contract ownership and provides tools to handle ownership transfers and function security — such as using onlyOwner to block everyone except the contract owner from calling a function. We’ll get into that more soon.
Some other things that stand out are the initial variables.
- _originalOwner — This is set in the constructor to the deploying wallet address. Even as the contract is transferred over and over again, this will always be equal to the very first deployer.
- isForSale — A true/false value that tells us whether the contract is actively for sale.
- priceOfContract — Important information for anyone looking to buy the contract
- FEE_PCT — The percentage of each contract sale that will go to _originalOwner
Here we have a modifier which we can use to make functions only callable by the original deployer of the contract.
Now it’s getting interesting! The sellContract function takes in a price, in terms of Wei, which the owner would like to sell the contract for. If you are not yet aware of what Wei is, it is the smallest unit of Ether. 1 Wei is equal to 0.000000000000000001 Ether. It is important to understand the conversions from Wei to Ether, otherwise you risk selling your contract for basically free!
The sellContract function is protected by the onlyOwner modifier, which is inherited from Ownable.sol. This means only the current owner of the contract can call this function. The require statement in line 32 makes sure the owner sets the contract price at no less than 0.000001ETH. Once those preconditions are met, isForSale is set to true, and the price of the contract is updated. Now the contract is officially for sale!
The endSale function is also protected by the onlyOwner modifier and returns the contract to its not-for-sale state.
Above is the function that will be used to buy ownership of the smart contract. If the contract is actively for sale and the function caller has sent the correct amount of Ether, the purchase will be successful. isForSale is returned to false, the contract price is returned to 0, the current owner and original owner are paid their respective dues, and the ownership of the contract is finally transferred to the buyer. Cha-ching!
The last function in this contract is the setFee function. This is only callable by the original owner of the contract, and is used to update the royalty fee the original owner will get for each sale of the contract. To prevent original owners from setting the fee percentage to ridiculous amounts, I’ve set the limit at 10%.
To illustrate the benefits of Buyable.sol and how to use it, let’s see it in action.
Here I have a very simple smart contract, called BuyMe. All it does is write and erase strings to an array. Not very exciting, I know, but still useful for demonstration. Every function in this contract is onlyOwner protected, so we will confirm the ownership has been successfully transferred if the buyer can now call these functions and the seller cannot.
After deploying the contract, here are the initial variable states:
Currently the data array is empty, but I will now write to it using the owner wallet.
Using the ‘write’ function from the owner wallet, we have now stored the string ‘Hello Ethereum’ in the smart contract. I will now set the fee percentage to 10 and the price of the contract to 0.1 and attempt to buy ownership of the contract from another wallet.
Done! Now the contract is purchasable for 0.1 Ether, and I will earn 10% royalties on all future sales.
Now for the purchase.
Here are the transaction details. The transaction is sent!
The transaction is successful. The Ether has been transferred to my wallet and the contract ownership has been transferred to the buyer.
Now the ‘owner’ function returns the buyer’s address.
Above you can see that trying to call the ‘write’ function from the original owner’s wallet returns an error. This means the ownership transfer was successful and only the new owner can call protected functions.
That is all the demonstration I will do here, but if you would like to interact further and even buy the contract for yourself (for 0.001 Rinkeby Test Ether), click here.
You can also view the code on GitHub.
Thank you for following along, please feel free to drop your thoughts in the comments below or on Twitter.
Cheers!