Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete contract #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions contracts/CCQuery.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand Down
36 changes: 14 additions & 22 deletions contracts/CCQueryUC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
);
}

/**
Expand All @@ -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));
Expand All @@ -92,7 +86,6 @@ contract CCQueryUC is UniversalChanIbcApp {

return AckPacket(true, abi.encode(_ackData));
}
*/
}

/**
Expand All @@ -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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion contracts/Counter/XCounter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion contracts/Counter/XCounterUC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand All @@ -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
Expand Down