Skill v1.2.0
currentAutomated scan100/100name: pharos-solidity-authoring description: "Write or refactor Pharos Solidity contracts with clear structure, custom errors, events, modifiers, and testable patterns. Use when implementing, writing, or refactoring Solidity smart contracts for Pharos, including ERC-20/721/1155 tokens, staking, vault, AMM, lending, or DeFi/RealFi protocols. Keywords: Solidity, write contract, implement, refactor, smart contract, ERC-20, ERC-721, ERC-1155, staking, vault, AMM, lending, DeFi, RealFi, Pharos, PHRS, custom errors, events, modifiers, Foundry, forge, Remix." metadata: audience: developer version: 1.2.0 category: contract slash: true
Solidity Authoring
Write or refactor Solidity contracts with clear structure, custom errors, events, modifiers, and testable patterns for Pharos mainnet (chain ID 1672) using PHRS as native currency (18 decimals).
When to Use
write Solidity, implement contract, refactor contract, contract code, Solidity, write a contract, implement the staking contract, write smart contract
When NOT to Use
designing architecture (use contract-architecture), reviewing code (use contract-review), or writing tests (use test-generation)
Prerequisites
- Security:
- .env Usage: Environment variables MUST be stored in a
.envfile in the project root. NEVER useexport VAR=...for sensitive data. - Mandatory Check: The Agent MUST verify
.envexists and variables are set usinggrep -q(NEVERcat,head,tail— those expose secrets) before any deployment or on-chain action. - Git: Ensure
.envis listed in.gitignoreto prevent accidental commits.
- Foundry:
forge buildmust succeed. Runforge --versionto verify installation. - RPC endpoint: Set
PHAROS_TESTNET_RPC=$PHAROS_TESTNET_RPC_URLorPHAROS_MAINNET_RPC=$PHAROS_MAINNET_RPC_URLin your environment or.env. - PharosScan API key: Set
PHAROSSCAN_API_KEYfor contract verification (https://www.pharosscan.xyz). - Network reachability: Run
cast chain-id --rpc-url $RPC_URLto confirm the target network is reachable. - Foundry config:
foundry.tomlshould have[rpc_endpoints]section withpharos_testnetandpharos_mainnetentries.
Pharos-Specific Contract Patterns
Chain ID Validation
Always verify the target chain in constructors and critical functions:
error WrongChain(uint256 actual, uint256 expected);uint256 public constant PHAROS_MAINNET = 1672;uint256 public constant PHAROS_TESTNET = 688689;modifier onlyPharosMainnet() {if (block.chainid != PHAROS_MAINNET) revert WrongChain(block.chainid, PHAROS_MAINNET);_;}
Native Currency Handling (PROS/PHRS)
The native currency (PROS on mainnet, PHRS on testnet, 18 decimals) forwards all remaining gas in .call{value:} — there is no 2300 gas stipend. Always specify a gas limit or use a pull-over-push pattern:
// WRONG — forwards all gas, dangerous in loops(bool sent,) = payable(receiver).call{value: amount}("");// RIGHT — cap gas to prevent reentrancy(bool sent,) = payable(receiver).call{value: amount, gas: 10000}("");// RIGHT — pull-over-push (recommended)mapping(address => uint256) public pendingWithdrawals;function withdraw() external {uint256 amount = pendingWithdrawals[msg.sender];pendingWithdrawals[msg.sender] = 0;(bool sent,) = payable(msg.sender).call{value: amount, gas: 10000}("");if (!sent) pendingWithdrawals[msg.sender] = amount; // revert on failure}
PHRS Receive Pattern for Staking/Vault
// SPDX-License-Identifier: MITpragma solidity ^0.8.20;contract PharosStaking {error WrongChain(uint256 actual, uint256 expected);uint256 public constant PHAROS_CHAIN_ID = 1672;mapping(address => uint256) public stakes;mapping(address => uint256) public rewards;constructor() {if (block.chainid != PHAROS_CHAIN_ID) revert WrongChain(block.chainid, PHAROS_CHAIN_ID);}function stake() external payable {stakes[msg.sender] += msg.value;}function unstake(uint256 amount) external {require(stakes[msg.sender] >= amount, "insufficient stake");stakes[msg.sender] -= amount;(bool sent,) = payable(msg.sender).call{value: amount, gas: 10000}("");require(sent, "PHRS transfer failed");}// PHRS-native receive — no 2300 gas stipendreceive() external payable {}}
Workflow
- Strict .env Check: Confirm
.envexists withtest -f .envand check variables viagrep -q(without printing values). NEVER print.envcontents. Do NOT proceed if missing or if the user suggestsexport.
- Requirement Gathering: Analyze the user's request to identify the specific task, target environment (Atlantic 688689 or Pacific 1672), and any missing context. Zero-assumption delivery.
- Mandatory Plan (`PLAN.md`): Create or update
PLAN.mdin the project root with the proposed strategy. Wait for explicit 'Approve' or 'Proceed' from the user before taking any action. - Capture the contract goal, inputs, outputs, and invariants.
- Check prerequisites: verify Foundry is installed, RPC endpoints are reachable, and required env vars are set. Ask the user for any missing values before proceeding.
- Draft the contract shape, custom errors, events, and modifiers.
- Include Pharos chain ID validation and PHRS gas stipend warnings.
- Present the implementation plan and ask for confirmation.
- Write the contract in a way that is easy to test and review.
Output
- contract draft
- event and error plan
- implementation notes
- test hooks
- invariants
Examples
- "Write a Pharos staking contract with PHRS stake/unstake and chain ID 1672 validation"
- "Refactor this PHRS token contract to use custom errors and events"
- "Implement a PHRS staking contract with deposit, withdraw, and reward distribution using pull-over-push"
- "Add chain ID validation and gas-stipend-safe .call to this Pharos vault contract"
Verification
forge build or npx hardhat compile. Then forge test for unit tests. Verify chain ID in deployment script (see foundry-hardhat-contract-workflow).
Related
contract-architecture (design), interface-abi-design (ABI), test-generation (tests)
Gate
High risk — two-phase execution required:
Phase 1 — Plan (present freely):
- Draft the
PLAN.mdwith the full implementation strategy, environment-aware safeguards, and verification steps. - Wait for explicit 'Approve' or 'Proceed' from the user.
Phase 2 — Execute (wait for approval):
- Execute the approved plan from
PLAN.md. - Do NOT Write contract files to disk, modify existing files, or compile/deploy
- Perform a final "Ready to Broadcast?" check for any high-risk on-chain actions.
- Wait for explicit user confirmation ("I approve", "proceed", "looks good") before taking any of the Phase 2 actions.
Pharos Solidity Patterns (with Real Examples)
Immutable Chain ID Pattern
Every contract stores the chain ID as an immutable:
// contracts/PharosLendingPool.soluint256 public immutable i_chainId;constructor(uint256 _chainId) {i_chainId = _chainId;}
Pull-Over-Push Withdrawal
Instead of sending tokens directly, let users pull:
// contracts/StakingPool.solfunction withdraw(uint256 _amount) external nonReentrant {// ... validate ...s_rewardToken.safeTransfer(msg.sender, rewards);s_stakingToken.safeTransfer(msg.sender, _amount);}
Custom Error Pattern
// All contracts use this patternerror DEXPool__NotOwner();error DEXPool__InsufficientLiquidity();error DEXPool__SlippageExceeded();if (amountOut < _minAmountOut) revert DEXPool__SlippageExceeded();
Reentrancy Guard
// contracts/DEXPool.solimport {ReentrancyGuard} from "./interfaces/ReentrancyGuard.sol";contract DEXPool is ReentrancyGuard {function swap(uint256 _amountIn, uint256 _minAmountOut) external nonReentrant {// ... swap logic ...}}
SafeERC20 Pattern
// contracts/SimpleLender.solimport {SafeERC20} from "./interfaces/SafeERC20.sol";using SafeERC20 for IERC20;i_borrowToken.safeTransferFrom(msg.sender, address(this), _amount);
Contract Quick Reference
| Pattern | Where Applied | |
|---|---|---|
| Immutable owner + chain ID | All contracts | |
| Custom errors | All contracts | |
| ReentrancyGuard | SimpleLender, DEXPool, StakingPool | |
| SafeERC20 | SimpleLender, StakingPool | |
| Pull-over-push | StakingPool, PharosSPNPaymaster | |
| Event emission per state change | All contracts | |
| NatSpec documentation | All contracts |
References
contracts/— All contract implementationscontracts/interfaces/— ReentrancyGuard, SafeERC20, IERC20- OpenZeppelin patterns adapted for Pharos