Skip to content

Commit

Permalink
fix BTC -> EVM trasnfer amount
Browse files Browse the repository at this point in the history
  • Loading branch information
nmlinaric committed Jul 10, 2024
1 parent 144bcbb commit 8cf473f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
5 changes: 3 additions & 2 deletions contracts/handlers/NativeTokenHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ contract NativeTokenHandler is IHandler, ERCHandlerHelpers {
(uint256 amount) = abi.decode(data, (uint256));
address tokenAddress = _resourceIDToTokenContractAddress[resourceID];
address recipientAddress = address(bytes20(bytes(data[64:84])));
uint256 convertedAmount = convertToExternalBalance(tokenAddress, amount);

(bool success, ) = address(recipientAddress).call{value: amount}("");
(bool success, ) = address(recipientAddress).call{value: convertedAmount}("");
if(!success) revert FailedFundsTransfer();
emit FundsTransferred(recipientAddress, amount);

return abi.encode(tokenAddress, address(recipientAddress), amount);
return abi.encode(tokenAddress, address(recipientAddress), convertedAmount);
}

/**
Expand Down
59 changes: 59 additions & 0 deletions test/adapters/native/decimalConversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ contract("Bridge - [decimal conversion - native token]", async (accounts) => {
const destinationDomainID = 2;
const adminAddress = accounts[0];
const depositorAddress = accounts[1];
const evmRecipientAddress = accounts[2];
const relayer1Address = accounts[3];

const expectedDepositNonce = 1;
const resourceID = "0x0000000000000000000000000000000000000000000000000000000000000650";
Expand Down Expand Up @@ -137,4 +139,61 @@ contract("Bridge - [decimal conversion - native token]", async (accounts) => {
);
});
});

it("Proposal execution converts sent token amount with 18 decimals to 8 decimal places", async () => {
const expectedRecipientTransferAmount = Ethers.utils.parseUnits("0.9", originDecimalPlaces);
const proposalData = Helpers.createERCDepositData(
convertedTransferAmount, // 18 decimals
20,
evmRecipientAddress
);

const dataHash = Ethers.utils.keccak256(
NativeTokenHandlerInstance.address + proposalData.substr(2)
);

const proposal = {
originDomainID: originDomainID,
depositNonce: expectedDepositNonce,
resourceID: resourceID,
data: proposalData,
};

const proposalSignedData = await Helpers.signTypedProposal(
BridgeInstance.address,
[proposal]
);

const recipientBalanceBefore = await web3.eth.getBalance(evmRecipientAddress);

const proposalTx = await BridgeInstance.executeProposal(
proposal,
proposalSignedData,
{from: relayer1Address}
);

TruffleAssert.eventEmitted(proposalTx, "ProposalExecution", (event) => {
return (
event.originDomainID.toNumber() === originDomainID &&
event.depositNonce.toNumber() === expectedDepositNonce &&
event.dataHash === dataHash &&
event.handlerResponse === Ethers.utils.defaultAbiCoder.encode(
["address", "address", "uint256"],
[NativeTokenHandlerInstance.address, evmRecipientAddress, expectedRecipientTransferAmount]
)
);
});

// check that deposit nonce has been marked as used in bitmap
assert.isTrue(
await BridgeInstance.isProposalExecuted(
originDomainID,
expectedDepositNonce
)
);

// check that tokens are transferred to recipient address
const recipientBalanceAfter = await web3.eth.getBalance(evmRecipientAddress);
assert.strictEqual(transferredAmount.add(recipientBalanceBefore).toString(), recipientBalanceAfter);
});
});

0 comments on commit 8cf473f

Please sign in to comment.