Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevouchAttester with multiAttest Support & Fee Recollection #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 83 additions & 51 deletions src/DevouchAttester.sol
Original file line number Diff line number Diff line change
@@ -1,79 +1,111 @@


// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
pragma solidity ^0.8.19;

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

contract DevouchAttester
{
event Log(string func, uint256 gas);
contract DeVouchAttester {
address public owner;
uint256 public fee = 0.00003 ether;
address multisig = 0x7D52A0Ab02A6A49a1B4b7c4e79C80F977971f700;
bytes32 schema = 0x421da38e6ff5eb5d0402a4e9be70e70f961bce228e8a20d1eca19634556247fd;

/**
* @dev Set contract deployer as owner
*/
constructor() {
owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor
address public multisig;
bytes32 public schema = 0x421da38e6ff5eb5d0402a4e9be70e70f961bce228e8a20d1eca19634556247fd;

IEAS public eas;
bool public paused;

error Unauthorized();
error InvalidFee();
error InvalidSchema();
error ContractPaused();
error WithdrawFailed();
error InvalidMultisig();

/**
* @dev Set contract deployer as owner and initial multisig
*/
constructor(address _easAddress) {
owner = msg.sender;
multisig = 0x7D52A0Ab02A6A49a1B4b7c4e79C80F977971f700;
eas = IEAS(_easAddress);
}

modifier ownerOnly() {
require(msg.sender == owner, "Only owner can access this function");
_;
if (msg.sender != owner) revert Unauthorized();
_;
}

modifier whenNotPaused() {
if (paused) revert ContractPaused();
_;
}

function updateOwner(address newOwner) public ownerOnly {
owner = newOwner;
emit Log("updateOwner", gasleft());
}

function updateMultisig(address newMultisig) public ownerOnly {
if (newMultisig == address(0)) revert InvalidMultisig();
multisig = newMultisig;
}

function updateFee(uint256 newFee) public ownerOnly {
fee = newFee;
emit Log("updateFee", gasleft());
}

function updateSchema(bytes32 newSchema) public ownerOnly{
function updateSchema(bytes32 newSchema) public ownerOnly {
if (newSchema == 0) revert InvalidSchema();
schema = newSchema;
emit Log("updateSchema", gasleft());
}

function attest(address recipient, bytes32 refUID, bytes calldata data) public payable
{
require(address(msg.sender).balance >= fee, "Insufficient balance to pay fee");
require(msg.value == fee, "Must pay the fee amount");

IEAS(0xC2679fBD37d54388Ce493F1DB75320D236e1815e).attest(
AttestationRequest({
schema: schema,
data: AttestationRequestData({
recipient: recipient,
expirationTime: NO_EXPIRATION_TIME,
revocable: true,
refUID: refUID,
data: data,
value:0
})
})
);

emit Log("attested", gasleft());
function updateEAS(address newEASAddress) public ownerOnly {
eas = IEAS(newEASAddress);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will EAS Address for a chain change frequently ? If no, why do we need it as a variable?

Copy link
Member Author

@mateodaza mateodaza Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my thought was about uncertainty in the future, it's unlikely but it could happen. We can remove it if you want anyway - I don't think it matters that much on gas costs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make an unchanging value as a variable we also add the getter / setter methods. So its better to keep it as a constant for now.

}

receive() external payable {}
function togglePause() public ownerOnly {
paused = !paused;
}

function attest(
address recipient,
bytes32[] calldata refUIDArray,
bytes calldata data
) public payable whenNotPaused {
if (msg.value != fee) revert InvalidFee();

uint256 len = refUIDArray.length;
AttestationRequestData[] memory requestDataArray = new AttestationRequestData[](len);

for (uint256 i = 0; i < len; ++i) {
requestDataArray[i] = AttestationRequestData({
recipient: recipient,
expirationTime: NO_EXPIRATION_TIME,
revocable: true,
refUID: refUIDArray[i],
data: data,
value: 0
});
}

fallback() external payable {
emit Log("fallback", gasleft());
MultiAttestationRequest[] memory multiRequests = new MultiAttestationRequest[](1);
multiRequests[0] = MultiAttestationRequest({
schema: schema,
data: requestDataArray
});

eas.multiAttest(multiRequests);
}

// Function to withdraw Ether from the contract (for testing purposes)
receive() external payable {}

fallback() external payable {}

function withdraw() public {
// Transfer the Ether to the multisig address
(bool success, ) = multisig.call{value: address(this).balance}("");
require(success, "Failed to send Ether");
emit Log("withdraw", gasleft());
uint256 amount = address(this).balance;
(bool success, ) = multisig.call{value: amount}("");
if (!success) revert WithdrawFailed();
}
}
}
118 changes: 118 additions & 0 deletions test/DeVouchAttester.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import {Test, console} from "forge-std/Test.sol";
import {Vm} from "forge-std/Vm.sol";
import {SchemaRegistry, ISchemaRegistry} from "eas-contracts/contracts/SchemaRegistry.sol";
import {EAS, NO_EXPIRATION_TIME, EMPTY_UID} from "eas-contracts/contracts/EAS.sol";
import {IEAS, AttestationRequestData, AttestationRequest, MultiAttestationRequest} from "eas-contracts/contracts/IEAS.sol";
import {ISchemaResolver} from "eas-contracts/contracts/resolver/ISchemaResolver.sol";
import {DeVouchAttester} from "../src/DeVouchAttester.sol";

contract DevouchAttesterTest is Test {
SchemaRegistry schemaRegistry;
string schema = "string projectSource, string projectId, bool vouch, string comment";
EAS easContract;
IEAS easInterface;
DeVouchAttester devouchAttester;
bytes32 schemaUID;

address owner;
address user = address(2);
address multisig;

event Attested(bytes32 indexed uid, bytes32 indexed schema, address indexed recipient);

function setUp() public {
owner = address(this);

schemaRegistry = new SchemaRegistry();
easContract = new EAS(ISchemaRegistry(address(schemaRegistry)));

devouchAttester = new DeVouchAttester(address(easContract));

schemaUID = schemaRegistry.register(schema, ISchemaResolver(address(0)), true);

devouchAttester.updateSchema(schemaUID);

multisig = devouchAttester.multisig();

vm.label(address(easContract), "EAS");
vm.label(address(schemaRegistry), "SchemaRegistry");
vm.label(address(devouchAttester), "DeVouchAttester");
vm.label(multisig, "Multisig");
}

function testMultiAttest() public {
address recipient = address(0);
bytes32[] memory refUIDArray = new bytes32[](2);
refUIDArray[0] = EMPTY_UID;
refUIDArray[1] = EMPTY_UID;
bytes memory data = abi.encode("giveth", "55", true, "this is awesome");

uint256 fee = devouchAttester.fee();

vm.deal(user, 1 ether);

uint256 userInitialBalance = user.balance;
uint256 contractInitialBalance = address(devouchAttester).balance;
uint256 multisigInitialBalance = multisig.balance;

console.log("\n--------------------");
console.log("Starting Multi-Attest Test");
console.log("--------------------");
console.log("Attestation fee:", fee);
console.log("User initial balance:", userInitialBalance);
console.log("Contract initial balance:", contractInitialBalance);
console.log("Multisig initial balance:", multisigInitialBalance);

vm.prank(user);
vm.recordLogs();
devouchAttester.attest{value: fee}(recipient, refUIDArray, data);

Vm.Log[] memory entries = vm.getRecordedLogs();

uint256 userFinalBalance = user.balance;
uint256 contractFinalBalance = address(devouchAttester).balance;
uint256 multisigFinalBalance = multisig.balance;

console.log("\n--------------------");
console.log("Attestation Complete");
console.log("--------------------");
console.log("Number of emitted events:", entries.length);
console.log("User final balance:", userFinalBalance);
console.log("Contract final balance:", contractFinalBalance);
console.log("Multisig final balance:", multisigFinalBalance);

console.log("\n--------------------");
console.log("Assertions");
console.log("--------------------");

assertEq(entries.length, 2, "Should emit two events for multi-attest");
console.log(" >> Correct number of events emitted");

assertEq(userFinalBalance, userInitialBalance - fee, "User balance should decrease by fee amount");
console.log(" >> User balance decreased correctly");

if (contractFinalBalance > contractInitialBalance) {
assertEq(contractFinalBalance, contractInitialBalance + fee, "Contract balance should increase by fee amount");
console.log(" >> Fee is held in the contract");
} else if (multisigFinalBalance > multisigInitialBalance) {
assertEq(multisigFinalBalance, multisigInitialBalance + fee, "Multisig balance should increase by fee amount");
console.log(" >> Fee was sent to the multisig");
} else {
fail();
console.log(" >> Fee was not properly accounted for");
}

assertEq(devouchAttester.schema(), schemaUID, "Schema in DevouchAttester should match the set schema");
console.log(" >> Schema in DevouchAttester is correct");

assertEq(address(devouchAttester.eas()), address(easContract), "EAS address in DevouchAttester should be correct");
console.log(" >> EAS address in DevouchAttester is correct");

console.log("\n--------------------");
console.log("Test Complete");
console.log("--------------------");
}
}