-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
190 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package koios | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cardano-community/koios-go-client/v2" | ||
"github.com/echovl/cardano-go" | ||
) | ||
|
||
type KoiosCli struct { | ||
*koios.Client | ||
ctx context.Context | ||
network cardano.Network | ||
} | ||
|
||
func NewNode(network cardano.Network, ctx context.Context, opts ...koios.Option) cardano.Node { | ||
if ctx == nil { | ||
ctx = context.Background() | ||
} | ||
if network == cardano.Testnet { | ||
opts = append(opts, koios.Host("testnet.koios.rest")) | ||
} | ||
|
||
c, err := koios.New(opts...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return &KoiosCli{Client: c, ctx: ctx, network: network} | ||
} | ||
|
||
// // utility function to know if continue pagination or not | ||
// // simply check if the "end of content range" is matching the PageSize | ||
// func isResponseComplete(res koios.Response) bool { | ||
// fields := strings.FieldsFunc(res.ContentRange, func(r rune) bool { return r == '-' || r == '/' }) | ||
// if len(fields) != 3 { | ||
// return false | ||
// } | ||
// if endSide_, err := strconv.ParseUint(fields[1], 10, 16); err == nil { | ||
// return (uint16(endSide_) % uint16(koios.PageSize)) < uint16(koios.PageSize-1) | ||
// } | ||
// return false | ||
// } | ||
|
||
func (kc *KoiosCli) UTxOs(a cardano.Address) ([]cardano.UTxO, error) { | ||
utxos := []cardano.UTxO{} | ||
opts := kc.NewRequestOptions() | ||
opts.QuerySet("select", "utxo_set") | ||
res, err := kc.GetAddressInfo(kc.ctx, koios.Address(a.Bech32()), opts) | ||
if err != nil || res.Data == nil { | ||
return nil, err | ||
} | ||
|
||
for _, utxo := range res.Data.UTxOs { | ||
utxo_ := cardano.UTxO{ | ||
TxHash: cardano.Hash32(utxo.TxHash), | ||
Index: uint64(utxo.TxIndex), | ||
Spender: a, | ||
Amount: cardano.NewValue( | ||
cardano.Coin(utxo.Value.IntPart())), | ||
} | ||
if len(utxo.AssetList) > 0 { | ||
ma := utxo_.Amount.MultiAsset | ||
for _, as := range utxo.AssetList { | ||
asset := cardano.NewAssets() | ||
policyId := cardano.NewPolicyIDFromHash( | ||
cardano.Hash28(as.PolicyID.String())) | ||
asset.Set(cardano.NewAssetName(string(as.AssetName)), | ||
cardano.BigNum(as.Quantity.IntPart())) | ||
ma.Set(policyId, asset) | ||
} | ||
} | ||
utxos = append(utxos, utxo_) | ||
} | ||
return utxos, nil | ||
} | ||
|
||
// Tip returns the node's current tip | ||
func (kc *KoiosCli) Tip() (*cardano.NodeTip, error) { | ||
opts := kc.NewRequestOptions() | ||
opts.QuerySet("select", "block_no,epoch_no,abs_slot") | ||
res, err := kc.GetTip(kc.ctx, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &cardano.NodeTip{ | ||
Epoch: uint64(res.Data.EpochNo), | ||
Block: uint64(res.Data.BlockNo), | ||
Slot: uint64(res.Data.AbsSlot), | ||
}, nil | ||
} | ||
|
||
// SubmitTx submits a transaction to the node using cbor encoding | ||
func (kc *KoiosCli) SubmitTx(*cardano.Tx) (*cardano.Hash32, error) { | ||
return nil, nil | ||
} | ||
|
||
// ProtocolParams returns the Node's Protocol Parameters | ||
func (kc *KoiosCli) ProtocolParams() (*cardano.ProtocolParams, error) { | ||
opts := kc.NewRequestOptions() | ||
opts.QuerySet("limit", "1") | ||
res, err := kc.GetEpochParams(kc.ctx, nil, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ep := res.Data[0] | ||
return &cardano.ProtocolParams{ | ||
MinFeeA: cardano.Coin(ep.MinFeeA.IntPart()), | ||
MinFeeB: cardano.Coin(ep.MinFeeB.IntPart()), | ||
MaxBlockBodySize: uint(ep.MaxBlockSize), | ||
MaxTxSize: uint(ep.MaxTxSize), | ||
// ... | ||
}, nil | ||
|
||
} | ||
|
||
// Network returns the node's current network type | ||
func (kc *KoiosCli) Network() cardano.Network { | ||
return kc.network | ||
} |