From b6c287fa8be644c0d185ebb16e018efd6f635146 Mon Sep 17 00:00:00 2001 From: wregulski Date: Wed, 31 May 2023 11:20:39 +0200 Subject: [PATCH] feat: working policy quote --- examples/policy_quote/policy_quote.go | 7 ++++--- policy_quote.go | 29 ++++++++++++++++++--------- query_transaction.go | 1 + 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/examples/policy_quote/policy_quote.go b/examples/policy_quote/policy_quote.go index dc89121..938f69e 100644 --- a/examples/policy_quote/policy_quote.go +++ b/examples/policy_quote/policy_quote.go @@ -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 @@ -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) } diff --git a/policy_quote.go b/policy_quote.go index af7d04a..2715eb1 100644 --- a/policy_quote.go +++ b/policy_quote.go @@ -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, @@ -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 { diff --git a/query_transaction.go b/query_transaction.go index 3e27607..6141bc6 100644 --- a/query_transaction.go +++ b/query_transaction.go @@ -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: