diff --git a/contracts/CCQuery.sol b/contracts/CCQuery.sol index f0708aa..16c0245 100644 --- a/contracts/CCQuery.sol +++ b/contracts/CCQuery.sol @@ -8,6 +8,9 @@ import "@openzeppelin/contracts/utils/Strings.sol"; contract CCQuery is CustomChanIbcApp { // app specific state uint64 private counter; + + uint64 public immutable CONST_TIME = 1e9; + mapping(uint64 => address) public counterMap; event LogQuery(address indexed caller, string query, uint64 counter); @@ -67,13 +70,13 @@ contract CCQuery is CustomChanIbcApp { function sendPacket(bytes32 channelId, uint64 timeoutSeconds) external { // NOTE: This should be never used // encoding the caller address to update counterMap on destination chain - // bytes memory payload = abi.encode(msg.sender, "crossChainQuery"); + bytes memory payload = abi.encode(msg.sender, "crossChainQuery"); // // setting the timeout timestamp at 10h from now - // uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * 1000000000); + uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * CONST_TIME); // // // calling the Dispatcher to send the packet - // dispatcher.sendPacket(channelId, payload, timeoutTimestamp); + dispatcher.sendPacket(channelId, payload, timeoutTimestamp); } /** diff --git a/contracts/CCQueryUC.sol b/contracts/CCQueryUC.sol index c1d3102..5fee5b7 100644 --- a/contracts/CCQueryUC.sol +++ b/contracts/CCQueryUC.sol @@ -8,6 +8,7 @@ import "@openzeppelin/contracts/utils/Strings.sol"; contract CCQueryUC is UniversalChanIbcApp { // app specific state uint64 private counter; + uint64 public immutable CONST_TIME = 1e9; mapping(uint64 => address) public counterMap; mapping(address => bool) public addressMap; @@ -41,15 +42,13 @@ contract CCQueryUC is UniversalChanIbcApp { * @param timeoutSeconds The timeout in seconds (relative). */ function sendUniversalPacket(address destPortAddr, bytes32 channelId, uint64 timeoutSeconds) external { - // TODO - Implement sendUniversalPacket to send a packet which will be received by the other chain - // The packet should contain the caller's address and a query string - // See onRecvUniversalPacket for the expected packet format in https://forum.polymerlabs.org/t/challenge-3-cross-contract-query-with-polymer/475 - // Steps: - // 1. Encode the caller's address and the query string into a payload - // 2. Set the timeout timestamp at 10h from now - // 3. Call the IbcUniversalPacketSender to send the packet - - // Example of how to properly encode, set timestamp and send a packet can be found in XCounterUC.sol + bytes memory payload = abi.encode(msg.sender, "crossChainQuery"); + + uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * CONST_TIME); + + IbcUniversalPacketSender(mw).sendUniversalPacket( + channelId, IbcUtils.toBytes32(destPortAddr), payload, timeoutTimestamp + ); } /** @@ -65,12 +64,7 @@ contract CCQueryUC is UniversalChanIbcApp { onlyIbcMw returns (AckPacket memory ackPacket) { - // You can leave the following function empty - // This contract will need to be sending and acknowledging packets and not receiving them to complete the challenge - // The reference implemention of onRecvUniversalPacket on the base contract you will be calling is below - - /* - recvedPackets.push(UcPacketWithChannel(channelId, packet)); + recvedPackets.push(UcPacketWithChannel(channelId, packet)); uint64 _counter = getCounter(); (address _caller, string memory _query) = abi.decode(packet.appData, (address, string)); @@ -92,7 +86,6 @@ contract CCQueryUC is UniversalChanIbcApp { return AckPacket(true, abi.encode(_ackData)); } - */ } /** @@ -108,13 +101,12 @@ contract CCQueryUC is UniversalChanIbcApp { override onlyIbcMw { - // TODO - Implement onUniversalAcknowledgement to handle the received acknowledgment packet - // The packet should contain the secret message from the Base Contract at address: 0x528f7971cE3FF4198c3e6314AA223C83C7755bf7 - // Steps: - // 1. Decode the counter from the ack packet - // 2. Emit a LogAcknowledgement event with the message + ackPackets.push(UcAckWithChannel(channelId, packet, ack)); + + // decode the counter from the ack packet + (string memory _secretMessage) = abi.decode(ack.data, (string)); - // An example of how to properly decode and handle an ack packet can be found in XCounterUC.sol + emit LogAcknowledgement(_secretMessage); } /** diff --git a/contracts/Counter/XCounter.sol b/contracts/Counter/XCounter.sol index e1d7f62..90fccd5 100644 --- a/contracts/Counter/XCounter.sol +++ b/contracts/Counter/XCounter.sol @@ -9,6 +9,7 @@ contract XCounter is CustomChanIbcApp { uint64 public counter; mapping (uint64 => address) public counterMap; + uint64 public immutable CONST_TIME = 1e9; constructor(IbcDispatcher _dispatcher) CustomChanIbcApp(_dispatcher) {} @@ -37,7 +38,7 @@ contract XCounter is CustomChanIbcApp { bytes memory payload = abi.encode(msg.sender); // setting the timeout timestamp at 10h from now - uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * 1000000000); + uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * CONST_TIME); // calling the Dispatcher to send the packet dispatcher.sendPacket(channelId, payload, timeoutTimestamp); diff --git a/contracts/Counter/XCounterUC.sol b/contracts/Counter/XCounterUC.sol index 3859a17..a03ed37 100644 --- a/contracts/Counter/XCounterUC.sol +++ b/contracts/Counter/XCounterUC.sol @@ -8,6 +8,7 @@ contract XCounterUC is UniversalChanIbcApp { // application specific state uint64 public counter; mapping(uint64 => address) public counterMap; + uint64 public immutable CONST_TIME = 1e9; constructor(address _middleware) UniversalChanIbcApp(_middleware) {} @@ -32,7 +33,7 @@ contract XCounterUC is UniversalChanIbcApp { increment(); bytes memory payload = abi.encode(msg.sender, counter); - uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * 1000000000); + uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * CONST_TIME); IbcUniversalPacketSender(mw).sendUniversalPacket( channelId, IbcUtils.toBytes32(destPortAddr), payload, timeoutTimestamp