|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import {Test, console} from "forge-std/Test.sol"; |
| 5 | +import {Swan} from "../src/Swan.sol"; |
| 6 | +import {UUPSUpgradeable} from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol"; |
| 7 | +import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; |
| 8 | +import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; |
| 9 | +import {SwanManager, SwanMarketParameters} from "../src/SwanManager.sol"; |
| 10 | +import {LLMOracleTaskParameters} from "@firstbatch/dria-oracle-contracts/LLMOracleCoordinator.sol"; |
| 11 | +import {MockERC20} from "./mock/MockERC20.sol"; |
| 12 | +import {MockLLMOracleCoordinator} from "./mock/MockLLMOracleCoordinator.sol"; |
| 13 | +import {MockSwanAgentFactory} from "./mock/MockSwanAgentFactory.sol"; |
| 14 | +import {MockSwanArtifactFactory} from "./mock/MockSwanArtifactFactory.sol"; |
| 15 | + |
| 16 | +contract SwanUpgradeTest is Test { |
| 17 | + Swan swanImplementationV1; |
| 18 | + Swan proxy; |
| 19 | + |
| 20 | + MockERC20 token; |
| 21 | + MockLLMOracleCoordinator coordinator; |
| 22 | + MockSwanAgentFactory agentFactory; |
| 23 | + MockSwanArtifactFactory artifactFactory; |
| 24 | + |
| 25 | + address owner = address(0x1); |
| 26 | + address user = address(0x2); |
| 27 | + |
| 28 | + function setUp() public { |
| 29 | + // Deploy mock dependencies |
| 30 | + token = new MockERC20("Test Token", "TEST"); |
| 31 | + coordinator = new MockLLMOracleCoordinator(); |
| 32 | + agentFactory = new MockSwanAgentFactory(); |
| 33 | + artifactFactory = new MockSwanArtifactFactory(); |
| 34 | + |
| 35 | + vm.startPrank(owner); |
| 36 | + |
| 37 | + // Deploy the original implementation |
| 38 | + swanImplementationV1 = new Swan(); |
| 39 | + |
| 40 | + SwanMarketParameters memory marketParams = SwanMarketParameters({ |
| 41 | + withdrawInterval: 1 days, |
| 42 | + listingInterval: 2 days, |
| 43 | + buyInterval: 3 days, |
| 44 | + platformFee: 10, |
| 45 | + maxArtifactCount: 10, |
| 46 | + minArtifactPrice: 1 ether, |
| 47 | + timestamp: block.timestamp, |
| 48 | + maxAgentFee: 20 |
| 49 | + }); |
| 50 | + |
| 51 | + LLMOracleTaskParameters memory oracleParams = |
| 52 | + LLMOracleTaskParameters({difficulty: 1, numGenerations: 1, numValidations: 1}); |
| 53 | + |
| 54 | + bytes memory initData = abi.encodeCall( |
| 55 | + Swan.initialize, |
| 56 | + ( |
| 57 | + marketParams, |
| 58 | + oracleParams, |
| 59 | + address(coordinator), |
| 60 | + address(token), |
| 61 | + address(agentFactory), |
| 62 | + address(artifactFactory) |
| 63 | + ) |
| 64 | + ); |
| 65 | + |
| 66 | + // Deploy the proxy with the implementation and initialization data |
| 67 | + ERC1967Proxy proxyContract = new ERC1967Proxy(address(swanImplementationV1), initData); |
| 68 | + |
| 69 | + // Cast the proxy to Swan for easier interaction |
| 70 | + proxy = Swan(address(proxyContract)); |
| 71 | + |
| 72 | + vm.stopPrank(); |
| 73 | + |
| 74 | + // Verify initial setup |
| 75 | + assertEq(proxy.owner(), owner); |
| 76 | + } |
| 77 | + |
| 78 | + function testUpgrade() public { |
| 79 | + // Pre-upgrade checks |
| 80 | + assertEq(address(proxy.agentFactory()), address(agentFactory)); |
| 81 | + assertEq(address(proxy.artifactFactory()), address(artifactFactory)); |
| 82 | + |
| 83 | + // Deploy the new implementation |
| 84 | + vm.startPrank(owner); |
| 85 | + Swan swanImplementationV2 = new Swan(); |
| 86 | + |
| 87 | + proxy.upgradeToAndCall(address(swanImplementationV2), ""); |
| 88 | + vm.stopPrank(); |
| 89 | + |
| 90 | + // Get implementation address |
| 91 | + bytes32 implementationSlot = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc; |
| 92 | + bytes32 implementationValue = vm.load(address(proxy), implementationSlot); |
| 93 | + address currentImplementation = address(uint160(uint256(implementationValue))); |
| 94 | + |
| 95 | + assertEq(currentImplementation, address(swanImplementationV2)); |
| 96 | + |
| 97 | + assertEq(address(proxy.agentFactory()), address(agentFactory)); |
| 98 | + assertEq(address(proxy.artifactFactory()), address(artifactFactory)); |
| 99 | + |
| 100 | + vm.startPrank(owner); |
| 101 | + address newAgentFactory = address(new MockSwanAgentFactory()); |
| 102 | + address newArtifactFactory = address(new MockSwanArtifactFactory()); |
| 103 | + proxy.setFactories(newAgentFactory, newArtifactFactory); |
| 104 | + vm.stopPrank(); |
| 105 | + |
| 106 | + // Verify the new factories were set correctly |
| 107 | + assertEq(address(proxy.agentFactory()), newAgentFactory); |
| 108 | + assertEq(address(proxy.artifactFactory()), newArtifactFactory); |
| 109 | + } |
| 110 | + |
| 111 | + // Test that the upgrade fails if called by a non-owner |
| 112 | + function testUpgradeFailsWhenCalledByNonOwner() public { |
| 113 | + vm.startPrank(owner); |
| 114 | + Swan swanImplementationV2 = new Swan(); |
| 115 | + vm.stopPrank(); |
| 116 | + |
| 117 | + // Try to upgrade from a non-owner account |
| 118 | + vm.startPrank(user); |
| 119 | + vm.expectRevert(); |
| 120 | + proxy.upgradeToAndCall(address(swanImplementationV2), ""); |
| 121 | + vm.stopPrank(); |
| 122 | + } |
| 123 | +} |
0 commit comments