To install the SDK with the go get
command, run the following:
go get github.com/egiftcard/go-sdk/v2/egiftcard
To start using this SDK, you just need to pass in a provider configuration. It's also strongly recommended that you use your egiftcard API keys with the SDK in order to get the best infrastructure performance (across RPCs, IPFS, etc.) - you can learn more about creating and using API keys here.
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/egiftcard/go-sdk/v2/egiftcard"
)
func main() {
// Your secret key from the egiftcard api keys dashboard
secretKey := "..."
// 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 := egiftcard.NewEgiftcardSDK("mumbai", &egiftcard.SDKOptions{
SecretKey: secretKey,
})
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)
}
Once you instantiate the SDK, you can use it to access your egiftcard 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/egiftcard/go-sdk/v2/egiftcard"
)
func main() {
// Get your secret key that you created from the egiftcard dashboard
secretKey := "..."
sdk, err := egiftcard.NewEgiftcardSDK("mumbai", &egiftcard.SDKOptions{
SecretKey: secretKey,
})
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))
}
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/egiftcard/go-sdk/v2/egiftcard"
)
func main() {
// Get your secret key that you created from the egiftcard dashboard
secretKey := "..."
// Get your private key securely (preferably from an environment variable)
privateKey := "..."
// Instantiate the SDK with your privateKey
sdk, err := egiftcard.NewEgiftcardSDK("mumbai", &egiftcard.SDKOptions{
SecretKey: secretKey,
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(
&egiftcard.NFTMetadataInput{
Name: "Test NFT",
Description: "Minted with the egiftcard Go SDK",
Image: "ipfs://QmcCJC4T37rykDjR6oorM8hpB9GQWHKWbAi2YR1uTabUZu/0",
},
)
if err != nil {
panic(err)
}
result, _ := json.Marshal(&tx)
fmt.Println(string(result))
}