Giter Club home page Giter Club logo

go-sdk's Introduction



thirdweb Go SDK

Join our Discord!

Installation

To install the SDK with the go get command, run the following:

go get github.com/thirdweb-dev/go-sdk/v2/thirdweb

Getting Started

To start using this SDK, you just need to pass in a provider configuration.

Instantiating the SDK

Once you have all the necessary dependencies, you can follow the following setup steps to get started with SDK read-only functions:

package main

import (
	"fmt"

	"github.com/thirdweb-dev/go-sdk/v2/thirdweb"
)

func main() {
	// Creates a new SDK instance to get read-only data for your contracts, you can pass:
	// - a chain name (mainnet, rinkeby, goerli, polygon, mumbai, avalanche, fantom)
	// - a custom RPC URL
	sdk, err := thirdweb.NewThirdwebSDK("mumbai", nil)
	if err != nil {
		panic(err)
	}

	// Now we can interact with the SDK, like displaying the connected chain ID
	chainId, err := sdk.GetChainID()
	if err != nil {
		panic(err)
	}

	fmt.Println("New SDK instance create on chain", chainId)
}

Working With Contracts

Once you instantiate the SDK, you can use it to access your thirdweb contracts. You can use SDK's contract getter functions like GetNFTCollection, GetEdition, and GetNFTDrop, to get the respective SDK contract instances. To use an NFT Collection contract for example, you can do the following.

package main

import (
	"fmt"

	"github.com/thirdweb-dev/go-sdk/v2/thirdweb"
)

func main() {
	sdk, err := thirdweb.NewThirdwebSDK("mumbai", nil)
	if err != nil {
		panic(err)
	}

	// Add your NFT Collection contract address here
	address := "0x..."
	nft, err := sdk.GetNFTCollection(address)
	if err != nil {
		panic(err)
	}

	// Now you can use any of the read-only SDK contract functions
	nfts, err := nft.GetAll()
	if err != nil {
		panic(err)
	}

	fmt.Printf("%d NFTs found on this contract\n", len(nfts))
}

Signing Transactions

In order to execute transactions on your contract, the SDK needs to know the wallet that is performing those transactions. For that it needs the private key of the wallet you want to execute transactions from.

โš ๏ธ Never commit private keys to file tracking history, or your account could be compromised.

To connect your wallet to the SDK, you can use the following setup:

package main

import (
	"fmt"
	"encoding/json"

	"github.com/thirdweb-dev/go-sdk/v2/thirdweb"
)

func main() {
	// Get your private key securely (preferably from an environment variable)
	privateKey := "..."

	// Instantiate the SDK with your privateKey
	sdk, err := thirdweb.NewThirdwebSDK("mumbai", &thirdweb.SDKOptions{
		PrivateKey: privateKey,
	})
	if err != nil {
		panic(err)
	}

	// Replace your contract address here
	address := "0x..."
	nft, err := sdk.GetNFTCollection(address)
	if err != nil {
		panic(err)
	}

	// Now you can execute transactions using the SDK contract functions
	tx, err := nft.Mint(
		&thirdweb.NFTMetadataInput{
			Name:        "Test NFT",
			Description: "Minted with the thirdweb Go SDK",
			Image: "ipfs://QmcCJC4T37rykDjR6oorM8hpB9GQWHKWbAi2YR1uTabUZu/0",
		},
	)
	if err != nil {
		panic(err)
	}

	result, _ := json.Marshal(&tx)
	fmt.Println(string(result))
}

go-sdk's People

Contributors

atbe avatar adam-maj avatar jakeloo avatar joaquim-verges avatar nachoiacovino 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.