Skip to content

Commit 14c7479

Browse files
authored
Add CheckAddress (#198)
1 parent d1ca38a commit 14c7479

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

address.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"net/url"
78
"time"
89
)
910

@@ -25,6 +26,11 @@ type Address struct {
2526
UpdatedAt string `json:"updated_at"`
2627
}
2728

29+
type SimpleAddress struct {
30+
Destination string `json:"destination"`
31+
Tag string `json:"tag"`
32+
}
33+
2834
func CreateAddress(ctx context.Context, in *AddressInput, user *SafeUser) (*Address, error) {
2935
tipBody := TipBodyForAddressAdd(in.AssetId, in.Destination, in.Tag, in.Label)
3036
var err error
@@ -159,3 +165,29 @@ func GetAddressesByAssetId(ctx context.Context, assetId string, user *SafeUser)
159165
}
160166
return resp.Data, nil
161167
}
168+
169+
func CheckAddress(ctx context.Context, asset, destination, tag string) (*SimpleAddress, error) {
170+
v := url.Values{}
171+
v.Set("asset", asset)
172+
v.Set("destination", destination)
173+
if tag != "" {
174+
v.Set("tag", tag)
175+
}
176+
path := fmt.Sprintf("/external/addresses/check?" + v.Encode())
177+
body, err := Request(ctx, "GET", path, nil, "")
178+
if err != nil {
179+
return nil, ServerError(ctx, err)
180+
}
181+
var resp struct {
182+
Data *SimpleAddress `json:"data"`
183+
Error Error `json:"error"`
184+
}
185+
err = json.Unmarshal(body, &resp)
186+
if err != nil {
187+
return nil, BadDataError(ctx)
188+
}
189+
if resp.Error.Code > 0 {
190+
return nil, resp.Error
191+
}
192+
return resp.Data, nil
193+
}

address_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package bot
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestAddress(t *testing.T) {
11+
assert := assert.New(t)
12+
ctx := context.Background()
13+
14+
address, err := CheckAddress(ctx, "c94ac88f-4671-3976-b60a-09064f1811e8", "0x1616b057f8a89955d4a4f9fd9eb10289ac0e44a1", "")
15+
assert.Nil(err)
16+
assert.NotNil(address)
17+
assert.Equal("0x1616b057F8a89955d4A4f9fd9Eb10289ac0e44A1", address.Destination)
18+
assert.Equal("", address.Tag)
19+
}

0 commit comments

Comments
 (0)