Skip to content

Commit

Permalink
Merge branch 'master' into viet/balance-tx
Browse files Browse the repository at this point in the history
  • Loading branch information
vvtran committed Jan 26, 2022
2 parents 1695b45 + 1c116c5 commit 91c386b
Show file tree
Hide file tree
Showing 21 changed files with 634 additions and 125 deletions.
4 changes: 2 additions & 2 deletions .gitignore
@@ -1,5 +1,4 @@
/bower_components/
/node_modules/
/.pulp-cache/
/output/
/generated-docs/
Expand All @@ -13,6 +12,7 @@ result
result-*
.envrc
.direnv
node_modules
/node_modules
/node_modules/
.node
.node-cfg
@@ -0,0 +1 @@
["0","12","-1","1.5","-1.431","1123098123928347023894729384","1\"k\"0","10","10E-20","-2.5324e-23",null,false,true,"10"]
@@ -0,0 +1 @@
{"10":"10.5","1 0":null,"1\"0":"10","2\"0":"-20e-20","nest":[{"a":"10e-20"},{"b":false}]}
@@ -0,0 +1 @@
{"a":"\\\\","b":"123"}
1 change: 1 addition & 0 deletions fixtures/test/parsing/json_stringify_numbers/input/t1.json
@@ -0,0 +1 @@
[0,12,-1,1.5,-1.431,1123098123928347023894729384,"1\"k\"0","10",10E-20,-2.5324e-23,null,false,true,10]
10 changes: 10 additions & 0 deletions fixtures/test/parsing/json_stringify_numbers/input/t2.json
@@ -0,0 +1,10 @@
{
"10": 10.5,
"1 0": null,
"1\"0": 10,
"2\"0": "-20e-20",
"nest": [
{"a":10e-20},
{"b":false}
]
}
1 change: 1 addition & 0 deletions fixtures/test/parsing/json_stringify_numbers/input/t3.json
@@ -0,0 +1 @@
{"a":"\\\\","b":123}
1 change: 1 addition & 0 deletions nix/dev-shell.nix
Expand Up @@ -18,6 +18,7 @@ pkgs.mkShell {
spago
purescript-language-server
purty
pscid
spago2nix
pkgs.nodePackages.node2nix
nodejs
Expand Down
24 changes: 24 additions & 0 deletions spago-packages.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions spago.dhall
Expand Up @@ -19,6 +19,7 @@ You can edit this file as you like.
, "exceptions"
, "foldable-traversable"
, "foreign-object"
, "gen"
, "identity"
, "lists"
, "maybe"
Expand All @@ -27,17 +28,23 @@ You can edit this file as you like.
, "newtype"
, "node-buffer"
, "node-fs-aff"
, "node-path"
, "nonempty"
, "ordered-collections"
, "partial"
, "prelude"
, "psci-support"
, "quickcheck"
, "rationals"
, "refs"
, "spec"
, "strings"
, "these"
, "transformers"
, "tuples"
, "uint"
, "undefined"
, "unordered-collections"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
Expand Down
6 changes: 3 additions & 3 deletions src/BalanceTx.purs
Expand Up @@ -53,7 +53,7 @@ import Data.Foldable as Foldable
import Data.Generic.Rep (class Generic)
import Data.List ((:), List(Nil), partition)
import Data.Map as Map
import Data.Maybe (fromMaybe, Maybe(Just, Nothing))
import Data.Maybe (fromMaybe, maybe, Maybe(Just, Nothing))
import Data.Newtype (class Newtype, unwrap, wrap)
import Data.Show.Generic (genericShow)
import Data.Tuple (fst)
Expand Down Expand Up @@ -712,7 +712,7 @@ balanceTxIns' utxos fees (TxBody txBody) = do
nonMintedValue :: Value
nonMintedValue =
Array.foldMap getAmount txOutputs
`minus` fromMaybe emptyValue txBody.mint
`minus` maybe emptyValue unwrap txBody.mint

minSpending :: Value
minSpending = lovelaceValueOf (fees + changeMinUtxo) <> nonMintedValue
Expand Down Expand Up @@ -810,7 +810,7 @@ balanceNonAdaOuts' changeAddr utxos txBody'@(TxBody txBody) =

nonMintedOutputValue :: Value
nonMintedOutputValue =
outputValue `minus` fromMaybe emptyValue txBody.mint
outputValue `minus` maybe emptyValue unwrap txBody.mint

nonAdaChange :: Value
nonAdaChange =
Expand Down
53 changes: 53 additions & 0 deletions src/Helpers.js
@@ -0,0 +1,53 @@

// jsonTurnNumbersToStrings :: String -> String
exports.jsonTurnNumbersToStrings = (str) => {
const s = String(str);

var arr = [];
var prev_in_number = false;
var in_number = false;
var in_string = false;
var escaped = -1;

for (var i = 0, n = s.length; i < n; ++i) {
const c = s[i];
// set the escape flag
if (in_string && escaped!=i){
if (c == "\\"){
escaped = i+1;
}
}
// set in_string flag
if (c == '"' && escaped!=i) {
in_string = !in_string;
}
// set in_number flag and quote numbers
if(!in_string){
if (c >= '0' && c <= '9' || c == '-') {
in_number=true;
}
// assuming a number can only end with:
if (c == ',' || c == '}' || c == ']' || /\s/.test(c)){
if(in_number){
arr.push("\"");
}
in_number=false;
}
if(in_number){
if(!prev_in_number){
arr.push("\"");
}
}
prev_in_number = in_number;
}
// push char if in string or is not a whitespace
if(in_string || !/\s/.test(c)){
arr.push(c);
}
}
// simple number case - close with quote
if (!in_string && in_number){
arr.push("\"");
}
return arr.join('');
};
19 changes: 19 additions & 0 deletions src/Helpers.purs
@@ -0,0 +1,19 @@
module Helpers(parseJsonStringifyNumbers, jsonTurnNumbersToStrings) where

import Data.Either (Either)
import Data.Argonaut (JsonDecodeError, Json, parseJson)

import Prelude

-- | Assuming a valid JSON string in its input, the function will quote each
-- | value that would otherwise be parsed by JSON.parse() as a number.
-- | NOTE it discards whitespaces outside of the would be json strings
foreign import jsonTurnNumbersToStrings :: String -> String


-- | Parse JSON from string. It parses numbers as strings.
parseJsonStringifyNumbers :: String -> Either JsonDecodeError Json
parseJsonStringifyNumbers s = do
_ <- parseJson s
-- valid json ensured at this point
parseJson $ jsonTurnNumbersToStrings s
12 changes: 7 additions & 5 deletions src/Lib.purs
Expand Up @@ -13,9 +13,10 @@ tx
:: Array Types.TransactionInput
-> Array Types.TransactionOutput
-> Array Types.TransactionInput
-> Types.Transaction
tx i o c = Types.Transaction
{ body: txBody i o c
-> Types.NetworkId
-> Types.Transaction
tx i o c id = Types.Transaction
{ body: txBody i o c id
, witness_set: txWitness
, is_valid: true -- why is this?
, auxiliary_data: Nothing
Expand All @@ -25,8 +26,9 @@ txBody
:: Array Types.TransactionInput
-> Array Types.TransactionOutput
-> Array Types.TransactionInput
-> Types.NetworkId
-> Types.TxBody
txBody inputs outputs collateral = Types.TxBody
txBody inputs outputs collateral id = Types.TxBody
{ inputs: inputs
, outputs: outputs
, fee: Types.Coin $ BigInt.fromInt 1000000
Expand All @@ -40,7 +42,7 @@ txBody inputs outputs collateral = Types.TxBody
, script_data_hash: Nothing
, collateral: Just $ collateral
, required_signers: Nothing
, network_id: Just $ Types.NetworkId 1
, network_id: Just $ id
}

txWitness :: Types.TransactionWitnessSet
Expand Down
3 changes: 2 additions & 1 deletion src/Ogmios.purs
Expand Up @@ -16,6 +16,7 @@ import Effect.Console (log)
import Effect.Exception (Error, error)
import Effect.Ref as Ref
import Types.JsonWsp (Address, JsonWspResponse, UtxoQR, mkUtxosAtQuery, parseJsonWspResponse)
import Helpers as Helpers

-- This module defines an Aff interface for Ogmios Websocket Queries
-- Since WebSockets do not define a mechanism for linking request/response
Expand Down Expand Up @@ -190,7 +191,7 @@ utxoQueryDispatch
-> String
-> Effect (Either Json.JsonDecodeError (Effect Unit))
utxoQueryDispatch ref str = do
let parsed' = parseJsonWspResponse =<< Json.parseJson str
let parsed' = parseJsonWspResponse =<< Helpers.parseJsonStringifyNumbers str
case parsed' of
(Left err) -> pure $ Left err
(Right res) -> afterParse res
Expand Down

0 comments on commit 91c386b

Please sign in to comment.