-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_wallet.go
More file actions
105 lines (86 loc) · 2.9 KB
/
create_wallet.go
File metadata and controls
105 lines (86 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (C) 2021 Creditor Corp. Group.
// See LICENSE for copying information.
package venly
import (
"bytes"
"context"
"encoding/json"
"net/http"
"github.com/zeebo/errs"
)
// CreateWalletRequest fields that required for create wallet request.
type CreateWalletRequest struct {
WalletType string `json:"walletType"`
SecretType string `json:"secretType"`
Identifier string `json:"identifier"`
Description string `json:"description"`
Pincode string `json:"pincode"`
}
// Balance struct ...
type Balance struct {
Available bool `json:"available"`
SecretType string `json:"secretType"`
Balance float64 `json:"balance"`
GasBalance float64 `json:"gasBalance"`
Symbol string `json:"symbol"`
GasSymbol string `json:"gasSymbol"`
RawBalance string `json:"rawBalance"`
RawGasBalance string `json:"rawGasBalance"`
Decimals int `json:"decimals"`
}
// CreateWalletResult struct ...
type CreateWalletResult struct {
ID string `json:"id"`
Address string `json:"address"`
WalletType string `json:"walletType"`
SecretType string `json:"secretType"`
CreatedAt string `json:"createdAt"`
Archived bool `json:"archived"`
Alias string `json:"alias"`
Description string `json:"description"`
Primary bool `json:"primary"`
HasCustomPin bool `json:"hasCustomPin"`
Identifier string `json:"identifier"`
Balance Balance `json:"balance"`
}
// CreateWalletResponse fields that returns from create wallet.
type CreateWalletResponse struct {
Success bool `json:"success"`
Result CreateWalletResult `json:"result"`
}
// CreateWallet creates Venly wallet.
func (client *Client) CreateWallet(ctx context.Context, accessToken string, wallet CreateWalletRequest) (response CreateWalletResponse, err error) {
jsonBody, err := json.Marshal(wallet)
if err != nil {
return CreateWalletResponse{}, err
}
req, err := http.NewRequest(http.MethodPost, client.config.DefaultURL+"wallets", bytes.NewReader(jsonBody))
if err != nil {
return CreateWalletResponse{}, err
}
var bearer = "Bearer " + accessToken
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", bearer)
resp, err := client.http.Do(req.WithContext(ctx))
if err != nil {
return CreateWalletResponse{}, err
}
defer func() {
err = errs.Combine(err, resp.Body.Close())
}()
if resp.StatusCode != http.StatusOK {
errorResp := ErrorResponse{}
if err = json.NewDecoder(resp.Body).Decode(&errorResp); err != nil {
return CreateWalletResponse{}, err
}
if !errorResp.Success {
return CreateWalletResponse{}, errs.New(errorResp.Errors[0].Code)
}
return CreateWalletResponse{}, errs.New(resp.Status)
}
var createWalletResponse CreateWalletResponse
if err = json.NewDecoder(resp.Body).Decode(&createWalletResponse); err != nil {
return CreateWalletResponse{}, err
}
return createWalletResponse, nil
}