Skip to content

Commit

Permalink
test: add some tests for this behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
b00ste committed Aug 14, 2023
1 parent c55e68a commit 7b06a09
Show file tree
Hide file tree
Showing 2 changed files with 269 additions and 38 deletions.
196 changes: 165 additions & 31 deletions tests/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP.behaviour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const shouldBehaveLikeLSP1Delegate = (

describe('When testing EOA call to URD through the UR function', () => {
describe('when calling with token/vault typeId', () => {
it('should revert with custom error', async () => {
it('should return error message', async () => {
const URD_TypeIds = [
LSP1_TYPE_IDS.LSP7Tokens_RecipientNotification,
LSP1_TYPE_IDS.LSP7Tokens_SenderNotification,
Expand All @@ -122,11 +122,20 @@ export const shouldBehaveLikeLSP1Delegate = (
.connect(context.accounts.any)
.universalReceiver(URD_TypeIds[i], '0x'),
)
.to.be.revertedWithCustomError(
context.lsp1universalReceiverDelegateUP,
'CannotRegisterEOAsAsAssets',
)
.withArgs(context.accounts.any.address);
.to.emit(context.universalProfile1, 'UniversalReceiver')
.withArgs(
context.accounts.any.address,
0,
URD_TypeIds[i],
'0x',
abiCoder.encode(
['bytes', 'bytes'],
[
ethers.utils.solidityPack(['string'], ['LSP1: Cannot register EOAs as assets']),
'0x',
],
),
);
}
});
});
Expand Down Expand Up @@ -1040,19 +1049,26 @@ export const shouldBehaveLikeLSP1Delegate = (
});

it('should revert when trying to transfer some tokens to UP but UP cannot register any more tokens', async () => {
const lsp1DelegateAddress = await context.universalProfile1['getData(bytes32)'](
ERC725YDataKeys.LSP1['LSP1UniversalReceiverDelegate'],
);

const lsp1DelegateInstance = await new LSP1UniversalReceiverDelegateUP__factory(
context.accounts.random,
).attach(lsp1DelegateAddress);

// try to transfer (= mint) some tokens to the UP
// this should revert because the UP cannot register any more tokens
await expect(token.mint(context.universalProfile1.address, 10_000, false, '0x'))
.to.be.revertedWithCustomError(lsp1DelegateInstance, 'MaxLSP5ReceivedAssetsCountReached')
.withArgs(token.address);
.to.emit(context.universalProfile1, 'UniversalReceiver')
.withArgs(
token.address,
0,
LSP1_TYPE_IDS.LSP7Tokens_RecipientNotification,
ethers.utils.solidityPack(
['address', 'address', 'uint256', 'bytes'],
[ethers.constants.AddressZero, context.universalProfile1.address, 10_000, '0x'],
),
abiCoder.encode(
['bytes', 'bytes'],
[
ethers.utils.solidityPack(['string'], ['LSP5: Max received assets count reached']),
'0x',
],
),
);
});
});

Expand Down Expand Up @@ -1335,7 +1351,7 @@ export const shouldBehaveLikeLSP1Delegate = (

const lsp1ReturnedData = ethers.utils.defaultAbiCoder.encode(
['string', 'bytes'],
['LSP1: asset data corrupted', '0x'],
['LSP5: Out of bounds', '0x'],
);

await expect(tokenTransferTx)
Expand Down Expand Up @@ -1576,7 +1592,7 @@ export const shouldBehaveLikeLSP1Delegate = (
it('it should emit UniversalReceiver event', async () => {
const lsp1ReturnedData = ethers.utils.defaultAbiCoder.encode(
['string', 'bytes'],
['LSP1: asset data corrupted', '0x'],
['LSP10: Out of bounds', '0x'],
);

await expect(acceptOwnershipTx)
Expand Down Expand Up @@ -2462,17 +2478,23 @@ export const shouldBehaveLikeLSP1Delegate = (

// deploy a Vault setting the UP as owner
// this should revert because the UP has already the max number of vaults allowed
const lsp1DelegateAddress = await context.universalProfile1['getData(bytes32)'](
ERC725YDataKeys.LSP1['LSP1UniversalReceiverDelegate'],
);

const lsp1DelegateInstance = await new LSP1UniversalReceiverDelegateUP__factory(
context.accounts.random,
).attach(lsp1DelegateAddress);
const tx = await new LSP9Vault__factory(context.accounts.random).deploy(
context.universalProfile1.address,
);

await expect(
new LSP9Vault__factory(context.accounts.random).deploy(context.universalProfile1.address),
).to.be.revertedWithCustomError(lsp1DelegateInstance, 'MaxLSP10VaultsCountReached');
await expect(tx.deployTransaction)
.to.emit(context.universalProfile1, 'UniversalReceiver')
.withArgs(
tx.address,
0,
LSP1_TYPE_IDS.LSP9OwnershipTransferred_RecipientNotification,
'0x',
ethers.utils.defaultAbiCoder.encode(
['string', 'bytes'],
['LSP10: Max vaults count reached', '0x'],
),
);
});
});
});
Expand Down Expand Up @@ -2607,6 +2629,7 @@ export const shouldBehaveLikeLSP1Delegate = (
describe("when having a Vault set in LSP10 before it's ownership was transfered", () => {
let vault: LSP9Vault;
let vaultOwner: SignerWithAddress;
const bytes16Value1 = '0x' + '00'.repeat(15) + '01';

before(async () => {
vaultOwner = context.accounts.any;
Expand All @@ -2620,9 +2643,8 @@ export const shouldBehaveLikeLSP1Delegate = (
ERC725YDataKeys.LSP10['LSP10Vaults[]'].index + '00'.repeat(16),
ERC725YDataKeys.LSP10['LSP10VaultsMap'] + vault.address.substring(2),
];

const lsp10DataValues = [
abiCoder.encode(['uint256'], ['1']),
bytes16Value1,
vault.address,
abiCoder.encode(['bytes4', 'uint64'], [INTERFACE_IDS.LSP9Vault, 0]),
];
Expand All @@ -2644,7 +2666,7 @@ export const shouldBehaveLikeLSP1Delegate = (
ERC725YDataKeys.LSP10['LSP10Vaults[]'].length,
);

expect(lsp10VaultArrayLengthValue).to.equal(abiCoder.encode(['uint256'], ['1']));
expect(lsp10VaultArrayLengthValue).to.equal(bytes16Value1);

const lsp10VaultArrayIndexValue = await context.universalProfile1['getData(bytes32)'](
ERC725YDataKeys.LSP10['LSP10Vaults[]'].index + '00'.repeat(16),
Expand Down Expand Up @@ -2713,7 +2735,7 @@ export const shouldBehaveLikeLSP1Delegate = (
ERC725YDataKeys.LSP10['LSP10Vaults[]'].length,
);

expect(lsp10VaultArrayLengthValue).to.equal(abiCoder.encode(['uint256'], ['1']));
expect(lsp10VaultArrayLengthValue).to.equal(bytes16Value1);

const lsp10VaultArrayIndexValue = await context.universalProfile1['getData(bytes32)'](
ERC725YDataKeys.LSP10['LSP10Vaults[]'].index + '00'.repeat(16),
Expand All @@ -2731,6 +2753,118 @@ export const shouldBehaveLikeLSP1Delegate = (
);
});
});

describe('when URD has no permissions', () => {
before('deploying new URD', async () => {
const newURD = await new LSP1UniversalReceiverDelegateUP__factory(
context.accounts.owner1,
).deploy();

await context.universalProfile1
.connect(context.accounts.owner1)
.setData(ERC725YDataKeys.LSP1.LSP1UniversalReceiverDelegate, newURD.address);

await context.universalProfile2
.connect(context.accounts.owner2)
.setData(ERC725YDataKeys.LSP1.LSP1UniversalReceiverDelegate, newURD.address);
});

describe('when receiving LSP7', () => {
it('should not revert', async () => {
// Deploy LSP7 (mint on SC level 1000 tokens)
const LSP7 = await new LSP7MintWhenDeployed__factory(context.accounts.owner1).deploy(
'MyToken',
'MTK',
context.universalProfile1.address,
);

expect(await LSP7.balanceOf(context.universalProfile1.address)).to.equal(1000);
expect(await LSP7.balanceOf(context.universalProfile2.address)).to.equal(0);

// Encode LSP7 tokens tarnsfer (UP1 to UP2)
const LSP7_TransferCalldata = LSP7.interface.encodeFunctionData('transfer', [
context.universalProfile1.address,
context.universalProfile2.address,
1,
false,
'0x',
]);

// Transfer LSP7 tokens
await context.universalProfile1
.connect(context.accounts.owner1)
.execute(OPERATION_TYPES.CALL, LSP7.address, 0, LSP7_TransferCalldata);

expect(await LSP7.balanceOf(context.universalProfile1.address)).to.equal(999);
expect(await LSP7.balanceOf(context.universalProfile2.address)).to.equal(1);
});
});

describe('when receiving LSP8', () => {
it('should not revert', async () => {
// Deploy LSP8
const LSP8 = await new LSP8Tester__factory(context.accounts.owner1).deploy(
'MyToken',
'MTK',
context.universalProfile1.address,
);
// Mint token for UP1
await LSP8.mint(context.universalProfile1.address, '0x' + '0'.repeat(64), true, '0x');

expect(await LSP8.tokenOwnerOf('0x' + '0'.repeat(64))).to.equal(
context.universalProfile1.address,
);

// Encode LSP8 token tarnsfer (UP1 to UP2)
const LSP8_TransferCalldata = LSP8.interface.encodeFunctionData('transfer', [
context.universalProfile1.address,
context.universalProfile2.address,
'0x' + '0'.repeat(64),
false,
'0x',
]);

// Transfer LSP8 token
await context.universalProfile1
.connect(context.accounts.owner1)
.execute(OPERATION_TYPES.CALL, LSP8.address, 0, LSP8_TransferCalldata);

expect(await LSP8.tokenOwnerOf('0x' + '0'.repeat(64))).to.equal(
context.universalProfile2.address,
);
});
});

describe('when becoming the owner of the LSP9 Vault', () => {
it('should not revert', async () => {
// Deploy LSP9 (UP1 ownwer)
const LSP9 = await new LSP9Vault__factory(context.accounts.owner1).deploy(
context.universalProfile1.address,
);

expect(await LSP9.owner()).to.equal(context.universalProfile1.address);

// Encode LSP9 transfer & accept ownership (UP1 to UP2)
const LSP9_TransferOwnerhsipCalldata = LSP9.interface.encodeFunctionData(
'transferOwnership',
[context.universalProfile2.address],
);
const LSP9_AcceptOwnerhsipCalldata = LSP9.interface.encodeFunctionData('acceptOwnership');

// Transfer Ownership of LSP9
await context.universalProfile1
.connect(context.accounts.owner1)
.execute(OPERATION_TYPES.CALL, LSP9.address, 0, LSP9_TransferOwnerhsipCalldata);

// Accept Ownership of LSP9
await context.universalProfile2
.connect(context.accounts.owner2)
.execute(OPERATION_TYPES.CALL, LSP9.address, 0, LSP9_AcceptOwnerhsipCalldata);

expect(await LSP9.owner()).to.equal(context.universalProfile2.address);
});
});
});
};

export type LSP1DelegateInitializeTestContext = {
Expand Down
Loading

0 comments on commit 7b06a09

Please sign in to comment.