A token vault smart contract enables users to deposit tokens and receive proportional shares, with withdrawals returning tokens based on the ratio of deposited shares to total supply, allowing the vault to generate and distribute profits to token holders.
Solidity Vault Smart Contract with Deposit and Withdraw Functions
Added:let's write a contract for a bull i've already defined the interface for ierc20 and then imported here so let's start with the state variables the first thing that we're going to need is the token that's going to be inside the spot so i'll declare as ierc20 public the token will never change so we'll make this immutable and we'll name it token when a user deposits token we'll mint shares and when a user withdraws we'll burn the shares so we'll need to keep track of the total share and the share for each user we'll keep track of the total share in a state variable we'll name it total supply so i'll type unit public total supply share per user we'll store it in a mapping from address to uint this will be a public state variable and i'll name it balance of next let's write the constructor we'll set the token when this contract is deployed so i'll type construct tor it's going to take in a single input of address of the token and then we'll set the immutable token here to the token from the input by typing token is equal to ierc20 token from the input underscore token next we'll write some internal functions to mint and burn shares the first function we'll write is mint so i'll type function underscore mint this function will take two parameters the address to mint the token to and the amount uint amount this function will be internal so i'll declare it as private and then we'll increment the total supply so the total shares will increase by the amount i'll type total supply plus equals amount and then we'll allocate this amount to the user too so type balance up to increment it by amount we'll do something similar for the function burn so i'll copy this and then paste it here and we'll name this function burn we'll burn from an address this function will again be private since we're burning we'll decrement the amount and then deduct the amount from the from address and that completes the internal function to mint shares and to burn shares let's now write the function to deposit and withdraw first we'll write the function to deposit so i'll type function deposit it's going to take in a single input of unit amount this will be the amount of token that the user is going to deposit this function will be external when the user deposits a token we will need to amend some shares we already discussed that the amount of shares to min is a times t over b where a is the amount of tokens that is being deposited b is the balance of tokens before the deposit t is the total supply the total shares and s is the shares to mint the amount of shares to me is proportional to the increase in the balance of the bolt and from this equation we get that s the amount of shares to min is equal to a times d divided by b so let's put this into code so you'll say unit shares now notice that when the total supply the total share is equal to zero then this equation does not make any sense since total share is equal to zero and we'll be dividing by zero so when the total share is equal to zero then we'll have to handle the amount of shares cement separately from this equation if total supply is equal to zero then we want to say that the amount of shares to men is equal to the amount that was deposited so we'll type if total supply is equal to zero then the amount of shares to mint shares is equal to amount that was deposited else we'll follow this equation so we'll say shares is equal to the amount multiplied by total supply and then divided by the balance of the token locked inside this contract so you'll say token dot balance of address this first we'll mint the shares so we'll use the internal function that we defined above by typing underscore mint to message dot sender for the amount shares and lastly we'll actually transfer the token from message.sender into this contract we'll type token.transfer from message.sender to this contract address this for the amount amount that was specified by the input and that completes the function deposit let's now write the function to withdraw so i'll name it function withdraw the input for this function will be the amount of shares to burn so i'll type uint underscore shares this function will be external and when we burn the shares we need to calculate the amount of tokens to withdrawal for the user and similar to deposit we already discussed that the amount to withdrawal is equal to a is equal to s being the shares multiplied by the balance of token in this contract divided by t total shares let's write this in code unit amount is equal to shares multiplied by balance of the token in this contract that will be token dot balance of address this and then divided by the total supply just to be clear that we want to multiply before dividing i'll put a parenthesis here and before we transfer the amount to the user we'll first burn the shares underscore burn this is the function that we already defined above for message dot sender the amount to burn is shares specified by the input and once we're done burning the shares we'll transfer the token by typing token dot transfer to message dot sender diamond that we're going to transfer is amount and that completes the function for withdrawal let's deploy this contract i've already created an erc20 token contract that you can see over here we'll deploy this contract and mint some tokens to a user and then the user will deposit the token into the bolt after the user deposits we'll generate some profit for the ball by directly sending some token into the bolt so that when the user calls the function withdrawal they'll be able to withdraw more tokens than what they deposited so let's deploy the erc20 contract first i'll hit control s to also compile this contract and then i'll select the erc20 contract and then deploy it next i'll deploy the bulk contract i'll select this hit control s and then select the bulk contract the bulk contract needs the address of the token which we deployed over here copy it paste it here and then deploy next we'll mint some tokens for the user so i'll open the erc20 contract and we'll meant some token to the first user so i'll scroll down and then we'll mint 1000 token to the first user click on mint now before the user can deposit this token into the bulk contract this user will have to approve the ball to be able to spend to be able to pull this token into the bolt so make sure that we're still user one and we'll approve the ball to spend one thousand token i'll scroll down to get the address of the ball contract paste it here and since we're gonna deposit 1000 token i'll copy this and put it in here user one is going to approve the ball to spend 1000 token and then hit approve okay we're now ready to deposit this token into the bulk contract so i'll copy this amount scroll down open the bulk contract and user one is going to call deposit for a 1000 token and then click on deposit let's check the total supply total supply is one thousand and let's check the balance of user one i'll scroll up to copy the address of user one scroll down paste it in here and the balance of user one is one thousand next let's increase the balance of the token locked inside the bolt here we're simulating a ball where it took this 1000 token and somehow it made it more now in d5 this profit is usually made by depositing into other d5 protocols but for this example we'll keep it simple just mint some tokens and directly send it over to the block contract so i'll scroll up but before i do that i'll copy the address of the block contract and then scroll up since i cannot directly meant tokens the ball contract what i'll do here instead is mint some token to user and then have the user transfer the token to the ball so i'll hit mint and this will meant 1000 token to the user and we'll now transfer this token to the ball contract paste the address to the bulk here we just minted 1000 token we'll copy this paste it here and now the bulk contract has 2 000 tokens in total 1000 that was deposited by user 1 and the extra 1000 that was directly transferred next user 1 will call the function withdrawal on the ball contract but before we do that let's check the balance of the token so i'll scroll up copy the address of user one scroll down and we'll check the balance of the token for user one and it is zero right now after we withdraw we expect this number to be two thousand so i'll scroll down user one currently has one thousand shares there are two thousand tokens inside this bulk contract so if user one withdraws all of his chairs then user one's balance of token should increase by 2 000. let's call withdraw we'll withdraw everything so 1000 shares paste it in here and then call withdraw if we checked the balance of user one after we called withdrawal we burnt all the shares so shares is zero total supply is also zero let's now check the balance of user one before it was zero we deposited 1 000 the ball made an extra 1000 so the balance of user should be 2 000 and it is
Up Next

Building an Aave V3 Flash Loan Arbitrage Smart Contract | Step-by-Step Tutorial
@BlockExplorerMedia
185.3K views•2022-10-02

Hybrid Key Establishment in Production: Post-Quantum Cryptography
@durumcrustulum
14.7K views•2025-08-27

Arbitrage Algorithm for Uniswap V2 and V3 Pools
@smartcontractprogrammer
166 views•2026-02-18

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto

































