Skip to content

Commit

Permalink
feat: working policy quote
Browse files Browse the repository at this point in the history
  • Loading branch information
wregulski committed Jun 1, 2023
1 parent 3b00eb9 commit b6c287f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
7 changes: 4 additions & 3 deletions examples/policy_quote/policy_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
func main() {

// Create a new client
client, err := minercraft.NewClient(nil, nil, minercraft.MAPI, nil, nil)
client, err := minercraft.NewClient(nil, nil, minercraft.Arc, nil, nil)
if err != nil {
log.Fatalf("error occurred: %s", err.Error())
}

// Select the miner
miner := client.MinerByName(minercraft.MinerGorillaPool)
miner := client.MinerByName(minercraft.MinerTaal)

// Get a policy quote from a miner
var response *minercraft.PolicyQuoteResponse
Expand All @@ -25,8 +25,9 @@ func main() {
}

// Display the results
// todo: At this time (1/10/21) there is no policy response from any miner
log.Printf("miner: %s", response.Miner.Name)
log.Printf("is valid: %t", response.Validated)
log.Printf("callbacks: %+v", response.Quote.Callbacks)
log.Printf("policy quote: %+v", response.Quote.Policies)
log.Printf("fee payload fields: %+v", response.Quote.FeePayloadFields)
}
29 changes: 19 additions & 10 deletions policy_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (c *Client) PolicyQuote(ctx context.Context, miner *Miner) (*PolicyQuoteRes
return nil, result.Response.Error
}

queryResponse := &PolicyQuoteResponse{
quoteResponse := &PolicyQuoteResponse{
JSONEnvelope: JSONEnvelope{
ApiType: c.apiType,
Miner: result.Miner,
Expand All @@ -104,31 +104,40 @@ func (c *Client) PolicyQuote(ctx context.Context, miner *Miner) (*PolicyQuoteRes
switch c.apiType {
case MAPI:
model := &mapi.PolicyQuoteModel{}
err := queryResponse.process(result.Miner, result.Response.BodyContents)
if err != nil || len(queryResponse.Payload) <= 0 {
err := quoteResponse.process(result.Miner, result.Response.BodyContents)
if err != nil || len(quoteResponse.Payload) <= 0 {
return nil, err
}

err = json.Unmarshal([]byte(queryResponse.Payload), model)
err = json.Unmarshal([]byte(quoteResponse.Payload), model)
if err != nil {
return nil, err
}

modelAdapter = &PolicyQuoteMapiAdapter{PolicyQuoteModel: model}
case Arc:
model := &arc.PolicyQuoteModel{}
err := json.Unmarshal(result.Response.BodyContents, model)
if err != nil {
return nil, err
}

modelAdapter = &PolicyQuoteArcAdapter{PolicyQuoteModel: model}
default:
return nil, errors.New("unsupported api type")
}

// Parse the response
quoteResponse.Quote = modelAdapter.GetPolicyData()

isValid, err := quoteResponse.IsValid()
if err != nil {
return nil, err
}

// Valid?
if response.Quote == nil || len(response.Quote.Fees) == 0 {
return nil, errors.New("failed getting policy from: " + miner.Name)
}
quoteResponse.Validated = isValid

// Return the fully parsed response
return &response, nil
return quoteResponse, nil
}

func (a *PolicyQuoteMapiAdapter) GetPolicyData() *PolicyPayload {
Expand Down
1 change: 1 addition & 0 deletions query_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func (c *Client) QueryTransaction(ctx context.Context, miner *Miner, txID string
if err != nil {
return nil, err
}

modelAdapter = &QueryTxArcAdapter{QueryTxModel: model}

default:
Expand Down

0 comments on commit b6c287f

Please sign in to comment.