Skip to content

Commit

Permalink
test(forge): update forge tests
Browse files Browse the repository at this point in the history
  • Loading branch information
envin3 committed Oct 23, 2024
1 parent 3d10372 commit caa783b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 57 deletions.
23 changes: 11 additions & 12 deletions solidity/test/forge/Helper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -209,29 +209,28 @@ abstract contract Helper is Test, DeployHelper {
console.log("");
}

bytes memory eventBytes = abi
.decode(log.data, (IAdapter.EventBytes))
.content;
bytes memory eventBytes = log.data;

uint256 dataStart = uint256(bytes32(BytesLib.slice(eventBytes, 128, 32)));
// uint256 dataEnd = uint256(bytes32(BytesLib.slice(eventBytes, 160, 32)));
uint256 recipientLen = uint256(
bytes32(BytesLib.slice(eventBytes, 160, 32))
bytes32(BytesLib.slice(eventBytes, dataStart, 32))
);

uint256 dataLen = eventBytes.length - recipientLen - 192;
uint256 dataLen = uint256(bytes32(BytesLib.slice(eventBytes, dataStart + 96, 32)));
return
IAdapter.Operation(
blockHash,
txHash,
uint256(log.topics[1]), // nonce
bytes32(BytesLib.slice(eventBytes, 32, 32)), // erc20
bytes32(BytesLib.slice(eventBytes, 0, 32)), // erc20
bytes32(block.chainid),
bytes32(BytesLib.slice(eventBytes, 64, 32)), // destination chain id
uint256(bytes32(BytesLib.slice(eventBytes, 96, 32))), // amount
bytes32(BytesLib.slice(eventBytes, 128, 32)), // sender
bytes32(BytesLib.slice(eventBytes, 32, 32)), // destination chain id
uint256(bytes32(BytesLib.slice(eventBytes, 64, 32))), // amount
bytes32(BytesLib.slice(eventBytes, 96, 32)), // sender
_hexStringToAddress(
string(BytesLib.slice(eventBytes, 192, recipientLen)) // recipient
string(BytesLib.slice(eventBytes, dataStart + 32, recipientLen)) // recipient
),
BytesLib.slice(eventBytes, 192 + recipientLen, dataLen) // data
BytesLib.slice(eventBytes, dataStart + 128, dataLen) // data
);
}

Expand Down
36 changes: 12 additions & 24 deletions solidity/test/forge/Integration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,12 @@ contract IntegrationTest is Test, Helper {
vm.expectEmit(address(adapter_A));
emit IAdapter.Swap(
nonce,
IAdapter.EventBytes(
bytes.concat(
bytes32(nonce),
erc20Bytes,
bytes32(CHAIN_B),
bytes32(netAmount),
bytes32(uint256(uint160(user))),
bytes32(bytes(recipientStr).length),
bytes(recipientStr),
data
)
)
erc20Bytes,
bytes32(CHAIN_B),
bytes32(netAmount),
bytes32(uint256(uint160(user))),
bytes(recipientStr),
data
);

adapter_A.swap(address(erc20), amount, CHAIN_B, recipientStr, data);
Expand Down Expand Up @@ -177,18 +171,12 @@ contract IntegrationTest is Test, Helper {

emit IAdapter.Swap(
nonce,
IAdapter.EventBytes(
bytes.concat(
bytes32(nonce),
erc20Bytes,
bytes32(CHAIN_B),
bytes32(amount - fees),
bytes32(uint256(uint160(user))),
bytes32(bytes(recipientStr).length),
bytes(recipientStr),
data
)
)
erc20Bytes,
bytes32(CHAIN_B),
bytes32(amount - fees),
bytes32(uint256(uint160(user))),
bytes(recipientStr),
data
);

adapter_A.swap(address(xerc20_A), amount, CHAIN_B, recipientStr, data);
Expand Down
27 changes: 8 additions & 19 deletions solidity/test/hardhat/utils/Operation.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,23 @@ const { ethers } = require('hardhat')
const { padLeft32 } = require('./pad-left-32.cjs')

class Operation {
constructor({ blockId, txId, originChainId, eventContent }) {
constructor({ blockId, txId, originChainId, nonce, erc20, destinationChainId, amount, sender, recipient, data}) {
this.blockId = blockId
this.txId = txId
let offset = 0
this.nonce = ethers.toBigInt(
ethers.getBytes(ethers.dataSlice(eventContent, 0, (offset += 32))),
)
this.erc20 = ethers.dataSlice(eventContent, offset, (offset += 32))
this.nonce = nonce
this.erc20 = erc20
this.originChainId = padLeft32(originChainId)
this.destinationChainId = ethers.dataSlice(
eventContent,
offset,
(offset += 32),
)
this.destinationChainId = destinationChainId
this.amount = ethers.toBigInt(
ethers.getBytes(ethers.dataSlice(eventContent, offset, (offset += 32))),
)
this.sender = ethers.dataSlice(eventContent, offset, (offset += 32))
const recipientLen = ethers.toNumber(
ethers.getBytes(ethers.dataSlice(eventContent, offset, (offset += 32))),
ethers.getBytes(amount),
)
this.sender = sender
this.recipient = Buffer.from(
ethers
.dataSlice(eventContent, offset, (offset += recipientLen))
.replace('0x', ''),
recipient.replace('0x', ''),
'hex',
).toString('utf-8')
this.data = ethers.dataSlice(eventContent, offset)
this.data = data
}

serialize() {
Expand Down
3 changes: 1 addition & 2 deletions solidity/test/hardhat/utils/get-swap-event.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const R = require('ramda')

const SWAP_EVENT_TOPIC =
'0x9b706941b48091a1c675b439064f40b9d43c577d9c7134cce93179b9b0bf2a52'
const SWAP_EVENT_TOPIC = '0x7a62b8c6141da7e579d5a8ec90e6a5fbd5ce9a3d16ca0a9955ee3c2a095a0c2d'

module.exports.getSwapEvent = _tx =>
_tx
Expand Down

0 comments on commit caa783b

Please sign in to comment.