Skip to content

Commit

Permalink
feat: refactor folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Dec 20, 2024
1 parent b164593 commit 3b71da2
Show file tree
Hide file tree
Showing 22 changed files with 391 additions and 422 deletions.
2 changes: 1 addition & 1 deletion models/asset.go → asset.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package rum

import "strconv"

Expand Down
38 changes: 18 additions & 20 deletions providers/blockfrost.go → blockfrost.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package providers
package rum

import (
"context"
"encoding/hex"
"strconv"

"github.com/blockfrost/blockfrost-go"
"github.com/sidan-lab/rum/models"
"github.com/sidan-lab/rum/utils"
)

var NetworkMap = map[string]string{
Expand Down Expand Up @@ -53,10 +51,10 @@ func (bf *BlockfrostProvider) SubmitTx(txCbor string) (string, error) {
return txHash, err
}

func (bf *BlockfrostProvider) FetchTxInfo(hash string) (models.TransactionInfo, error) {
func (bf *BlockfrostProvider) FetchTxInfo(hash string) (TransactionInfo, error) {
bfTxInfo, err := bf.blockfrostClient.Transaction(context.TODO(), hash)
if err != nil {
return models.TransactionInfo{}, err
return TransactionInfo{}, err
}

invalidHereafter := ""
Expand All @@ -68,7 +66,7 @@ func (bf *BlockfrostProvider) FetchTxInfo(hash string) (models.TransactionInfo,
invalidBefore = *bfTxInfo.InvalidBefore
}

txInfo := models.TransactionInfo{
txInfo := TransactionInfo{
Block: bfTxInfo.Block,
Deposit: bfTxInfo.Deposit,
Fees: bfTxInfo.Fees,
Expand All @@ -82,32 +80,32 @@ func (bf *BlockfrostProvider) FetchTxInfo(hash string) (models.TransactionInfo,
return txInfo, nil
}

func (bf *BlockfrostProvider) FetchUTxOs(hash string, index *int) ([]models.UTxO, error) {
func (bf *BlockfrostProvider) FetchUTxOs(hash string, index *int) ([]UTxO, error) {
res, err := bf.blockfrostClient.TransactionUTXOs(context.TODO(), hash)
if err != nil {
return nil, err
}
bfOutputs := res.Outputs
utxos := BfToUtxos(bfOutputs, hash)
if index != nil {
utxo := utils.FindUtxoByIndex(utxos, *index)
utxo := FindUtxoByIndex(utxos, *index)
if utxo != nil {
return []models.UTxO{*utxo}, nil
return []UTxO{*utxo}, nil
}
return []models.UTxO{}, nil
return []UTxO{}, nil
}
return utxos, nil
}

func BfToUtxos(bfUtxos []blockfrost.TransactionOutput, hash string) []models.UTxO {
utxos := make([]models.UTxO, len(bfUtxos))
func BfToUtxos(bfUtxos []blockfrost.TransactionOutput, hash string) []UTxO {
utxos := make([]UTxO, len(bfUtxos))
for i, bfUtxo := range bfUtxos {
utxos[i] = BfToUtxo(bfUtxo, hash)
}
return utxos
}

func BfToUtxo(bfUtxo blockfrost.TransactionOutput, hash string) models.UTxO {
func BfToUtxo(bfUtxo blockfrost.TransactionOutput, hash string) UTxO {
dataHash := ""
if bfUtxo.DataHash != nil {
dataHash = *bfUtxo.DataHash
Expand All @@ -121,12 +119,12 @@ func BfToUtxo(bfUtxo blockfrost.TransactionOutput, hash string) models.UTxO {
referenceScriptHash = *bfUtxo.ReferenceScriptHash
}

return models.UTxO{
Input: models.Input{
return UTxO{
Input: Input{
TxHash: hash,
OutputIndex: int(bfUtxo.OutputIndex),
},
Output: models.Output{
Output: Output{
Amount: BfToAssets(bfUtxo.Amount),
Address: bfUtxo.Address,
DataHash: dataHash,
Expand All @@ -137,16 +135,16 @@ func BfToUtxo(bfUtxo blockfrost.TransactionOutput, hash string) models.UTxO {
}
}

func BfToAssets(bfAssets []blockfrost.TxAmount) []models.Asset {
assets := make([]models.Asset, len(bfAssets))
func BfToAssets(bfAssets []blockfrost.TxAmount) []Asset {
assets := make([]Asset, len(bfAssets))
for i, bfAsset := range bfAssets {
assets[i] = BfToAsset(bfAsset)
}
return assets
}

func BfToAsset(bfAsset blockfrost.TxAmount) models.Asset {
return models.Asset{
func BfToAsset(bfAsset blockfrost.TxAmount) Asset {
return Asset{
Quantity: bfAsset.Quantity,
Unit: bfAsset.Unit,
}
Expand Down
31 changes: 31 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package rum

type IProvider interface {
IFetcher
ISubmitter
}

type IFetcher interface {
// FetchAccountInfo(address string) (AccountInfo, error)
// FetchAddressUTxOs(address string, asset *string) ([]UTxO, error)
// FetchAssetAddresses(asset string) ([]struct {
// Address string
// Quantity string
// }, error)
// FetchAssetMetadata(asset string) (AssetMetadata, error)
// FetchBlockInfo(hash string) (BlockInfo, error)
// FetchCollectionAssets(policyId string, cursor *interface{}) (struct {
// Assets Assets
// Next *interface{}
// }, error)
// FetchHandle(handle string) (map[string]interface{}, error)
// FetchHandleAddress(handle string) (string, error)
// FetchProtocolParameters(epoch int) (Protocol, error)
FetchTxInfo(hash string) (TransactionInfo, error)
FetchUTxOs(hash string, index *int) ([]UTxO, error)
// Get(url string) (interface{}, error)
}

type ISubmitter interface {
SubmitTx(txCbor string) (string, error)
}
33 changes: 0 additions & 33 deletions interfaces/provider.go

This file was deleted.

40 changes: 19 additions & 21 deletions providers/maestro.go → maestro.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package providers
package rum

import (
"encoding/json"
"strconv"

"github.com/maestro-org/go-sdk/client"
msModel "github.com/maestro-org/go-sdk/models"
"github.com/sidan-lab/rum/models"
"github.com/sidan-lab/rum/utils"
)

type MaestroProvider struct {
maestroClient *client.Client
}

func NewMaestroProvider(apiKey string, network models.Network) *MaestroProvider {
func NewMaestroProvider(apiKey string, network Network) *MaestroProvider {
maestroClient := client.NewClient(apiKey, string(network))
return &MaestroProvider{
maestroClient: maestroClient,
Expand All @@ -26,13 +24,13 @@ func (ms *MaestroProvider) SubmitTx(txCbor string) (string, error) {
return txHash, err
}

func (ms *MaestroProvider) FetchTxInfo(hash string) (models.TransactionInfo, error) {
func (ms *MaestroProvider) FetchTxInfo(hash string) (TransactionInfo, error) {
tx, err := ms.maestroClient.TransactionDetails(hash)
if err != nil {
return models.TransactionInfo{}, err
return TransactionInfo{}, err
}
msTxInfo := tx.Data
txInfo := models.TransactionInfo{
txInfo := TransactionInfo{
Index: int(msTxInfo.BlockTxIndex),
Block: msTxInfo.BlockHash,
Hash: msTxInfo.TxHash,
Expand All @@ -52,32 +50,32 @@ type MsDatum struct {
Json any `json:"json"`
}

func (ms *MaestroProvider) FetchUTxOs(hash string, index *int) ([]models.UTxO, error) {
func (ms *MaestroProvider) FetchUTxOs(hash string, index *int) ([]UTxO, error) {
res, err := ms.maestroClient.TransactionDetails(hash)
if err != nil {
return nil, err
}
msOutputs := res.Data.Outputs
utxos := MsToUtxos(msOutputs)
if index != nil {
utxo := utils.FindUtxoByIndex(utxos, *index)
utxo := FindUtxoByIndex(utxos, *index)
if utxo != nil {
return []models.UTxO{*utxo}, nil
return []UTxO{*utxo}, nil
}
return []models.UTxO{}, nil
return []UTxO{}, nil
}
return utxos, nil
}

func MsToUtxos(msUtxos []msModel.Utxo) []models.UTxO {
utxos := make([]models.UTxO, len(msUtxos))
func MsToUtxos(msUtxos []msModel.Utxo) []UTxO {
utxos := make([]UTxO, len(msUtxos))
for i, msUtxo := range msUtxos {
utxos[i] = MsToUtxo(msUtxo)
}
return utxos
}

func MsToUtxo(msUtxo msModel.Utxo) models.UTxO {
func MsToUtxo(msUtxo msModel.Utxo) UTxO {
var datum MsDatum
if msUtxo.Datum != nil {
datumBytes, err := json.Marshal(msUtxo.Datum)
Expand All @@ -86,12 +84,12 @@ func MsToUtxo(msUtxo msModel.Utxo) models.UTxO {
json.Unmarshal(datumBytes, &datum)
}
}
return models.UTxO{
Input: models.Input{
return UTxO{
Input: Input{
TxHash: msUtxo.TxHash,
OutputIndex: int(msUtxo.Index),
},
Output: models.Output{
Output: Output{
Amount: MsToAssets(msUtxo.Assets),
Address: msUtxo.Address,
DataHash: datum.Hash,
Expand All @@ -102,16 +100,16 @@ func MsToUtxo(msUtxo msModel.Utxo) models.UTxO {
}
}

func MsToAssets(msAssets []msModel.Asset) []models.Asset {
assets := make([]models.Asset, len(msAssets))
func MsToAssets(msAssets []msModel.Asset) []Asset {
assets := make([]Asset, len(msAssets))
for i, msAsset := range msAssets {
assets[i] = MsToAsset(msAsset)
}
return assets
}

func MsToAsset(msAsset msModel.Asset) models.Asset {
return models.Asset{
func MsToAsset(msAsset msModel.Asset) Asset {
return Asset{
Quantity: strconv.FormatInt(msAsset.Amount, 10),
Unit: msAsset.Unit,
}
Expand Down
7 changes: 0 additions & 7 deletions main.go

This file was deleted.

Loading

0 comments on commit 3b71da2

Please sign in to comment.