From 1795a6ce5fb807e272cd32453f168a4f2173aeda Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Tue, 5 Nov 2024 16:40:57 +0300 Subject: [PATCH] run ton in docker --- packages/localnet/src/index.ts | 2 + packages/localnet/src/tonDocker.ts | 104 +++++++++++++++++++++++++++++ packages/localnet/src/tonStart.ts | 7 ++ 3 files changed, 113 insertions(+) create mode 100644 packages/localnet/src/tonDocker.ts create mode 100644 packages/localnet/src/tonStart.ts diff --git a/packages/localnet/src/index.ts b/packages/localnet/src/index.ts index 6d11dc6..3333c46 100755 --- a/packages/localnet/src/index.ts +++ b/packages/localnet/src/index.ts @@ -15,6 +15,7 @@ import { deployOpts } from "./deployOpts"; import { handleOnEVMDeposited } from "./handleOnEVMDeposited"; import { handleOnZEVMWithdrawn } from "./handleOnZEVMWithdrawn"; import { createToken } from "./createToken"; +import { tonStart } from "./tonStart"; const FUNGIBLE_MODULE_ADDRESS = "0x735b14BB79463307AAcBED86DAf3322B1e6226aB"; @@ -296,6 +297,7 @@ export const initLocalnet = async ({ port: number; exitOnError: boolean; }) => { + tonStart(); const provider = new ethers.JsonRpcProvider(`http://127.0.0.1:${port}`); provider.pollingInterval = 100; // anvil test mnemonic diff --git a/packages/localnet/src/tonDocker.ts b/packages/localnet/src/tonDocker.ts new file mode 100644 index 0000000..92df83f --- /dev/null +++ b/packages/localnet/src/tonDocker.ts @@ -0,0 +1,104 @@ +// @ts-ignore +import Docker from "dockerode"; + +const docker = new Docker(); + +async function removeExistingContainer(containerName: string) { + try { + const container = docker.getContainer(containerName); + await container.remove({ force: true }); + console.log(`Removed existing container: ${containerName}`); + } catch (err: any) { + if (err.statusCode !== 404) { + console.error("Error removing container:", err); + } + } +} + +async function createNetworkIfNotExists(networkName: string) { + const networks = await docker.listNetworks(); + const networkExists = networks.some( + (network: any) => network.Name === networkName + ); + + if (!networkExists) { + console.log(`Creating network: ${networkName}`); + await docker.createNetwork({ + Name: networkName, + IPAM: { + Config: [{ Subnet: "172.21.0.0/16" }], + }, + }); + console.log(`Network ${networkName} created`); + } +} + +export async function runZetaChainTonDocker() { + const containerName = "zeta_chain_ton_docker"; + const networkName = "mynetwork"; + + try { + await createNetworkIfNotExists(networkName); + + console.log("Pulling the ZetaChain TON Docker image..."); + + await new Promise((resolve, reject) => { + docker.pull( + "ghcr.io/zeta-chain/ton-docker:a69ea0f", + (err: any, stream: any) => { + if (err) return reject(err); + + docker.modem.followProgress(stream, onFinished, onProgress); + + function onFinished(err: any) { + if (err) return reject(err); + console.log("Image pulled successfully!"); + resolve(); + } + + function onProgress(event: any) { + console.log(event.status, event.progress || ""); + } + } + ); + }); + + console.log("Removing any existing container..."); + await removeExistingContainer(containerName); + + console.log("Creating and starting the container..."); + + const container = await docker.createContainer({ + Image: "ghcr.io/zeta-chain/ton-docker:a69ea0f", + name: containerName, + ExposedPorts: { + "8000/tcp": {}, + "4443/tcp": {}, + }, + HostConfig: { + AutoRemove: true, + PortBindings: { + "8000/tcp": [{ HostPort: "8111" }], + "4443/tcp": [{ HostPort: "4443" }], + }, + }, + Env: ["DOCKER_IP=172.21.0.104"], + NetworkingConfig: { + EndpointsConfig: { + [networkName]: { + IPAMConfig: { + IPv4Address: "172.21.0.104", + }, + }, + }, + }, + }); + + await container.start(); + console.log( + "ZetaChain TON Docker container started on ports 8111 and 4443!" + ); + } catch (error) { + console.error("Error running container:", error); + } +} diff --git a/packages/localnet/src/tonStart.ts b/packages/localnet/src/tonStart.ts new file mode 100644 index 0000000..258fc1e --- /dev/null +++ b/packages/localnet/src/tonStart.ts @@ -0,0 +1,7 @@ +import { runZetaChainTonDocker } from "./tonDocker"; + +export const tonStart = () => { + runZetaChainTonDocker(); +}; + +tonStart();