Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Allow overriding IPFS client (#99)
Browse files Browse the repository at this point in the history
* pass context in a few more places (#96)

* pass context in more places
remove a few unnecessary calls to GetMerkleMetadata()

* pass context in more places

* allow overriding ipfs http.client (#97)

Co-authored-by: ricebin <ricebin@users.noreply.github.com>
  • Loading branch information
adam-maj and ricebin authored Dec 6, 2022
1 parent 3589803 commit 7e52e6c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 43 deletions.
14 changes: 8 additions & 6 deletions thirdweb/ipfs_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ type uploadResponse struct {
}

type IpfsStorage struct {
GatewayUrl string
gatewayUrl string
httpClient *http.Client
}

func newIpfsStorage(gatewayUrl string) *IpfsStorage {
func newIpfsStorage(gatewayUrl string, httpClient *http.Client) *IpfsStorage {
return &IpfsStorage{
GatewayUrl: gatewayUrl,
gatewayUrl: gatewayUrl,
httpClient: httpClient,
}
}

Expand All @@ -51,8 +53,8 @@ func newIpfsStorage(gatewayUrl string) *IpfsStorage {
//
// returns: byte data of the IPFS data at the URI
func (ipfs *IpfsStorage) Get(uri string) ([]byte, error) {
gatewayUrl := replaceHashWithGatewayUrl(uri, ipfs.GatewayUrl)
resp, err := http.Get(gatewayUrl)
gatewayUrl := replaceHashWithGatewayUrl(uri, ipfs.gatewayUrl)
resp, err := ipfs.httpClient.Get(gatewayUrl)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -258,7 +260,7 @@ func (ipfs *IpfsStorage) uploadBatchWithCid(

// returns - map[string]interface{}
func (ipfs *IpfsStorage) batchUploadProperties(data []map[string]interface{}) (interface{}, error) {
sanitizedMetadatas, err := ipfs.replaceGatewayUrlWithHash(data, "ipfs://", ipfs.GatewayUrl)
sanitizedMetadatas, err := ipfs.replaceGatewayUrlWithHash(data, "ipfs://", ipfs.gatewayUrl)
if err != nil {
return nil, err
}
Expand Down
16 changes: 11 additions & 5 deletions thirdweb/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package thirdweb

import (
"fmt"
"net/http"
"strings"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -40,6 +41,7 @@ func NewThirdwebSDKFromProvider(provider *ethclient.Client, options *SDKOptions)
// Define defaults for all the options
privateKey := ""
gatewayUrl := defaultIpfsGatewayUrl
httpClient := http.DefaultClient

// Override defaults with the options that are defined
if options != nil {
Expand All @@ -50,9 +52,13 @@ func NewThirdwebSDKFromProvider(provider *ethclient.Client, options *SDKOptions)
if options.GatewayUrl != "" {
gatewayUrl = options.GatewayUrl
}

if options.HttpClient != nil {
httpClient = options.HttpClient
}
}

storage := newIpfsStorage(gatewayUrl)
storage := newIpfsStorage(gatewayUrl, httpClient)

handler, err := NewProviderHandler(provider, privateKey)
if err != nil {
Expand Down Expand Up @@ -230,13 +236,13 @@ func getDefaultRpcUrl(rpcUrlorName string) (string, error) {
case "avalanche":
return defaultRpc("avalanche")
case "optimism":
return defaultRpc("optimism");
return defaultRpc("optimism")
case "optimism-goerli":
return defaultRpc("optimism-goerli");
return defaultRpc("optimism-goerli")
case "arbitrum":
return defaultRpc("arbitrum");
return defaultRpc("arbitrum")
case "arbitrum-goerli":
return defaultRpc("arbitrum-goerli");
return defaultRpc("arbitrum-goerli")
default:
if strings.HasPrefix(rpcUrlorName, "http") {
return rpcUrlorName, nil
Expand Down
7 changes: 4 additions & 3 deletions thirdweb/sharded_merkle_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -18,7 +19,7 @@ func TestMerkleTreeSmall(t *testing.T) {
}

uri := "ipfs://QmeAx8aRvsYXN6mzky72b9V1HWokb271FoBmDu4tatC8hE/0"
storage := newIpfsStorage(defaultIpfsGatewayUrl)
storage := newIpfsStorage(defaultIpfsGatewayUrl, http.DefaultClient)

body, err := storage.Get(uri)
if err != nil {
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestMerkleTreeEdgeCase(t *testing.T) {
}

uri := "ipfs://QmacDnA4i7Za19LpE3pngwLfUtakn71ghaKKjTkM2Phzj8/0"
storage := newIpfsStorage(defaultIpfsGatewayUrl)
storage := newIpfsStorage(defaultIpfsGatewayUrl, http.DefaultClient)

body, err := storage.Get(uri)
if err != nil {
Expand Down Expand Up @@ -83,4 +84,4 @@ func TestMerkleTreeEdgeCase(t *testing.T) {

assert.Equal(t, bytes.Compare(proof.Proof[i][:], proofBytes), 0)
}
}
}
60 changes: 31 additions & 29 deletions thirdweb/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package thirdweb

import (
"math/big"
"net/http"
"time"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -11,6 +12,7 @@ import (
type SDKOptions struct {
PrivateKey string
GatewayUrl string
HttpClient *http.Client
}

type Metadata struct {
Expand Down Expand Up @@ -65,26 +67,26 @@ type EditionMetadataInput struct {
}

type ClaimVerification struct {
Value *big.Int
Proofs [][32]byte
MaxClaimable *big.Int
Price *big.Int
CurrencyAddress string
PriceInProof *big.Int
CurrencyAddressInProof string
Value *big.Int
Proofs [][32]byte
MaxClaimable *big.Int
Price *big.Int
CurrencyAddress string
PriceInProof *big.Int
CurrencyAddressInProof string
}

type ClaimConditionOutput struct {
StartTime time.Time
MaxClaimableSupply *big.Int
MaxClaimablePerWallet *big.Int
CurrentMintSupply *big.Int
AvailableSupply *big.Int
WaitInSeconds *big.Int
Price *big.Int
CurrencyAddress string
CurrencyMetadata *CurrencyValue
MerkleRootHash [32]byte
StartTime time.Time
MaxClaimableSupply *big.Int
MaxClaimablePerWallet *big.Int
CurrentMintSupply *big.Int
AvailableSupply *big.Int
WaitInSeconds *big.Int
Price *big.Int
CurrencyAddress string
CurrencyMetadata *CurrencyValue
MerkleRootHash [32]byte
}
type Currency struct {
Name string
Expand Down Expand Up @@ -528,7 +530,7 @@ type ClaimArguments struct {
Currency common.Address
PricePerToken *big.Int
AllowlistProof abi.IDropAllowlistProof
Data []byte
Data []byte
}

type ClaimInfo struct {
Expand Down Expand Up @@ -557,7 +559,7 @@ func (condition *ClaimConditionInput) fillDefaults() {
type SnapshotEntryWithProof struct {
Address string
MaxClaimable string
Price string
Price string
CurrencyAddress string
Proof [][32]byte
}
Expand All @@ -569,20 +571,20 @@ func (entry *SnapshotEntryWithProof) fillDefaults() {
}

type ShardedMerkleTreeInfo struct {
MerkleRoot string `json:"merkleRoot"`
BaseUri string `json:"baseUri"`
OriginalEntriesUri string `json:"originalEntriesUri"`
ShardNybbles int `json:"shardNybbles"`
TokenDecimals int `json:"tokenDecimals"`
MerkleRoot string `json:"merkleRoot"`
BaseUri string `json:"baseUri"`
OriginalEntriesUri string `json:"originalEntriesUri"`
ShardNybbles int `json:"shardNybbles"`
TokenDecimals int `json:"tokenDecimals"`
}

type SnapshotEntry struct {
Address string `json:"address"`
MaxClaimable string `json:"maxClaimable"`
Price string `json:"price"`
CurrencyAddress string `json:"currencyAddress"`
Address string `json:"address"`
MaxClaimable string `json:"maxClaimable"`
Price string `json:"price"`
CurrencyAddress string `json:"currencyAddress"`
}
type ShardData struct {
Proofs []string `json:"proofs"`
Proofs []string `json:"proofs"`
Entries []SnapshotEntry `json:"entries"`
}

0 comments on commit 7e52e6c

Please sign in to comment.