Ethereum: Sepolia Smart Contract – Withdrawal Function
I successfully deployed a basic Solidity smart contract to the Sepsolia testnet, but unfortunately I ran into a critical issue with the withdrawal functionality of my wallet. After several attempts, I was frustrated and wondering if I had made any mistakes during deployment or configuration.
In this article, I will describe the steps I took to deploy and test my Sepolia smart contract, as well as the issues that arise when I try to withdraw funds from the contract.
Deploying the Smart Contract
To deploy the smart contract, I used the Truffle suite for Solidity development. The contract was created using a simple example of a basic ERC-20 token smart contract, which is suitable for a testnet like Sepsolia.
Here is a snippet of my contract code:
pragma solidity ^0.8.0;
Sepolia contract {
public address owner;
uint256 balance public;
constructor() public {
owner = msg.sender;
balance = 10 ether;
}
function deposit(uint256 amount) public {
require(msg.sender == owner, "Only the owner can call this function");
balance += amount;
}
function withdraw(uint256 amount) public {
require(amount <= balance, "Insufficient funds");
balance -= amount;
}
}
To deploy the contract to Sepsolia, I used the Truffle migrate command:
truffle-migrate --sepolia-network
This deployed the smart contract to the Sepsolia testnet.
Withdrawal Tests
After deploying the contract and setting it up on Sepsolia, I tried to withdraw funds from the contract using a simple withdrawal function. Here’s how I tested it:
pragma solidity ^0.8.0;
contract Sepolia {
public address owner;
uint256 balance public;
constructor() public {
owner = msg.sender;
balance = 10 ether;
}
function deposit(uint256 amount) public {
require(msg.sender == owner, "Only the owner can call this function");
balance += amount;
}
function withdraw(uint256 amount) public {
require(amount <= balance, "Insufficient funds");
balance -= amount;
}
}
contract SepoliaTest {
public Sepolia Sepolia;
public constructor() {
Sepolia = new Sepiola();
}
function testWithdrawal() public payable {
// Set the account that wants to withdraw
address recipientAddress = 0x... (replace with your own wallet address);
// Call the withdrawal function
Sepolia.withdraw(10 ether); // Attempt to withdraw 10 ether
// Assert that the withdrawal was successful
assert(Sepolia.balance >= 10);
}
}
In this test, I deployed a new contract called “SepiolaTest” and configured it on Sepsolia. Then, I created a testWithdrawal
event emitter function in my main contract to trigger the withdrawal of funds from the Sepolia smart contract.
After running the test, I was pleased to see that the withdrawal was successful! The balance of 10 ether increased by 10 ether when I called the “withdraw” function.
The Problem
However, I quickly ran into a problem: despite successfully withdrawing 10 ether during my test, I received an “insufficient funds” error from the Sepolia wallet contract. This meant that even though the withdrawal was successful on my main contract, the funds were still not released to my own wallet.
After looking at my code and experimenting with different scenarios, I discovered that the problem was caused by the fact that I had used require(amount <= balance)
in the withdraw function. This condition ensures that the user cannot withdraw more than their current balance.