Skip to content

Commit

Permalink
update contracts with new vIBC core API
Browse files Browse the repository at this point in the history
  • Loading branch information
tmsdkeys committed Feb 15, 2024
1 parent cbc692b commit a099ce8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ node_modules

# solidity-coverage files
/coverage
/coverage.json
/coverage.json

# Foundry files
/out
/cache_forge
39 changes: 19 additions & 20 deletions contracts/CustomChanIbcContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity ^0.8.9;
import '@open-ibc/vibc-core-smart-contracts/contracts/Ibc.sol';
import '@open-ibc/vibc-core-smart-contracts/contracts/IbcReceiver.sol';
import '@open-ibc/vibc-core-smart-contracts/contracts/IbcDispatcher.sol';
import '@open-ibc/vibc-core-smart-contracts/contracts/ProofVerifier.sol';

contract CustomChanIbcContract is IbcReceiverBase, IbcReceiver {
// received packet as chain B
Expand All @@ -31,6 +32,10 @@ contract CustomChanIbcContract is IbcReceiverBase, IbcReceiver {
dispatcher = _dispatcher;
}

function getConnectedChannels() external view returns (ChannelMapping[] memory) {
return connectedChannels;
}

/**
* @dev Sends a packet with a greeting message over a specified channel.
* @param message The greeting message to be sent.
Expand Down Expand Up @@ -66,35 +71,33 @@ contract CustomChanIbcContract is IbcReceiverBase, IbcReceiver {
* @param proof not implemented for now
*/
function createChannel(
string calldata version,
CounterParty calldata local,
uint8 ordering,
bool feeEnabled,
string[] calldata connectionHops,
CounterParty calldata counterparty,
Proof calldata proof
Ics23Proof calldata proof
) external {

dispatcher.openIbcChannel(
IbcChannelReceiver(address(this)),
version,
local,
ChannelOrder(ordering),
feeEnabled,
connectionHops,
counterparty,
proof
);
}
}

function onOpenIbcChannel(
string calldata version,
ChannelOrder ordering,
bool feeEnabled,
string[] calldata connectionHops,
string calldata counterpartyPortId,
bytes32 counterpartyChannelId,
string calldata counterpartyVersion
) external onlyIbcDispatcher returns (string memory selectedVersion) {
if (bytes(counterpartyPortId).length <= 8) {
ChannelOrder,
bool,
string[] calldata,
CounterParty calldata counterparty
) external view onlyIbcDispatcher returns (string memory selectedVersion) {
if (bytes(counterparty.portId).length <= 8) {
revert invalidCounterPartyPortId();
}
/**
Expand All @@ -105,7 +108,7 @@ contract CustomChanIbcContract is IbcReceiverBase, IbcReceiver {
*/
bool foundVersion = false;
selectedVersion = keccak256(abi.encodePacked(version)) == keccak256(abi.encodePacked(''))
? counterpartyVersion
? counterparty.version
: version;
for (uint256 i = 0; i < supportedVersions.length; i++) {
if (keccak256(abi.encodePacked(selectedVersion)) == keccak256(abi.encodePacked(supportedVersions[i]))) {
Expand All @@ -115,9 +118,9 @@ contract CustomChanIbcContract is IbcReceiverBase, IbcReceiver {
}
require(foundVersion, 'Unsupported version');
// if counterpartyVersion is not empty, then it must be the same foundVersion
if (keccak256(abi.encodePacked(counterpartyVersion)) != keccak256(abi.encodePacked(''))) {
if (keccak256(abi.encodePacked(counterparty.version)) != keccak256(abi.encodePacked(''))) {
require(
keccak256(abi.encodePacked(counterpartyVersion)) == keccak256(abi.encodePacked(selectedVersion)),
keccak256(abi.encodePacked(counterparty.version)) == keccak256(abi.encodePacked(selectedVersion)),
'Version mismatch'
);
}
Expand Down Expand Up @@ -151,11 +154,7 @@ contract CustomChanIbcContract is IbcReceiverBase, IbcReceiver {
connectedChannels.push(channelMapping);
}

function onCloseIbcChannel(
bytes32 channelId,
string calldata counterpartyPortId,
bytes32 counterpartyChannelId
) external onlyIbcDispatcher {
function onCloseIbcChannel(bytes32 channelId, string calldata, bytes32) external onlyIbcDispatcher {
// logic to determin if the channel should be closed
bool channelFound = false;
for (uint256 i = 0; i < connectedChannels.length; i++) {
Expand Down
6 changes: 3 additions & 3 deletions contracts/UniversalChanIbcContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract UniversalChanIbcContract is IbcMwUser, IbcUniversalPacketReceiver {
function sendGreet(address destPortAddr, bytes32 channelId, bytes calldata message, uint64 timeoutTimestamp) external {
IbcUniversalPacketSender(mw).sendUniversalPacket(
channelId,
Ibc.toBytes32(destPortAddr),
IbcUtils.toBytes32(destPortAddr),
message,
timeoutTimestamp
);
Expand All @@ -48,7 +48,7 @@ contract UniversalChanIbcContract is IbcMwUser, IbcUniversalPacketReceiver {
recvedPackets.push(UcPacketWithChannel(channelId, packet));
// do logic
// below is an example, the actual ackpacket data should be implemented by the contract developer
return AckPacket(true, abi.encodePacked(address(this), Ibc.toAddress(packet.srcPortAddr), 'ack-', packet.appData));
return AckPacket(true, abi.encodePacked(address(this), IbcUtils.toAddress(packet.srcPortAddr), 'ack-', packet.appData));
}

function onUniversalAcknowledgement(
Expand All @@ -60,7 +60,7 @@ contract UniversalChanIbcContract is IbcMwUser, IbcUniversalPacketReceiver {
// check onRecvUniversalPacket for the encoded ackpacket data
require(ack.data.length >= 20, 'ack data too short');
address ackSender = address(bytes20(ack.data[0:20]));
require(Ibc.toAddress(packet.destPortAddr) == ackSender, 'ack address mismatch');
require(IbcUtils.toAddress(packet.destPortAddr) == ackSender, 'ack address mismatch');
ackPackets.push(UcAckWithChannel(channelId, packet, ack));
// do logic
}
Expand Down
2 changes: 1 addition & 1 deletion lib/vibc-core-smart-contracts

0 comments on commit a099ce8

Please sign in to comment.