Skip to content

Commit e7c9fb0

Browse files
committed
Add Read contracts
1 parent 54e0637 commit e7c9fb0

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
import {Script} from "forge-std/Script.sol";
5+
import {console} from "forge-std/console.sol";
6+
import {Fees} from "socket-protocol/contracts/protocol/utils/common/Structs.sol";
7+
import {ETH_ADDRESS} from "socket-protocol/contracts/protocol/utils/common/Constants.sol";
8+
9+
import {ReadAppGateway} from "../../src/read/ReadAppGateway.sol";
10+
11+
contract DeployEVMxContracts is Script {
12+
function run() external {
13+
address addressResolver = vm.envAddress("ADDRESS_RESOLVER");
14+
string memory rpc = vm.envString("EVMX_RPC");
15+
vm.createSelectFork(rpc);
16+
17+
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
18+
vm.startBroadcast(deployerPrivateKey);
19+
20+
Fees memory fees = Fees({feePoolChain: 421614, feePoolToken: ETH_ADDRESS, amount: 0.001 ether});
21+
22+
ReadAppGateway gateway = new ReadAppGateway(addressResolver, fees);
23+
24+
console.log("Contracts deployed:");
25+
console.log("AppGateway:", address(gateway));
26+
}
27+
}

script/read/RunEVMxRead.s.sol

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
import {console} from "forge-std/console.sol";
5+
import {SetupScript} from "../SetupScript.sol";
6+
import {ReadAppGateway} from "../../src/read/ReadAppGateway.sol";
7+
8+
contract RunEVMxRead is SetupScript {
9+
ReadAppGateway readAppGateway;
10+
address opSepForwarder;
11+
address arbSepForwarder;
12+
13+
function appGateway() internal view override returns (address) {
14+
return address(readAppGateway);
15+
}
16+
17+
function getForwarderAddresses() internal {
18+
vm.createSelectFork(rpcEVMx);
19+
opSepForwarder = readAppGateway.forwarderAddresses(readAppGateway.multichain(), opSepChainId);
20+
arbSepForwarder = readAppGateway.forwarderAddresses(readAppGateway.multichain(), arbSepChainId);
21+
22+
console.log("Optimism Sepolia Forwarder:", opSepForwarder);
23+
console.log("Arbitrum Sepolia Forwarder:", arbSepForwarder);
24+
}
25+
26+
function runAllTriggers() internal {
27+
vm.createSelectFork(rpcEVMx);
28+
vm.startBroadcast(privateKey);
29+
30+
console.log("Running all trigger functions...");
31+
32+
// 1. Trigger Parallel Read
33+
console.log("triggerParallelRead...");
34+
readAppGateway.triggerParallelRead(opSepForwarder);
35+
36+
// 2. Trigger Alternating Read between chains
37+
console.log("triggerAltRead...");
38+
readAppGateway.triggerAltRead(opSepForwarder, arbSepForwarder);
39+
40+
vm.stopBroadcast();
41+
console.log("All triggers executed successfully");
42+
}
43+
44+
function checkResults() internal {
45+
vm.createSelectFork(rpcEVMx);
46+
47+
console.log("\n----- RESULTS -----");
48+
49+
// Check values array
50+
console.log("Values array:");
51+
for (uint256 i = 0; i < 10; i++) {
52+
try readAppGateway.values(i) returns (uint256 value) {
53+
console.log("values[%s]: %s", i, value);
54+
} catch {
55+
console.log("values[%s]: not set", i);
56+
break;
57+
}
58+
}
59+
}
60+
61+
function executeScriptSpecificLogic() internal override {
62+
// Initialize contract references
63+
readAppGateway = ReadAppGateway(appGatewayAddress);
64+
65+
// Deploy to both test chains
66+
uint32[] memory chainIds = new uint32[](2);
67+
chainIds[0] = opSepChainId;
68+
chainIds[1] = arbSepChainId;
69+
deployOnchainContracts(chainIds);
70+
71+
getForwarderAddresses();
72+
runAllTriggers();
73+
checkResults();
74+
}
75+
76+
function run() external {
77+
_run(arbSepChainId);
78+
}
79+
}

src/read/IReadMultichain.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: GPL-3.0
2+
pragma solidity >=0.7.0 <0.9.0;
3+
4+
interface IReadMultichain {
5+
function counter() external;
6+
7+
function values() external;
8+
9+
function increase() external;
10+
11+
function setValues(uint256[] memory values_) external;
12+
13+
function getValue(uint256 index_) external;
14+
15+
function connectSocket(address appGateway_, address socket_, address switchboard_) external;
16+
}

src/read/ReadAppGateway.sol

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// SPDX-License-Identifier: GPL-3.0
2+
pragma solidity >=0.7.0 <0.9.0;
3+
4+
import "socket-protocol/contracts/base/AppGatewayBase.sol";
5+
import "socket-protocol/contracts/interfaces/IForwarder.sol";
6+
import "socket-protocol/contracts/interfaces/IPromise.sol";
7+
import "./IReadMultichain.sol";
8+
import "./ReadMultichain.sol";
9+
10+
contract ReadAppGateway is AppGatewayBase {
11+
bytes32 public multichain = _createContractId("ReadMultichain");
12+
uint256[] public values;
13+
14+
event ValueRead(address instance, uint256 index, uint256 value);
15+
16+
constructor(address addressResolver_, Fees memory fees_) AppGatewayBase(addressResolver_) {
17+
creationCodeWithArgs[multichain] = abi.encodePacked(type(ReadMultichain).creationCode);
18+
_setOverrides(fees_);
19+
}
20+
21+
function deployContracts(uint32 chainSlug_) external async {
22+
_deploy(multichain, chainSlug_, IsPlug.YES);
23+
}
24+
25+
function initialize(uint32) public pure override {
26+
return;
27+
}
28+
29+
function triggerParallelRead(address instance_) public async {
30+
_setOverrides(Read.ON, Parallel.ON);
31+
for (uint256 i = 0; i < 10; i++) {
32+
IReadMultichain(instance_).getValue(i);
33+
IPromise(instance_).then(this.handleValue.selector, abi.encode(i));
34+
}
35+
}
36+
37+
function triggerAltRead(address instance1_, address instance2_) public async {
38+
_setOverrides(Read.ON, Parallel.ON);
39+
for (uint256 i = 0; i < 10; i++) {
40+
if (i % 2 == 0) {
41+
IReadMultichain(instance1_).getValue(i);
42+
IPromise(instance1_).then(this.handleValue.selector, abi.encode(i));
43+
} else {
44+
IReadMultichain(instance2_).getValue(i);
45+
IPromise(instance2_).then(this.handleValue.selector, abi.encode(i));
46+
}
47+
}
48+
}
49+
50+
function handleValue(bytes memory data, bytes memory returnData) public onlyPromises {
51+
uint256 index_ = abi.decode(data, (uint256));
52+
uint256 value_ = abi.decode(returnData, (uint256));
53+
values[index_] = value_;
54+
emit ValueRead(msg.sender, index_, value_);
55+
}
56+
57+
function setFees(Fees memory fees_) public {
58+
fees = fees_;
59+
}
60+
61+
function withdrawFeeTokens(uint32 chainSlug_, address token_, uint256 amount_, address receiver_) external {
62+
_withdrawFeeTokens(chainSlug_, token_, amount_, receiver_);
63+
}
64+
}

src/read/ReadMultichain.sol

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: GPL-3.0
2+
pragma solidity >=0.7.0 <0.9.0;
3+
4+
import "socket-protocol/contracts/base/PlugBase.sol";
5+
6+
contract ReadMultichain is PlugBase {
7+
uint256[] public values;
8+
9+
event ValuesInitialized(uint256[] values);
10+
11+
constructor() {
12+
values = new uint256[](10);
13+
uint256 seed = (block.number % 10) + 1;
14+
for (uint256 i = 0; i < 10; i++) {
15+
values[i] = seed + i;
16+
}
17+
emit ValuesInitialized(values);
18+
}
19+
20+
function getValue(uint256 index_) external view returns (uint256) {
21+
return values[index_];
22+
}
23+
24+
function connectSocket(address appGateway_, address socket_, address switchboard_) external {
25+
_connectSocket(appGateway_, socket_, switchboard_);
26+
}
27+
}

0 commit comments

Comments
 (0)