Add support for unilateral exit (#79)

* v0 unilateral redemption

* add fee outputs to congestion tree

* unilateral exit

* rework unilateral exit verbosity

* substract fee from vtxo amount

* remove unused functions and variables

* fix after reviews

* Update noah/explorer.go

Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com>
Signed-off-by: Louis Singer <41042567+louisinger@users.noreply.github.com>

* remove bufferutils

---------

Signed-off-by: Louis Singer <41042567+louisinger@users.noreply.github.com>
Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com>
This commit is contained in:
Louis Singer
2024-01-16 14:13:47 +01:00
committed by GitHub
parent b438eb638f
commit cf78fc1ab3
33 changed files with 1240 additions and 630 deletions

View File

@@ -170,42 +170,111 @@ func getOffchainBalance(
return balance, nil
}
func getOnchainBalance(addr string) (uint64, error) {
type utxo struct {
Txid string `json:"txid"`
Vout uint32 `json:"vout"`
Amount uint64 `json:"value"`
Asset string `json:"asset"`
}
func getOnchainUtxos(addr string) ([]utxo, error) {
_, net, err := getNetwork()
if err != nil {
return 0, err
return nil, err
}
baseUrl := explorerUrl[net.Name]
resp, err := http.Get(fmt.Sprintf("%s/address/%s/utxo", baseUrl, addr))
if err != nil {
return 0, err
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
return nil, err
}
if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf(string(body))
return nil, fmt.Errorf(string(body))
}
payload := []interface{}{}
payload := []utxo{}
if err := json.Unmarshal(body, &payload); err != nil {
return nil, err
}
return payload, nil
}
func getOnchainBalance(addr string) (uint64, error) {
payload, err := getOnchainUtxos(addr)
if err != nil {
return 0, err
}
_, net, err := getNetwork()
if err != nil {
return 0, err
}
balance := uint64(0)
for _, p := range payload {
utxo := p.(map[string]interface{})
asset, ok := utxo["asset"].(string)
if !ok || asset != net.AssetID {
if p.Asset != net.AssetID {
continue
}
balance += uint64(utxo["value"].(float64))
balance += p.Amount
}
return balance, nil
}
func getTxHex(txid string) (string, error) {
_, net, err := getNetwork()
if err != nil {
return "", err
}
baseUrl := explorerUrl[net.Name]
resp, err := http.Get(fmt.Sprintf("%s/tx/%s/hex", baseUrl, txid))
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf(string(body))
}
return string(body), nil
}
func broadcast(txHex string) (string, error) {
_, net, err := getNetwork()
if err != nil {
return "", err
}
body := bytes.NewBuffer([]byte(txHex))
baseUrl := explorerUrl[net.Name]
resp, err := http.Post(fmt.Sprintf("%s/tx", baseUrl), "text/plain", body)
if err != nil {
return "", err
}
defer resp.Body.Close()
bodyResponse, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf(string(bodyResponse))
}
return string(bodyResponse), nil
}
func getNetwork() (*common.Network, *network.Network, error) {
state, err := getState()
if err != nil {