Skip to content

Commit

Permalink
Merge pull request #7 from SocketDotTech/parallel-payloads
Browse files Browse the repository at this point in the history
Delivery Utils
  • Loading branch information
arthcp authored Jan 3, 2025
2 parents 10c1369 + 5a75aed commit 97ab33d
Show file tree
Hide file tree
Showing 25 changed files with 416 additions and 359 deletions.
46 changes: 6 additions & 40 deletions contracts/AddressResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ contract AddressResolver is Ownable, IAddressResolver {
/// @param chainSlug_ The chain slug
/// @return The address of the deployed Forwarder contract
function getOrDeployForwarderContract(
address appDeployer_,
address chainContractAddress_,
uint32 chainSlug_
) public returns (address) {
Expand Down Expand Up @@ -111,51 +112,16 @@ contract AddressResolver is Ownable, IAddressResolver {
revert(0, 0)
}
}

_setConfig(appDeployer_, newForwarder);
emit ForwarderDeployed(newForwarder, salt);
return newForwarder;
}

/// @notice Deploys a Forwarder contract
/// @param appDeployer_ The address of the app deployer
/// @param chainContractAddress_ The address of the chain contract
/// @param chainSlug_ The chain slug
/// @return The address of the deployed Forwarder contract
function deployForwarderContract(
address appDeployer_,
address chainContractAddress_,
uint32 chainSlug_
) public returns (address) {
bytes memory constructorArgs = abi.encode(
chainSlug_,
chainContractAddress_,
address(this)
);

bytes memory combinedBytecode = abi.encodePacked(
forwarderBytecode,
constructorArgs
);

bytes32 salt = keccak256(constructorArgs);
address newForwarder;

assembly {
newForwarder := create2(
callvalue(),
add(combinedBytecode, 0x20),
mload(combinedBytecode),
salt
)
if iszero(extcodesize(newForwarder)) {
revert(0, 0)
}
}
emit ForwarderDeployed(newForwarder, salt);

function _setConfig(address appDeployer_, address newForwarder_) internal {
address gateway = contractsToGateways[appDeployer_];
gatewaysToContracts[gateway] = newForwarder;
contractsToGateways[newForwarder] = gateway;
return newForwarder;
gatewaysToContracts[gateway] = newForwarder_;
contractsToGateways[newForwarder_] = gateway;
}

/// @notice Deploys an AsyncPromise contract
Expand Down
10 changes: 5 additions & 5 deletions contracts/AsyncPromise.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity 0.8.13;

import {AddressResolverUtil} from "./utils/AddressResolverUtil.sol";

import {IPromise} from "./interfaces/IPromise.sol";
/// @notice The state of the async promise
enum AsyncPromiseState {
WAITING_FOR_SET_CALLBACK_SELECTOR,
Expand All @@ -13,7 +13,7 @@ enum AsyncPromiseState {
/// @title AsyncPromise
/// @notice this contract stores the callback address and data to be executed once the previous call is executed
/// This promise expires once the callback is executed
contract AsyncPromise is AddressResolverUtil {
contract AsyncPromise is AddressResolverUtil, IPromise {
/// @notice The callback data to be used when the promise is resolved.
bytes public callbackData;

Expand All @@ -28,7 +28,7 @@ contract AsyncPromise is AddressResolverUtil {
address public immutable forwarder;

/// @notice Indicates whether the promise has been resolved.
bool public resolved = false;
bool public override resolved = false;

/// @notice Error thrown when attempting to resolve an already resolved promise.
error PromiseAlreadyResolved();
Expand All @@ -55,7 +55,7 @@ contract AsyncPromise is AddressResolverUtil {
/// @dev Only callable by the watcher precompile.
function markResolved(
bytes memory returnData
) external onlyWatcherPrecompile {
) external override onlyWatcherPrecompile {
if (resolved) revert PromiseAlreadyResolved();
resolved = true;
state = AsyncPromiseState.RESOLVED;
Expand All @@ -79,7 +79,7 @@ contract AsyncPromise is AddressResolverUtil {
function then(
bytes4 selector,
bytes memory data
) external returns (address promise_) {
) external override returns (address promise_) {
require(
msg.sender == forwarder || msg.sender == localInvoker,
"Only the forwarder or local invoker can set this promise's callback"
Expand Down
2 changes: 1 addition & 1 deletion contracts/Forwarder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ contract Forwarder is IForwarder {
IAuctionHouse(auctionHouse).queue(
chainSlug,
onChainAddress,
bytes32(uint256(uint160(latestAsyncPromise))),
latestAsyncPromise,
isReadCall ? CallType.READ : CallType.WRITE,
msg.data
);
Expand Down
8 changes: 1 addition & 7 deletions contracts/apps/counter/app-gateway/CounterComposer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
pragma solidity >=0.7.0 <0.9.0;

import "../../../base/AppGatewayBase.sol";
import "../Counter.sol";
import "../../../utils/Ownable.sol";
import "../Counter.sol";

contract CounterComposer is AppGatewayBase, Ownable {
constructor(
Expand All @@ -15,12 +15,6 @@ contract CounterComposer is AppGatewayBase, Ownable {
_setFeesData(feesData_);
}

// function incrementCounters(address[] calldata _instance, uint256 _counter) public queueAndExecute {
// for (uint256 i = 0; i < _instance.length; i++) {
// Counter(_instance[i]).setCounter(_counter);
// }
// }

function incrementCounter(
address _instance,
uint256 _counter
Expand Down
5 changes: 5 additions & 0 deletions contracts/apps/payload-delivery/ContractFactoryPlug.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.13;
import {PlugBase} from "../../base/PlugBase.sol";
import {Ownable} from "../../utils/Ownable.sol";

/// @title ContractFactory
/// @notice Abstract contract for deploying contracts
contract ContractFactoryPlug is PlugBase, Ownable {
Expand All @@ -17,6 +18,10 @@ contract ContractFactoryPlug is PlugBase, Ownable {
bytes memory creationCode,
bytes32 salt
) public returns (address) {
if (msg.sender != address(socket__)) {
revert("Only socket can deploy contracts");
}

address addr;
assembly {
addr := create2(
Expand Down

This file was deleted.

28 changes: 14 additions & 14 deletions contracts/apps/payload-delivery/app-gateway/AuctionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,43 @@ import {Ownable} from "../../../utils/Ownable.sol";
import {SignatureVerifier} from "../../../socket/utils/SignatureVerifier.sol";
import {AddressResolverUtil} from "../../../utils/AddressResolverUtil.sol";
import {Bid, FeesData} from "../../../common/Structs.sol";
import {IAuctionContract} from "../../../interfaces/IAuctionContract.sol";
import {IAuctionHouse} from "../../../interfaces/IAuctionHouse.sol";

/// @title AuctionHouse
/// @notice Contract for managing auctions and placing bids
contract AuctionManager is AddressResolverUtil, Ownable(msg.sender) {
SignatureVerifier public immutable signatureVerifier__;
uint32 public immutable vmChainSlug;
mapping(bytes32 => Bid) public winningBids;
// asyncId => auction status
mapping(bytes32 => bool) public auctionClosed;
mapping(bytes32 => bool) public auctionStarted;

uint256 public constant auctionEndDelaySeconds = 0;

/// @notice Constructor for AuctionHouse
/// @param addressResolver_ The address of the address resolver
/// @param signatureVerifier_ The address of the signature verifier
constructor(
uint32 vmChainSlug_,
address addressResolver_,
SignatureVerifier signatureVerifier_
) AddressResolverUtil(addressResolver_) {
vmChainSlug = vmChainSlug_;
signatureVerifier__ = signatureVerifier_;
}

event AuctionStarted(bytes32 asyncId_);
event AuctionEnded(bytes32 asyncId_, Bid winningBid);
event BidPlaced(bytes32 asyncId_, Bid bid);

function startAuction(bytes32 asyncId_) external {
function startAuction(bytes32 asyncId_) external onlyPayloadDelivery {
require(!auctionClosed[asyncId_], "Auction closed");
require(!auctionStarted[asyncId_], "Auction already started");

auctionStarted[asyncId_] = true;
emit AuctionStarted(asyncId_);
uint256 auctionEndDelaySeconds = IAuctionContract(address(this))
.auctionEndDelaySeconds();

watcherPrecompile().setTimeout(
abi.encodeWithSelector(this.endAuction.selector, asyncId_),
auctionEndDelaySeconds
Expand All @@ -52,13 +55,16 @@ contract AuctionManager is AddressResolverUtil, Ownable(msg.sender) {
function bid(
bytes32 asyncId_,
uint256 fee,
FeesData memory feesData,
bytes memory transmitterSignature,
bytes memory extraData
) external {
require(!auctionClosed[asyncId_], "Auction closed");

address transmitter = signatureVerifier__.recoverSigner(
keccak256(abi.encode(address(this), asyncId_, fee)),
keccak256(
abi.encode(address(this), vmChainSlug, asyncId_, fee, extraData)
),
transmitterSignature
);

Expand All @@ -67,16 +73,10 @@ contract AuctionManager is AddressResolverUtil, Ownable(msg.sender) {
transmitter: transmitter,
extraData: extraData
});
(address auctionContract, FeesData memory feesData) = AuctionHouse()
.getAuctionContractAndFeesData(asyncId_);

require(fee <= feesData.maxFees, "Bid exceeds max fees");
require(
IAuctionContract(auctionContract).isNewBidBetter(
winningBids[asyncId_],
newBid
),
"Bid is not better"
);
if (fee < winningBids[asyncId_].fee) return;

winningBids[asyncId_] = newBid;
emit BidPlaced(asyncId_, newBid);
}
Expand Down
Loading

0 comments on commit 97ab33d

Please sign in to comment.