Skip to content

Commit

Permalink
test: add lsp8 delegatee tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skimaharvey committed Dec 12, 2024
1 parent be4372b commit 4e39856
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/lsp8-contracts/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export const LSP8_TYPE_IDS = {
// keccak256('LSP8Tokens_OperatorNotification')
LSP8Tokens_OperatorNotification:
'0x8a1c15a8799f71b547e08e2bcb2e85257e81b0a07eee2ce6712549eef1f00970',

// keccak256('LSP8Tokens_VotesDelegateeNotification')
LSP8Tokens_VotesDelegateeNotification:
'0x4aab908d8f4bc502ab79ddf13d56eb08e6ef25ebab36358bc42e5527adb08b8b',

// keccak256('LSP8Tokens_VotesDelegatorNotification')
LSP8Tokens_VotesDelegatorNotification:
'0x54a91786b7c126f5ca201cf70323711e2b609e1259aab5cea150cfde4b9a748c',
} as const;

/**
Expand Down
29 changes: 29 additions & 0 deletions packages/lsp8-contracts/contracts/Mocks/UniversalReceiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import {
ILSP1UniversalReceiver
} from "@lukso/lsp1-contracts/contracts/ILSP1UniversalReceiver.sol";
import {
_INTERFACEID_LSP1
} from "@lukso/lsp1-contracts/contracts/LSP1Constants.sol";

contract MockUniversalReceiver is ILSP1UniversalReceiver {
event UniversalReceiverCalled(
address indexed from,
bytes32 indexed typeId,
bytes receivedData
);

function universalReceiver(
bytes32 typeId,
bytes calldata receivedData
) external payable override returns (bytes memory) {
emit UniversalReceiverCalled(msg.sender, typeId, receivedData);
return "";
}

function supportsInterface(bytes4 interfaceId) public view returns (bool) {
return interfaceId == _INTERFACEID_LSP1;
}
}
36 changes: 36 additions & 0 deletions packages/lsp8-contracts/tests/LSP8Votes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ethers } from 'hardhat';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { MyVotingNFT, MyVotingNFT__factory, MyGovernor, MyGovernor__factory } from '../types';
import { time, mine } from '@nomicfoundation/hardhat-network-helpers';
import { LSP8_TYPE_IDS } from '../constants';

describe('Comprehensive Governor and NFT Tests', () => {
let nft: MyVotingNFT;
Expand Down Expand Up @@ -173,4 +174,39 @@ describe('Comprehensive Governor and NFT Tests', () => {
expect(await nft.getPastTotalSupply(blockNumber2 - 1)).to.equal(initialSupply + BigInt(1));
});
});
describe('Delegation Notifications', () => {
let mockUniversalReceiver;

beforeEach(async () => {
const MockUniversalReceiver = await ethers.getContractFactory('MockUniversalReceiver');
mockUniversalReceiver = await MockUniversalReceiver.deploy();
});

it('should notify delegatee with correct data format', async () => {
const expectedDelegateeData = ethers.AbiCoder.defaultAbiCoder().encode(
['address', 'address', 'uint256'],
[voter1.address, voter1.address, 1],
);

const expectedDelegatorData = ethers.AbiCoder.defaultAbiCoder().encode(
['address', 'address', 'uint256'],
[voter1.address, voter1.address, 1],
);

await expect(nft.connect(voter1).delegate(await mockUniversalReceiver.getAddress()))
.to.emit(mockUniversalReceiver, 'UniversalReceiverCalled')
.withArgs(
await nft.getAddress(),
LSP8_TYPE_IDS.LSP8Tokens_VotesDelegateeNotification,
expectedDelegateeData,
);
});

it('should not notify when delegating to address(0)', async () => {
await expect(nft.connect(voter1).delegate(ethers.ZeroAddress)).to.not.emit(
mockUniversalReceiver,
'UniversalReceiverCalled',
);
});
});
});

0 comments on commit 4e39856

Please sign in to comment.