Skip to content

Commit 1ef5d8a

Browse files
committed
Add method to notify for incoming funds to the SDK
1 parent b6cb9b0 commit 1ef5d8a

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

pkg/client-sdk/ark_sdk.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type ArkClient interface {
3939
RedeemNotes(ctx context.Context, notes []string, opts ...Option) (string, error)
4040
SetNostrNotificationRecipient(ctx context.Context, nostrRecipient string) error
4141
SignTransaction(ctx context.Context, tx string) (string, error)
42+
NotifyIncomingFunds(ctx context.Context, address string) ([]types.Vtxo, error)
4243
Stop() error
4344
}
4445

pkg/client-sdk/client.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/hex"
66
"fmt"
77
"strings"
8+
"sync"
89
"time"
910

1011
"github.com/ark-network/ark/common"
@@ -142,6 +143,35 @@ func (a *arkClient) ListVtxos(
142143
return
143144
}
144145

146+
func (a *arkClient) NotifyIncomingFunds(
147+
ctx context.Context, addr string,
148+
) ([]types.Vtxo, error) {
149+
eventCh, closeFn, err := a.client.SubscribeForAddress(ctx, addr)
150+
if err != nil {
151+
return nil, err
152+
}
153+
154+
wg := &sync.WaitGroup{}
155+
wg.Add(1)
156+
incomingVtxos := make([]types.Vtxo, 0)
157+
go func() {
158+
for event := range eventCh {
159+
if event.Err != nil {
160+
err = event.Err
161+
} else {
162+
for _, vtxo := range event.NewVtxos {
163+
incomingVtxos = append(incomingVtxos, toTypesVtxo(vtxo))
164+
}
165+
}
166+
closeFn()
167+
return
168+
}
169+
}()
170+
wg.Done()
171+
172+
return incomingVtxos, nil
173+
}
174+
145175
func (a *arkClient) initWithWallet(
146176
ctx context.Context, args InitWithWalletArgs,
147177
) error {

pkg/client-sdk/wasm/browser/exports.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func init() {
4343
js.Global().Set("setNostrNotificationRecipient", SetNostrNotificationRecipientWrapper())
4444
js.Global().Set("listVtxos", ListVtxosWrapper())
4545
js.Global().Set("signTransaction", SignTransactionWrapper())
46+
js.Global().Set("notifyIncomingFunds", NotifyIncomingFundsWrapper())
4647

4748
js.Global().Set("getServerUrl", GetServerUrlWrapper())
4849
js.Global().Set("getServerPubkeyHex", GetServerPubkeyWrapper())

pkg/client-sdk/wasm/browser/wrappers.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,31 @@ func SignTransactionWrapper() js.Func {
553553
})
554554
}
555555

556+
func NotifyIncomingFundsWrapper() js.Func {
557+
return JSPromise(func(args []js.Value) (interface{}, error) {
558+
if len(args) != 1 {
559+
return nil, fmt.Errorf("invalid number of args")
560+
}
561+
562+
address := args[0].String()
563+
564+
incomingVtxos, err := arkSdkClient.NotifyIncomingFunds(context.Background(), address)
565+
if err != nil {
566+
return nil, err
567+
}
568+
569+
rawList := map[string]interface{}{
570+
"incomingVtxos": incomingVtxos,
571+
}
572+
result, err := json.Marshal(rawList)
573+
if err != nil {
574+
return nil, err
575+
}
576+
577+
return result, nil
578+
})
579+
}
580+
556581
type promise func(args []js.Value) (interface{}, error)
557582

558583
func JSPromise(fn promise) js.Func {

0 commit comments

Comments
 (0)