Giter Site home page Giter Site logo

matic-sdk-go's Introduction

Matic SDK for Go

Warning

Initial development is in progress, but there has not yet been a stable.

This repository contains the matic go client library. converted maticjs in go and compatible go-ethereum

This library will help developers to move assets from Ethereum chain to Matic chain, and withdraw from Matic to Ethereum using fraud proofs.

Note

Requires Go 1.19+


Setup Client

import (
    "github.com/MinseokOh/matic-sdk-go/pos"
    "github.com/MinseokOh/matic-sdk-go/types"
)

posClient, err := pos.NewClient(types.NewDefaultConfig(types.TestNet))
if err != nil {
	panic(err)
}


// ether
childWETH := posClient.ERC20(WETHAddress, types.Child)

// erc20
rootToken := posClient.ERC20(rootTokenAddress, types.Root)
childToken := posClient.ERC20(childTokenAddress, types.Root)

Ether Deposit and Withdraw Guide

Deposit ETH

  • Make the depositEtherFor call on the RootChainManager and send the ether asset.
txHash, err := posClient.DepositEtherFor(context.Background(), big.NewInt(10000), &types.TxOption{
	PrivateKey: privateKey
})
if err != nil {
    // handle error
}
fmt.Println(txHash)

Withdraw ETH

  1. Burn tokens on Polygon chain.
txHash, err := childWETH.Withdraw(context.Background(), big.NewInt(10000), &types.TxOption{
    PrivateKey: privateKey
})
if err != nil {
	// handle error
}
fmt.Println(txHash)
  1. Call exit function on RootChainManager to submit proof of burn transaction. This call can be made after checkpoint is submitted for the block containing burn transaction.

Note

The Withdraw transaction must be checkpointed in order to exit the withdraw.

txHash, err := posClient.ExitEther(context.Background(), burnTxHash, &types.TxOption{
    PrivateKey: privateKey
})
if err != nil {
	// handle error
}
fmt.Println(txHash)

or

// token address can be null for native tokens like ethereum or matic
txHash, err := posClient.ERC20(common.Address{}, types.Root).Exit(context.Background(), burnTxHash, &types.TxOption{
    PrivateKey: privateKey
})
if err != nil {
	// handle error
}
fmt.Println(txHash)

ERC20 Deposit and Withdraw Guide

Deposit ERC20

  1. Approve ERC20Predicate contract to spend the tokens that have to be deposited.
// approve predicate address
txHash, err := rootToken.Approve(context.Background(), common.Address{}, big.NewInt(10000), &types.TxOption{
    PrivateKey: privateKey
})
if err != nil {
    // handle error
}
fmt.Println(txHash)
  1. Make depositFor call on RootChainManager.
txHash, err := rootToken.Deposit(context.Background(), big.NewInt(10000), &types.TxOption{
    PrivateKey: privateKey
})
if err != nil {
    // handle error
}
fmt.Println(txHash)

Withdraw ERC20

  1. Burn tokens on the Polygon chain.
txHash, err := childToken.Withdraw(context.Background(), big.NewInt(10000), &types.TxOption{
    PrivateKey: privateKey
})
if err != nil {
    // handle error
}
fmt.Println(txHash)
  1. Call the exit() function on RootChainManager to submit proof of burn transaction. This call can be made after the checkpoint is submitted for the block containing the burn transaction.

Note

The Withdraw transaction must be checkpointed in order to exit the withdraw.

txHash, err := rootToken.Exit(context.Background(), burnTxHash, &types.TxOption{
    PrivateKey: privateKey
})
if err != nil {
	// handle error
}
fmt.Println(txHash)

Exit With Raw CallData

chainId, err := posClient.Root.ChainID(ctx)
if err != nil {
    // handle error
}

nonce, err := posClient.Root.PendingNonceAt(ctx, address)
if err != nil {
    // handle error
}

gasTipCap, err := posClient.Root.SuggestGasTipCap(ctx)
if err != nil {
    // handle error
}

payload, err := posClient.BuildPayloadForExit(ctx, txHash, types.ERC20Transfer)
if err != nil {
    // handle error
}

data, err := maticabi.RootChainManager.Pack("exit", payload)
if err != nil {
    // handle error
}

signer := ether.NewLondonSigner(chainId)
tx, err := ether.SignNewTx(privateKey, signer, &ether.DynamicFeeTx{
    ChainID:   chainId,
    GasTipCap: gasTipCap,
    GasFeeCap: gasTipCap,
    Gas:       1e6,
    Nonce:     nonce,
    To:        rootChainManagerAddress,
    Value:     big.NewInt(0),
    Data:      data,
})
if err != nil {
    // handle error
}

err = posClient.Root.SendTransaction(ctx, tx)
if err != nil {
    // handle error
}

matic-sdk-go's People

Contributors

minseokoh avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.