Skip to content

Commit e143c3e

Browse files
committed
TO BE AMENDED
1 parent 5d486b0 commit e143c3e

File tree

4 files changed

+190
-17
lines changed

4 files changed

+190
-17
lines changed

cardano-cli/cli.go

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cardanocli
22

33
import (
4-
"bytes"
4+
"context"
55
"encoding/hex"
66
"encoding/json"
77
"errors"
@@ -12,11 +12,44 @@ import (
1212
"strconv"
1313
"strings"
1414

15+
flag "github.com/spf13/pflag"
16+
1517
"github.com/echovl/cardano-go"
1618
)
1719

20+
var socketPath, fallbackSocketPath string
21+
22+
func AddFlags(fs *flag.FlagSet) {
23+
fs.StringVar(&socketPath, "cardano-node-socket-path", "", "")
24+
fs.StringVar(&fallbackSocketPath, "fallback-cardano-node-socket-path", "", "")
25+
}
26+
27+
func availableAsSocket(fn string) bool {
28+
fi, err := os.Stat(fn)
29+
if err != nil {
30+
return false
31+
}
32+
if (fi.Mode().Type() & os.ModeSocket) != os.ModeSocket {
33+
return false
34+
}
35+
return true
36+
}
37+
38+
func getSocketPathToUse() string {
39+
sp := socketPath
40+
if sp != "" && availableAsSocket(sp) {
41+
return sp
42+
}
43+
sp = fallbackSocketPath
44+
if sp != "" && availableAsSocket(sp) {
45+
return sp
46+
}
47+
return os.Getenv("CARDANO_NODE_SOCKET_PATH")
48+
}
49+
1850
// CardanoCli implements Node using cardano-cli and a local node.
1951
type CardanoCli struct {
52+
ctx context.Context
2053
network cardano.Network
2154
}
2255

@@ -35,27 +68,39 @@ type cliTx struct {
3568
}
3669

3770
// NewNode returns a new instance of CardanoCli.
38-
func NewNode(network cardano.Network) cardano.Node {
71+
func NewNode(network cardano.Network, rest ...any) cardano.Node {
72+
if len(rest) > 0 {
73+
return &CardanoCli{network: network, ctx: rest[0].(context.Context)}
74+
}
3975
return &CardanoCli{network: network}
4076
}
4177

42-
func (c *CardanoCli) runCommand(args ...string) ([]byte, error) {
43-
out := &bytes.Buffer{}
78+
func (c *CardanoCli) runCommand(args ...string) (string, error) {
79+
out := &strings.Builder{}
4480

4581
if c.network == cardano.Mainnet {
4682
args = append(args, "--mainnet")
4783
} else {
4884
args = append(args, "--testnet-magic", strconv.Itoa(cardano.ProtocolMagic))
4985
}
5086

51-
cmd := exec.Command("cardano-cli", args...)
87+
var cmd *exec.Cmd
88+
if c.ctx == nil {
89+
cmd = exec.Command("cardano-cli", args...)
90+
} else {
91+
cmd = exec.CommandContext(c.ctx, "cardano-cli", args...)
92+
}
5293
cmd.Stdout = out
5394
cmd.Stderr = os.Stderr
5495
if err := cmd.Run(); err != nil {
55-
return nil, err
96+
return "", err
5697
}
5798

58-
return out.Bytes(), nil
99+
return out.String(), nil
100+
}
101+
102+
func (c *CardanoCli) DoCommand(args ...string) (string, error) {
103+
return c.runCommand(args...)
59104
}
60105

61106
func (c *CardanoCli) UTxOs(addr cardano.Address) ([]cardano.UTxO, error) {
@@ -65,7 +110,7 @@ func (c *CardanoCli) UTxOs(addr cardano.Address) ([]cardano.UTxO, error) {
65110
}
66111

67112
utxos := []cardano.UTxO{}
68-
lines := strings.Split(string(out), "\n")
113+
lines := strings.Split(out, "\n")
69114

70115
if len(lines) < 3 {
71116
return utxos, nil
@@ -142,7 +187,7 @@ func (c *CardanoCli) Tip() (*cardano.NodeTip, error) {
142187
}
143188

144189
cliTip := &tip{}
145-
if err = json.Unmarshal(out, cliTip); err != nil {
190+
if err = json.Unmarshal([]byte(out), cliTip); err != nil {
146191
return nil, err
147192
}
148193

@@ -171,7 +216,7 @@ func (c *CardanoCli) SubmitTx(tx *cardano.Tx) (*cardano.Hash32, error) {
171216

172217
out, err := c.runCommand("transaction", "submit", "--tx-file", txFile.Name())
173218
if err != nil {
174-
return nil, errors.New(string(out))
219+
return nil, errors.New(out)
175220
}
176221

177222
txHash, err := tx.Hash()
@@ -191,11 +236,11 @@ type protocolParameters struct {
191236
func (c *CardanoCli) ProtocolParams() (*cardano.ProtocolParams, error) {
192237
out, err := c.runCommand("query", "protocol-parameters")
193238
if err != nil {
194-
return nil, errors.New(string(out))
239+
return nil, errors.New(out)
195240
}
196241

197242
var cparams protocolParameters
198-
if err := json.Unmarshal(out, &cparams); err != nil {
243+
if err := json.Unmarshal([]byte(out), &cparams); err != nil {
199244
return nil, err
200245
}
201246

go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ go 1.18
55
require (
66
filippo.io/edwards25519 v1.0.0
77
github.com/blockfrost/blockfrost-go v0.1.0
8+
github.com/cardano-community/koios-go-client/v2 v2.0.2
89
github.com/dgraph-io/badger/v3 v3.2103.2
910
github.com/echovl/ed25519 v0.2.0
1011
github.com/matoous/go-nanoid/v2 v2.0.0
1112
github.com/spf13/cobra v1.4.0
13+
github.com/spf13/pflag v1.0.5
1214
github.com/spf13/viper v1.12.0
1315
github.com/tyler-smith/go-bip39 v1.1.0
1416
github.com/x448/float16 v0.8.4
@@ -37,17 +39,18 @@ require (
3739
github.com/pelletier/go-toml v1.9.5 // indirect
3840
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
3941
github.com/pkg/errors v0.9.1 // indirect
42+
github.com/shopspring/decimal v1.3.1 // indirect
4043
github.com/spf13/afero v1.8.2 // indirect
4144
github.com/spf13/cast v1.5.0 // indirect
4245
github.com/spf13/jwalterweatherman v1.1.0 // indirect
43-
github.com/spf13/pflag v1.0.5 // indirect
4446
github.com/subosito/gotenv v1.3.0 // indirect
4547
go.opencensus.io v0.23.0 // indirect
4648
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
4749
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
4850
golang.org/x/text v0.3.7 // indirect
51+
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
4952
google.golang.org/protobuf v1.28.0 // indirect
5053
gopkg.in/ini.v1 v1.66.4 // indirect
5154
gopkg.in/yaml.v2 v2.4.0 // indirect
52-
gopkg.in/yaml.v3 v3.0.0 // indirect
55+
gopkg.in/yaml.v3 v3.0.1 // indirect
5356
)

go.sum

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE
4545
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
4646
github.com/blockfrost/blockfrost-go v0.1.0 h1:s9+kk1L2pM+GEZZCZxX7z6+1eNYyyFFX0lUVp6fUgx8=
4747
github.com/blockfrost/blockfrost-go v0.1.0/go.mod h1:TYp7iHyuEm87IrTziSUA2+UaAor8a1lGGR499YyfPO4=
48+
github.com/cardano-community/koios-go-client/v2 v2.0.2 h1:/9x8w5KO/xRr8QwWlqMGkYpXi4w2VimcqUW7YBKA9d8=
49+
github.com/cardano-community/koios-go-client/v2 v2.0.2/go.mod h1:l6n5BGaZq8pj6iQ/cPsEi4DDNAea++MZBCi8qIm5/gs=
4850
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
4951
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
5052
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
@@ -212,6 +214,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
212214
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
213215
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
214216
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
217+
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
218+
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
215219
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
216220
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
217221
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
@@ -239,8 +243,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
239243
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
240244
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
241245
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
242-
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
243246
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
247+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
244248
github.com/subosito/gotenv v1.3.0 h1:mjC+YW8QpAdXibNi+vNWgzmgBH4+5l5dCXv8cNysBLI=
245249
github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs=
246250
github.com/tyler-smith/go-bip39 v1.1.0 h1:5eUemwrMargf3BSLRRCalXT93Ns6pQJIjYQN2nyfOP8=
@@ -408,6 +412,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
408412
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
409413
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
410414
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
415+
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af h1:Yx9k8YCG3dvF87UAn2tu2HQLf2dt/eR1bXxpLMWeH+Y=
416+
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
411417
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
412418
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
413419
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
@@ -564,8 +570,8 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
564570
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
565571
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
566572
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
567-
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
568-
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
573+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
574+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
569575
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
570576
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
571577
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

koios/client.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package koios
2+
3+
import (
4+
"context"
5+
6+
"github.com/cardano-community/koios-go-client/v2"
7+
"github.com/echovl/cardano-go"
8+
)
9+
10+
type KoiosCli struct {
11+
*koios.Client
12+
ctx context.Context
13+
network cardano.Network
14+
}
15+
16+
func NewNode(network cardano.Network, ctx context.Context, opts ...koios.Option) cardano.Node {
17+
if ctx == nil {
18+
ctx = context.Background()
19+
}
20+
if network == cardano.Testnet {
21+
opts = append(opts, koios.Host("testnet.koios.rest"))
22+
}
23+
24+
c, err := koios.New(opts...)
25+
if err != nil {
26+
panic(err)
27+
}
28+
return &KoiosCli{Client: c, ctx: ctx, network: network}
29+
}
30+
31+
// // utility function to know if continue pagination or not
32+
// // simply check if the "end of content range" is matching the PageSize
33+
// func isResponseComplete(res koios.Response) bool {
34+
// fields := strings.FieldsFunc(res.ContentRange, func(r rune) bool { return r == '-' || r == '/' })
35+
// if len(fields) != 3 {
36+
// return false
37+
// }
38+
// if endSide_, err := strconv.ParseUint(fields[1], 10, 16); err == nil {
39+
// return (uint16(endSide_) % uint16(koios.PageSize)) < uint16(koios.PageSize-1)
40+
// }
41+
// return false
42+
// }
43+
44+
func (kc *KoiosCli) UTxOs(a cardano.Address) ([]cardano.UTxO, error) {
45+
utxos := []cardano.UTxO{}
46+
opts := kc.NewRequestOptions()
47+
opts.QuerySet("select", "utxo_set")
48+
res, err := kc.GetAddressInfo(kc.ctx, koios.Address(a.Bech32()), opts)
49+
if err != nil || res.Data == nil {
50+
return nil, err
51+
}
52+
53+
for _, utxo := range res.Data.UTxOs {
54+
utxo_ := cardano.UTxO{
55+
TxHash: cardano.Hash32(utxo.TxHash),
56+
Index: uint64(utxo.TxIndex),
57+
Spender: a,
58+
Amount: cardano.NewValue(
59+
cardano.Coin(utxo.Value.IntPart())),
60+
}
61+
if len(utxo.AssetList) > 0 {
62+
ma := utxo_.Amount.MultiAsset
63+
for _, as := range utxo.AssetList {
64+
asset := cardano.NewAssets()
65+
policyId := cardano.NewPolicyIDFromHash(
66+
cardano.Hash28(as.PolicyID.String()))
67+
asset.Set(cardano.NewAssetName(string(as.AssetName)),
68+
cardano.BigNum(as.Quantity.IntPart()))
69+
ma.Set(policyId, asset)
70+
}
71+
}
72+
utxos = append(utxos, utxo_)
73+
}
74+
return utxos, nil
75+
}
76+
77+
// Tip returns the node's current tip
78+
func (kc *KoiosCli) Tip() (*cardano.NodeTip, error) {
79+
opts := kc.NewRequestOptions()
80+
opts.QuerySet("select", "block_no,epoch_no,abs_slot")
81+
res, err := kc.GetTip(kc.ctx, opts)
82+
if err != nil {
83+
return nil, err
84+
}
85+
return &cardano.NodeTip{
86+
Epoch: uint64(res.Data.EpochNo),
87+
Block: uint64(res.Data.BlockNo),
88+
Slot: uint64(res.Data.AbsSlot),
89+
}, nil
90+
}
91+
92+
// SubmitTx submits a transaction to the node using cbor encoding
93+
func (kc *KoiosCli) SubmitTx(*cardano.Tx) (*cardano.Hash32, error) {
94+
return nil, nil
95+
}
96+
97+
// ProtocolParams returns the Node's Protocol Parameters
98+
func (kc *KoiosCli) ProtocolParams() (*cardano.ProtocolParams, error) {
99+
opts := kc.NewRequestOptions()
100+
opts.QuerySet("limit", "1")
101+
res, err := kc.GetEpochParams(kc.ctx, nil, opts)
102+
if err != nil {
103+
return nil, err
104+
}
105+
ep := res.Data[0]
106+
return &cardano.ProtocolParams{
107+
MinFeeA: cardano.Coin(ep.MinFeeA.IntPart()),
108+
MinFeeB: cardano.Coin(ep.MinFeeB.IntPart()),
109+
MaxBlockBodySize: uint(ep.MaxBlockSize),
110+
MaxTxSize: uint(ep.MaxTxSize),
111+
// ...
112+
}, nil
113+
114+
}
115+
116+
// Network returns the node's current network type
117+
func (kc *KoiosCli) Network() cardano.Network {
118+
return kc.network
119+
}

0 commit comments

Comments
 (0)