I'm working on building a crypto arbitrage bot in Purescript, mostly to learn about the functional programming paradigm. As a JS programmer primarily, I find Purescript's error messages very difficult to interpret. Currently, while trying to use the Affjax library to make an http request I'm running into a 'TypesDoNotUnify' error. My code looks like this:
import Affjax.ResponseFormat (ResponseFormat(..))
import Affjax.ResponseFormat as ResponseFormat
import Data.Argonaut.Core (Json, fromString, stringify)
import Data.Either (Either(..))
import Data.HTTP.Method (Method(..))
import Effect (Effect)
import Effect.Aff (Aff, launchAff_)
import Effect.Console (log)
import Node.Express.App (App, listenHttp, get)
import Node.Express.Response (send)
import Node.HTTP (Server)
import Node.HTTP.Client (method)
makeQuoteRequest :: String -> String -> String -> String
makeQuoteRequest fromAddress toAddress amount = "https://api.1inch.exchange/v3.0/137/quote?fromTokenAddress=" <> fromAddress <> "&toTokenAddress=" <> toAddress <> "&amount=" <> amount
sendQuoteRequest :: Either Error Unit
sendQuoteRequest = launchAff_ do
result <- AX.request(AX.defaultRequest {
url = makeQuoteRequest "0xd6df932a45c0f255f85145f286ea0b292b21c90b" "0xc2132d05d31c914a87c6611c10748aeb04b58e8f" "1000000000000000000"
,method = Left GET
,responseFormat = ResponseFormat.json
})
case result of
Left err -> log $ "Quote failed: " <> AX.printError err
Right response -> log $ "GET /api response: " <> stringify response.body
app :: App
app = do
get "/" $ send "<a href='http://localhost:8080/quotes'><button>Get Quotes</button></a>"
get "/quotes" $ send "This is where your quotes should go"
main :: Effect Server
main = do
listenHttp app 8080 \_ ->
log $ "Listening on " <> show 8080
VsCode highlights the line beginning with Left err -> log as the source of the problem, and when I hover over that the error I get the following additional info:
printError :: Error → String Could not match type
Effect
with type
Aff
while trying to match type Effect Unit with type Aff t0 while checking that expression (apply log) ((append "Quote failed: ") (printError err)) has type Aff t0 in value declaration sendQuoteRequest
where t0 is an unknown type PureScript(TypesDoNotUnify)
What I'm hoping to understand is not only how to fix this error, but to learn a little more about how to interpret the errors that Purescript gives me. What steps would you take to work through this error if it came up in your code?