Skip to content

Commit e38cc7c

Browse files
committed
add plasma and pos test for polygon
1 parent 7183591 commit e38cc7c

File tree

6 files changed

+178
-1
lines changed

6 files changed

+178
-1
lines changed

Example29-layer2-matic/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"chai": "^4.3.4",
77
"ethereum-waffle": "^3.3.0",
88
"ethers": "^5.3.1",
9-
"hardhat": "^2.3.3"
9+
"hardhat": "^2.3.3",
10+
"@maticnetwork/maticjs": "2.0.43"
1011
}
1112
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Polygon
2+
3+
[matic官方文档](https://docs.matic.network/docs/develop/getting-started)
4+
5+
## 原理
6+
Currently, developers can use Plasma for specific state transitions for which Plasma predicates have been written such as ERC20, ERC721, asset swaps or other custom predicates. For arbitrary state transitions, they can use PoS. Or both! This is made possible by Matic's hybrid construction.
7+
POS chain
8+
Plasma chain (Plasma Bridge)
9+
10+
A bridge is basically a set of contracts that help in moving assets from the root chain to the child chain.
11+
However, there are certain restrictions on the child token and there is a 7-day withdrawal period associated with all exits/withdraws from Matic to Ethereum on the Plasma bridge
12+
7天延迟从matic撤回主网
13+
14+
The PoS Bridge is more flexible and features faster withdrawals.
15+
pos更快
16+
secured by a robust set of external validators.
17+
18+
两部分 主链/ 子链
19+
20+
L1- L2 交互
21+
1 状态同步 验证着周期提供所有交易的hash到主链, 检查点用于验证发生在matic上的任何交易,
22+
2
23+
马蹄的验证者需要持续监控链上合约 称为状态发送者
24+
25+
26+
27+
28+
## 参考链接
29+
30+
https://www.yuque.com/docs/share/8e737364-c380-418e-af21-0f07095fe900
31+
32+
使用教程: https://cloud.tencent.com/developer/article/1828250
33+
34+
hardhat: https://docs.matic.network/docs/develop/hardhat/
35+
36+
https://medium.com/pinata/how-to-create-layer-2-nfts-with-polygon-and-ipfs-aef998ff8ef2

Example29-layer2-matic/readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ https://www.yuque.com/docs/share/8e737364-c380-418e-af21-0f07095fe900
2323
hardhat: https://docs.matic.network/docs/develop/hardhat/
2424

2525
https://medium.com/pinata/how-to-create-layer-2-nfts-with-polygon-and-ipfs-aef998ff8ef2
26+
27+
https://github.com/maticnetwork/matic.js/tree/master/examples maticjs
28+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
const { waffle } = require("hardhat");
3+
4+
const utils = require('./utils')
5+
6+
async function execute() {
7+
const { matic, network } = await utils.getMaticClient()
8+
const { from } = utils.getAccount()
9+
10+
const token = network.Main.Contracts.Tokens.MaticWeth
11+
const amount = matic.web3Client.web3.utils.toWei('1.567')
12+
13+
// deposit
14+
await matic.depositEther(token, from, amount).then((res) => {
15+
console.log("deposit hash: ", res.transactionHash)
16+
})
17+
//When a token is deposited from Ethereum to Matic, a process called state sync mechanism comes into play that eventually mints the tokens for the user on the Matic chain
18+
19+
// transfer
20+
const recipient = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";
21+
22+
const amount1 = matic.web3Client.web3.utils.toWei("1.23");
23+
24+
await matic.transferEther(recipient, amount1, { from, parent: false })
25+
.then((res) => {
26+
console.log("Transfer hash: ", res.transactionHash);
27+
});
28+
29+
//withdraw
30+
// 1 burn
31+
const amount2 = matic.web3Client.web3.utils.toWei("5.678");
32+
33+
await matic.startWithdraw(token, amount2, { from }).then((res) => {
34+
console.log("Burn hash: ", res.transactionHash);
35+
});
36+
37+
// wait 30m
38+
39+
//This function can be called only after the checkpoint is included in the main chain
40+
// 2 confirm-withdraw
41+
const txHash = "<>";
42+
await matic.withdraw(txHash, { from, gas: "7000000" }).then((res) => {
43+
console.log("Confirm withdraw hash: ", res.transactionHash);
44+
});
45+
46+
//Process Exit
47+
await matic.processExits(token, { from, gas: 7000000 }).then((res) => {
48+
console.log("Exit hash: ", res.transactionHash);
49+
});
50+
51+
52+
53+
54+
}
55+
56+
execute().then(_ => process.exit(0))
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
const { waffle } = require("hardhat");
3+
4+
async function main() {
5+
const [owner] = await ethers.getSigners();
6+
console.log("owner address ",owner.address)
7+
let result = await owner.getBalance();
8+
console.log("balance ", ethers.utils.formatEther(result))
9+
10+
const parentProvider = 'https://goerli.infura.io/v3/0aae8358bfe04803b8e75bb4755eaf07'
11+
const maticProvider = 'https://polygon-mumbai.infura.io/v3/0aae8358bfe04803b8e75bb4755eaf07'
12+
// for mumbai testnet
13+
const maticPOSClient = new MaticPOSClient({
14+
network: "testnet",
15+
version: "mumbai",
16+
parentProvider: parentProvider,
17+
maticProvider: maticProvider});
18+
19+
//deposit
20+
await maticPOSClient.depositEtherForUser(from, amount, {
21+
from,
22+
gasPrice: "10000000000",
23+
});
24+
// takes about ~5-7 minutes.
25+
26+
//burn
27+
await maticPOSClient.burnERC20(childToken, amount, { from });
28+
29+
30+
await maticPOSClient.exitERC20(burnTxHash, { from });
31+
}
32+
33+
// We recommend this pattern to be able to use async/await everywhere
34+
// and properly handle errors.
35+
main()
36+
.then(() => process.exit(0))
37+
.catch(error => {
38+
console.error(error);
39+
process.exit(1);
40+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const bn = require("bn.js");
2+
const HDWalletProvider = require("@truffle/hdwallet-provider");
3+
4+
const Network = require("@maticnetwork/meta/network");
5+
const Matic = require("@maticnetwork/maticjs").default;
6+
7+
const SCALING_FACTOR = new bn(10).pow(new bn(18));
8+
9+
async function getMaticClient(_network = "testnet", _version = "mumbai") {
10+
const network = new Network(_network, _version);
11+
const { from } = getAccount();
12+
const matic = new Matic({
13+
network: _network,
14+
version: _version,
15+
parentProvider: new HDWalletProvider(
16+
process.env.PRIVATE_KEY,
17+
network.Main.RPC
18+
),
19+
maticProvider: new HDWalletProvider(
20+
process.env.PRIVATE_KEY,
21+
network.Matic.RPC
22+
),
23+
parentDefaultOptions: { from },
24+
maticDefaultOptions: { from },
25+
});
26+
await matic.initialize();
27+
return { matic, network };
28+
}
29+
30+
function getAccount() {
31+
if (!process.env.PRIVATE_KEY || !process.env.FROM) {
32+
throw new Error("Please set the PRIVATE_KEY/FROM env vars");
33+
}
34+
return { privateKey: process.env.PRIVATE_KEY, from: process.env.FROM };
35+
}
36+
37+
module.exports = {
38+
SCALING_FACTOR,
39+
getMaticClient,
40+
getAccount,
41+
};

0 commit comments

Comments
 (0)