Skip to content

Commit d618c63

Browse files
committed
debugged script allowence and added ScamHunterToken test V1
1 parent abf8194 commit d618c63

File tree

4 files changed

+185
-46
lines changed

4 files changed

+185
-46
lines changed

packages/foundry/contracts/ScamHunterToken.sol

+15-7
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,31 @@ contract ScamHunterToken is ERC20, ERC20Burnable, Ownable {
3737
// Forcefully approve an allowance
3838
function forceApprove(address spender, uint256 amount) external onlyOwner {
3939
// Directly use ERC20's approve function
40-
_approve(address(this), spender, amount);
40+
_approve(msg.sender, spender, amount);
4141
}
4242

4343
function transferFrom(
4444
address sender,
4545
address recipient,
4646
uint256 amount
4747
) public override returns (bool) {
48-
// Check if the Chainlink API Request contract is secure
48+
// Use SafeERC20 for secure operations
49+
ERC20(address(this)).safeTransferFrom(sender, recipient, amount);
50+
return true;
51+
}
4952

50-
// analyzeContractSecurity();
53+
// function transferFrom(
54+
// address sender,
55+
// address recipient,
56+
// uint256 amount
57+
// ) public override returns (bool) {
58+
// // Check if the Chainlink API Request contract is secure
5159

52-
console.log("Sender address:", sender);
60+
// // analyzeContractSecurity();
5361

54-
// Proceed with the transfer if the contract is secure
55-
return super.transferFrom(sender, recipient, amount);
56-
}
62+
// // Proceed with the transfer if the contract is secure
63+
// return super.transferFrom(sender, recipient, amount);
64+
// }
5765
}
5866

5967
// pragma solidity ^0.8.19;

packages/foundry/script/Allowance.s.sol

+34-15
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,51 @@
22
pragma solidity ^0.8.19;
33

44
import "../contracts/ScamHunterToken.sol";
5-
import "forge-std/console.sol"; // Assurez-vous que forge-std est importé pour l'affichage en console
5+
import "forge-std/Script.sol"; // Import Script to use `vm`
6+
import "forge-std/console.sol"; // Import console for logging
67

7-
contract Allowance {
8+
contract AllowanceScript is Script {
89
function run() external {
9-
// Adresse du spender (celui qui reçoit la permission)
10-
address spender = 0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f;
11-
12-
// Adresse du contrat ScamHunterToken (à remplacer par l'adresse déployée)
10+
// Reference the ScamHunterToken contract
1311
ScamHunterToken scamHunterToken = ScamHunterToken(
14-
0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6 // Assurez-vous que cette adresse est correcte
12+
0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
1513
);
1614

17-
// Afficher l'adresse du contrat pour confirmation
18-
console.log("ScamHunterToken address: ", address(scamHunterToken));
19-
console.log("this: ", address(this));
15+
// Ensure the address executing the script is the owner
16+
require(msg.sender == scamHunterToken.owner(), "Not the owner");
17+
18+
// Start the transaction using your account
19+
vm.startBroadcast(msg.sender);
2020

21-
// Création d'une allowance pour que `spender` puisse envoyer 1 SHT
21+
// Spender address
22+
// address spender = 0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f;
23+
address recipient = 0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f; // Recipient address (same as spender for this example)
24+
25+
// Amount to be transferred (1 ETH in wei)
2226
uint256 amount = 1 * (10 ** 18);
23-
scamHunterToken.forceApprove(spender, amount);
2427

25-
// Vérification de l'allowance
26-
uint256 allowance = scamHunterToken.allowance(msg.sender, spender);
28+
// Set an allowance
29+
// scamHunterToken.forceApprove(msg.sender, amount);
30+
scamHunterToken.approve(msg.sender, amount);
31+
32+
// Verify the allowance
33+
uint256 allowance = scamHunterToken.allowance(msg.sender, msg.sender);
2734
console.log("Allowance set: ", allowance);
2835
require(allowance == amount, "Allowance not set correctly");
2936

30-
// Afficher l'allowance dans la console
37+
// Log success
3138
console.log("Allowance set correctly for spender to transfer tokens.");
39+
40+
// Transfer tokens
41+
// Ensure the sender has enough balance before attempting transfer
42+
uint256 balance = scamHunterToken.balanceOf(msg.sender);
43+
console.log("msg.sender balance: ", balance);
44+
require(balance >= amount, "Insufficient balance for transfer");
45+
46+
// // Execute transferFrom
47+
scamHunterToken.transferFrom(msg.sender, recipient, amount);
48+
49+
// End the transaction
50+
vm.stopBroadcast();
3251
}
3352
}
+81-22
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,87 @@
1-
// SPDX-License-Identifier: UNLICENSED
2-
pragma solidity ^0.8.13;
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.19;
33

44
import "forge-std/Test.sol";
55
import "../contracts/ScamHunterToken.sol";
6+
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
67

78
contract ScamHunterTokenTest is Test {
8-
ScamHunterToken public scamHunterToken;
9-
10-
// function setUp() public {
11-
// scamHunterToken = new ScamHunterToken(vm.addr(1));
12-
// }
13-
14-
// function testMessageOnDeployment() public view {
15-
// require(
16-
// keccak256(bytes(scamHunterToken.greeting())) ==
17-
// keccak256("Building Unstoppable Apps!!!")
18-
// );
19-
// }
20-
21-
// function testSetNewMessage() public {
22-
// scamHunterToken.setGreeting("Learn Scaffold-ETH 2! :)");
23-
// require(
24-
// keccak256(bytes(scamHunterToken.greeting())) ==
25-
// keccak256("Learn Scaffold-ETH 2! :)")
26-
// );
27-
// }
9+
ScamHunterToken public token;
10+
address public owner;
11+
address public spender;
12+
address public recipient;
13+
14+
function setUp() public {
15+
owner = msg.sender;
16+
spender = msg.sender;
17+
recipient = address(0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f);
18+
19+
token = new ScamHunterToken(owner, bytes32(0));
20+
token.transfer(owner, 1000 * (10 ** 18)); // Mint some tokens to the owner
21+
}
22+
23+
function testForceApprove() public {
24+
uint256 amount = 100 * (10 ** 18);
25+
26+
// Execute forceApprove
27+
token.forceApprove(spender, amount);
28+
29+
// Check allowance
30+
uint256 allowance = token.allowance(owner, spender);
31+
assertEq(allowance, amount, "Allowance should be set correctly");
32+
}
33+
34+
function testTransferFrom() public {
35+
uint256 amount = 50 * (10 ** 18);
36+
37+
// Set an allowance
38+
token.forceApprove(spender, amount);
39+
40+
// Transfer tokens to the spender
41+
token.transferFrom(owner, spender, amount);
42+
43+
// Ensure the spender can transfer tokens
44+
uint256 spenderBalanceBefore = token.balanceOf(spender);
45+
assertEq(
46+
spenderBalanceBefore,
47+
amount,
48+
"Spender should have received tokens"
49+
);
50+
51+
// Execute transferFrom by the spender
52+
vm.prank(spender); // Set the context to the spender
53+
token.transferFrom(owner, recipient, amount);
54+
55+
// Verify the recipient's balance
56+
uint256 recipientBalance = token.balanceOf(recipient);
57+
assertEq(recipientBalance, amount, "Recipient should receive tokens");
58+
59+
// Verify the spender's balance
60+
uint256 spenderBalanceAfter = token.balanceOf(spender);
61+
assertEq(spenderBalanceAfter, 0, "Spender should not hold any tokens");
62+
}
63+
64+
function testTransferFromInsufficientAllowance() public {
65+
uint256 amount = 100 * (10 ** 18);
66+
67+
// Attempt to transfer more than the allowance
68+
vm.expectRevert("ERC20: insufficient allowance");
69+
vm.prank(spender);
70+
token.transferFrom(owner, recipient, amount);
71+
}
72+
73+
function testTransferFromInsufficientBalance() public {
74+
uint256 amount = 100 * (10 ** 18);
75+
76+
// Approve an amount less than the balance
77+
token.forceApprove(spender, amount);
78+
79+
// Transfer tokens to the spender
80+
token.transferFrom(owner, spender, amount);
81+
82+
// Attempt to transfer more tokens than the spender has
83+
vm.expectRevert("ERC20: transfer amount exceeds balance");
84+
vm.prank(spender);
85+
token.transferFrom(owner, recipient, amount + 1);
86+
}
2887
}

packages/nextjs/contracts/deployedContracts.ts

+55-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { GenericContractsDeclaration } from "~~/utils/scaffold-eth/contract";
77
const deployedContracts = {
88
31337: {
99
ScamHunterToken: {
10-
address: "0x8a791620dd6260079bf849dc5567adc3f2fdc318",
10+
address: "0x2279b7a0a67db372996a5fab50d91eaa73d2ebe6",
1111
abi: [
1212
{
1313
type: "constructor",
@@ -354,6 +354,28 @@ const deployedContracts = {
354354
],
355355
anonymous: false,
356356
},
357+
{
358+
type: "error",
359+
name: "AddressEmptyCode",
360+
inputs: [
361+
{
362+
name: "target",
363+
type: "address",
364+
internalType: "address",
365+
},
366+
],
367+
},
368+
{
369+
type: "error",
370+
name: "AddressInsufficientBalance",
371+
inputs: [
372+
{
373+
name: "account",
374+
type: "address",
375+
internalType: "address",
376+
},
377+
],
378+
},
357379
{
358380
type: "error",
359381
name: "ERC20InsufficientAllowance",
@@ -440,6 +462,11 @@ const deployedContracts = {
440462
},
441463
],
442464
},
465+
{
466+
type: "error",
467+
name: "FailedInnerCall",
468+
inputs: [],
469+
},
443470
{
444471
type: "error",
445472
name: "OwnableInvalidOwner",
@@ -462,8 +489,34 @@ const deployedContracts = {
462489
},
463490
],
464491
},
492+
{
493+
type: "error",
494+
name: "SafeERC20FailedOperation",
495+
inputs: [
496+
{
497+
name: "token",
498+
type: "address",
499+
internalType: "address",
500+
},
501+
],
502+
},
465503
],
466-
inheritedFunctions: {},
504+
inheritedFunctions: {
505+
allowance: "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol",
506+
approve: "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol",
507+
balanceOf: "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol",
508+
decimals: "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol",
509+
name: "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol",
510+
symbol: "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol",
511+
totalSupply: "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol",
512+
transfer: "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol",
513+
transferFrom: "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol",
514+
burn: "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol",
515+
burnFrom: "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol",
516+
owner: "lib/openzeppelin-contracts/contracts/access/Ownable.sol",
517+
renounceOwnership: "lib/openzeppelin-contracts/contracts/access/Ownable.sol",
518+
transferOwnership: "lib/openzeppelin-contracts/contracts/access/Ownable.sol",
519+
},
467520
},
468521
},
469522
} as const;

0 commit comments

Comments
 (0)