From 443a5a4099da87f36836758fbb213684f4a92a06 Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 28 Jul 2025 20:31:21 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20seemingly=20working=20smart=20contract?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/contract/main.sol | 42 ++++++++++++++++++++++++++++++++++++++---- test/main.t.sol | 30 +++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/src/contract/main.sol b/src/contract/main.sol index 4f400ad..a3a2940 100644 --- a/src/contract/main.sol +++ b/src/contract/main.sol @@ -1,11 +1,45 @@ // SPDX-License-Identifier: MIT -pragma solidity ^0.8; +pragma solidity 0.8.28; + +struct score { + string p1; + string p2; + uint128 p1Score; + uint128 p2Score; +} contract scoreStore { - uint16 test = 0; + address public owner; + uint public lastId; + mapping (uint => score) public scores; - function up(uint16 plus) public { - test += plus; + constructor() { + owner = msg.sender; + lastId = 0; } + + modifier ownerOnly { + require(msg.sender == owner, "Need to be contract owner"); + _; + } + + function addScore(string memory p1, string memory p2, uint128 p1Score, uint128 p2Score) external ownerOnly returns (uint id) { + score memory s; + + s.p1 = p1; + s.p2 = p2; + s.p1Score = p1Score; + s.p2Score = p2Score; + + scores[lastId] = s; + id = lastId; + lastId++; + + return (id); + } + + function getScore(uint id) external view returns (score memory) { + return scores[id]; + } } diff --git a/test/main.t.sol b/test/main.t.sol index bb58846..0014208 100644 --- a/test/main.t.sol +++ b/test/main.t.sol @@ -1,13 +1,33 @@ // SPDX-License-Identifier: MIT -pragma solidity ^0.8; +pragma solidity 0.8.28; import "src/contract/main.sol"; import "forge-std/Test.sol"; -contract mainTest is Test, scoreStore { - function testUp() public { - up(2); - assertEq(test, 2); +contract scoreStoreTest is Test { + scoreStore scoreS; + address nonOwner = address(1); + + function setUp() public { + scoreS = new scoreStore(); } + + function testAddScore() public { + uint id = scoreS.addScore("omg", "test", 5, 8); + + score memory s = scoreS.getScore(id); + assertEq(s.p1, "omg"); + assertEq(s.p2, "test"); + assertEq(s.p1Score, 5); + assertEq(s.p2Score, 8); + + id = scoreS.addScore("ahhhhh", "test", 5, 8); + + s = scoreS.getScore(id); + assertEq(s.p1, "ahhhhh"); + assertEq(s.p2, "test"); + assertEq(s.p1Score, 5); + assertEq(s.p2Score, 8); + } }