Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
feat: add similar args sendToken tx test
Browse files Browse the repository at this point in the history
Signed-off-by: Jawad Tariq <[email protected]>
  • Loading branch information
JDawg287 committed Feb 15, 2024
1 parent 40e8972 commit 50baf3c
Showing 1 changed file with 87 additions and 8 deletions.
95 changes: 87 additions & 8 deletions test/topos-core/ToposMessaging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ describe('ToposMessaging', () => {
receiver.address,
admin,
cc.SOURCE_SUBNET_ID_2,
tc.TOKEN_SYMBOL_X
tc.TOKEN_SYMBOL_X,
tc.SEND_AMOUNT_50
)

const { proofBlob, receiptsRoot } = await getReceiptMptProof(
Expand Down Expand Up @@ -507,7 +508,8 @@ describe('ToposMessaging', () => {
receiver.address,
admin,
cc.SOURCE_SUBNET_ID_2,
tc.TOKEN_SYMBOL_X
tc.TOKEN_SYMBOL_X,
tc.SEND_AMOUNT_50
)

const { proofBlob, receiptsRoot } = await getReceiptMptProof(
Expand Down Expand Up @@ -556,7 +558,8 @@ describe('ToposMessaging', () => {
ethers.ZeroAddress, // sending to a zero address
admin,
cc.SOURCE_SUBNET_ID_2,
tc.TOKEN_SYMBOL_X
tc.TOKEN_SYMBOL_X,
tc.SEND_AMOUNT_50
)

const { proofBlob, receiptsRoot } = await getReceiptMptProof(
Expand All @@ -582,6 +585,80 @@ describe('ToposMessaging', () => {
).to.be.revertedWith('ERC20: mint to the zero address')
})

it('can execute a transaction with same inputs twice', async () => {
const { admin, receiver, defaultToken, toposCore, erc20Messaging } =
await loadFixture(deployERC20MessagingFixture)
await toposCore.setNetworkSubnetId(cc.SOURCE_SUBNET_ID_2)
// first transaction
const { erc20, proofBlob, receiptsRoot } = await deployDefaultToken(
admin,
receiver,
defaultToken,
erc20Messaging
)
const certificate = testUtils.encodeCertParam(
cc.PREV_CERT_ID_0,
cc.SOURCE_SUBNET_ID_1,
cc.STATE_ROOT_MAX,
cc.TX_ROOT_MAX,
receiptsRoot,
[cc.SOURCE_SUBNET_ID_2],
cc.VERIFIER,
cc.CERT_ID_1,
cc.DUMMY_STARK_PROOF,
cc.DUMMY_SIGNATURE
)
await toposCore.pushCertificate(certificate, cc.CERT_POS_1)
await expect(
erc20Messaging.execute([tc.TOKEN_SENT_INDEX_2], proofBlob, receiptsRoot)
)
.to.emit(erc20, 'Transfer')
.withArgs(ethers.ZeroAddress, receiver.address, tc.SEND_AMOUNT_50)
await expect(erc20.balanceOf(receiver.address)).to.eventually.equal(
tc.SEND_AMOUNT_50
)

// second transaction
await erc20.approve(await erc20Messaging.getAddress(), tc.SEND_AMOUNT_50)
const sendToken = await sendTokenTx(
erc20Messaging,
ethers.provider,
await receiver.getAddress(),
admin,
cc.SOURCE_SUBNET_ID_2,
await erc20.symbol(),
tc.SEND_AMOUNT_50
)

const { proofBlob: proofBlob2, receiptsRoot: receiptsRoot2 } =
await getReceiptMptProof(sendToken, ethers.provider)
const certificate2 = testUtils.encodeCertParam(
cc.CERT_ID_1,
cc.SOURCE_SUBNET_ID_1,
cc.STATE_ROOT_MAX,
cc.TX_ROOT_MAX,
receiptsRoot2,
[cc.SOURCE_SUBNET_ID_2],
cc.VERIFIER,
cc.CERT_ID_2,
cc.DUMMY_STARK_PROOF,
cc.DUMMY_SIGNATURE
)
await toposCore.pushCertificate(certificate2, cc.CERT_POS_2)
await expect(
erc20Messaging.execute(
[tc.TOKEN_SENT_INDEX_2],
proofBlob2,
receiptsRoot2
)
)
.to.emit(erc20, 'Transfer')
.withArgs(ethers.ZeroAddress, receiver.address, tc.SEND_AMOUNT_50)
await expect(erc20.balanceOf(receiver.address)).to.eventually.equal(
tc.SEND_AMOUNT_50 * 2
)
})

it('emits the Transfer success event', async () => {
const { admin, receiver, defaultToken, toposCore, erc20Messaging } =
await loadFixture(deployERC20MessagingFixture)
Expand Down Expand Up @@ -702,7 +779,7 @@ describe('ToposMessaging', () => {
tc.SEND_AMOUNT_50
)
.to.emit(toposCore, 'CrossSubnetMessageSent')
.withArgs(cc.TARGET_SUBNET_ID_4)
.withArgs(cc.TARGET_SUBNET_ID_4, cc.SOURCE_SUBNET_ID_2, 1)
})
})

Expand Down Expand Up @@ -730,7 +807,8 @@ describe('ToposMessaging', () => {
receiverAddress,
admin,
cc.SOURCE_SUBNET_ID_2,
await erc20.symbol()
await erc20.symbol(),
tc.SEND_AMOUNT_50
)

const { proofBlob, receiptsRoot } = await getReceiptMptProof(
Expand All @@ -746,20 +824,21 @@ describe('ToposMessaging', () => {
receiver: string,
signer: Signer,
targetSubnetId: string,
symbol: string
symbol: string,
amount: number
) {
const estimatedGasLimit = await erc20Messaging.sendToken.estimateGas(
targetSubnetId,
symbol,
receiver,
tc.SEND_AMOUNT_50,
amount,
{ gasLimit: 4_000_000 }
)
const TxUnsigned = await erc20Messaging.sendToken.populateTransaction(
targetSubnetId,
symbol,
receiver,
tc.SEND_AMOUNT_50,
amount,
{ gasLimit: 4_000_000 }
)
TxUnsigned.chainId = BigInt(31337) // Hardcoded chainId for Hardhat local testing
Expand Down

0 comments on commit 50baf3c

Please sign in to comment.