Skip to content

Commit

Permalink
updated contract and test (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: Lohann Paterno Coutinho Ferreira <[email protected]>
  • Loading branch information
ManojJiSharma and Lohann authored Aug 15, 2024
1 parent 83c3934 commit d9fe295
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 28 deletions.
11 changes: 2 additions & 9 deletions examples/simple/Counter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,10 @@ contract CounterTest is Test {
GmpTestTools.switchNetwork(sepoliaId);
assertEq(counter.number(), 0);

// Convert Alice address to GmpSender, passing `false` indicates
// that the sender is an EOA, not a contract
GmpSender sender = alice.toSender(false);

// Deposit funds to pay for the execution cost from Shibuya to Sepolia
sepoliaGateway.deposit{value: 1 ether}(sender, shibuyaId);
assertEq(counter.number(), 0);

// Submit a new GMP from Shibuya to Sepolia
GmpTestTools.switchNetwork(shibuyaId);
shibuyaGateway.submitMessage(address(counter), sepoliaId, 100_000, "");
uint256 deposit = shibuyaGateway.estimateMessageCost(sepoliaId, 0, 100_000);
shibuyaGateway.submitMessage{value: deposit}(address(counter), sepoliaId, 100_000, "");

// Check the counter before relaying the GMP message
GmpTestTools.switchNetwork(sepoliaId);
Expand Down
9 changes: 7 additions & 2 deletions examples/teleport-tokens/BasicERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,18 @@ contract BasicERC20 is ERC20, IGmpReceiver {
/**
* @dev Teleport tokens from `msg.sender` to `recipient` in `_recipientNetwork`
*/
function teleport(address recipient, uint256 amount) external returns (bytes32 messageID) {
function teleport(address recipient, uint256 amount) external payable returns (bytes32 messageID) {
_burn(msg.sender, amount);
bytes memory message = abi.encode(TeleportCommand({from: msg.sender, to: recipient, amount: amount}));
messageID = _trustedGateway.submitMessage(address(_recipientErc20), _recipientNetwork, MSG_GAS_LIMIT, message);
messageID = _trustedGateway.submitMessage{value: msg.value}(address(_recipientErc20), _recipientNetwork, MSG_GAS_LIMIT, message);
emit OutboundTransfer(messageID, msg.sender, recipient, amount);
}

function teleportCost(uint16 networkid, address recipient, uint256 amount) public view returns (uint256 deposit) {
bytes memory message = abi.encode(TeleportCommand({from: msg.sender, to: recipient, amount: amount}));
return _trustedGateway.estimateMessageCost(networkid, message.length, MSG_GAS_LIMIT);
}

function onGmpReceived(bytes32 id, uint128 network, bytes32 sender, bytes calldata data)
external
payable
Expand Down
22 changes: 6 additions & 16 deletions examples/teleport-tokens/BasicERC20.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,19 @@ contract GmpTestToolsTest is Test {
assertEq(address(shibuyaErc20), vm.computeCreateAddress(ALICE, 0), "unexpected shibuyaErc20 address");
assertEq(address(sepoliaErc20), vm.computeCreateAddress(BOB, 0), "unexpected sepoliaErc20 address");

///////////////////////////////////////////////////////////
// Step 3: Deposit funds to destination Gateway Contract //
///////////////////////////////////////////////////////////

// Switch to Sepolia network and Alice account
GmpTestTools.switchNetwork(SEPOLIA_NETWORK, ALICE);
// If the sender is a contract, it's address must be converted
GmpSender sender = address(shibuyaErc20).toSender(true);
// Alice deposit 1 ether to Sepolia gateway contract
SEPOLIA_GATEWAY.deposit{value: 1 ether}(sender, SHIBUYA_NETWORK);

//////////////////////////////
// Step 4: Send GMP message //
// Step 3: Send GMP message //
//////////////////////////////

// Switch to Shibuya network and Alice account
GmpTestTools.switchNetwork(SHIBUYA_NETWORK, ALICE);

// Teleport 100 tokens from Alice to to Bob's account in sepolia
// Obs: The `teleport` method internally calls `gateway.submitMessage(...)`
// Teleport 100 tokens from Alice to Bob's account in Sepolia
// Obs: The `teleport` method calls `gateway.submitMessage(...)` with value
uint256 deposit = shibuyaErc20.teleportCost(SEPOLIA_NETWORK, BOB, 100);
vm.expectEmit(false, true, false, true, address(shibuyaErc20));
emit BasicERC20.OutboundTransfer(bytes32(0), ALICE, BOB, 100);
bytes32 messageID = shibuyaErc20.teleport(BOB, 100);
bytes32 messageID = shibuyaErc20.teleport{value: deposit}(BOB, 100);

// Now with the `messageID`, Alice can check the message status in the destination gateway contract
// status 0: means the message is pending
Expand All @@ -95,7 +85,7 @@ contract GmpTestToolsTest is Test {
);

///////////////////////////////////////////////////
// Step 5: Wait Chronicles Relay the GMP message //
// Step 4: Wait Chronicles Relay the GMP message //
///////////////////////////////////////////////////

// The GMP hasn't been executed yet...
Expand Down

0 comments on commit d9fe295

Please sign in to comment.