This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1910 from bandprotocol/request-search
chain: Implement request-search REST and CLI endpoint
- Loading branch information
Showing
8 changed files
with
121 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/context" | ||
authclient "github.com/cosmos/cosmos-sdk/x/auth/client" | ||
|
||
"github.com/bandprotocol/bandchain/chain/x/oracle/types" | ||
) | ||
|
||
func queryRequest(route string, cliCtx context.CLIContext, rid string) ([]byte, int64, error) { | ||
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryRequests, rid), nil) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
return res, height, nil | ||
} | ||
|
||
func QuerySearchLatestRequest( | ||
route string, cliCtx context.CLIContext, oid, calldata, askCount, minCount string, | ||
) ([]byte, int64, error) { | ||
|
||
events := []string{ | ||
fmt.Sprintf("%s.%s='%s'", types.EventTypeRequest, types.AttributeKeyOracleScriptID, oid), | ||
fmt.Sprintf("%s.%s='%s'", types.EventTypeRequest, types.AttributeKeyCalldata, calldata), | ||
fmt.Sprintf("%s.%s='%s'", types.EventTypeRequest, types.AttributeKeyAskCount, askCount), | ||
fmt.Sprintf("%s.%s='%s'", types.EventTypeRequest, types.AttributeKeyMinCount, minCount), | ||
} | ||
searchResult, err := authclient.QueryTxsByEvents(cliCtx, events, 1, 30, "desc") | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
for _, tx := range searchResult.Txs { | ||
for _, log := range tx.Logs { | ||
for _, ev := range log.Events { | ||
if ev.Type != types.EventTypeRequest { | ||
continue | ||
} | ||
rid := "" | ||
ok := true | ||
for _, attr := range ev.Attributes { | ||
if attr.Key == types.AttributeKeyID { | ||
rid = attr.Value | ||
} | ||
if attr.Key == types.AttributeKeyOracleScriptID && attr.Value != oid || | ||
attr.Key == types.AttributeKeyCalldata && attr.Value != calldata || | ||
attr.Key == types.AttributeKeyAskCount && attr.Value != askCount || | ||
attr.Key == types.AttributeKeyMinCount && attr.Value != minCount { | ||
ok = false | ||
break | ||
} | ||
} | ||
if ok && rid != "" { | ||
// TODO: Check if request has resolved. | ||
return queryRequest(route, cliCtx, rid) | ||
} | ||
} | ||
} | ||
} | ||
return nil, 0, fmt.Errorf("request not found") | ||
} |
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
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