'exchange Ethereum into a ERC20 stable Coin (usdt or Dai)?

I'm making a decentralized wallet built on the Ethereum network, and I'm currently trying to convert 1 Ethereum to get the equivalent of the StableCoin for a smart contract [in this case, DAI]

I was able to find out how to know my balance of this currency, and I think that I can also transfer money from one account to another in its Coin smart contract

The problem is that when I transfer the value of [assuming 100 DAI], it tells me that I do not have any balance value from this token (this is very logical) because I did not transfer Ethereum against this currency!!

Error Message : [Smart contract error: Dai/insufficient-balance]

Here is the question: What is the name of the Function which I can convert my Ethereum to the currency that I deal with its smart contract?? Or is it not going this way??

            //Dai Stablecoin Address on Ropsten Network =>
            string SmartContractAddress = "0x31F42841c2db5173425b5223809CF3A38FEde360";

            var account = new Account(PrivateKey);
            web3 = new Web3(account, ropsten);
            web3.TransactionManager.UseLegacyAsDefault = true;
            
            // Get Balance Of an address
            var balanceOfFunctionMessage = new BalanceOfFunction()
            {
                Owner = account.Address,
            };
            var balanceHandler = web3.Eth.GetContractQueryHandler<BalanceOfFunction>();
            var balance = await balanceHandler.QueryAsync<BigInteger>(SmartContractAddress, balanceOfFunctionMessage);

            // Try To transfer some DAI to receiver Address 
            var receiverAddress = "0xC10cAA4668427F05d0D95B9409f87D0662A25f97";
            var transferHandler = web3.Eth.GetContractTransactionHandler<TransferFunction>();
            var transfer = new TransferFunction()
            {
                To = receiverAddress,
                TokenAmount = 100
            };
            var transactionReceipt = await transferHandler.SendRequestAndWaitForReceiptAsync(SmartContractAddress, transfer);
        

According to my simple understanding: I think I need to know the name of the function by which I can exchange assets from Ethereum to the currency of this contract



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source