generated from open-ibc/ibc-app-solidity-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniversalChanIbcApp.sol
97 lines (87 loc) · 3.86 KB
/
UniversalChanIbcApp.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
import {UniversalPacket, AckPacket, IbcUtils} from "@open-ibc/vibc-core-smart-contracts/contracts/libs/Ibc.sol";
import {
IbcMwUser,
IbcUniversalPacketReceiver,
IbcUniversalPacketSender
} from "@open-ibc/vibc-core-smart-contracts/contracts/interfaces/IbcMiddleware.sol";
// UniversalChanIbcApp is a contract that can be used as a base contract
// for IBC-enabled contracts that send packets over the universal channel.
contract UniversalChanIbcApp is IbcMwUser, IbcUniversalPacketReceiver {
struct UcPacketWithChannel {
bytes32 channelId;
UniversalPacket packet;
}
struct UcAckWithChannel {
bytes32 channelId;
UniversalPacket packet;
AckPacket ack;
}
// received packet as chain B
UcPacketWithChannel[] public recvedPackets;
// received ack packet as chain A
UcAckWithChannel[] public ackPackets;
// received timeout packet as chain A
UcPacketWithChannel[] public timeoutPackets;
constructor(address _middleware) IbcMwUser(_middleware) {}
/**
* @dev Implement a function to send a packet that calls the IbcUniversalPacketSender(mw).sendUniversalPacket function
* It has the following function handle:
* function sendUniversalPacket(
* bytes32 channelId,
* bytes32 destPortAddr,
* bytes calldata appData,
* uint64 timeoutTimestamp
* ) external;
*/
/**
* @dev Packet lifecycle callback that implements packet receipt logic and returns and acknowledgement packet.
* MUST be overriden by the inheriting contract.
*
* @param channelId the ID of the channel (locally) the packet was received on.
* @param packet the Universal packet encoded by the source and relayed by the relayer.
*/
function onRecvUniversalPacket(bytes32 channelId, UniversalPacket calldata packet)
external
virtual
onlyIbcMw
returns (AckPacket memory ackPacket)
{
recvedPackets.push(UcPacketWithChannel(channelId, packet));
// 1. decode the packet.data
// 2. do logic
// 3. encode the ack packet (encoding format should be agreed between the two applications)
// below is an example, the actual ackpacket data should be implemented by the contract developer
return AckPacket(
true, abi.encodePacked(address(this), IbcUtils.toAddress(packet.srcPortAddr), "ack-", packet.appData)
);
}
/**
* @dev Packet lifecycle callback that implements packet acknowledgment logic.
* MUST be overriden by the inheriting contract.
* @param channelId the ID of the channel (locally) the ack was received on.
* @param packet the Universal packet encoded by the source and relayed by the relayer.
* @param ack the acknowledgment packet encoded by the destination and relayed by the relayer.
*/
function onUniversalAcknowledgement(bytes32 channelId, UniversalPacket memory packet, AckPacket calldata ack)
external
virtual
onlyIbcMw
{
ackPackets.push(UcAckWithChannel(channelId, packet, ack));
// 1. decode the ack.data
// 2. do logic
}
/**
* @dev Packet lifecycle callback that implements packet receipt logic and return and acknowledgement packet.
* MUST be overriden by the inheriting contract.
* NOT SUPPORTED YET
* @param channelId the ID of the channel (locally) the timeout was submitted on.
* @param packet the Universal packet encoded by the counterparty and relayed by the relayer
*/
function onTimeoutUniversalPacket(bytes32 channelId, UniversalPacket calldata packet) external virtual onlyIbcMw {
timeoutPackets.push(UcPacketWithChannel(channelId, packet));
// do logic
}
}