Rise In Logo



Build on Aptos

In this homework you will modify the Aptos Vault smart contract to improve efficiency by directly using the vault address instead of deriving it from the admin address. Then, deploy and test the modified contract.

Part 1: Code Modification

Current Implementation

In the current contract, many functions take admin_address as a parameter and use get_vault_address to retrieve the vault's address:

move

public fun get_vault_address(admin_address: address): address acquires VaultSignerCapability {

    let vault_signer_cap = &borrow_global<VaultSignerCapability>(admin_address).cap;

    account::get_signer_capability_address(vault_signer_cap)

}

public entry fun deposit_tokens(admin: &signer, amount: u64) acquires Vault, VaultSignerCapability {

    let admin_address = signer::address_of(admin);

    let vault_address = get_vault_address(admin_address);

    // ... rest of the function

}

Task

Modify the contract to directly use vault_address as a parameter instead of admin_address. This change should be applied to all relevant functions.

Steps

1. Remove the get_vault_address function.

2. Update the function signatures to use vault_address instead of admin_address.

3. Remove any calls to get_vault_address within the functions.

4. Update any assertions that check the admin address to use the admin field from the Vault struct.

Example of Modified Function

move

public entry fun deposit_tokens(admin: &signer, vault_address: address, amount: u64) acquires Vault {

    let vault = borrow_global_mut<Vault>(vault_address);

    assert!(vault.admin == signer::address_of(admin), E_NOT_ADMIN);

    

    coin::transfer<AptosCoin>(admin, vault.vault_address, amount);

    [vault.total](http://vault.total)\_balance = [vault.total](http://vault.total)\_balance + amount;

    event::emit_event(&mut vault.tokens_deposited_events, TokensDepositedEvent { amount });

}

Additional Changes

  • Update all other functions (`allocate_tokens`, claim_tokens, withdraw_tokens, etc.) similarly.
  • Modify any view functions to take vault_address directly.
  • Ensure that the init_module function still correctly sets up the vault with the admin address.

Part 2: Deployment and Testing

Deployment

1. Set up Remix IDE environment if you haven't already.

2. Modify your project's Move.toml file to include any necessary dependencies.

3. Use the Remix IDE and Welldone wallet to deploy your contract after compilation.

4. Note the address where your contract is deployed.

Testing

1. Use the functions on Remix IDE to test your contract’s functionality.

2. Test each modified function to ensure it works correctly with the new vault_address parameter.

3. Verify that admin functions can only be called by the admin address.

4. Test error cases to ensure proper access control and balance checks.

Example Test Cases

  • Deploy the contract and initialize the vault
  • Deposit tokens into the vault
  • Allocate tokens to a test address
  • Attempt to allocate tokens from a non-admin address (should fail)
  • Claim allocated tokens
  • Withdraw unallocated tokens

Submission

1. Provide the modified contract code.

2. Share the address of the deployed contract on the Aptos testnet or devnet.

3. Optionally, include a brief report detailing:

  • The changes made to the contract
  • Results of your test cases
  • Any challenges encountered and how you resolved them

Bonus

Implement a new function that allows the admin to transfer ownership of the vault to a new admin address. Explain how this impacts the overall security model of the contract.

Good luck with your homework assignment! Remember to test thoroughly and always prioritize security when making changes to smart contracts.

Project

Comments

You need to enroll in the course to be able to comment!

Stay in the know

Never miss updates on new programs and opportunities.

Rise In Logo

Rise together in web3!