0

I am relatively new to Solidity development, I'd like any thoughts or warnings about this type of implementation. It is not complete as far as receiving ERC20 tokens goes, or checking ownership, it is more of a proof of concept.

Concept: sort of like how balancer V2 pools all of the users tokens into one liquidity pool, I want to pool all of the users' assets into one smart contract. It would save on gas because the base implementation would only need to happen once, and the user would only need to pay for the gas to add or change token values to their account. I am storing all user wallet addresses in an array, and also using a mapping to their token info.

pragma solidity >=0.7.0 <0.9.0;

contract test1 {
    
    // creates user address (wallet) array and mapping to smartWallet struct

    address[] public SWaddresses;
    mapping(address => smartWallet) public smartWallets;
    
    struct smartWallet {
        uint index;    // store user index in the unordered array in the struct
        mapping(address => uint) walletTokens;
    }
    
    function addSmartWallet(address newSWaddress, address[] memory _tokens, uint[] memory _tokenamounts) public {
        
        // loops over the tokens and creates the mapping to the struct
        for (uint i = 0; i < _tokenamounts.length; i++) {
            smartWallets[newSWaddress].walletTokens[_tokens[i]] = _tokenamounts[i];
        }
    
        // pushes new address onto unordered array and saves the index
        SWaddresses.push(newSWaddress);
        smartWallets[newSWaddress].index = SWaddresses.length - 1;
    }
    
    function addOrUpdateWalletTokens(address SWaddress, address[] memory _tokens, uint[] memory _tokenamounts) public {

        //adds or updates using the input array of token amounts and addresses
        for (uint i = 0; i < _tokenamounts.length; i++) {
            smartWallets[SWaddress].walletTokens[_tokens[i]] = _tokenamounts[i];
        }
    }
    
    function removeSmartWallet(address SWaddress) public {
        
        // deletes a wallet mapping by reordering the stack and deleting the address mapping
        smartWallets[SWaddresses[SWaddresses.length - 1]].index = smartWallets[SWaddress].index;
        SWaddresses[smartWallets[SWaddress].index] = SWaddresses[SWaddresses.length - 1];
        SWaddresses.pop();
        delete(smartWallets[SWaddress]);
    }
    
    // uses the mapping to return token balances for a particular user
    function retrieveSmartWalletTokensandAmounts(address SWaddress, address tokenAddress) public view returns(uint) {
        return smartWallets[SWaddress].walletTokens[tokenAddress];
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.