mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-18 04:34:19 +01:00
* Add gRPC, REST, and gRPC-Web clients for server access This commit introduces clients for gRPC, REST, and gRPC-Web to access the server. - gRPC client: Includes additional argument opts ...grpc.CallOption in the interface for future extensibility. - REST client: Factory function accepts http.Client as an argument to allow user customization. - gRPC-Web client: Added a Log method for fast debugging in JavaScript. The decision to use different interfaces for each client type is to accommodate specific features and extensibility requirements for each protocol. * remove grpc web * generate rest * use grpc sdk in CLI * temp wasm * ark sdk * renaming * pr review refactor * pr review refactor * walletStore & configStore * ark sdk wasm wrapper * handle event stream with rest * wip on supporting rest * store init * simulate event stream with rest * fix rest sdk wip * Fix returning forfeit txs in round event * wasm first working e2e example * pr review refactor * pr review refactor * pr review refactor * Fixes --------- Co-authored-by: altafan <18440657+altafan@users.noreply.github.com>
92 lines
1.8 KiB
Go
92 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
arkgrpcclient "github.com/ark-network/ark-sdk/grpc"
|
|
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
type vtxo struct {
|
|
amount uint64
|
|
txid string
|
|
vout uint32
|
|
poolTxid string
|
|
expireAt *time.Time
|
|
}
|
|
|
|
func getVtxos(
|
|
ctx *cli.Context, explorer Explorer, client arkv1.ArkServiceClient,
|
|
addr string, computeExpiration bool,
|
|
) ([]vtxo, error) {
|
|
response, err := client.ListVtxos(ctx.Context, &arkv1.ListVtxosRequest{
|
|
Address: addr,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
vtxos := make([]vtxo, 0, len(response.GetSpendableVtxos()))
|
|
for _, v := range response.GetSpendableVtxos() {
|
|
var expireAt *time.Time
|
|
if v.ExpireAt > 0 {
|
|
t := time.Unix(v.ExpireAt, 0)
|
|
expireAt = &t
|
|
}
|
|
if v.Swept {
|
|
continue
|
|
}
|
|
vtxos = append(vtxos, vtxo{
|
|
amount: v.Receiver.Amount,
|
|
txid: v.Outpoint.Txid,
|
|
vout: v.Outpoint.Vout,
|
|
poolTxid: v.PoolTxid,
|
|
expireAt: expireAt,
|
|
})
|
|
}
|
|
|
|
if !computeExpiration {
|
|
return vtxos, nil
|
|
}
|
|
|
|
redeemBranches, err := getRedeemBranches(ctx.Context, explorer, client, vtxos)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for vtxoTxid, branch := range redeemBranches {
|
|
expiration, err := branch.expireAt(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for i, vtxo := range vtxos {
|
|
if vtxo.txid == vtxoTxid {
|
|
vtxos[i].expireAt = expiration
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return vtxos, nil
|
|
}
|
|
|
|
func getClientFromState(ctx *cli.Context) (arkv1.ArkServiceClient, func(), error) {
|
|
state, err := getState(ctx)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
addr := state[ASP_URL]
|
|
if len(addr) <= 0 {
|
|
return nil, nil, fmt.Errorf("missing asp url")
|
|
}
|
|
return getClient(addr)
|
|
}
|
|
|
|
func getClient(addr string) (arkv1.ArkServiceClient, func(), error) {
|
|
client, cleanFn, err := arkgrpcclient.New(addr)
|
|
return client.Service(), cleanFn, err
|
|
}
|