-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer_native_token.go
More file actions
86 lines (68 loc) · 2.28 KB
/
transfer_native_token.go
File metadata and controls
86 lines (68 loc) · 2.28 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
// Copyright (C) 2021 Creditor Corp. Group.
// See LICENSE for copying information.
package venly
import (
"bytes"
"context"
"encoding/json"
"net/http"
"github.com/zeebo/errs"
)
// TransferNativeRequest fields that required for transfer native request.
type TransferNativeRequest struct {
TransactionRequest TxRequest `json:"transactionRequest"`
Pincode string `json:"pincode"`
}
// TxRequest struct ...
type TxRequest struct {
Type string `json:"type"`
WalletID string `json:"walletId"`
To string `json:"to"`
SecretType string `json:"secretType"`
Value float64 `json:"value"`
Data string `json:"data"`
}
// TransferNativeResponse fields that returns from transfer native.
type TransferNativeResponse struct {
Success bool `json:"success"`
Result struct {
TransactionHash string `json:"transactionHash"`
} `json:"result"`
}
// TransferType indicates that type required for transfer token.
const TransferType string = "TRANSFER"
// TransferNative transfers native token via Venly.
func (client *Client) TransferNative(ctx context.Context, accessToken string, r TransferNativeRequest) (response TransferNativeResponse, err error) {
jsonBody, err := json.Marshal(r)
if err != nil {
return TransferNativeResponse{}, err
}
req, err := http.NewRequest(http.MethodPost, client.config.DefaultURL+"transactions/execute", bytes.NewReader(jsonBody))
if err != nil {
return TransferNativeResponse{}, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+accessToken)
resp, err := client.http.Do(req.WithContext(ctx))
if err != nil {
return TransferNativeResponse{}, 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 TransferNativeResponse{}, err
}
if !errorResp.Success {
return TransferNativeResponse{}, errs.New(errorResp.Errors[0].Message)
}
return TransferNativeResponse{}, errs.New(resp.Status)
}
var transferNativeResponse TransferNativeResponse
if err = json.NewDecoder(resp.Body).Decode(&transferNativeResponse); err != nil {
return TransferNativeResponse{}, err
}
return transferNativeResponse, nil
}