This project contains a smart contract for swapping two ERC-20 tokens. The contract includes two ERC-20 token implementations, Bol token, and Sol token.
The token swap contract facilitates the exchange of Bol tokens for Sol tokens and vice versa. The process involves deploying the individual ERC-20 tokens first, followed by the deployment of the token swap contract, which references the addresses of the deployed token contracts and the owner's address.
-
Deploy Bol Token:
- Deploy the Bol token contract to the blockchain.
-
Deploy Sol Token:
- Deploy the Sol token contract to the blockchain.
-
Deploy Token Swap Contract:
- Deploy the token swap contract, providing the addresses of the Bol and Sol token contracts along with the owner's address as constructor arguments.
After deploying the contracts, you need to grant approval for the token swap contract to spend a certain amount of Bol tokens and Sol tokens. This is achieved by calling the approve
function on both ERC-20 token contracts:
-
Approve Bol Token:
- Call the
approve
function on the Bol token contract, allowing the token swap contract to spend a specific amount of Bol tokens in wei.
- Call the
-
Approve Sol Token:
- Call the
approve
function on the Sol token contract, allowing the token swap contract to spend a specific amount of Bol tokens in wei.
- Call the
Once the approval is successful, you can execute the token swap by calling the swap function on the token swap contract:
Swap Tokens: Call the swap function on the token swap contract to exchange Bol tokens for Sol tokens or vice versa.
// Example Solidity code for approving Bol tokens
function swap(uint256 amount1, uint256 amount2) public {
require(msg.sender == owner1 || msg.sender == owner2, "You are not allowed to swap");
require(
token1.allowance(owner1, address(this)) >= amount1,
"Token 1 has not allowed or approved this amount"
);
require(
token2.allowance(owner2, address(this)) >= amount2,
"Token 2 has not allowed or approved this amount"
);
_safeTransferFrom(token1, owner1, owner2, amount1);
_safeTransferFrom(token2, owner2, owner1, amount2);
}