code.vegaprotocol.io/vega@v0.79.0/core/faucet/client/client.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package client
    17  
    18  import (
    19  	"bytes"
    20  	"context"
    21  	"encoding/json"
    22  	"errors"
    23  	"io/ioutil"
    24  	"net/http"
    25  	"net/url"
    26  	"path"
    27  	"time"
    28  
    29  	"code.vegaprotocol.io/vega/core/faucet"
    30  )
    31  
    32  const (
    33  	// use default address of faucet.
    34  	defaultAddress = "http://0.0.0.0:1790"
    35  )
    36  
    37  type Client struct {
    38  	clt     *http.Client
    39  	addr    string
    40  	mintURL string
    41  }
    42  
    43  func New(addr string) (*Client, error) {
    44  	mintURL, err := url.Parse(addr)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	mintURL.Path = path.Join(mintURL.Path, "/api/v1/mint")
    49  	return &Client{
    50  		clt:     &http.Client{},
    51  		addr:    defaultAddress,
    52  		mintURL: mintURL.String(),
    53  	}, nil
    54  }
    55  
    56  func NewDefault() (*Client, error) {
    57  	return New(defaultAddress)
    58  }
    59  
    60  func (c *Client) Mint(party, asset, amount string) error {
    61  	body := faucet.MintRequest{
    62  		Party:  party,
    63  		Asset:  asset,
    64  		Amount: amount,
    65  	}
    66  	jbytes, err := json.Marshal(body)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    71  	defer cancel()
    72  	req, err := http.NewRequestWithContext(
    73  		ctx, http.MethodPost, c.mintURL, bytes.NewReader(jbytes))
    74  	if err != nil {
    75  		return err
    76  	}
    77  	req.Header.Set("Content-Type", "application/json")
    78  	res, err := c.clt.Do(req)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	defer res.Body.Close()
    83  	resbody, err := ioutil.ReadAll(res.Body)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	faucetRes := &faucet.MintResponse{}
    88  	err = json.Unmarshal(resbody, faucetRes)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	if !faucetRes.Success {
    93  		return errors.New("unable to allocate new funds")
    94  	}
    95  	return nil
    96  }