Verified contractExactMIT
SwarmMandate
Address0x52d9f87aabdf68bf1f79960affaeac97d121d12d
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.26
- Same code
Only this contract
Details
- Compiler
- solc 0.8.26+commit.8a97fa7a
- Language
- solidity
- Optimization
- Yes, 200 runs
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,021,377
- Creation transaction
- 0x4f3a537c16…d0e92f9473
swarm/Mandate.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
/// @title Mandate
/// @notice Encodes, decodes, and deterministically mutates the packed permission
/// word an agent trades under.
/// @dev A mandate is a single bytes32. Mask fields occupy the low bits and are
/// copied unchanged from parent to heir; numeric fields occupy the high
/// bits and are perturbed within a bounded range on inheritance. All
/// functions here are pure: this contract holds no state and every result
/// is fully determined by its arguments.
contract SwarmMandate {
struct Fields {
uint16 venueMask;
uint16 assetMask;
uint16 maxPositionBps;
uint16 maxDrawdownBps;
uint16 holdWindow;
uint16 rebalanceCadence;
uint32 strategyHash;
}
uint16 public constant FIELD_MIN = 1;
uint16 public constant FIELD_MAX = 10_000;
uint16 public constant MUTATION_BPS = 900;
/// @notice pack a set of fields into a single mandate word
function encode(Fields memory f) public pure returns (bytes32 mandate) {
uint256 packed;
packed |= uint256(f.venueMask);
packed |= uint256(f.assetMask) << 16;
packed |= uint256(f.maxPositionBps) << 32;
packed |= uint256(f.maxDrawdownBps) << 48;
packed |= uint256(f.holdWindow) << 64;
packed |= uint256(f.rebalanceCadence) << 80;
packed |= uint256(f.strategyHash) << 96;
mandate = bytes32(packed);
}
/// @notice unpack a mandate word into its component fields
function decode(bytes32 mandate) public pure returns (Fields memory f) {
uint256 packed = uint256(mandate);
f.venueMask = uint16(packed);
f.assetMask = uint16(packed >> 16);
f.maxPositionBps = uint16(packed >> 32);
f.maxDrawdownBps = uint16(packed >> 48);
f.holdWindow = uint16(packed >> 64);
f.rebalanceCadence = uint16(packed >> 80);
f.strategyHash = uint32(packed >> 96);
}
/// @notice a mandate with at least one venue and one asset permitted
function isViable(bytes32 mandate) external pure returns (bool) {
Fields memory f = decode(mandate);
return f.venueMask != 0 && f.assetMask != 0 && f.maxPositionBps > 0;
}
/// @notice derive an heir mandate from a parent mandate
/// @dev masks pass through unchanged; the four numeric fields are each
/// perturbed independently by up to MUTATION_BPS, seeded from the
/// parent id, the heir id, and a caller-supplied block hash so the
/// result is reproducible by any indexer but unknowable in advance.
function mutate(
bytes32 parentMandate,
uint32 parentId,
uint32 heirId,
bytes32 blockSeed
) external pure returns (bytes32 heirMandate) {
Fields memory p = decode(parentMandate);
Fields memory h;
h.venueMask = p.venueMask;
h.assetMask = p.assetMask;
h.strategyHash = p.strategyHash;
h.maxPositionBps = _perturb(p.maxPositionBps, parentId, heirId, blockSeed, 0);
h.maxDrawdownBps = _perturb(p.maxDrawdownBps, parentId, heirId, blockSeed, 1);
h.holdWindow = _perturb(p.holdWindow, parentId, heirId, blockSeed, 2);
h.rebalanceCadence = _perturb(p.rebalanceCadence, parentId, heirId, blockSeed, 3);
heirMandate = encode(h);
}
function _perturb(
uint16 field,
uint32 parentId,
uint32 heirId,
bytes32 blockSeed,
uint8 slot
) internal pure returns (uint16) {
bytes32 h = keccak256(abi.encodePacked(parentId, heirId, blockSeed, slot));
int256 delta = int256(uint256(h) % (2 * uint256(MUTATION_BPS) + 1)) - int256(uint256(MUTATION_BPS));
int256 moved = (int256(uint256(field)) * (10_000 + delta)) / 10_000;
if (moved < int256(uint256(FIELD_MIN))) moved = int256(uint256(FIELD_MIN));
if (moved > int256(uint256(FIELD_MAX))) moved = int256(uint256(FIELD_MAX));
return uint16(uint256(moved));
}
/// @notice the seed a given mutation actually used, for offline verification
function seedFor(uint32 parentId, uint32 heirId, bytes32 blockSeed, uint8 slot) external pure returns (bytes32) {
return keccak256(abi.encodePacked(parentId, heirId, blockSeed, slot));
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": true
},
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.