0%

Giwa sepolia跨链教程

Docs:https://docs.giwa.io/undefined/en/get-started/bridging/eth#set-up-development-environment

英文好的建议直接看原文。

预先安装:

node pnpm

node的安装就不再演示,我们来安装pnpm

1
npm install -g pnpm@latest-10

请注意版本兼容问题,确认node版本在18以上。

Node.js pnpm 8 pnpm 9 pnpm 10
Node.js 14
Node.js 16 ✔️
Node.js 18 ✔️ ✔️ ✔️
Node.js 20 ✔️ ✔️ ✔️
Node.js 22 ✔️ ✔️ ✔️

初始化项目:

1
2
3
pnpm init
pnpm add -D tsx @types/node
pnpm add viem

设置私钥:

打开 Git Bash(安装 Git for Windows 时会附带)

1
export TEST_PRIVATE_KEY=0x

无法读取,我换用另外一种方法

1
pnpm add dotenv

创建文件.env

1
TEST_PRIVATE_KEY=0x私钥

搞毛线,设置失败,直接把私钥写在代码里面用明文得了

配置链客户端:

新建文件src/config.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {defineChain, createPublicClient, http, createWalletClient} from "viem";
import {privateKeyToAccount} from "viem/accounts";
import {publicActionsL1, publicActionsL2, walletActionsL1, walletActionsL2} from "viem/op-stack";
import {sepolia} from "viem/chains";

// GIWA Sepolia chain config
export const giwaSepolia = defineChain({
id: 91342,
name: 'Giwa Sepolia',
nativeCurrency: { name: 'Sepolia Ether', symbol: 'ETH', decimals: 18 },
rpcUrls: {
default: {
http: ['https://sepolia-rpc.giwa.io'],
},
},
contracts: {
multicall3: {
address: '0xcA11bde05977b3631167028862bE2a173976CA11',
},
l2OutputOracle: {},
disputeGameFactory: {
[sepolia.id]: {
address: '0x37347caB2afaa49B776372279143D71ad1f354F6',
},
},
portal: {
[sepolia.id]: {
address: '0x956962C34687A954e611A83619ABaA37Ce6bC78A',
},
},
l1StandardBridge: {
[sepolia.id]: {
address: '0x77b2ffc0F57598cAe1DB76cb398059cF5d10A7E7',
},
},
},
testnet: true,
});

// Prepare the wallet
export const PRIVATE_KEY = "0x这里写私钥,一定要改";
export const account = privateKeyToAccount(PRIVATE_KEY);

// Client for reading Ethereum Sepolia chain data
export const publicClientL1 = createPublicClient({
chain: sepolia,
transport: http(),
}).extend(publicActionsL1())

// Client for sending transactions on Ethereum Sepolia
export const walletClientL1 = createWalletClient({
account,
chain: sepolia,
transport: http(),
}).extend(walletActionsL1());

// Client for reading GIWA Sepolia chain data
export const publicClientL2 = createPublicClient({
chain: giwaSepolia,
transport: http(),
}).extend(publicActionsL2());

// Client for sending transactions on GIWA Sepolia
export const walletClientL2 = createWalletClient({
account,
chain: giwaSepolia,
transport: http(),
}).extend(walletActionsL2());

中间唯一有中文的地方写私钥,可以新建1个钱包,记得转一点Sepolia ETH进去。

我们进行的是跨链操作,从Sepolia ETH发送到Giwa Sepolia网络。

质押ETH:

新建文件src/deposit_eth.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import {publicClientL1, publicClientL2, account, walletClientL1} from './config';
import {formatEther, parseEther} from "viem";
import {getL2TransactionHashes} from "viem/op-stack";

async function main() {
// Before bridging to GIWA, check your ETH balance on your Layer 1 wallet.
const l1Balance = await publicClientL1.getBalance({ address: account.address });
console.log(`L1 Balance: ${formatEther(l1Balance)} ETH`);

// Build the params to send a deposit transaction on Layer 1.
const depositArgs = await publicClientL2.buildDepositTransaction({
mint: parseEther("0.001"),
to: account.address,
chain: undefined
});

// Send the deposit transaction on Layer 1.
// In this step, your ETH is sent to the OptimismPortal contract.
// @ts-ignore
const depositHash = await walletClientL1.depositTransaction(depositArgs);
console.log(`Deposit transaction hash on L1: ${depositHash}`);

// Wait until the Layer 1 transaction above is fully processed.
const depositReceipt = await publicClientL1.waitForTransactionReceipt({ hash: depositHash });
console.log('L1 transaction confirmed:', depositReceipt);

// From the Layer 1 receipt, pre-compute the hash of the corresponding Layer 2 deposit transaction.
const [l2Hash] = getL2TransactionHashes(depositReceipt);
console.log(`Corresponding L2 transaction hash: ${l2Hash}`);

// Wait until the computed Layer 2 deposit transaction is processed.
// This usually takes about 1–3 minutes.
const l2Receipt = await publicClientL2.waitForTransactionReceipt({
hash: l2Hash,
});
console.log('L2 transaction confirmed:', l2Receipt);
console.log('Deposit completed successfully!');
}

main().then();

运行代码:

1
node --import=tsx src/deposit_eth.ts

可能需要几分钟存款操作才会完成(请不要误触Ctrl+C人为终止进程),但是超过15分钟肯定是出现问题了,请注意排查.

取款操作就省略不写了,我想你应该不会有把ETH跨回来的需求。想体验取款的可以参考定置的参考文档。