Мне нужна помощь в проекте. Твоя задача использовать язык Solidity, фреймворк MUD(второй версии), а для написания тестов использовать Foundry. Мне надо написать тесты для библиотеки LibExperience.sol . Эта библиотека связана с компонентом ExperienceComponent.sol . Так же в новой версии фреймворка MUD появились таблицы, который заменили компоненты.
Код таблицы :
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
/* Autogenerated file. Do not edit manually. */
// Import schema type
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
// Import store internals
import { IStore } from "@latticexyz/store/src/IStore.sol";
import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
import { StoreCore } from "@latticexyz/store/src/StoreCore.sol";
import { Bytes } from "@latticexyz/store/src/Bytes.sol";
import { Memory } from "@latticexyz/store/src/Memory.sol";
import { SliceLib } from "@latticexyz/store/src/Slice.sol";
import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol";
import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol";
import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol";
library Experience {
/** Get the table's schema */
function getSchema() internal pure returns (Schema) {
SchemaType[] memory _schema = new SchemaType;
_schema[0] = SchemaType.UINT32_ARRAY;
code
return SchemaLib.encode(_schema);
}
function getKeySchema() internal pure returns (Schema) {
SchemaType[] memory _schema = new SchemaType;
_schema[0] = SchemaType.UINT256;
code
return SchemaLib.encode(_schema);
}
/** Get the table's metadata */
function getMetadata() internal pure returns (string memory, string[] memory) {
string[] memory _fieldNames = new string;
_fieldNames[0] = "value";
return ("Experience", _fieldNames);
}
/** Register the table's schema */
function registerSchema() internal {
StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema());
}
/** Register the table's schema (using the specified store) */
function registerSchema(IStore _store) internal {
_store.registerSchema(_tableId, getSchema(), getKeySchema());
}
/** Set the table's metadata */
function setMetadata() internal {
(string memory _tableName, string[] memory _fieldNames) = getMetadata();
StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames);
}
Assistant
Based on the provided information, I'll create a new contract named TestLibExperience that will inherit from BaseTest. This contract will test the functions in LibExperience.sol, making use of the components from BaseTest.sol:
solidity
/** Set the table's metadata (using the specified store) */
function setMetadata(IStore _store) internal {
(string memory _tableName, string[] memory _fieldNames) = getMetadata();
_store.setMetadata(_tableId, _tableName, _fieldNames);
}
/** Get value */
function get(uint256 entity) internal view returns (uint32[3] memory value) {
bytes32[] memory _primaryKeys = new bytes32;
_primaryKeys[0] = bytes32(uint256((entity)));
/** Push an element to value */
function push(uint256 entity, uint32 _element) internal {
bytes32[] memory _primaryKeys = new bytes32;
_primaryKeys[0] = bytes32(uint256((entity)));
/** Push an element to value (using the specified store) */
function push(IStore _store, uint256 entity, uint32 _element) internal {
bytes32[] memory _primaryKeys = new bytes32;
_primaryKeys[0] = bytes32(uint256((entity)));
/** Update an element of value at _index */
function update(uint256 entity, uint256 _index, uint32 _element) internal {
bytes32[] memory _primaryKeys = new bytes32;
_primaryKeys[0] = bytes32(uint256((entity)));
/** Update an element of value (using the specified store) at _index */
function update(IStore _store, uint256 entity, uint256 _index, uint32 _element) internal {
bytes32[] memory _primaryKeys = new bytes32;
_primaryKeys[0] = bytes32(uint256((entity)));
/* Delete all data for given keys */
function deleteRecord(uint256 entity) internal {
bytes32[] memory _primaryKeys = new bytes32;
_primaryKeys[0] = bytes32(uint256((entity)));
code
StoreSwitch.deleteRecord(_tableId, _primaryKeys);
}
/* Delete all data for given keys (using the specified store) */
function deleteRecord(IStore _store, uint256 entity) internal {
bytes32[] memory _primaryKeys = new bytes32;
_primaryKeys[0] = bytes32(uint256((entity)));
code
_store.deleteRecord(_tableId, _primaryKeys);
}
}
function toStaticArray_uint32_3(uint32[] memory _value) pure returns (uint32[3] memory _result) {
// in memory static arrays are just dynamic arrays without the length byte
assembly {
_result := add(_value, 0x20)
}
}
Код LibExperience.sol :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import { IUint256Component } from "solecs/interfaces/IUint256Component.sol";
import { PStat, PS_L, ExperienceComponent, ID as ExperienceComponentID } from "./ExperienceComponent.sol";
import { Experience } from "../codegen/Tables.sol";
@dev Allow target to receive exp, set exp to 0s
*/
function initExp(uint256 targetEntity) internal {
uint32[PS_L] memory exp;
Experience.set(targetEntity, exp);
}
/**
@dev Increase target's experience
Exp must be initialized
*/
function increaseExp(uint256 targetEntity, uint32[PS_L] memory addExp) internal {
// get current exp, or revert if it doesn't exist
if (!Experience.get(targetEntity)) {
revert LibExperience__ExpNotInitialized();
}
uint32[PS_L] memory exp = Experience.get(targetEntity);
code
// increase
code
for (uint256 i; i < PS_L; i++) {
exp[i] += addExp[i];
}
// set increased exp
Experience.set(targetEntity, exp);
}
/**
@dev Calculate aggregate level based on weighted sum of pstat exp
*/
function getAggregateLevel(uint256 targetEntity, uint32[PS_L] memory levelMul) internal view returns (uint32) {
uint32[PS_L] memory exp = getExp(targetEntity);
uint256 expTotal;
uint256 mulTotal;
for (uint256 i; i < PS_L; i++) {
expTotal += exp[i] * levelMul[i];
mulTotal += levelMul[i];
}
code
expTotal /= mulTotal;
code
return _getLevel(expTotal);
}
/**
@dev Calculate level based on single exp value
*/
function _getLevel(uint256 expVal) private pure returns (uint32) {
// expVal per level rises exponentially with polynomial easing
// 1-0, 2-96, 3-312, 4-544, 5-804, 6-1121...
for (uint32 level = 1; level < MAX_LEVEL; level++) {
// (1<<i) == 2**i ; can't overflow due to maxLevel
if (expVal < _getExpForLevel(level + 1)) {
return level;
}
}
return MAX_LEVEL;
}
/**
@dev Utility function to reverse a level into its required exp
*/
function _getExpForLevel(uint32 level) private pure returns (uint32) {
if (level < 1 || level > MAX_LEVEL) revert LibExperience__InvalidLevel();
@dev Get exp amount to get base primary to pstats (assuming 0 current exp).
This is a utility for testing
*/
function getExpForPStats(uint32[PS_L] memory pstats) internal pure returns (uint32[PS_L] memory exp) {
for (uint256 i; i < PS_L; i++) {
exp[i] = _getExpForLevel(pstats[i]);
}
}
/**
@dev same as getExpForPStats but for 1 specific pstat
*/
function getExpForPStat(uint32 pstat) internal pure returns (uint32) {
return _getExpForLevel(pstat);
}
}
Код ExperienceComponent.sol : // SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import { LibTypes } from "solecs/LibTypes.sol";
import { BareComponent } from "solecs/BareComponent.sol";
uint256 constant ID = uint256(keccak256("component.Experience"));
contract ExperienceComponent is BareComponent {
constructor(address _world) BareComponent(_world, ID) {}
function getSchema() public pure override returns (string[] memory keys, LibTypes.SchemaValue[] memory values) {
keys = new string;
values = new LibTypes.SchemaValue;
function getValue(uint256 entity) public view returns (uint32[PS_L] memory result) {
return abi.decode(getRawValue(entity), (uint32[3])); /* 3 == PS_L */
}
function set(uint256 entity, uint32[PS_L] memory pstats) public {
set(entity, abi.encode(pstats));
}
}
Так же я в своем проекте создал базу для тестов на Foundry, она называется BaseTest.sol , используй ее для написания контракта :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import { IUint256Component } from "solecs/interfaces/IUint256Component.sol";
import { World } from "solecs/World.sol";
import { getAddressById } from "solecs/utils.sol";
import { PRBTest } from "@prb/test/src/PRBTest.sol";
import { LibDeploy } from "../test/LibDeploy.sol";
// Components
import { ExperienceComponent, ID as ExperienceComponentID } from "./charstat/ExperienceComponent.sol";
import { LifeCurrentComponent, ID as LifeCurrentComponentID } from "./charstat/LifeCurrentComponent.sol";
import { ManaCurrentComponent, ID as ManaCurrentComponentID } from "./charstat/ManaCurrentComponent.sol";
abstract contract BaseTest is PRBTest {
World world;
function setUp() public virtual {
alice = address(bytes20(keccak256("alice")));
bob = address(bytes20(keccak256("bob")));
code
// deploy world
world = new World();
world.init();
LibDeploy.deploy(address(this), address(world), false);
// Assign all systems and components to storage vars for convenience
components = world.components();
systems = world.systems();
// Components
experienceComponent = ExperienceComponent(getAddressById(components, ExperienceComponentID));
lifeCurrentComponent = LifeCurrentComponent(getAddressById(components, LifeCurrentComponentID));
manaCurrentComponent = ManaCurrentComponent(getAddressById(components, ManaCurrentComponentID));
}
} I need help with a project. Your task is to use the Solidity language, the MUD framework, and use Foundry to write tests. I need to write tests for the LibExperience.sol library. This library is linked to the ExperienceComponent.sol component.
LibExperience.sol code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import { IUint256Component } from "solecs/interfaces/IUint256Component.sol";
import { getAddressById } from "solecs/utils.sol";
import { PStat, PS_L, ExperienceComponent, ID as ExperienceComponentID } from "./ExperienceComponent.sol";
@dev Allow target to receive exp, set exp to 0s
*/
function initExp(Self memory __self) internal {
uint32[PS_L] memory exp;
__self.comp.set(__self.targetEntity, exp);
}
/**
@dev Increase target's experience
Exp must be initialized
*/
function increaseExp(Self memory __self, uint32[PS_L] memory addExp) internal {
// get current exp, or revert if it doesn't exist
if (!__self.comp.has(__self.targetEntity)) {
revert LibExperience__ExpNotInitialized();
}
uint32[PS_L] memory exp = __self.comp.getValue(__self.targetEntity);
code
// increase
code
for (uint256 i; i < PS_L; i++) {
exp[i] += addExp[i];
}
// set increased exp
__self.comp.set(__self.targetEntity, exp);
}
/**
@dev Calculate aggregate level based on weighted sum of pstat exp
*/
function getAggregateLevel(Self memory __self, uint32[PS_L] memory levelMul) internal view returns (uint32) {
uint32[PS_L] memory exp = getExp(__self);
uint256 expTotal;
uint256 mulTotal;
for (uint256 i; i < PS_L; i++) {
expTotal += exp[i] * levelMul[i];
mulTotal += levelMul[i];
}
code
expTotal /= mulTotal;
code
return _getLevel(expTotal);
}
/**
@dev Calculate level based on single exp value
*/
function _getLevel(uint256 expVal) private pure returns (uint32) {
// expVal per level rises exponentially with polynomial easing
// 1-0, 2-96, 3-312, 4-544, 5-804, 6-1121...
for (uint32 level = 1; level < MAX_LEVEL; level++) {
// (1<<i) == 2**i ; can't overflow due to maxLevel
if (expVal < _getExpForLevel(level + 1)) {
return level;
}
}
return MAX_LEVEL;
}
/**
@dev Utility function to reverse a level into its required exp
*/
function _getExpForLevel(uint32 level) private pure returns (uint32) {
if (level < 1 || level > MAX_LEVEL) revert LibExperience__InvalidLevel();
@dev Get exp amount to get base primary to pstats (assuming 0 current exp).
This is a utility for testing
*/
function getExpForPStats(uint32[PS_L] memory pstats) internal pure returns (uint32[PS_L] memory exp) {
for (uint256 i; i < PS_L; i++) {
exp[i] = _getExpForLevel(pstats[i]);
}
}
/**
@dev same as getExpForPStats but for 1 specific pstat
*/
function getExpForPStat(uint32 pstat) internal pure returns (uint32) {
return _getExpForLevel(pstat);
}
} ExperienceComponent.sol code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import { LibTypes } from "solecs/LibTypes.sol";
import { BareComponent } from "solecs/BareComponent.sol";
uint256 constant ID = uint256(keccak256("component.Experience"));
contract ExperienceComponent is BareComponent {
constructor(address _world) BareComponent(_world, ID) {}
function getSchema() public pure override returns (string[] memory keys, LibTypes.SchemaValue[] memory values) {
keys = new string;
values = new LibTypes.SchemaValue;
function getValue(uint256 entity) public view returns (uint32[PS_L] memory result) {
return abi.decode(getRawValue(entity), (uint32[3])); /* 3 == PS_L */
}
function set(uint256 entity, uint32[PS_L] memory pstats) public {
set(entity, abi.encode(pstats));
}
} I also created a base for tests on Foundry in my project, it is called BaseTest.sol , use it to write a contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import { IUint256Component } from "solecs/interfaces/IUint256Component.sol";
import { World } from "solecs/World.sol";
import { getAddressById } from "solecs/utils.sol";
import { PRBTest } from "@prb/test/src/PRBTest.sol";
import { LibDeploy } from "../test/LibDeploy.sol";
// Components
import { ExperienceComponent, ID as ExperienceComponentID } from "./charstat/ExperienceComponent.sol";
import { LifeCurrentComponent, ID as LifeCurrentComponentID } from "./charstat/LifeCurrentComponent.sol";
import { ManaCurrentComponent, ID as ManaCurrentComponentID } from "./charstat/ManaCurrentComponent.sol";
abstract contract BaseTest is PRBTest {
World world;
function setUp() public virtual {
alice = address(bytes20(keccak256("alice")));
bob = address(bytes20(keccak256("bob")));
code
// deploy world
world = new World();
world.init();
LibDeploy.deploy(address(this), address(world), false);
// Assign all systems and components to storage vars for convenience
components = world.components();
systems = world.systems();
// Components
experienceComponent = ExperienceComponent(getAddressById(components, ExperienceComponentID));
lifeCurrentComponent = LifeCurrentComponent(getAddressById(components, LifeCurrentComponentID));
manaCurrentComponent = ManaCurrentComponent(getAddressById(components, ManaCurrentComponentID));
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "./BaseTest.sol";
import { LibExperience } from "./LibExperience.sol";
import { PStat, PS_L } from "./ExperienceComponent.sol";
contract TestLibExperience is BaseTest {
uint256 constant ENTITY_ID = 1;
function testGetPStats() public {
// Initialize some values for testing
uint32[PS_L] memory pstats = [10, 20, 15];
uint32[PS_L] memory expectedExp = LibExperience.getExpForPStats(pstats);
// Set values in the experience component
experienceComponent.set(ENTITY_ID, expectedExp);
// Test LibExperience.getPStats
uint32[PS_L] memory resultPStats = LibExperience.getPStats(ENTITY_ID);
for (uint256 i; i < pstats.length; i++) {
assertEq32(resultPStats[i], pstats[i]);
}
}
function testGetPStat() public {
// Initialize some values for testing
uint32 pstatStrength = 10;
uint32 expStrength = LibExperience.getExpForPStat(pstatStrength);
// Set values in the experience component
experienceComponent.update(ENTITY_ID, uint256(PStat.STRENGTH), expStrength);
// Test LibExperience.getPStat
uint32 resultPStat = LibExperience.getPStat(ENTITY_ID, PStat.STRENGTH);
assertEq32(resultPStat, pstatStrength);
}
// ... You may continue to add more tests below for other functions in LibExperience.sol
// Utility function to assert equality for 32-bit unsigned integers
function assertEq32(uint32 a, uint32 b) internal pure {
assert(a == b, "TestLibExperience: values are not equal");
}
}
Now, you can deploy and run the TestLibExperience contract to test the functions in LibExperience.sol, making use of components from BaseTest.sol for easier testing. Make sure to continue adding more tests for other functions in LibExperience.sol.