Last active
April 25, 2020 18:15
-
-
Save kelonye/b9f5700dbdbf448e2064886064938e2b to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"bytes" | |
"encoding/binary" | |
"fmt" | |
) | |
var ETX uint32 = 0x03 | |
var EOT uint32 = 0x04 | |
var ENQ uint32 = 0x05 | |
func main() { | |
if h, err := cryptoPrice("BTC", 10); err != nil { | |
panic(fmt.Sprintf("%s", err)) | |
} else { | |
fmt.Printf("030000004254430a00000000000000 ==\n%s\n", h) | |
} | |
if h, err := openWeatherMap("paris", "main", "temp", 1); err != nil { | |
panic(fmt.Sprintf("%s", err)) | |
} else { | |
fmt.Printf("050000007061726973040000006d61696e0400000074656d700100000000000000 ==\n%s\n", h) | |
} | |
} | |
func cryptoPrice(symbol string, multiplier int64) (string, error) { | |
// script: 2 | |
var data = []interface{}{ | |
uint32(ETX), | |
[]byte(symbol), | |
uint64(multiplier), | |
} | |
h, err := buildCalldataHex(data) | |
return h, err | |
} | |
func openWeatherMap(country string, main_field string, sub_field string, multiplier int64) (string, error) { | |
// script: 11 | |
var data = []interface{}{ | |
uint32(ENQ), | |
[]byte(country), | |
uint32(EOT), | |
[]byte(main_field), | |
uint32(EOT), | |
[]byte(sub_field), | |
uint64(multiplier), | |
} | |
h, err := buildCalldataHex(data) | |
return h, err | |
} | |
func buildCalldataHex(data []interface{}) (string, error) { | |
calldata := new(bytes.Buffer) | |
for _, v := range data { | |
err := binary.Write(calldata, binary.LittleEndian, v) | |
if err != nil { | |
return "", fmt.Errorf("binary.Write failed:", err) | |
} | |
} | |
return fmt.Sprintf("%x", calldata.Bytes()), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment