Skip to content

Commit

Permalink
Merge pull request #7 from ThomasJ0nes/listing-connection-attester-co…
Browse files Browse the repository at this point in the history
…ntract

add ListingConnectionAttester contract and createListingConnection func
  • Loading branch information
ThomasJ0nes authored Aug 9, 2024
2 parents e6b8113 + c9e0c06 commit 5132fbe
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 7 deletions.
44 changes: 44 additions & 0 deletions packages/hardhat/contracts/ListingConnectionAttester.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

import { IEAS, AttestationRequest, AttestationRequestData } from "@ethereum-attestation-service/eas-contracts/contracts/IEAS.sol";
import { NO_EXPIRATION_TIME, EMPTY_UID } from "@ethereum-attestation-service/eas-contracts/contracts/Common.sol";

contract ListingConnectionAttester {
IEAS public immutable _eas;
bytes32 public immutable _listingConnectionSchemaUID;

error InvalidEAS();

constructor(IEAS eas, bytes32 listingConnectionSchemaUID) {
if (address(eas) == address(0)) {
revert InvalidEAS();
}

_eas = eas;
_listingConnectionSchemaUID = listingConnectionSchemaUID;
}

function attestListingConnection(
uint256 listingConnectionId,
address seller,
address buyer
) external returns (bytes32 attestationUID) {
// return
// _eas.attest(
// AttestationRequest({
// schema: _listingConnectionSchemaUID,
// data: AttestationRequestData({
// recipient: seller,
// expirationTime: NO_EXPIRATION_TIME, // No expiration time
// revocable: true,
// refUID: EMPTY_UID, // No references UI
// data: abi.encode(listingConnectionId, seller, buyer),
// value: 0 // No value/ETH
// })
// })
// );
return
0x0d455486a3dadeacfba5f340fe5bf84d1f6678b2e2af53536acc8a4274626f82; // Return sample value for local testing
}
}
33 changes: 29 additions & 4 deletions packages/hardhat/contracts/Listings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity >=0.8.0 <0.9.0;

import "./ListingAttester.sol";
import "./ListingConnectionAttester.sol";

contract Listings {
struct Listing {
Expand All @@ -20,7 +21,10 @@ contract Listings {
mapping(uint256 => bool) public existingIds;
Listing[] public listings;
mapping(uint256 => uint256) public idToIndex;
ListingAttester public _listingAttester;
ListingAttester public immutable _listingAttester;

mapping(uint256 => mapping(address => bool)) public connectedBuyers;
ListingConnectionAttester public immutable _listingConnectionAttester;

event AddListing(address indexed seller, uint256 listingId);
event UpdateListing(
Expand All @@ -29,9 +33,11 @@ contract Listings {
string newName
);
event DeleteListing(address indexed seller, uint256 listingId);
event CreateListingConnection(address indexed buyer, uint256 listingId);

error Listings__NotExistedListingId(uint256 listingId);
error Listings__InvalidSeller(address seller);
error Listings__BuyerAlreadyConnected(address buyer, uint256 listingId);

modifier checkExistedListingId(uint256 id) {
if (!existingIds[id]) {
Expand All @@ -48,8 +54,11 @@ contract Listings {
_;
}

constructor(address listingAttester) {
constructor(address listingAttester, address listingConnectionAttester) {
_listingAttester = ListingAttester(listingAttester);
_listingConnectionAttester = ListingConnectionAttester(
listingConnectionAttester
);
}

function addListing(
Expand Down Expand Up @@ -78,10 +87,10 @@ contract Listings {
cid
);
listings.push(listing);
currentId++;

existingIds[currentId] = true;
idToIndex[listing.id] = listings.length - 1;
idToIndex[listing.id] = currentId;
currentId++;

emit AddListing(msg.sender, listing.id);
}
Expand Down Expand Up @@ -133,6 +142,22 @@ contract Listings {
emit DeleteListing(msg.sender, id);
}

function createListingConnection(
uint256 listingId
) public checkExistedListingId(listingId) returns (bytes32 attestationUID) {
if (connectedBuyers[listingId][msg.sender]) {
revert Listings__BuyerAlreadyConnected(msg.sender, listingId);
}

connectedBuyers[listingId][msg.sender] = true;
attestationUID = _listingConnectionAttester.attestListingConnection(
currentId,
listings[idToIndex[listingId]].seller,
msg.sender
);
emit CreateListingConnection(msg.sender, listingId);
}

function getAllListings()
public
view
Expand Down
21 changes: 21 additions & 0 deletions packages/hardhat/deploy/01_deploy_listing_connection_attester.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";

const deployListingConnectionAttester: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployer } = await hre.getNamedAccounts();
const { deploy } = hre.deployments;

await deploy("ListingConnectionAttester", {
from: deployer,
args: [
"0x4200000000000000000000000000000000000021",
"0xa844aad897e631c5200bc2a0f5c093eeb3e96baa4f8428d93dbcb76e775906a9", // Sample SchemaUID
],
log: true,
autoMine: true,
});
};

export default deployListingConnectionAttester;

deployListingConnectionAttester.tags = ["ListingConnectionAttester"];
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ const deployListings: DeployFunction = async function (hre: HardhatRuntimeEnviro
const listingAttester = await hre.ethers.getContract<Contract>("ListingAttester", deployer);
const listingAttesterAddress = await listingAttester.getAddress();

const listingConnectionAttester = await hre.ethers.getContract<Contract>("ListingConnectionAttester", deployer);
const listingConnectionAttesterAddress = await listingConnectionAttester.getAddress();

await deploy("Listings", {
from: deployer,
args: [listingAttesterAddress],
args: [listingAttesterAddress, listingConnectionAttesterAddress],
log: true,
autoMine: true,
});
Expand Down
182 changes: 180 additions & 2 deletions packages/nextjs/contracts/deployedContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { GenericContractsDeclaration } from "~~/utils/scaffold-eth/contract";
const deployedContracts = {
31337: {
ListingAttester: {
address: "0x5FbDB2315678afecb367f032d93F642f64180aa3",
address: "0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690",
abi: [
{
inputs: [
Expand Down Expand Up @@ -88,8 +88,90 @@ const deployedContracts = {
],
inheritedFunctions: {},
},
ListingConnectionAttester: {
address: "0x84eA74d481Ee0A5332c457a4d796187F6Ba67fEB",
abi: [
{
inputs: [
{
internalType: "contract IEAS",
name: "eas",
type: "address",
},
{
internalType: "bytes32",
name: "listingConnectionSchemaUID",
type: "bytes32",
},
],
stateMutability: "nonpayable",
type: "constructor",
},
{
inputs: [],
name: "InvalidEAS",
type: "error",
},
{
inputs: [],
name: "_eas",
outputs: [
{
internalType: "contract IEAS",
name: "",
type: "address",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "_listingConnectionSchemaUID",
outputs: [
{
internalType: "bytes32",
name: "",
type: "bytes32",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "uint256",
name: "listingConnectionId",
type: "uint256",
},
{
internalType: "address",
name: "seller",
type: "address",
},
{
internalType: "address",
name: "buyer",
type: "address",
},
],
name: "attestListingConnection",
outputs: [
{
internalType: "bytes32",
name: "attestationUID",
type: "bytes32",
},
],
stateMutability: "nonpayable",
type: "function",
},
],
inheritedFunctions: {},
},
Listings: {
address: "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512",
address: "0x9E545E3C0baAB3E08CdfD552C960A1050f373042",
abi: [
{
inputs: [
Expand All @@ -98,10 +180,31 @@ const deployedContracts = {
name: "listingAttester",
type: "address",
},
{
internalType: "address",
name: "listingConnectionAttester",
type: "address",
},
],
stateMutability: "nonpayable",
type: "constructor",
},
{
inputs: [
{
internalType: "address",
name: "buyer",
type: "address",
},
{
internalType: "uint256",
name: "listingId",
type: "uint256",
},
],
name: "Listings__BuyerAlreadyConnected",
type: "error",
},
{
inputs: [
{
Expand Down Expand Up @@ -143,6 +246,25 @@ const deployedContracts = {
name: "AddListing",
type: "event",
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "buyer",
type: "address",
},
{
indexed: false,
internalType: "uint256",
name: "listingId",
type: "uint256",
},
],
name: "CreateListingConnection",
type: "event",
},
{
anonymous: false,
inputs: [
Expand Down Expand Up @@ -200,6 +322,19 @@ const deployedContracts = {
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "_listingConnectionAttester",
outputs: [
{
internalType: "contract ListingConnectionAttester",
name: "",
type: "address",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
Expand Down Expand Up @@ -238,6 +373,49 @@ const deployedContracts = {
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
{
internalType: "address",
name: "",
type: "address",
},
],
name: "connectedBuyers",
outputs: [
{
internalType: "bool",
name: "",
type: "bool",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "uint256",
name: "listingId",
type: "uint256",
},
],
name: "createListingConnection",
outputs: [
{
internalType: "bytes32",
name: "attestationUID",
type: "bytes32",
},
],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{
Expand Down

0 comments on commit 5132fbe

Please sign in to comment.