Recent posts

#31
So, what can you actually DO with newLISP? / Re: Minimalistic 2048
Last post by rrq - November 12, 2024, 05:14:52 PM
Fun :)

May I also suggest that you lift out the command key dispatch as a key-action association list between key and action. Eg

(setf ACTIONS '(
    (87 up)    (119 up)
    (65 left)  (97  left)
    (85 down)  (115 down)
    (68 right) (100 right)
    (48 exit) ; "0" --> quit the game
))

The "case" statement would be replaced by something like the following "if" statement:

(if (lookup (setf key (read-key)) ACTIONS) (apply $it)
    (println "Wrong key.") (setq key-error true))

That would open it up slightly for easier change of command keys.
#32
newLISP in the real world / Re: replace help
Last post by rrq - November 12, 2024, 04:35:25 PM
So 'a gets set to a list of a string ? Did you mean to set it to the string rather? like
(set 'a "{'symbol': 'BURT/USDT', ... 'markPrice': None}")
#33
newLISP in the real world / replace help
Last post by joejoe - November 12, 2024, 12:04:58 PM
Hi and thanks for guidance!

I am trying to format this into valid json so I can json-parse it:

("{'symbol': 'BURT/USDT', 'timestamp': 1731440509530, 'datetime': '2024-11-12T19:41:49.530Z', 'high': 0.00353408, 'low': 0.00208606, 'bid': None, 'bidVolume': None, 'ask': None, 'askVolume': None, 'vwap': None, 'open': 0.00303488, 'close': 0.00246695, 'last': 0.00246695, 'previousClose': None, 'change': -0.00056793, 'percentage': -18.71, 'average': 0.00275091, 'baseVolume': None, 'quoteVolume': 281487.40662491, 'info': {'s': 'burt_usdt', 't': '1731440509530', 'cv': '-0.00056793', 'cr': '-0.1871', 'o': '0.00303488', 'l': '0.00208606', 'h': '0.00353408', 'c': '0.00246695', 'q': '97965253', 'v': '281487.40662491'}, 'indexPrice': None, 'markPrice': None}")
From what I understand, I have to change the single quotes to double quotes and change None to null, in order for it to be valid json, below, which validates as correct json:

{"symbol": "BURT/USDT", "timestamp": 1731440509530, "datetime": "2024-11-12T19:41:49.530Z", "high": 0.00353408, "low": 0.00208606, "bid": null, "bidVolume": null, "ask": null, "askVolume": null, "vwap": null, "open": 0.00303488, "close": 0.00246695, "last": 0.00246695, "previousClose": null, "change": -0.00056793, "percentage": -18.71, "average": 0.00275091, "baseVolume": null, "quoteVolume": 281487.40662491, "info": {"s": "burt_usdt", "t": "1731440509530", "cv": "-0.00056793", "cr": "-0.1871", "o": "0.00303488", "l": "0.00208606", "h": "0.00353408", "c": "0.00246695", "q": "97965253", "v": "281487.40662491"}, "indexPrice": null, "markPrice": null}
Here is what I tried:

(set 'a '("{'symbol': 'BURT/USDT', 'timestamp': 1731439797068, 'datetime': '2024-11-12T19:29:57.068Z', 'high': 0.00353408, 'low': 0.00208606, 'bid': None, 'bidVolume': None, 'ask': None, 'askVolume': None, 'vwap': None, 'open': 0.00294771, 'close': 0.00248834, 'last': 0.00248834, 'previousClose': None, 'change': -0.00045937, 'percentage': -15.58, 'average': 0.00271802, 'baseVolume': None, 'quoteVolume': 281817.75855221, 'info': {'s': 'burt_usdt', 't': '1731439797068', 'cv': '-0.00045937', 'cr': '-0.1558', 'o': '0.00294771', 'l': '0.00208606', 'h': '0.00353408', 'c': '0.00248834', 'q': '97998750', 'v': '281817.75855221'}, 'indexPrice': None, 'markPrice': None}"))

(replace "\'" a "\"")  ; gave the below with no changes

("{'symbol': 'BURT/USDT', 'timestamp': 1731439797068, 'datetime': '2024-11-12T19:29:57.068Z', 'high': 0.00353408, 'low': 0.00208606, 'bid': None, 'bidVolume': None, 'ask': None, 'askVolume': None, 'vwap': None, 'open': 0.00294771, 'close': 0.00248834, 'last': 0.00248834, 'previousClose': None, 'change': -0.00045937, 'percentage': -15.58, 'average': 0.00271802, 'baseVolume': None, 'quoteVolume': 281817.75855221, 'info': {'s': 'burt_usdt', 't': '1731439797068', 'cv': '-0.00045937', 'cr': '-0.1558', 'o': '0.00294771', 'l': '0.00208606', 'h': '0.00353408', 'c': '0.00248834', 'q': '97998750', 'v': '281817.75855221'}, 'indexPrice': None, 'markPrice': None}")

Do I need to use regex for this or what would be the best approach to take? Thanks very much!
#34
newLISP in the real world / Re: hmac signatures
Last post by joejoe - October 26, 2024, 03:36:08 PM
Gotcha,

Thanks for jogging the memory.

https://newlispfanclub.com/index.php?topic=4862.0

I will attempt to piece this together, much appreciated!

--jj
#35
newLISP in the real world / Re: hmac signatures
Last post by rrq - October 26, 2024, 01:30:46 PM
Wouldn't you need to use crypto:hmac ?
#36
newLISP in the real world / hmac signatures
Last post by joejoe - October 26, 2024, 10:54:59 AM
Hi there,

I am looking to follow step 2 of the api hmac authentication where it generates a signature from the string of GET + urlbody + timestamp, as indicated here:

https://docs.coinex.com/api/v2/authorization

As I understand it, the string to encrypt should look like this:

GET/assets/spot/balance1729963117000

which hashes to this:

ea68fa414dd208129b72ee5f4dcf34fd4b2a2c83121616da3f4cca454bd8d6e6

I am constructing the string with similar code to this:

(set 'timestamp (date-value (now)))
(set 'timestamp (push "000" (string timestamp) -1))  ; convert timestamp to milliseconds
(set 'user "xxx")

(set 'urlbase "https://api.coinex.com/v2")
(set 'urlendpoint "/assets/spot/balance")

(set 'urlbody (string "GET" urlendpoint timestamp))
(set 'urlbodyhash (crypto:sha256 urlbody))
(set 'url (string urlbase urlendpoint))

(set 'call (string "curl -X 'GET' " url " -H 'Content-type: application/json' -H 'X-COINEX-KEY: " user "' -H 'X-COINEX-SIGN: " urlbodyhash "' -H 'X-COINEX-TIMESTAMP: " timestamp "'"))

And the formatted curl request looks like this:

curl -X 'GET' https://api.coinex.com/v2/assets/spot/balance -H 'Content-type: application/json' -H 'X-COINEX-KEY: xxx' -H 'X-COINEX-SIGN: ea68fa414dd208129b72ee5f4dcf34fd4b2a2c83121616da3f4cca454bd8d6e6' -H 'X-COINEX-TIMESTAMP: 1729963117000'

When this is submitted to the api, I get this response:

("{\"code\":25,\"data\":{},\"message\":\"Signature Incorrect\"}")
This is how the docs suggest to construct the string (in python I believe):

prepared_str = "GET"+"/v2/spot/balance"+"1700490703564"
Have I generated the string correctly? or any thought to why the signature fails?

Thanks very much in advance for any tips! :+)

And this is the python example signature generation:

https://github.com/coinexcom/coinex_api_demo/blob/feat-api-v2/python/api.py
#37
So, what can you actually DO with newLISP? / Re: Minimalistic 2048
Last post by itistoday - August 01, 2024, 07:01:49 PM
Hah! This is pretty cool  :D

Some improvement suggestions: support for arrow keys and make `q` and ^C keys work to quit.
#38
So, what can you actually DO with newLISP? / Minimalistic 2048
Last post by cameyo - July 10, 2024, 05:58:29 AM
;---------------------------------
; Minimalistic 2048 (4x4)
; Use "W" "A" "S" "D" key to move
; (load "g2048.lsp")
;---------------------------------
(define (print-grid)
  (for (i 0 3)
    (for (j 0 3)
      (print (format "%4d " (grid i j))))
    (println)) '>)
;
(define (find-zeros)
  (let (pts '())
    (for (i 0 3)
      (for (j 0 3)
        (if (zero? (grid i j)) (push (list i j) pts -1)))) pts))
;
(define (new-game)
  (setq grid (array-list (array 4 4 '(0))))
  (setq zeros (randomize (find-zeros)))
  (setf (grid (zeros 0)) 2)
  (setf (grid (zeros 1)) 2)
  (input))
;
(define (input)
  (print-grid)
  (case (setq key (read-key))
    (87 (up))    (119 (up))
    (65 (left))  (97  (left))
    (85 (down))  (115 (down))
    (68 (right)) (100 (right))
    (48 (exit)) ; "0" --> quit the game
    (true (begin (println "Wrong key.") (setq key-error true)))
  )
  (cond ((ref 2048 grid) (println "Bravo! You win.") (print-grid))
        ((= key-error true) (setq key-error nil) (input))
        (true
          (setq zeros (randomize (find-zeros)))
          (when zeros ; put 2 or 4 in a free cell
            (if (zero? (rand 2))
                (setf (grid (zeros 0)) 2)
                (setf (grid (zeros 0)) 4)))
          (input))))
;
(define (shift-right row)
  (let ((non-zero (filter (fn (x) (!= x 0)) row))
        (zeroes (filter (fn (x) (= x 0)) row)))
    (extend zeroes non-zero)))
;
(define (shift-left row)
  (let ((non-zero (filter (fn (x) (!= x 0)) row))
        (zeroes (filter (fn (x) (= x 0)) row)))
    (extend non-zero zeroes)))
;
(define (matrix-left matrix) (map shift-left matrix))
;
(define (matrix-right matrix) (map shift-right matrix))
;
(define (matrix-down matrix)
  (let (trans (transpose matrix))
    (transpose (map shift-right trans))))
;
(define (matrix-up matrix)
  (let (trans (transpose matrix))
    (transpose (map shift-left trans))))
;
(define (merge-numbers row)
  (let ( (result '()) (idx 0) (len (length row)) )
    (while (< idx len)
      (if (and (< idx (- len 1)) (= (row idx) (row (+ idx 1))))
          (begin
            (push (* 2 (row idx)) result -1)
            (++ idx 2))
          (begin
            (push (row idx) result -1)
            (++ idx 1))))
    (extend result (dup 0 (- len (length result))))))
;
(define (matrix-merge matrix) (map merge-numbers matrix))
;
(define (right)
  (println "right")
  (setq grid (matrix-right grid))  ; move the numbers to right
  (setq grid (matrix-merge grid))  ; merge the numbers
  (setq grid (matrix-right grid))) ; move the numbers to right
;
(define (left)
  (println "left")
  (setq grid (matrix-left grid))
  (setq grid (matrix-merge grid))
  (setq grid (matrix-left grid)))
;
(define (up)
  (println "up")
  (setq grid (matrix-up grid))
  (setq grid (transpose (matrix-merge (transpose grid))))
  (setq grid (matrix-up grid)))
;
(define (down)
  (println "down")
  (setq grid (matrix-down grid))
  (setq grid (transpose (matrix-merge (transpose grid))))
  (setq grid (matrix-down grid)))
;
(new-game)
#39
newLISP in the real world / Re: A simple(?) problem
Last post by rrq - June 26, 2024, 02:08:55 PM
Yeah. Way faster than mine :)

chhers
#40
newLISP in the real world / Re: A simple(?) problem
Last post by cameyo - June 26, 2024, 11:36:06 AM
Thanks Ralph.
This is my solution:
(define (create-valid-numbers current)
  (if (<= current max-val)
      (begin
        (if (and (>= current min-val) (<= current max-val))
            (push current valid-numbers))
        ; creates numbers recursively
        (dolist (d digits)
          (create-valid-numbers (+ (* current 10) d))))))

(define (random-integer min-val max-val digits)
  (let (valid-numbers'())
    (dolist (d digits) (create-valid-numbers d))
    (valid-numbers (rand (length valid-numbers)))))

(random-integer 1 300 '(1 2 3))
;-> 21

(time (println (random-integer 1 90000000 '(1 2 3 4 5 6 7))))
;-> 44377532
;-> 6875.421