Skip to content

Commit

Permalink
Merge pull request #11 from piqba/dev
Browse files Browse the repository at this point in the history
Dev migrate cardano API
  • Loading branch information
kenriortega authored Dec 18, 2021
2 parents ac84ffd + 2844b71 commit 8429ab4
Show file tree
Hide file tree
Showing 14 changed files with 252 additions and 136 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ dist/

dist/
*.txt
wallets.json
wallets.json
bin
47 changes: 38 additions & 9 deletions cmd/cli/commands/bb8.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ func Exce(ctx context.Context, repo domain.TxRepository, wallet storage.Wallet)
case "ADA":
addrInfo := lastTXForADA(networkType, address)

lastTX := addrInfo.Result.CATxList[len(addrInfo.Result.CATxList)-1]
lastTX := addrInfo.Data.Utxos[0]
tx := domain.ResultLastTxADA{
Addr: address,
CtbID: lastTX.CtbID,
CtbTimeIssued: fmt.Sprintf("%d", lastTX.CtbTimeIssued),
FromAddr: lastTX.CtbOutputs[0].CtaAddress,
ToAddr: lastTX.CtbOutputs[1].CtaAddress,
Balance: addrInfo.Result.CABalance.GetCoin,
Ammount: lastTX.CtbOutputs[1].CtaAmount.GetCoin,
CtbID: lastTX.TxHash,
CtbTimeIssued: lastTX.Transaction.IncludedAt,
FromAddr: lastTX.Transaction.Outputs[0].Address,
ToAddr: lastTX.Transaction.Outputs[1].Address,
Ammount: lastTX.Transaction.Inputs[0].Value,
Balance: lastTX.Transaction.Inputs[1].Value,
Symbol: symbol,
}

Expand Down Expand Up @@ -269,15 +269,44 @@ func Exce(ctx context.Context, repo domain.TxRepository, wallet storage.Wallet)
}

// lastTXForADA return las TX from Cardano
func lastTXForADA(networkType, address string) web3.AddrSumary {
func lastTXForADA(networkType, address string) web3.TxByAddrADAV2 {
cardano, err := web3.NewAPICardanoClient(web3.APIClientOptions{
NetworkType: networkType,
})
if err != nil {
logger.LogError(errors.Errorf("bb8: %s", err).Error())
}

sumary, err := cardano.InfoByAddress(context.TODO(), address)
pld := web3.PayloadReqJSONGQL{
Query: `
query utxoSetForAddress (
$address: String!
){
utxos(
order_by: { value: desc }
where: { address: { _eq: $address }}
limit :1
) {
# address,
value,
txHash,
transaction{
block{number, hash},
fee,
totalOutput,
includedAt,
inputs{address,value},
outputs{address, value}
},
}
}
`,
Variables: map[string]string{
"address": address,
},
}

sumary, err := cardano.LastTxByAddressADA(context.Background(), pld)
if err != nil {
logger.LogError(errors.Errorf("bb8: %s", err).Error())
}
Expand Down
52 changes: 52 additions & 0 deletions examples/ADA/gql/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"context"
"fmt"

"github.com/piqba/wallertme/pkg/errors"
"github.com/piqba/wallertme/pkg/logger"
"github.com/piqba/wallertme/pkg/web3"
)

func main() {

cardanoApi, err := web3.NewAPICardanoClient(web3.APIClientOptions{})
if err != nil {
logger.LogError(errors.Errorf("main:%s", err).Error())
}
pld := web3.PayloadReqJSONGQL{
Query: `
query utxoSetForAddress (
$address: String!
){
utxos(
order_by: { value: desc }
where: { address: { _eq: $address }}
limit :1
) {
# address,
value,
txHash,
transaction{
block{number, hash},
fee,
totalOutput,
includedAt,
inputs{address,value},
outputs{address, value}
},
}
}
`,
Variables: map[string]string{
"address": "addr_test1qq6g6s99g9z9w0mlvew28w40lpml9rwfkfgerpkg6g2vpn6dp4cf7k9drrdy0wslarr6hxspcw8ev5ed8lfrmaengneqz34lcx",
},
}

data, err := cardanoApi.LastTxByAddressADA(context.Background(), pld)
if err != nil {
logger.LogError(err.Error())
}
fmt.Println(data.ToJSON())
}
23 changes: 0 additions & 23 deletions examples/bb8/api/main.go

This file was deleted.

53 changes: 40 additions & 13 deletions examples/bb8/exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ func Exce(ctx context.Context, repo domain.TxRepository, wallet map[string]inter
case "ADA":
addrInfo := lastTXForADA(wallet["address"].(string))

lastTX := addrInfo.Result.CATxList[len(addrInfo.Result.CATxList)-1]
lastTX := addrInfo.Data.Utxos[0]
tx := domain.ResultLastTxADA{
Addr: wallet["address"].(string),
CtbID: lastTX.CtbID,
CtbTimeIssued: fmt.Sprintf("%d", lastTX.CtbTimeIssued),
FromAddr: lastTX.CtbOutputs[0].CtaAddress,
ToAddr: lastTX.CtbOutputs[1].CtaAddress,
Balance: addrInfo.Result.CABalance.GetCoin,
Ammount: lastTX.CtbOutputs[1].CtaAmount.GetCoin,
CtbID: lastTX.TxHash,
CtbTimeIssued: lastTX.Transaction.IncludedAt,
FromAddr: lastTX.Transaction.Outputs[0].Address,
ToAddr: lastTX.Transaction.Outputs[1].Address,
Ammount: lastTX.Transaction.Inputs[0].Value,
Balance: lastTX.Transaction.Inputs[1].Value,
}
switch exporterType {
case exporters.REDIS:
Expand Down Expand Up @@ -196,18 +196,45 @@ func Exce(ctx context.Context, repo domain.TxRepository, wallet map[string]inter

}

func lastTXForADA(address string) web3.AddrSumary {
cardano, err := web3.NewAPICardanoClient(web3.APIClientOptions{})
func lastTXForADA(address string) web3.TxByAddrADAV2 {
cardanoApi, err := web3.NewAPICardanoClient(web3.APIClientOptions{})
if err != nil {
logger.LogError(errors.Errorf("main:%s", err).Error())
}
pld := web3.PayloadReqJSONGQL{
Query: `
query utxoSetForAddress (
$address: String!
){
utxos(
order_by: { value: desc }
where: { address: { _eq: $address }}
limit :1
) {
# address,
value,
txHash,
transaction{
block{number, hash},
fee,
totalOutput,
includedAt,
inputs{address,value},
outputs{address, value}
},
}
}
`,
Variables: map[string]string{
"address": address,
},
}

sumary, err := cardano.InfoByAddress(context.TODO(), address)
data, err := cardanoApi.LastTxByAddressADA(context.Background(), pld)
if err != nil {
logger.LogError(errors.Errorf("main:%s", err).Error())
logger.LogError(err.Error())
}

return sumary
return data
}
func lastTXForSOL(address string) web3.TxInfo {
solanaApi, err := web3.NewAPISolanaClient(web3.APIClientOptions{})
Expand Down
4 changes: 2 additions & 2 deletions examples/r2d2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
)

var (
ADA = exporters.TXS_STREAM_KEY + ":" + "ADA"
SOL = exporters.TXS_STREAM_KEY + ":" + "SOL"
ADA = exporters.TXS_STREAM_KEY + "::" + "addr_test1qq6g6s99g9z9w0mlvew28w40lpml9rwfkfgerpkg6g2vpn6dp4cf7k9drrdy0wslarr6hxspcw8ev5ed8lfrmaengneqz34lcx"
SOL = exporters.TXS_STREAM_KEY + "::" + "9hZaTvCVMcfbheTzebkeGR6Xi2EzMqTtPasbhGoPB94C"
)

func init() {
Expand Down
39 changes: 0 additions & 39 deletions internal/bb8/domain/tx_ada.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package bb8
import (
"encoding/json"
"log"

"github.com/piqba/wallertme/pkg/web3"
)

// ResultLastTxADA return the last TX by ADA symbol (Cardano blockchain)
Expand Down Expand Up @@ -50,40 +48,3 @@ func (rtx *ResultLastTxADA) TruncateAddress(address string) string {
cleanAddress := prefix + "..." + sufix
return cleanAddress
}

// ResultInfoForADA information for ADA address
type ResultInfoForADA struct {
Address string `json:"address,omitempty"`
Type string `json:"type,omitempty"`
BlockNO int64 `json:"block_no,omitempty"`
BlockHash string `json:"block_hash,omitempty"`
TxTotal int64 `json:"tx_total,omitempty"`
Balance string `json:"balance,omitempty"`
TotalIn string `json:"total_in,omitempty"`
TotalOut string `json:"total_out,omitempty"`
TotalFee string `json:"total_fee,omitempty"`
TxList []web3.CATxList `json:"tx_list,omitempty"`
}

// ToJSON ...
func (rtx *ResultInfoForADA) ToJSON() string {
bytes, err := json.Marshal(rtx)
if err != nil {
log.Fatalf(err.Error())
}
return string(bytes)
}

// ToMAP ...
func (rtx *ResultInfoForADA) ToMAP() (toHashMap map[string]interface{}, err error) {

fromStruct, err := json.Marshal(rtx)
if err != nil {
return nil, err
}
if err := json.Unmarshal(fromStruct, &toHashMap); err != nil {
return toHashMap, err
}

return toHashMap, nil
}
34 changes: 0 additions & 34 deletions internal/bb8/domain/tx_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/go-redis/redis/v8"
"github.com/jmoiron/sqlx"
"github.com/piqba/wallertme/pkg/exporters"
"github.com/piqba/wallertme/pkg/web3"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
Expand Down Expand Up @@ -95,39 +94,6 @@ func (r *TxRepository) ExportData(ctx context.Context, data interface{}) error {
return nil
}

// InfoByAddress get info by address
func (r *TxRepository) InfoByAddress(ctx context.Context, address string) (ResultInfoForADA, error) {
_, span := otel.Tracer(nameBb8).Start(ctx, "InfoByAddress")
defer span.End()
cardano, err := web3.NewAPICardanoClient(web3.APIClientOptions{})
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return ResultInfoForADA{}, err
}

sumary, err := cardano.InfoByAddress(context.TODO(), address)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return ResultInfoForADA{}, err
}
span.SetAttributes(attribute.String("bb8.domain.info.address", "Success"))

return ResultInfoForADA{
Address: address,
Type: sumary.Result.CAType,
BlockNO: sumary.Result.CAChainTip.CTBlockNo,
BlockHash: sumary.Result.CAChainTip.CTBlockHash,
TxTotal: sumary.Result.CATxNum,
Balance: sumary.Result.CABalance.GetCoin,
TotalIn: sumary.Result.CATotalInput.GetCoin,
TotalOut: sumary.Result.CATotalOutput.GetCoin,
TotalFee: sumary.Result.CATotalFee.GetCoin,
TxList: sumary.Result.CATxList,
}, nil
}

// Set an address as a key and the last TX as a value
func (r *TxRepository) Set(ctx context.Context, address, lastTx string, expiration time.Duration) error {
_, span := otel.Tracer(nameBb8).Start(ctx, "Set")
Expand Down
1 change: 0 additions & 1 deletion internal/bb8/domain/txer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const (

// Txer define all methods that can be used for our worker...
type Txer interface {
InfoByAddress(ctx context.Context, address string) (ResultInfoForADA, error)
Set(ctx context.Context, key, value string, expiration time.Duration) error
Get(ctx context.Context, key string) (string, error)
}
Loading

0 comments on commit 8429ab4

Please sign in to comment.