code.vegaprotocol.io/vega@v0.79.0/core/examples/nullchain/faucet.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 nullchain
    17  
    18  import (
    19  	"bytes"
    20  	"encoding/json"
    21  	"errors"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"net/http"
    25  
    26  	config "code.vegaprotocol.io/vega/core/examples/nullchain/config"
    27  )
    28  
    29  var ErrFaucet = errors.New("faucet failed")
    30  
    31  func mint(asset, amount, party string) error {
    32  	values := map[string]string{
    33  		"party":  party,
    34  		"amount": amount,
    35  		"asset":  asset,
    36  	}
    37  
    38  	jsonValue, _ := json.Marshal(values)
    39  
    40  	r, err := http.Post(config.FaucetAddress, "application/json", bytes.NewBuffer(jsonValue))
    41  	if err != nil {
    42  		return fmt.Errorf("faucet failed: %w", err)
    43  	}
    44  	defer r.Body.Close()
    45  
    46  	if r.StatusCode == http.StatusOK {
    47  		return nil
    48  	}
    49  
    50  	data, err := ioutil.ReadAll(r.Body)
    51  	if err != nil {
    52  		return fmt.Errorf("time forward failed: %w", err)
    53  	}
    54  	return fmt.Errorf("%w: %s", ErrFaucet, string(data))
    55  }
    56  
    57  func FillAccounts(asset, amount string, parties []*Party) error {
    58  	var err error
    59  	for _, party := range parties {
    60  
    61  		err = mint(asset, amount, party.pubkey)
    62  		if err != nil {
    63  			return err
    64  		}
    65  		err = MoveByDuration(config.BlockDuration)
    66  		if err != nil {
    67  			return err
    68  		}
    69  	}
    70  	err = MoveByDuration(config.BlockDuration)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	return nil
    76  }