To install the SDK with the go get
command, run the following:
go get github.com/itsahedge/go-cowswap
Before you can use the SDK, please set the ENV variables: PRIVATE_KEY
& ETH_ADDRESS
(refer to .env.example)
Config is of type ConfigOpts
To use default config, you only need to specify which network: mainnet
, goerli
, xdai
// config.go
network := "goerli"
options := ConfigOpts{
Network: network,
Host: HostConfig[network],
RpcUrl: RpcConfig[network],
EthAddress: os.Getenv("ETH_ADDRESS"),
PrivateKey: os.Getenv("PRIVATE_KEY"),
}
To use custom config, you need to specify which network: mainnet
, goerli
, xdai
, the host (cow api endpoint), and rpc
// config.go
network := "goerli"
host := "https://api.cow.fi/goerli/api/v1"
customRpc := ""
options := ConfigOpts{
Network: network,
Host: host,
RpcUrl: customRpc,
EthAddress: os.Getenv("ETH_ADDRESS"),
PrivateKey: os.Getenv("PRIVATE_KEY"),
}
package main
import (
"context"
"fmt"
cowswap "github.com/itsahedge/go-cowswap"
"log"
)
func main() {
// Initialize the go-cowswap client on Goerli
network := "goerli"
rpc := "https://eth-goerli-rpc.gateway.pokt.network"
host := "https://api.cow.fi/goerli/api/v1"
privateKey := "YOUR-PRIVATE-KEY"
address := "YOUR-ETHEREUM-ADDRESS"
options := cowswap.ConfigOpts{
Network: network,
Host: host,
RpcUrl: rpc,
EthAddress: address,
PrivateKey: privateKey,
}
client, err := cowswap.NewClient(options)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
// Fetch the Chain ID and Block Number from the Client
chainId, err := client.EthClient.ChainID(ctx)
if err != nil {
log.Fatal(err)
}
block, err := client.EthClient.BlockNumber(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("chaind ID: %v, block: %v", chainId, block)
}
In order to run the tests properly:
- set your ENV variables for
PRIVATE_KEY
Ð_ADDRESS
. - Set the network config to Goerli
- have some Goerli ETH/WETH
go test -coverprofile=coverage.out
go tool cover -html=coverage.out