├── .env.example
├── .gitignore
├── README.MD
├── package.json
├── public
└── index.html
├── src
├── components
│ ├── App.js
│ ├── Callback.js
│ ├── ContractCall.js
│ ├── Home.js
│ ├── Info.js
│ ├── Loading.js
│ ├── Login.js
│ └── SendTransaction.js
├── contract
│ ├── HelloWorld.sol
│ └── abi.js
├── index.js
├── magic.js
└── styles.css
└── yarn.lock
/.env.example:
--------------------------------------------------------------------------------
1 | REACT_APP_MAGIC_PUBLISHABLE_KEY=pk_live_
2 | REACT_APP_ALCHEMY_API_KEY=abc123
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .env
2 | /node_modules
--------------------------------------------------------------------------------
/README.MD:
--------------------------------------------------------------------------------
1 | # Resources
2 | - [GitHub Repo](https://github.com/magiclabs/magic-polygon)
3 | - [Demo](https://magic-polygon.vercel.app/login)
4 |
5 | # Quick Start
6 |
7 | ```
8 | $ git clone https://github.com/magiclabs/magic-polygon.git
9 | $ cd magic-polygon
10 | $ mv .env.local .env // enter your Magic Publishable Key (from https://dashboard.magic.link)
11 | $ yarn install
12 | $ yarn start
13 | ```
14 |
15 | # Introduction
16 |
17 | With the rising gas costs on Ethereum, many developers are looking towards scaling solutions to help with both improved transaction speed, as well as cheaper gas costs for users. Polygon (formerly Matic) is one such solution.
18 |
19 | Polygon is a protocol which enables connecting Ethereum-compatible blockchains, and is also a Proof of Stake side-chain scaling solution for Ethereum. The side-chain runs alongside Ethereum's blockchain, and processes transactions before finalizing them on Ethereum.
20 |
21 | With Magic, developers can connect to Polygon by simply specifying the network URL when initiating a Magic instance. This guide will show how you can create a web3-enabled app, allow users to switch between Ethereum and Polygon networks, call smart contracts, and send transactions.
22 |
23 | _Note: `ETH` is the native token to Ethereum, `MATIC` is the native token to Polygon._
24 |
25 | # Tutorial
26 |
27 | _Note: this app was bootstrapped with the `npx make-magic` React template._
28 |
29 | ## Connecting to Ethereum / Polgyon
30 |
31 | In `magic.js`, we will need two `Magic` and two `Web3` instances, one for each network, since we're allowing users to switch between the two. If you're only interested in connecting to Polygon, then only one instance of `Magic` and `Web3` should be created. We also are adding `magicEthereum.network = "ethereum"` to be able to identify the Magic network we're creating.
32 |
33 | You’ll use the same API key for both `Magic` instances so that the user’s public address does not change.
34 |
35 | ```js
36 | import { Magic } from 'magic-sdk';
37 | import Web3 from 'web3';
38 |
39 | /**
40 | * Configure Polygon Connection
41 | */
42 | const polygonNodeOptions = {
43 | rpcUrl: 'https://rpc-mumbai.matic.today',
44 | chainId: 80001,
45 | };
46 |
47 | export const magicMatic = new Magic(
48 | process.env.REACT_APP_MAGIC_PUBLISHABLE_KEY,
49 | {
50 | network: polygonNodeOptions,
51 | },
52 | );
53 | magicMatic.network = 'matic';
54 |
55 | export const maticWeb3 = new Web3(magicMatic.rpcProvider);
56 |
57 | // Connect to Ethereum (Goerli Testnet)
58 | export const magicEthereum = new Magic(
59 | process.env.REACT_APP_MAGIC_PUBLISHABLE_KEY,
60 | {
61 | network: 'goerli',
62 | },
63 | );
64 | magicEthereum.network = 'ethereum';
65 |
66 | export const ethWeb3 = new Web3(magicEthereum.rpcProvider);
67 |
68 | ```
69 |
70 | ## Switching Between Networks
71 |
72 | Users are able to switch between the Ethereum and Polygon networks with the `select` element dropdown list. Since one `Magic` instance points towards Ethereum, and the other Polygon, we simply update the instance that we’re using for our app based on whichever network the user selects.
73 |
74 | ```js
75 | import { magicEthereum, magicMatic, ethWeb3, maticWeb3 } from "../magic";
76 |
77 | const handleChangeNetwork = (e) => {
78 | e.target.value === 'ethereum' ? setMagic(magicEthereum) : setMagic(magicMatic);
79 | fetchBalance(userMetadata.publicAddress);
80 | fetchContractMessage();
81 | }
82 |
83 | return (
84 |
85 |
89 |
90 | )
91 | ```
92 |
93 | ## Viewing User Balance
94 |
95 | A user's public address will be the same on both Ethereum and Polygon (as long as you are using the same API key for each instance) so a simple `web3.eth.getBalance` call is all that is needed for either network. Because the native token of Ethereum is `ETH`, and for Polygon is `MATIC`, we're displaying the appropriate token symbol based on the network we're connected to.
96 |
97 | ```js
98 | const fetchBalance = (address) => {
99 | web3.eth.getBalance(address).then(bal => setBalance(web3.utils.fromWei(bal)))
100 | }
101 |
102 | return (
103 |
107 | )
108 | ```
109 |
110 | ## Send Transaction
111 |
112 | Sending a transaction is also very simple and the same for either network you're connected to. All that's needed is to provide an amount to send, and `from` and `to` addresses. If no `gas` or `gasPrice` are explicitly passed in, the gas limit and price will be calculated automatically. Otherwise, the values passed in will be used.
113 |
114 | ```js
115 | const web3 = magic.network === "ethereum" ? ethWeb3 : maticWeb3;
116 |
117 | const sendTransaction = async () => {
118 | if (!toAddress || !amount) return;
119 | const receipt = await web3.eth.sendTransaction({
120 | from: publicAddress,
121 | to: toAddress,
122 | value: web3.utils.toWei(amount)
123 | });
124 | }
125 |
126 | return (
127 |