Skip to content

Commit

Permalink
succeed to generate signature of the exchange func
Browse files Browse the repository at this point in the history
  • Loading branch information
thurendous committed Sep 9, 2024
1 parent e665318 commit 5e5d53e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions script/DeployContracts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ contract DeployContracts is Script {
govToken.grantRole(govToken.MINTER_ROLE(), address(votingPowerExchange));
// give exchange the burner role of utilityToken
utilityToken.grantRole(utilityToken.BURNER_ROLE(), address(votingPowerExchange));
govToken.grantRole(govToken.VOTING_POWER_EXCHANGE_ROLE(), address(votingPowerExchange));
vm.stopPrank();

return DeploymentResult({
Expand Down
2 changes: 1 addition & 1 deletion src/VotingPowerExchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ contract VotingPowerExchange is AccessControl, EIP712 {
}

// burn utilityToken from the `sender`
utilityToken.burnByBurner(msg.sender, burningTokenAmount);
utilityToken.burnByBurner(sender, burningTokenAmount);

// update the burned amount of the `sender`
govToken.setBurnedAmountOfUtilToken(sender, currentBurnedAmount + burningTokenAmount);
Expand Down
10 changes: 8 additions & 2 deletions test/integration/VotingPowerExchange.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,15 @@ contract VotingPwoerExchangeTest is Test {
/////// Exchange tests ///////
function testExchangeCase1() public {
bytes32 nonce = bytes32(0);
bytes memory signature = helper.generateSignatureFromPrivateKey(dc.DEFAULT_ANVIL_KEY2(), 620 * 1e18, nonce, block.timestamp + 3600);
console.log(block.chainid);
console.log("Signer address:", vm.addr(dc.DEFAULT_ANVIL_KEY2()));
console.log("participant2 address:", participant2);

bytes memory signature = helper.generateSignatureFromPrivateKey(dc.DEFAULT_ANVIL_KEY2(), 1_000 * 1e18, nonce, 3600, address(votingPowerExchange));
address signer = vm.addr(dc.DEFAULT_ANVIL_KEY2());
vm.startPrank(exchanger);
votingPowerExchange.exchange(participant, 100_000 * 1e18, nonce, block.timestamp + 3600, signature);
votingPowerExchange.exchange(signer, 1_000 * 1e18, nonce, 3600, signature);
vm.stopPrank();

}
}
10 changes: 6 additions & 4 deletions test/integration/utils/VotingPowerExchangeTestHelper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import {DeployContracts, DeploymentResult} from "script/DeployContracts.s.sol";
import {Test, console} from "forge-std/Test.sol";

import {VotingPowerExchange} from "src/VotingPowerExchange.sol";
import {MessageHashUtils} from "lib/openzeppelin-contracts/contracts/utils/cryptography/MessageHashUtils.sol";

contract VotingPowerExchangeTestHelper is Test {
bytes32 private constant _EXCHANGE_TYPEHASH =
keccak256("Exchange(address sender,uint256 amount,bytes32 nonce,uint256 expiration)");

function generateSignatureFromPrivateKey(uint256 privateKey, uint256 amount, bytes32 nonce, uint256 expiration)
function generateSignatureFromPrivateKey(uint256 privateKey, uint256 amount, bytes32 nonce, uint256 expiration, address exchangeAddr)
public
view
returns (bytes memory)
{
address sender = address(uint160(privateKey));
address sender = vm.addr(privateKey);
console.log("sender", sender);
bytes32 structHash = keccak256(abi.encode(_EXCHANGE_TYPEHASH, sender, amount, nonce, expiration));

bytes32 domainSeparator = keccak256(
Expand All @@ -25,11 +27,11 @@ contract VotingPowerExchangeTestHelper is Test {
keccak256(bytes("VotingPowerExchange")),
keccak256(bytes("1")),
block.chainid,
address(this)
exchangeAddr
)
);

bytes32 hash = keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
bytes32 hash = MessageHashUtils.toTypedDataHash(domainSeparator, structHash);

(uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, hash);

Expand Down

0 comments on commit 5e5d53e

Please sign in to comment.