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

Receive token #54

Open
wants to merge 2 commits 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
193 changes: 193 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,196 @@
# Interchain Connection between Fuji C-Chain and MaGGA L1 Chain

Important variables:

```

MaGGA_BLOCKCHAIN_ID = 0x02f8b60b4d4070cf62e67802fe6532d4fcfc6582b4048d040ed6cfc7247d86d8
MaGGA_RPC = "https://subnets.avax.network/gaming/testnet/rpc"

FUJI_C_BLOCKCHAIN_ID = 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
FUJI_RPC = "https://api.avax-test.network/ext/bc/C/rpc"

```

# Create simple interchain messaging between Fuji C-Chain and MaGGA L1 Chain

1. Set up privat the privat key of your crypto wallet and save it as an environmental variable:

```
export PK=YOUR_PRIVATE_KEY
```


2. Deploy Sender Contract on C chain

```
forge create --rpc-url fuji-c --private-key $PK contracts/MaGGA/senderMaGGA.sol:SenderOnCChain
```


The output should look like this:

```
[⠊] Compiling...
[⠢] Compiling 2 files with Solc 0.8.18
[⠆] Solc 0.8.18 finished in 158.51ms
Compiler run successful!
Deployer: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Deployed to: 0xAAASENDER_CONTRACT_ADDRESS888
Transaction hash: 0x48a1ffaa8aa8011f842147a908ff35a1ebfb75a5a07eb37ae96a4cc8d7feafd7
```

3. If you are using Avacloud, authorize the Sender Contract on your L1 Blockchain.

4. Save the adress where the smart constract was deployed to:

```
export SENDER_CONTRACT_ADDRESS=0xAAASENDER_CONTRACT_ADDRESS888
```


5. Deploy Receiver Contract on MaGGA chain:

```
forge create --rpc-url MaGGA --private-key $PK contracts/MaGGA/receiverMaGGA.sol:ReceiverOnSubnet
```

The output should look like this:

```
[⠊] Compiling...
[⠒] Compiling 2 files with Solc 0.8.18
[⠢] Solc 0.8.18 finished in 81.53ms
Compiler run successful!
Deployer: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Deployed to: 0xYYYRECEIVER_CONTRACT_ADDRESS444
Transaction hash: 0xcde7873e9e3c68fb00a2ad6644dceb64a01a41941da46de5a0f559d6d70a1638
```

6. If you are using Avacloud, authorize the Sender Contract on your L1 Blockchain.

7. Save the adress where the smart constract was deployed to, executing the :

```
export RECEIVER_CONTRACT_ADDRESS=0xYYYRECEIVER_CONTRACT_ADDRESS444
```

8. Send Transaction from C-Chain to MaGGA L1 Chain:

```
cast send --rpc-url MaGGA --private-key $PK $SENDER_CONTRACT_ADDRESS "sendMessage(address,string)" $RECEIVER_CONTRACT_ADDRESS "Hello MaGGA!"
```

9. Read variable on MaGGA L1 Chain to make sure, that the right message was received:

```
cast call --rpc-url MaGGA $RECEIVER_CONTRACT_ADDRESS "lastMessage()(string)"
```


# Create simple token creation in MaGGA L1 Chain depending on a message from the Fuji C-Chain

1. If you do not have already set up a privat key, please refer to Step 1 of "Create simple interchain messaging between Fuji C-Chain and MaGGA L1 Chain":

```
export PK=YOUR_PRIVATE_KEY
```


2. Deploy the token creation contract on Fuji C-chain:

```
forge create --rpc-url fuji-c --private-key $PK contracts/MaGGA/createToken.sol:MyToken
```


The output should look like this:

```
[⠊] Compiling...
[⠢] Compiling 2 files with Solc 0.8.18
[⠆] Solc 0.8.18 finished in 158.51ms
Compiler run successful!
Deployer: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Deployed to: 0xAAATOKEN_CREATION_CONTRACT_ADDRESS888
Transaction hash: 0x48a1ffaa8aa8011f842147a908ff35a1ebfb75a5a07eb37ae96a4cc8d7feafd7
```

3. If you are using Avacloud, authorize the token creation smart contract (i.e. 0xAAATOKEN_CREATION_CONTRACT_ADDRESS888) on your L1 Blockchain.

4. Save the adress where the token creation smart contract was deployed to as an environmental variable:

```
export TOKEN_CREATION_CONTRACT_ADDRESS=0xAAATOKEN_CREATION_CONTRACT_ADDRESS888
```

5. Deploy Sender Contract on C chain

```
forge create --rpc-url fuji-c --private-key $PK contracts/MaGGA/sendAmount.sol:SendAmountOnCChain
```


The output should look like this:

```
[⠊] Compiling...
[⠢] Compiling 2 files with Solc 0.8.18
[⠆] Solc 0.8.18 finished in 158.51ms
Compiler run successful!
Deployer: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Deployed to: 0xAAASENDER_CONTRACT_ADDRESS888
Transaction hash: 0x48a1ffaa8aa8011f842147a908ff35a1ebfb75a5a07eb37ae96a4cc8d7feafd7
```

6. If you are using Avacloud, authorize the sender smart contract (i.e. 0xAAASENDER_CONTRACT_ADDRESS888) on your L1 Blockchain.

7. Save the adress where the sender smart contract was deployed to as an environmental variable:

```
export SENDER_CONTRACT_ADDRESS=0xAAASENDER_CONTRACT_ADDRESS888
```

8. Deploy Receiver Contract on MaGGA chain:

```
forge create --rpc-url MaGGA --private-key $PK contracts/MaGGA/receiveMaGGATokens.sol:ReceiveMaGGATokens
```

The output should look like this:

```
[⠊] Compiling...
[⠒] Compiling 2 files with Solc 0.8.18
[⠢] Solc 0.8.18 finished in 81.53ms
Compiler run successful!
Deployer: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Deployed to: 0xYYYRECEIVER_CONTRACT_ADDRESS444
Transaction hash: 0xcde7873e9e3c68fb00a2ad6644dceb64a01a41941da46de5a0f559d6d70a1638
```

9. If you are using Avacloud, authorize the Receiver Contract (i.e. 0xYYYRECEIVER_CONTRACT_ADDRESS444) on your L1 Blockchain.

10. Save the adress where the receiver smart constract was deployed to as an environmental varaible:

```
export RECEIVER_CONTRACT_ADDRESS=0xYYYRECEIVER_CONTRACT_ADDRESS444
```

11. Send Transaction from C-Chain to MaGGA L1 Chain:

```
cast send --rpc-url MaGGA --private-key $PK $SENDER_CONTRACT_ADDRESS "sendAmount(address,uint)" $RECEIVER_CONTRACT_ADDRESS 100
```

12. Read variable on MaGGA L1 Chain to make sure, that the right message was received:

```
cast call --rpc-url MaGGA $RECEIVER_CONTRACT_ADDRESS "lastAmount()(uint)"
```



# Avalanche Starter Kit

This starter kit will get you started with developing solidity smart contract dApps on the C-Chain or on an Avalanche L1. It provides all tools to build cross-L1 dApps using Teleporter. It includes:
Expand Down
19 changes: 19 additions & 0 deletions contracts/MaGGA/createToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT
// Compatible with OpenZeppelin Contracts ^5.0.0
pragma solidity ^0.8.20;

import "@openzeppelin/[email protected]/token/ERC20/ERC20.sol";
import "@openzeppelin/[email protected]/access/Ownable.sol";
import "@openzeppelin/[email protected]/token/ERC20/extensions/ERC20Permit.sol";

contract MyToken is ERC20, Ownable, ERC20Permit {
constructor(address initialOwner)
ERC20("MyToken", "MTK")
Ownable(initialOwner)
ERC20Permit("MyToken")
{}

function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}
30 changes: 30 additions & 0 deletions contracts/MaGGA/receiveMaGGATokens.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity ^0.8.18;

import "@teleporter/ITeleporterMessenger.sol";
import "@teleporter/ITeleporterReceiver.sol";
import "./createToken.sol";

contract ReceiveMaGGATokens is ITeleporterReceiver {
ITeleporterMessenger public immutable messenger = ITeleporterMessenger(0x253b2784c75e510dD0fF1da844684a1aC0aa5fcf);
MyToken public tokenNetwork;
uint public lastAmount;
address public receiver;
constructor(address token, address _receiver) {
tokenNetwork = new MyToken(token);
receiver = _receiver;
}
function receiveTeleporterMessage(bytes32, address, uint calldata amount) external {
// Only the Teleporter receiver can deliver a message.
require(msg.sender == address(messenger), "ReceiverOnSubnet: unauthorized TeleporterMessenger");

// Store the message.
lastAmount = abi.decode(amount, (uint));
tokenNetwork.mint(receiver, lastAmount);

}
}
23 changes: 23 additions & 0 deletions contracts/MaGGA/receiverMaGGA.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity ^0.8.18;

import "@teleporter/ITeleporterMessenger.sol";
import "@teleporter/ITeleporterReceiver.sol";

contract ReceiverOnSubnet is ITeleporterReceiver {
ITeleporterMessenger public immutable messenger = ITeleporterMessenger(0x253b2784c75e510dD0fF1da844684a1aC0aa5fcf);

string public lastMessage;

function receiveTeleporterMessage(bytes32, address, bytes calldata message) external {
// Only the Teleporter receiver can deliver a message.
require(msg.sender == address(messenger), "ReceiverOnSubnet: unauthorized TeleporterMessenger");

// Store the message.
lastMessage = abi.decode(message, (string));
}
}
29 changes: 29 additions & 0 deletions contracts/MaGGA/sendAmount.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity ^0.8.18;

import "@teleporter/ITeleporterMessenger.sol";

contract SendAmountOnCChain {
ITeleporterMessenger public immutable messenger = ITeleporterMessenger(0x253b2784c75e510dD0fF1da844684a1aC0aa5fcf);

/**
* @dev Sends a amount to another chain.
*/
function sendAmount(address destinationAddress, uint calldata amount) external {
messenger.sendCrossChainMessage(
TeleporterMessageInput({
// Replace with blockchainID of your Subnet (see instructions in Readme)
destinationBlockchainID: 0x02f8b60b4d4070cf62e67802fe6532d4fcfc6582b4048d040ed6cfc7247d86d8,
destinationAddress: destinationAddress,
feeInfo: TeleporterFeeInfo({feeTokenAddress: address(0), amount: 0}),
requiredGasLimit: 100000,
allowedRelayerAddresses: new address[](0),
amount: abi.encode(amount)
})
);
}
}
29 changes: 29 additions & 0 deletions contracts/MaGGA/senderMaGGA.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity ^0.8.18;

import "@teleporter/ITeleporterMessenger.sol";

contract SenderOnCChain {
ITeleporterMessenger public immutable messenger = ITeleporterMessenger(0x253b2784c75e510dD0fF1da844684a1aC0aa5fcf);

/**
* @dev Sends a message to another chain.
*/
function sendMessage(address destinationAddress, string calldata message) external {
messenger.sendCrossChainMessage(
TeleporterMessageInput({
// Replace with blockchainID of your Subnet (see instructions in Readme)
destinationBlockchainID: 0x02f8b60b4d4070cf62e67802fe6532d4fcfc6582b4048d040ed6cfc7247d86d8,
destinationAddress: destinationAddress,
feeInfo: TeleporterFeeInfo({feeTokenAddress: address(0), amount: 0}),
requiredGasLimit: 100000,
allowedRelayerAddresses: new address[](0),
message: abi.encode(message)
})
);
}
}
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ myblockchain = "http://localhost:9650/ext/bc/myblockchain/rpc"
fuji-c = "https://api.avax-test.network/ext/bc/C/rpc"
dispatch = "https://subnets.avax.network/dispatch/testnet/rpc"
mysubnet = "http://localhost:9650/ext/bc/mysubnet/rpc"
MaGGA = "https://subnets.avax.network/gaming/testnet/rpc"


# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options