2021-03-19 09:11:48 +00:00
|
|
|
/*
|
2022-04-20 18:30:09 +00:00
|
|
|
Package oracle provides an interface to OracleContract native contract.
|
2021-03-19 09:11:48 +00:00
|
|
|
Oracles allow you to get external (non-blockchain) data using HTTPS or NeoFS
|
|
|
|
protocols.
|
|
|
|
*/
|
2021-02-08 08:06:28 +00:00
|
|
|
package oracle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2021-12-08 19:33:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/neogointernal"
|
2021-02-08 08:06:28 +00:00
|
|
|
)
|
|
|
|
|
2021-04-06 18:45:15 +00:00
|
|
|
// These are potential response codes you get in your callback completing
|
|
|
|
// oracle request. Resulting data is only passed with Success code, it's
|
|
|
|
// nil otherwise.
|
|
|
|
const (
|
|
|
|
Success = 0x00
|
|
|
|
ProtocolNotSupported = 0x10
|
|
|
|
ConsensusUnreachable = 0x12
|
|
|
|
NotFound = 0x14
|
|
|
|
Timeout = 0x16
|
|
|
|
Forbidden = 0x18
|
|
|
|
ResponseTooLarge = 0x1a
|
|
|
|
InsufficientFunds = 0x1c
|
|
|
|
Error = 0xff
|
|
|
|
)
|
|
|
|
|
2021-02-08 08:06:28 +00:00
|
|
|
// Hash represents Oracle contract hash.
|
2021-02-15 13:40:44 +00:00
|
|
|
const Hash = "\x58\x87\x17\x11\x7e\x0a\xa8\x10\x72\xaf\xab\x71\xd2\xdd\x89\xfe\x7c\x4b\x92\xfe"
|
2021-02-08 08:06:28 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MinimumResponseGas is the minimum response fee permitted for a request (that is
|
2021-04-06 19:23:11 +00:00
|
|
|
// you can't attach less than that to your request). It's 0.1 GAS at the moment.
|
|
|
|
const MinimumResponseGas = 10_000_000
|
|
|
|
|
2021-04-06 16:32:50 +00:00
|
|
|
// Request makes an oracle request. It can only be successfully invoked by
|
2022-04-20 18:30:09 +00:00
|
|
|
// a deployed contract and it takes the following parameters:
|
2021-04-06 16:32:50 +00:00
|
|
|
//
|
2022-08-08 10:23:21 +00:00
|
|
|
// - url
|
|
|
|
// URL to fetch, only https and neofs URLs are supported like
|
|
|
|
// https://example.com/some.json or
|
|
|
|
// neofs:6pJtLUnGqDxE2EitZYLsDzsfTDVegD6BrRUn8QAFZWyt/5Cyxb3wrHDw5pqY63hb5otCSsJ24ZfYmsA8NAjtho2gr
|
2021-04-06 16:32:50 +00:00
|
|
|
//
|
2022-08-08 10:23:21 +00:00
|
|
|
// - filter
|
|
|
|
// JSONPath filter to process the result; if specified, it will be
|
|
|
|
// applied to the data returned from HTTP/NeoFS and you'll only get
|
|
|
|
// filtered data in your callback method.
|
2021-04-06 16:32:50 +00:00
|
|
|
//
|
2022-08-08 10:23:21 +00:00
|
|
|
// - cb
|
|
|
|
// name of the method that will process oracle data, it must be a method
|
|
|
|
// of the same contract that invokes Request and it must have the following
|
|
|
|
// signature for correct invocation:
|
2021-04-06 16:32:50 +00:00
|
|
|
//
|
2023-04-03 10:34:24 +00:00
|
|
|
// - Method(url string, userData any, code int, result []byte)
|
2022-08-08 10:23:21 +00:00
|
|
|
// where url is the same url specified for Request, userData is anything
|
|
|
|
// passed in the next parameter, code is the status of the reply and
|
|
|
|
// result is the data returned from the request if any.
|
2021-04-06 16:32:50 +00:00
|
|
|
//
|
2022-08-08 10:23:21 +00:00
|
|
|
// - userData
|
|
|
|
// data to pass to the callback function.
|
2021-04-06 16:32:50 +00:00
|
|
|
//
|
2022-08-08 10:23:21 +00:00
|
|
|
// - gasForResponse
|
|
|
|
// GAS attached to this request for reply callback processing,
|
|
|
|
// note that it's different from the oracle request price, this
|
|
|
|
// GAS is used for oracle transaction's network and system fees,
|
|
|
|
// so it should be enough to pay for reply data as well as
|
|
|
|
// its processing.
|
2023-04-03 10:34:24 +00:00
|
|
|
func Request(url string, filter []byte, cb string, userData any, gasForResponse int) {
|
2021-12-08 19:33:03 +00:00
|
|
|
neogointernal.CallWithTokenNoRet(Hash, "request",
|
|
|
|
int(contract.States|contract.AllowNotify),
|
2021-02-08 08:06:28 +00:00
|
|
|
url, filter, cb, userData, gasForResponse)
|
|
|
|
}
|
2021-03-05 11:38:16 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetPrice returns the current oracle request price.
|
2021-03-05 11:38:16 +00:00
|
|
|
func GetPrice() int {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "getPrice", int(contract.ReadStates)).(int)
|
2021-03-05 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// SetPrice allows to set the oracle request price. This method can only be
|
2021-04-06 16:32:50 +00:00
|
|
|
// successfully invoked by the committee.
|
2021-03-05 11:38:16 +00:00
|
|
|
func SetPrice(amount int) {
|
2021-12-08 19:33:03 +00:00
|
|
|
neogointernal.CallWithTokenNoRet(Hash, "setPrice", int(contract.States), amount)
|
2021-03-05 11:38:16 +00:00
|
|
|
}
|