Skip to content

Commit

Permalink
add receipt events checks into integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avkos committed Sep 18, 2023
1 parent 5da4487 commit 3d61132
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ describe('contract', () => {
it('should transfer tokens', async () => {
const acc2 = await createTempAccount();
const value = BigInt(10);
await contractDeployed.methods.transfer(acc2.address, value).send(sendOptions);
const receipt = await contractDeployed.methods
.transfer(acc2.address, value)
.send(sendOptions);

expect(receipt.events).toBeDefined();
expect(receipt.events?.Transfer).toBeDefined();
expect(receipt.events?.Transfer.event).toBe('Transfer');

expect(await contractDeployed.methods.balanceOf(acc2.address).call()).toBe(
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ describe('contract', () => {

it('should award item', async () => {
const tempAccount = await createTempAccount();
await contractDeployed.methods
const receipt = await contractDeployed.methods
.awardItem(tempAccount.address, 'http://my-nft-uri')
.send(sendOptions);

expect(receipt.events).toBeDefined();
expect(receipt.events?.Transfer).toBeDefined();
expect(receipt.events?.Transfer.event).toBe('Transfer');

const tokenId = toBigInt(0);
expect(
toUpperCaseHex(
Expand Down Expand Up @@ -289,9 +293,13 @@ describe('contract', () => {
});
});

await contractDeployed.methods
const receipt = await contractDeployed.methods
.awardItem(acc2.address, 'http://my-nft-uri')
.send(sendOptions);

expect(receipt.events).toBeDefined();
expect(receipt.events?.Transfer).toBeDefined();
expect(receipt.events?.Transfer.event).toBe('Transfer');
}),
).resolves.toEqual({
from: '0x0000000000000000000000000000000000000000',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ describe('contract', () => {
.setValues(1, 'string value', true)
.send(sendOptions);

expect(receipt.events).toBeUndefined();
expect(receipt).toEqual(
expect.objectContaining({
// status: BigInt(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ describe('contract', () => {
const acc = web3.eth.accounts.create();
const value = BigInt(10);

await contractDeployed.methods.transfer(acc.address, value).send({
const receipt = await contractDeployed.methods.transfer(acc.address, value).send({
...sendOptions,
type,
});

expect(receipt.events).toBeDefined();
expect(receipt.events?.Transfer).toBeDefined();
expect(receipt.events?.Transfer.event).toBe('Transfer');
expect(await contractDeployed.methods.balanceOf(acc.address).call()).toBe(value);
});

Expand All @@ -74,14 +76,21 @@ describe('contract', () => {
const transferFromValue = BigInt(4);
const tempAccount = await createLocalAccount(web3);
// approve
await contractDeployed.methods
const approvalReceipt = await contractDeployed.methods
.approve(tempAccount.address, value)
.send({ ...sendOptions, type });

expect(approvalReceipt.events).toBeDefined();
expect(approvalReceipt.events?.Approval).toBeDefined();
expect(approvalReceipt.events?.Approval.event).toBe('Approval');
// transferFrom
await contractDeployed.methods
const transferFromReceipt = await contractDeployed.methods
.transferFrom(localAccount.address, tempAccount.address, transferFromValue)
.send({ ...sendOptions, from: tempAccount.address, type });
expect(transferFromReceipt.events).toBeDefined();
expect(transferFromReceipt.events?.Approval).toBeDefined();
expect(transferFromReceipt.events?.Approval.event).toBe('Approval');
expect(transferFromReceipt.events?.Transfer).toBeDefined();
expect(transferFromReceipt.events?.Transfer.event).toBe('Transfer');

expect(await contractDeployed.methods.balanceOf(tempAccount.address).call()).toBe(
transferFromValue,
Expand All @@ -101,10 +110,12 @@ describe('contract', () => {
const tempAccount = await createLocalAccount(web3);

// approve
await contractDeployed.methods
const approvalReceipt = await contractDeployed.methods
.approve(tempAccount.address, value)
.send({ ...sendOptions, type });

expect(approvalReceipt.events).toBeDefined();
expect(approvalReceipt.events?.Approval).toBeDefined();
expect(approvalReceipt.events?.Approval.event).toBe('Approval');
// allowance
expect(
await contractDeployed.methods
Expand All @@ -113,10 +124,13 @@ describe('contract', () => {
).toBe(value);

// increaseAllowance
await contractDeployed.methods
const increaseAllowanceReceipt = await contractDeployed.methods
.increaseAllowance(tempAccount.address, extraAmount)
.send({ ...sendOptions, from: localAccount.address, type, gas: '2000000' });

expect(increaseAllowanceReceipt.events).toBeDefined();
expect(increaseAllowanceReceipt.events?.Approval).toBeDefined();
expect(increaseAllowanceReceipt.events?.Approval.event).toBe('Approval');
// check allowance
expect(
await contractDeployed.methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ describe('contract', () => {

it.each(['0x1', '0x2'])('should award item %p', async type => {
const tempAccount = web3.eth.accounts.create();
await contractDeployed.methods
const awardReceipt = await contractDeployed.methods
.awardItem(tempAccount.address, 'http://my-nft-uri')
.send({ ...sendOptions, type });
expect(awardReceipt.events).toBeDefined();
expect(awardReceipt.events?.Transfer).toBeDefined();
expect(awardReceipt.events?.Transfer.event).toBe('Transfer');

const logs = await contractDeployed.getPastEvents('Transfer');
const tokenId = (logs[0] as EventLog)?.returnValues?.tokenId as string;
Expand All @@ -77,20 +80,27 @@ describe('contract', () => {
it.each(['0x1', '0x2'])('should transferFrom item %p', async type => {
const tempAccount = await createLocalAccount(web3);
const toAccount = await createLocalAccount(web3);
await contractDeployed.methods
const awardReceipt = await contractDeployed.methods
.awardItem(tempAccount.address, 'http://my-nft-award')
.send({ ...sendOptions, type });
expect(awardReceipt.events).toBeDefined();
expect(awardReceipt.events?.Transfer).toBeDefined();
expect(awardReceipt.events?.Transfer.event).toBe('Transfer');

const logs = await contractDeployed.getPastEvents('Transfer');
const tokenId = (logs[0] as EventLog)?.returnValues?.tokenId as string;
await contractDeployed.methods
const transferFromReceipt = await contractDeployed.methods
.transferFrom(tempAccount.address, toAccount.address, tokenId)
.send({
...sendOptions,
type,
from: tempAccount.address,
});

expect(transferFromReceipt.events).toBeDefined();
expect(transferFromReceipt.events?.Transfer).toBeDefined();
expect(transferFromReceipt.events?.Transfer.event).toBe('Transfer');
expect(transferFromReceipt.events?.Approval).toBeDefined();
expect(transferFromReceipt.events?.Approval.event).toBe('Approval');
expect(
toUpperCaseHex(
(await contractDeployed.methods.ownerOf(tokenId).call()) as unknown as string,
Expand All @@ -101,29 +111,44 @@ describe('contract', () => {
it.each(['0x1', '0x2'])('should approve and then transferFrom item %p', async type => {
const tempAccount = await createLocalAccount(web3);
const toAccount = await createLocalAccount(web3);
await contractDeployed.methods
const awardReceipt = await contractDeployed.methods
.awardItem(tempAccount.address, 'http://my-nft-award')
.send({ ...sendOptions, type });
expect(awardReceipt.events).toBeDefined();
expect(awardReceipt.events?.Transfer).toBeDefined();
expect(awardReceipt.events?.Transfer.event).toBe('Transfer');

const logs = await contractDeployed.getPastEvents('Transfer');
const tokenId = (logs[0] as EventLog)?.returnValues?.tokenId as string;

await contractDeployed.methods.approve(toAccount.address, tokenId).send({
...sendOptions,
type,
from: tempAccount.address,
});
const approveReceipt = await contractDeployed.methods
.approve(toAccount.address, tokenId)
.send({
...sendOptions,
type,
from: tempAccount.address,
});
expect(approveReceipt.events).toBeDefined();
expect(approveReceipt.events?.Approval).toBeDefined();
expect(approveReceipt.events?.Approval.event).toBe('Approval');

const res = await contractDeployed.methods.getApproved(tokenId).call();
expect(res.toString().toUpperCase()).toBe(toAccount.address.toUpperCase());

await contractDeployed.methods
const safeTransferFromReceipt = await contractDeployed.methods
.safeTransferFrom(tempAccount.address, toAccount.address, tokenId)
.send({
...sendOptions,
type,
from: toAccount.address,
});

expect(safeTransferFromReceipt.events).toBeDefined();
expect(safeTransferFromReceipt.events?.Transfer).toBeDefined();
expect(safeTransferFromReceipt.events?.Transfer.event).toBe('Transfer');
expect(safeTransferFromReceipt.events?.Approval).toBeDefined();
expect(safeTransferFromReceipt.events?.Approval.event).toBe('Approval');

expect(
toUpperCaseHex(
(await contractDeployed.methods.ownerOf(tokenId).call()) as unknown as string,
Expand All @@ -137,24 +162,34 @@ describe('contract', () => {
const tempAccount = await createLocalAccount(web3);
const toAccount = await createLocalAccount(web3);

await contractDeployed.methods.setApprovalForAll(toAccount.address, true).send({
...sendOptions,
type,
from: tempAccount.address,
});

const setApprovalReceipt = await contractDeployed.methods
.setApprovalForAll(toAccount.address, true)
.send({
...sendOptions,
type,
from: tempAccount.address,
});
expect(setApprovalReceipt.events).toBeDefined();
expect(setApprovalReceipt.events?.ApprovalForAll).toBeDefined();
expect(setApprovalReceipt.events?.ApprovalForAll.event).toBe('ApprovalForAll');
expect(
await contractDeployed.methods
.isApprovedForAll(tempAccount.address, toAccount.address)
.call(),
).toBe(true);

await contractDeployed.methods.setApprovalForAll(toAccount.address, false).send({
...sendOptions,
type,
from: tempAccount.address,
});

const setApprovalForAllReceipt = await contractDeployed.methods
.setApprovalForAll(toAccount.address, false)
.send({
...sendOptions,
type,
from: tempAccount.address,
});
expect(setApprovalForAllReceipt.events).toBeDefined();
expect(setApprovalForAllReceipt.events?.ApprovalForAll).toBeDefined();
expect(setApprovalForAllReceipt.events?.ApprovalForAll.event).toBe(
'ApprovalForAll',
);
expect(
await contractDeployed.methods
.isApprovedForAll(tempAccount.address, toAccount.address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('Web3Eth.sendTransaction', () => {
};
const response = await web3Eth.sendTransaction(transaction);
expect(response.status).toBe(BigInt(1));
expect(response.events).toBeUndefined();

const minedTransactionData = await web3Eth.getTransaction(response.transactionHash);
expect(minedTransactionData).toMatchObject(transaction);
Expand All @@ -84,6 +85,7 @@ describe('Web3Eth.sendTransaction', () => {
};
const response = await web3EthWithWallet.sendTransaction(transaction);
expect(response.status).toBe(BigInt(1));
expect(response.events).toBeUndefined();

const minedTransactionData = await web3EthWithWallet.getTransaction(
response.transactionHash,
Expand Down Expand Up @@ -114,6 +116,7 @@ describe('Web3Eth.sendTransaction', () => {
};
const response = await web3EthWithWallet.sendTransaction(transaction);
expect(response.status).toBe(BigInt(1));
expect(response.events).toBeUndefined();

const minedTransactionData = await web3EthWithWallet.getTransaction(
response.transactionHash,
Expand Down Expand Up @@ -148,6 +151,7 @@ describe('Web3Eth.sendTransaction', () => {
};
const response = await web3EthWithWallet.sendTransaction(transaction);
expect(response.status).toBe(BigInt(1));
expect(response.events).toBeUndefined();

const minedTransactionData = await web3EthWithWallet.getTransaction(
response.transactionHash,
Expand All @@ -167,6 +171,7 @@ describe('Web3Eth.sendTransaction', () => {
};
const response = await web3Eth.sendTransaction(transaction);
expect(response.status).toBe(BigInt(1));
expect(response.events).toBeUndefined();

const minedTransactionData = await web3Eth.getTransaction(response.transactionHash);
expect(minedTransactionData).toMatchObject(transaction);
Expand All @@ -180,6 +185,7 @@ describe('Web3Eth.sendTransaction', () => {
};
const response = await web3Eth.sendTransaction(transaction);
expect(response.status).toBe(BigInt(1));
expect(response.events).toBeUndefined();

const minedTransactionData = await web3Eth.getTransaction(response.transactionHash);
expect(minedTransactionData).toMatchObject(transaction);
Expand All @@ -199,6 +205,7 @@ describe('Web3Eth.sendTransaction', () => {
};
const response = await web3Eth.sendTransaction(transaction);
expect(response.status).toBe(BigInt(1));
expect(response.events).toBeUndefined();
expect(response.contractAddress).toBeDefined();

const minedTransactionData = await web3Eth.getTransaction(response.transactionHash);
Expand All @@ -221,6 +228,7 @@ describe('Web3Eth.sendTransaction', () => {
input: contractFunctionCall,
};
const response = await web3Eth.sendTransaction(transaction);
expect(response.events).toBeUndefined();
expect(response.status).toBe(BigInt(1));

const minedTransactionData = await web3Eth.getTransaction(response.transactionHash);
Expand All @@ -241,6 +249,7 @@ describe('Web3Eth.sendTransaction', () => {
type: BigInt(0),
};
const response = await web3Eth.sendTransaction(transaction);
expect(response.events).toBeUndefined();
expect(response.type).toBe(BigInt(0));
expect(response.status).toBe(BigInt(1));

Expand All @@ -260,6 +269,7 @@ describe('Web3Eth.sendTransaction', () => {
accessList: [],
};
const response = await web3Eth.sendTransaction(transaction);
expect(response.events).toBeUndefined();
expect(response.type).toBe(BigInt(1));
expect(response.status).toBe(BigInt(1));

Expand All @@ -275,6 +285,7 @@ describe('Web3Eth.sendTransaction', () => {
type: BigInt(2),
};
const response = await web3Eth.sendTransaction(transaction);
expect(response.events).toBeUndefined();
expect(response.type).toBe(BigInt(2));
expect(response.status).toBe(BigInt(1));

Expand All @@ -291,6 +302,7 @@ describe('Web3Eth.sendTransaction', () => {
};
const response = await web3Eth.sendTransaction(transaction, DEFAULT_RETURN_FORMAT);
expect(response.type).toBe(BigInt(0));
expect(response.events).toBeUndefined();
expect(response.status).toBe(BigInt(1));
const minedTransactionData = await web3Eth.getTransaction(response.transactionHash);
expect(minedTransactionData).toMatchObject(transaction);
Expand All @@ -304,6 +316,7 @@ describe('Web3Eth.sendTransaction', () => {
maxFeePerGas: BigInt(2500000016),
};
const response = await web3Eth.sendTransaction(transaction);
expect(response.events).toBeUndefined();
expect(response.type).toBe(BigInt(2));
expect(response.status).toBe(BigInt(1));
const minedTransactionData = await web3Eth.getTransaction(response.transactionHash);
Expand All @@ -318,6 +331,7 @@ describe('Web3Eth.sendTransaction', () => {
maxPriorityFeePerGas: BigInt(100),
};
const response = await web3Eth.sendTransaction(transaction);
expect(response.events).toBeUndefined();
expect(response.type).toBe(BigInt(2));
expect(response.status).toBe(BigInt(1));
const minedTransactionData = await web3Eth.getTransaction(response.transactionHash);
Expand Down Expand Up @@ -379,8 +393,9 @@ describe('Web3Eth.sendTransaction', () => {
expect(typeof data.transactionIndex).toBe('bigint');
expect(data.status).toBe(BigInt(1));
expect(data.type).toBe(BigInt(0));
expect(data.events).toBeUndefined();
});
expect.assertions(8);
expect.assertions(9);
});

it('should listen to the confirmation event', async () => {
Expand Down

0 comments on commit 3d61132

Please sign in to comment.