Under the transfer section, paste the vendor contract’s address and the total amount of tokens (which can be obtained by clicking totalSupply.Ĭlick on transact to approve the MetaMask transaction, and all the tokens will be sent from your wallet to the vendor contract balance. Open the token contract in the Deploy tab. Now that we have deployed the vendor smart contract, we need to send it some tokens to work. This function allows you to send all the MATIC stored in the smart contract into the owner’s wallet.įinally, deploy the contract and pass the token contract’s address as a parameter. The withdraw() function can only be run by the owner of the contract. After the approval is granted, we perform the transferFrom() function to transfer the user’s funds to vendor contract’s wallet. This approval procedure takes place on the front end (of a Next.js application, in this case). The authorization is on MetaMask and the user can clearly see what amount of tokens is the contract allowed to handle. This method is secure because we need to set the limit of number of tokens we are asking approval for. We first need to ask for approval from the user to let us manage their tokens and send these tokens back to us. However, here comes the fun part: we can’t just transfer tokens from user’s wallet to our vendor contract’s balance. We perform checks on whether user has the specified number of tokens or not, calculate the amount of MATIC to be sent back to the user, and check whether the vendor contract has sufficient amount of MATIC in balance. The sellTokens() function is not as straightforward as the buyTokens() function. Finally, we are emitting an event and returning the number of tokens purchased. Then we are doing ansfer() to send our tokens from the vendor contract to the wallet that sent the contract call. In the buyTokens() function, we are firstly checking if any MATIC was sent and if the vendor contract has sufficient tokens in balance or not. This value will be used to calculate the amount of tokens to purchase based on the amount of MATIC sent to the contract. We are also setting a general price of the token inside the tokensPerMatic variable. We are importing our token contract and providing the address to it through the constructor (we will provide the address as an argument while deploying the contract). This means that we can transfer the ownership of this contract to some other wallet, if we wish to do so. In the above code, we are creating an Ownable contract, which is an OpenZeppelin template. Use the following code in the file: // SPDX-License-Identifier: UnlicenseĬonstructor() ERC20("TestToken", "TEST")("") Open Remix IDE and create a new file called TestToken.sol under the contracts folder. The token smart contract will help us mint tokens. Implementing purchasing and selling functionality.If you feel you’re stuck in the tutorial, feel free to refer to the GitHub repository. MetaMask connected to Polygon Mumbai testnet.MetaMask extension installed with at least one wallet.A code editor – I prefer Visual Studio Code.We will then use both of the smart contracts in our Next.js application in order to make buying and selling accessible to the users. The approach we are going to follow is first creating a smart contract, which will mint our tokens, and then another smart contract to facilitate buying and selling them. The token type we’re going to use here is ERC-20, and we are going to deploy them on the Polygon Mumbai network. Today, we are going to make a token exchange platform, where users can buy and sell a specific token. From using them in games to creating your own virtual currency for an event, you can do many things using Web3 technologies. They bring a lot of new possibilities to the table. Atharva Deosthale Follow Web Developer and Designer | JavaScript = ❤ | MERN Stack Developer How to build an ERC-20 exchange platformĪpplications of tokens and NFTs are increasing everyday.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |