Skip to content

Commit

Permalink
refactor: rename redundant gateway names
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Apr 23, 2022
1 parent cd54335 commit 75b23f6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 deletions.
14 changes: 7 additions & 7 deletions examples/starknet/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"fmt"
"context"
"fmt"

"github.com/dontpanicdao/caigo"
)
Expand All @@ -14,9 +14,9 @@ func main() {
if err != nil {
panic(err.Error())
}

// init starknet gateway client
gw := caigo.NewGateway() //defaults to goerli
gw := caigo.NewClient() //defaults to goerli

// get random value for salt
priv, _ := curve.GetRandomPrivateKey()
Expand All @@ -41,7 +41,7 @@ func main() {
if err != nil {
panic(err.Error())
}
fmt.Printf("Poll %dsec %dx \n\ttransaction(%s) status: %s\n\n", n * pollInterval, n, deployResponse.TransactionHash, status)
fmt.Printf("Poll %dsec %dx \n\ttransaction(%s) status: %s\n\n", n*pollInterval, n, deployResponse.TransactionHash, status)

// fetch transaction details
tx, err := gw.Transaction(context.Background(), caigo.TransactionOptions{TransactionHash: deployResponse.TransactionHash})
Expand All @@ -58,7 +58,7 @@ func main() {
panic(err.Error())
}
fmt.Println("Counter is currently at: ", callResp[0])

// invoke StarkNet contract external function
invResp, err := gw.Invoke(context.Background(), caigo.Transaction{
ContractAddress: tx.Transaction.ContractAddress,
Expand All @@ -72,7 +72,7 @@ func main() {
if err != nil {
panic(err.Error())
}
fmt.Printf("Poll %dsec %dx \n\ttransaction(%s) status: %s\n\n", n * pollInterval, n, deployResponse.TransactionHash, status)
fmt.Printf("Poll %dsec %dx \n\ttransaction(%s) status: %s\n\n", n*pollInterval, n, deployResponse.TransactionHash, status)

callResp, err = gw.Call(context.Background(), caigo.Transaction{
ContractAddress: tx.Transaction.ContractAddress,
Expand All @@ -82,4 +82,4 @@ func main() {
panic(err.Error())
}
fmt.Println("Counter is currently at: ", callResp[0])
}
}
4 changes: 2 additions & 2 deletions gateway/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func Test_BlockIDByHash(t *testing.T) {
gw := NewGateway()
gw := NewClient()

id, err := gw.BlockIDByHash(context.Background(), "0x5239614da0a08b53fa8cbdbdcb2d852e153027ae26a2ae3d43f7ceceb28551e")
if err != nil || id == 0 {
Expand All @@ -19,7 +19,7 @@ func Test_BlockIDByHash(t *testing.T) {
}

func Test_BlockHashByID(t *testing.T) {
gw := NewGateway()
gw := NewClient()

id, err := gw.BlockHashByID(context.Background(), 159179)
if err != nil || id == "" {
Expand Down
2 changes: 1 addition & 1 deletion gateway/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestProvider_Code(t *testing.T) {
blockNumber: big.NewInt(582),
}} {
ctx := context.Background()
sg := NewGateway(WithChain("main"))
sg := NewClient(WithChain("main"))
got, err := sg.CodeAt(ctx, tc.address, tc.blockNumber)
if err != nil {
t.Fatalf("getting code: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ type StarknetGateway struct {
Instantiate a new StarkNet Gateway client
- defaults to the GOERLI endpoints
*/
func NewGateway(opts ...GatewayOption) *StarknetGateway {
gopts := gatewayOptions{
func NewClient(opts ...Option) *StarknetGateway {
gopts := options{
chainID: GOERLI_ID,
client: http.DefaultClient,
}
Expand Down
34 changes: 17 additions & 17 deletions gateway/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,48 @@ import (
"net/http"
)

type gatewayOptions struct {
type options struct {
client *http.Client
chainID string
errorHandler func(e error) error
}

// funcGatewayOption wraps a function that modifies gatewayOptions into an
// implementation of the GatewayOption interface.
type funcGatewayOption struct {
f func(*gatewayOptions)
// funcOption wraps a function that modifies options into an
// implementation of the Option interface.
type funcOption struct {
f func(*options)
}

func (fso *funcGatewayOption) apply(do *gatewayOptions) {
func (fso *funcOption) apply(do *options) {
fso.f(do)
}

func newFuncGatewayOption(f func(*gatewayOptions)) *funcGatewayOption {
return &funcGatewayOption{
func newFuncOption(f func(*options)) *funcOption {
return &funcOption{
f: f,
}
}

// GatewayOption configures how we set up the connection.
type GatewayOption interface {
apply(*gatewayOptions)
// Option configures how we set up the connection.
type Option interface {
apply(*options)
}

func WithHttpClient(client http.Client) GatewayOption {
return newFuncGatewayOption(func(o *gatewayOptions) {
func WithHttpClient(client http.Client) Option {
return newFuncOption(func(o *options) {
o.client = &client
})
}

func WithChain(chainID string) GatewayOption {
return newFuncGatewayOption(func(o *gatewayOptions) {
func WithChain(chainID string) Option {
return newFuncOption(func(o *options) {
o.chainID = chainID
})
}

// WithErrorHandler returns an Option to set the error handler to be used.
func WithErrorHandler(f func(e error) error) GatewayOption {
return newFuncGatewayOption(func(o *gatewayOptions) {
func WithErrorHandler(f func(e error) error) Option {
return newFuncOption(func(o *options) {
o.errorHandler = f
})
}
6 changes: 3 additions & 3 deletions gateway/starknet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestExecuteGoerli(t *testing.T) {
priv, _ := new(big.Int).SetString("3904bd288b88a1dcd73e648b10642d63cb9b2ffd86526deee9d073f0690139e", 16)
x, y, _ := curve.PrivateToPoint(priv)

signer, err := curve.NewSigner(priv, x, y, NewGateway())
signer, err := curve.NewSigner(priv, x, y, NewClient())
if err != nil {
t.Errorf("Could not create signer: %v\n", err)
}
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestExecuteGoerli(t *testing.T) {
}

func TestInvokeContract(t *testing.T) {
gw := NewGateway()
gw := NewClient()

req := caigo.Transaction{
ContractAddress: "0x077fd9aee87891eb334448c26e01020c8cffec0bf62a959bd373490542bdd812",
Expand All @@ -74,7 +74,7 @@ func TestLocalStarkNet(t *testing.T) {

curve, _ := caigo.SC()

gw := NewGateway(WithChain("local"))
gw := NewClient(WithChain("local"))

pr, _ := curve.GetRandomPrivateKey()

Expand Down
2 changes: 1 addition & 1 deletion gateway/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestProvider_StorageAt(t *testing.T) {
opts: &StorageAtOptions{BlockHash: "0x182d83f0ed972e97fa529be0088e20b5a7cb32e3bba0d164d668147713e79f9"},
}} {
ctx := context.Background()
sg := NewGateway(WithChain("main"))
sg := NewClient(WithChain("main"))
got, err := sg.StorageAt(ctx, tc.address, tc.key, tc.opts)
if err != nil {
t.Fatalf("getting storage at: %v", err)
Expand Down

0 comments on commit 75b23f6

Please sign in to comment.