Skip to content

Commit

Permalink
refactor: Change abi.encodePacked => abi.encode in LSP7 & LSP8 (#699
Browse files Browse the repository at this point in the history
)

* refactor: `abi.encodePacked` => `abi.encode`

* test: update tests with the new behavior
  • Loading branch information
b00ste authored Sep 5, 2023
1 parent 4599e20 commit 848842d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 55 deletions.
11 changes: 3 additions & 8 deletions contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ abstract contract LSP7DigitalAssetCore is ILSP7DigitalAsset {
data
);

bytes memory lsp1Data = abi.encodePacked(address(0), to, amount, data);
bytes memory lsp1Data = abi.encode(address(0), to, amount, data);
_notifyTokenReceiver(to, allowNonLSP1Recipient, lsp1Data);
}

Expand Down Expand Up @@ -416,12 +416,7 @@ abstract contract LSP7DigitalAssetCore is ILSP7DigitalAsset {

emit Transfer(operator, from, address(0), amount, false, data);

bytes memory lsp1Data = abi.encodePacked(
from,
address(0),
amount,
data
);
bytes memory lsp1Data = abi.encode(from, address(0), amount, data);
_notifyTokenSender(from, lsp1Data);
}

Expand Down Expand Up @@ -473,7 +468,7 @@ abstract contract LSP7DigitalAssetCore is ILSP7DigitalAsset {

emit Transfer(operator, from, to, amount, allowNonLSP1Recipient, data);

bytes memory lsp1Data = abi.encodePacked(from, to, amount, data);
bytes memory lsp1Data = abi.encode(from, to, amount, data);

_notifyTokenSender(from, lsp1Data);
_notifyTokenReceiver(to, allowNonLSP1Recipient, lsp1Data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ abstract contract LSP8IdentifiableDigitalAssetCore is
data
);

bytes memory lsp1Data = abi.encodePacked(address(0), to, tokenId, data);
bytes memory lsp1Data = abi.encode(address(0), to, tokenId, data);
_notifyTokenReceiver(to, allowNonLSP1Recipient, lsp1Data);
}

Expand Down Expand Up @@ -389,7 +389,7 @@ abstract contract LSP8IdentifiableDigitalAssetCore is

emit Transfer(operator, tokenOwner, address(0), tokenId, false, data);

bytes memory lsp1Data = abi.encodePacked(
bytes memory lsp1Data = abi.encode(
tokenOwner,
address(0),
tokenId,
Expand Down Expand Up @@ -453,7 +453,7 @@ abstract contract LSP8IdentifiableDigitalAssetCore is

emit Transfer(operator, from, to, tokenId, allowNonLSP1Recipient, data);

bytes memory lsp1Data = abi.encodePacked(from, to, tokenId, data);
bytes memory lsp1Data = abi.encode(from, to, tokenId, data);

_notifyTokenSender(from, lsp1Data);
_notifyTokenReceiver(to, allowNonLSP1Recipient, lsp1Data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ contract UniversalReceiverDelegateTokenReentrant is ERC165Storage {
typeId == _TYPEID_LSP8_TOKENSRECIPIENT
) {
// if the optional data field when minting/transferring is existing, re-execute the data on token contract
if (data.length > 72) {
bytes memory tokenPayload = BytesLib.slice(
if (data.length > 160) {
(, , , bytes memory tokenPayload) = abi.decode(
data,
72,
data.length - 72
(address, address, uint256, bytes)
);

bytes memory executePayload = abi.encodeWithSelector(
IERC725X.execute.selector,
0, // OPERATION CALL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ export const shouldBehaveLikeLSP1Delegate = (

// the call to the universalReceiver(...) in LSP7 sends the transfer details as `data` argument
// all the params are packed/concatenated together.
const expectedReceivedData = ethers.utils.solidityPack(
const expectedReceivedData = abiCoder.encode(
['address', 'address', 'uint256', 'bytes'],
[txParams.from, txParams.to, txParams.amount, txParams.data],
);
Expand Down Expand Up @@ -1140,7 +1140,7 @@ export const shouldBehaveLikeLSP1Delegate = (
token.address,
0,
LSP1_TYPE_IDS.LSP7Tokens_RecipientNotification,
ethers.utils.solidityPack(
abiCoder.encode(
['address', 'address', 'uint256', 'bytes'],
[ethers.constants.AddressZero, context.universalProfile1.address, 10_000, '0x'],
),
Expand Down Expand Up @@ -1275,11 +1275,15 @@ export const shouldBehaveLikeLSP1Delegate = (
it('should emit UniversalReceiver event', async () => {
const tokensSentBytes32Value = ethers.utils.hexZeroPad(balance.toHexString(), 32);

const tokenTransferData = (
context.universalProfile1.address +
context.accounts.owner1.address.substring(2) +
tokensSentBytes32Value.substring(2)
).toLowerCase();
const tokenTransferData = abiCoder.encode(
['address', 'address', 'uint256', 'bytes'],
[
context.universalProfile1.address,
context.accounts.owner1.address,
tokensSentBytes32Value,
'0x',
],
);

const lsp1ReturnedData = ethers.utils.defaultAbiCoder.encode(
['string', 'bytes'],
Expand Down Expand Up @@ -1354,11 +1358,15 @@ export const shouldBehaveLikeLSP1Delegate = (
it('should emit UniversalReceiver event', async () => {
const tokensSentBytes32Value = ethers.utils.hexZeroPad(balance.toHexString(), 32);

const tokenTransferData = (
context.universalProfile1.address +
context.accounts.owner1.address.substring(2) +
tokensSentBytes32Value.substring(2)
).toLowerCase();
const tokenTransferData = abiCoder.encode(
['address', 'address', 'uint256', 'bytes'],
[
context.universalProfile1.address,
context.accounts.owner1.address,
tokensSentBytes32Value,
'0x',
],
);

const lsp1ReturnedData = ethers.utils.defaultAbiCoder.encode(
['string', 'bytes'],
Expand Down Expand Up @@ -1437,11 +1445,15 @@ export const shouldBehaveLikeLSP1Delegate = (
it('should emit UniversalReceiver event', async () => {
const tokensSentBytes32Value = ethers.utils.hexZeroPad(balance.toHexString(), 32);

const tokenTransferData = (
context.universalProfile1.address +
context.accounts.owner1.address.substring(2) +
tokensSentBytes32Value.substring(2)
).toLowerCase();
const tokenTransferData = abiCoder.encode(
['address', 'address', 'uint256', 'bytes'],
[
context.universalProfile1.address,
context.accounts.owner1.address,
tokensSentBytes32Value,
'0x',
],
);

const lsp1ReturnedData = ethers.utils.defaultAbiCoder.encode(
['string', 'bytes'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -919,18 +919,22 @@ export const shouldBehaveLikeLSP1Delegate = (buildContext: () => Promise<LSP1Tes
.execute(OPERATION_TYPES.CALL, context.lsp9Vault1.address, 0, vaultTokenTransferCalldata);
});

it('it should pass', async () => {
it('should pass', async () => {
expect(tokenTransferTx).to.not.be.reverted;
});

it('it should emit UniversalReceiver event', async () => {
it('should emit UniversalReceiver event', async () => {
const tokensSentBytes32Value = ethers.utils.hexZeroPad(balance.toHexString(), 32);

const tokenTransferData = (
context.lsp9Vault1.address +
context.accounts.owner1.address.substring(2) +
tokensSentBytes32Value.substring(2)
).toLowerCase();
const tokenTransferData = abiCoder.encode(
['address', 'address', 'uint256', 'bytes'],
[
context.lsp9Vault1.address,
context.accounts.owner1.address,
tokensSentBytes32Value,
'0x',
],
);

const lsp1ReturnedData = ethers.utils.defaultAbiCoder.encode(
['string', 'bytes'],
Expand Down Expand Up @@ -1012,11 +1016,15 @@ export const shouldBehaveLikeLSP1Delegate = (buildContext: () => Promise<LSP1Tes
it('should emit UniversalReceiver event', async () => {
const tokensSentBytes32Value = ethers.utils.hexZeroPad(balance.toHexString(), 32);

const tokenTransferData = (
context.lsp9Vault1.address +
context.accounts.owner1.address.substring(2) +
tokensSentBytes32Value.substring(2)
).toLowerCase();
const tokenTransferData = abiCoder.encode(
['address', 'address', 'uint256', 'bytes'],
[
context.lsp9Vault1.address,
context.accounts.owner1.address,
tokensSentBytes32Value,
'0x',
],
);

const lsp1ReturnedData = ethers.utils.defaultAbiCoder.encode(
['string', 'bytes'],
Expand Down Expand Up @@ -1098,11 +1106,15 @@ export const shouldBehaveLikeLSP1Delegate = (buildContext: () => Promise<LSP1Tes
it('should emit UniversalReceiver event', async () => {
const tokensSentBytes32Value = ethers.utils.hexZeroPad(balance.toHexString(), 32);

const tokenTransferData = (
context.lsp9Vault1.address +
context.accounts.owner1.address.substring(2) +
tokensSentBytes32Value.substring(2)
).toLowerCase();
const tokenTransferData = abiCoder.encode(
['address', 'address', 'uint256', 'bytes'],
[
context.lsp9Vault1.address,
context.accounts.owner1.address,
tokensSentBytes32Value,
'0x',
],
);

const lsp1ReturnedData = ethers.utils.defaultAbiCoder.encode(
['string', 'bytes'],
Expand Down
8 changes: 5 additions & 3 deletions tests/LSP7DigitalAsset/LSP7DigitalAsset.behaviour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
// constants
import { ERC725YDataKeys, INTERFACE_IDS, LSP1_TYPE_IDS, SupportedStandards } from '../../constants';

import { abiCoder } from '../utils/helpers';

export type LSP7TestAccounts = {
owner: SignerWithAddress;

Expand Down Expand Up @@ -812,7 +814,7 @@ export const shouldBehaveLikeLSP7 = (buildContext: () => Promise<LSP7TestContext
const tx = await transferSuccessScenario(txParams, operator);

const typeId = LSP1_TYPE_IDS.LSP7Tokens_RecipientNotification;
const packedData = ethers.utils.solidityPack(
const packedData = abiCoder.encode(
['address', 'address', 'uint256', 'bytes'],
[txParams.from, txParams.to, txParams.amount, txParams.data],
);
Expand Down Expand Up @@ -885,7 +887,7 @@ export const shouldBehaveLikeLSP7 = (buildContext: () => Promise<LSP7TestContext
const tx = await transferSuccessScenario(txParams, operator);

const typeId = LSP1_TYPE_IDS.LSP7Tokens_RecipientNotification;
const packedData = ethers.utils.solidityPack(
const packedData = abiCoder.encode(
['address', 'address', 'uint256', 'bytes'],
[txParams.from, txParams.to, txParams.amount, txParams.data],
);
Expand Down Expand Up @@ -1306,7 +1308,7 @@ export const shouldBehaveLikeLSP7 = (buildContext: () => Promise<LSP7TestContext
txParams.amount.map((_, index) => async () => {
const typeId =
'0x29ddb589b1fb5fc7cf394961c1adf5f8c6454761adf795e67fe149f658abe895';
const packedData = ethers.utils.solidityPack(
const packedData = abiCoder.encode(
['address', 'address', 'uint256', 'bytes'],
[
txParams.from[index],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {

// helpers
import { tokenIdAsBytes32 } from '../utils/tokens';
import { abiCoder } from '../utils/helpers';

// constants
import { ERC725YDataKeys, INTERFACE_IDS, LSP1_TYPE_IDS, SupportedStandards } from '../../constants';
Expand Down Expand Up @@ -633,7 +634,7 @@ export const shouldBehaveLikeLSP8 = (buildContext: () => Promise<LSP8TestContext
const tx = await transferSuccessScenario(txParams, operator);

const typeId = LSP1_TYPE_IDS.LSP8Tokens_RecipientNotification;
const packedData = ethers.utils.solidityPack(
const packedData = abiCoder.encode(
['address', 'address', 'bytes32', 'bytes'],
[txParams.from, txParams.to, txParams.tokenId, txParams.data],
);
Expand Down Expand Up @@ -732,7 +733,7 @@ export const shouldBehaveLikeLSP8 = (buildContext: () => Promise<LSP8TestContext
const tx = await transferSuccessScenario(txParams, operator);

const typeId = LSP1_TYPE_IDS.LSP8Tokens_RecipientNotification;
const packedData = ethers.utils.solidityPack(
const packedData = abiCoder.encode(
['address', 'address', 'bytes32', 'bytes'],
[txParams.from, txParams.to, txParams.tokenId, txParams.data],
);
Expand Down Expand Up @@ -1076,7 +1077,7 @@ export const shouldBehaveLikeLSP8 = (buildContext: () => Promise<LSP8TestContext
txParams.tokenId.map((_, index) => async () => {
const typeId =
'0x29ddb589b1fb5fc7cf394961c1adf5f8c6454761adf795e67fe149f658abe895';
const packedData = ethers.utils.solidityPack(
const packedData = abiCoder.encode(
['address', 'address', 'bytes32', 'bytes'],
[
txParams.from[index],
Expand Down

0 comments on commit 848842d

Please sign in to comment.