Skip to content

Commit

Permalink
Purs 0.15 (#32)
Browse files Browse the repository at this point in the history
* wip

* remove unused deps

* refactor FFI to use CommonJS imports

* Remove foreign-generic De-/Encode in favor of simple-json Read-/WriteForeign

* run travis on ubuntu jammy as its latest lts

* bump tidy version, always run tidy-check

* run tidy

* add .tidyrc.json for purs-tidy consistency

* remove named instances

* README

* Forcing an empty commit.

* Forcing an empty commit.

---------

Co-authored-by: martyall <[email protected]>
  • Loading branch information
iostat and martyall authored Aug 29, 2023
1 parent 8222ef4 commit 52e6c01
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 151 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
package-lock.json
yarn.lock
/.spago/
.vscode
8 changes: 8 additions & 0 deletions .tidyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"indent": 2,
"operatorsFile": null,
"ribbon": 1,
"typeArrowPlacement": "first",
"unicode": "never",
"width": null
}
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
dist: focal
dist: jammy
sudo: required
node_js: stable
install:
Expand All @@ -8,7 +8,7 @@ install:
- spago build
script:
- npm run test
- if [ "$TRAVIS_BRANCH" == "master" ]; then npm run tidy-check; fi
- npm run tidy-check
after_success:
- >-
test $TRAVIS_TAG &&
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@
[![Build Status](https://travis-ci.com/f-o-a-m/purescript-eth-core.svg?branch=master)](https://travis-ci.com/f-o-a-m/purescript-eth-core)

Common types and utility functions for all web3 libraries


⚠️ You will also need to install additional **npm** dependencies specified [here](https://github.com/f-o-a-m/purescript-eth-core/blob/master/package.json#L7-L10)
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
"dependencies": {
"bn.js": "^4.11.0",
"keccak": "^1.0.2",
"pulp": "^15.0.0",
"rlp": "^2.0.0",
"secp256k1": "^3.0.1"
},
"devDependencies": {
"purescript": "^0.14.2",
"purescript": "^0.15.8",
"purescript-psa": "^0.8.2",
"purs-tidy": "^0.8.0",
"spago": "^0.20.3"
"purs-tidy": "^0.10.0",
"spago": "^0.20.9"
},
"scripts": {
"build-strict": "spago build",
"build": "spago build",
"tidy": "purs-tidy format-in-place \"src/**/*.purs\" \"test/**/*.purs\"",
"tidy-check": "purs-tidy check \"src/**/*.purs\" \"test/**/*.purs\"",
"test": "spago -x test.dhall test"
Expand Down
36 changes: 34 additions & 2 deletions packages.dhall
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.14.2-20210629/packages.dhall sha256:534c490bb73cae75adb5a39871142fd8db5c2d74c90509797a80b8bb0d5c3f7b
https://github.com/purescript/package-sets/releases/download/psc-0.15.7-20230306/packages.dhall
sha256:0757626c7422b8b5b5b1d0df3d3628e5deac755d7f89c433a9bf89009787dcbd

in upstream
let additions =
{ bytestrings =
{ dependencies =
[ "arrays"
, "console"
, "effect"
, "exceptions"
, "foldable-traversable"
, "integers"
, "leibniz"
, "maybe"
, "newtype"
, "node-buffer"
, "partial"
, "prelude"
, "quickcheck"
, "quickcheck-laws"
, "quotient"
, "unsafe-coerce"
]
, repo =
"https://github.com/rightfold/purescript-bytestrings"
, version = "6733a32fca306015b3428e9985ffac65325a9864"
}
, quotient =
{ dependencies = [ "prelude", "quickcheck" ]
, repo = "https://github.com/rightfold/purescript-quotient.git"
, version = "v3.0.0"
}
}

in upstream // additions
3 changes: 0 additions & 3 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
, "effect"
, "either"
, "foreign"
, "foreign-generic"
, "functions"
, "integers"
, "maybe"
, "node-buffer"
, "ordered-collections"
, "parsing"
, "partial"
, "prelude"
, "psci-support"
, "quotient"
, "ring-modules"
, "simple-json"
Expand Down
34 changes: 17 additions & 17 deletions src/Network/Ethereum/Core/BigNumber.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
"use strict";
var BigNumber = require('bn.js');
import BigNumber from "bn.js";

//NOTE: According to the documentation, many binary operators can take a normal js Number
//by suffixing the version for BN with 'n'.

exports._intToBigNumber = function(value) {
export const _intToBigNumber = function(value) {
return new BigNumber(value.toString(10), 10);
};

exports._numberToBigNumber = function(value) {
export const _numberToBigNumber = function(value) {
return new BigNumber(value);
};

exports._eqBigNumber = function(n) {
export const _eqBigNumber = function(n) {
return function(m) { return m.eq(n); };
};

exports._addBigNumber = function(n) {
export const _addBigNumber = function(n) {
return function (m) { return n.add(m); };
};

exports._mulBigNumber = function(n) {
export const _mulBigNumber = function(n) {
return function (m) { return n.mul(m); };
};

exports._subBigNumber = function(n) {
export const _subBigNumber = function(n) {
return function (m) { return n.sub(m); };
};

exports._divBigNumber = function(n) {
export const _divBigNumber = function(n) {
return function (m) { return n.div(m); }
}

exports._modBigNumber = function(n) {
export const _modBigNumber = function(n) {
return function(m) { return n.mod(m); }
}

exports.comparedTo = function (a) {
export const comparedTo = function (a) {
return function (b) {
return a.cmp(b);
};
};

exports.fromStringAsImpl = function (just) {
export const fromStringAsImpl = function (just) {
return function (nothing) {
return function (radix) {
var digits;
Expand Down Expand Up @@ -72,32 +72,32 @@ exports.fromStringAsImpl = function (just) {
};
};

exports.toString = function (radix) {
export const toString = function (radix) {
return function (bn) { return bn.toString(radix); };
};

exports.toTwosComplement = function (bn) {
export const toTwosComplement = function (bn) {
if (bn.ltn(0)) {
return new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).add(bn).addn(1);
} else {
return bn;
}
};

exports.floorBigNumber = function(bn) {
export const floorBigNumber = function(bn) {
var bnStr = bn.toString(10);
var newBn = new BigNumber(bnStr, 10);
return newBn;
};

exports.pow = function(n) {
export const pow = function(n) {
return function (m) {
var exp = new BigNumber(m, 10);
return n.pow(exp);
};
};

exports.toNumber = function (n) {
export const toNumber = function (n) {
var newN = new BigNumber(n);
return newN.toNumber();
};
Expand All @@ -112,7 +112,7 @@ var isString = function (object) {
(object && object.constructor && object.constructor.name === 'String');
};

exports.divide = function (n) {
export const divide = function (n) {
return function (d) {
var newN = n.div(d);
return newN;
Expand Down
41 changes: 17 additions & 24 deletions src/Network/Ethereum/Core/BigNumber.purs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import Data.Int (Radix, binary, decimal, hexadecimal, floor) as Int
import Data.Maybe (Maybe(..))
import Data.Ring.Module (class LeftModule, class RightModule)
import Foreign (ForeignError(..), readString, fail)
import Foreign.Class (class Decode, class Encode, decode, encode)
import Simple.JSON (class ReadForeign, class WriteForeign)
import Simple.JSON (class ReadForeign, class WriteForeign, writeImpl)

--------------------------------------------------------------------------------
-- * BigNumber
Expand All @@ -34,17 +33,17 @@ foreign import data BigNumber :: Type
-- | Convert a Big number into a string in the given base
foreign import toString :: Int.Radix -> BigNumber -> String

instance showBigNumber :: Show BigNumber where
instance Show BigNumber where
show = toString Int.decimal

foreign import _eqBigNumber :: BigNumber -> BigNumber -> Boolean

instance eqBigNumber :: Eq BigNumber where
instance Eq BigNumber where
eq = _eqBigNumber

foreign import comparedTo :: BigNumber -> BigNumber -> Int

instance ordBigNumber :: Ord BigNumber where
instance Ord BigNumber where
compare bn1 bn2 =
let
n = comparedTo bn1 bn2
Expand All @@ -65,35 +64,35 @@ embedInt = _intToBigNumber

foreign import _numberToBigNumber :: Number -> BigNumber

instance semiringBigNumber :: Semiring BigNumber where
instance Semiring BigNumber where
add = _addBigNumber
mul = _mulBigNumber
zero = embedInt 0
one = embedInt 1

foreign import _subBigNumber :: BigNumber -> BigNumber -> BigNumber

instance ringBigNumber :: Ring BigNumber where
instance Ring BigNumber where
sub = _subBigNumber

instance commutativeRingBigNumber :: CommutativeRing BigNumber
instance CommutativeRing BigNumber

foreign import _divBigNumber :: BigNumber -> BigNumber -> BigNumber

foreign import _modBigNumber :: BigNumber -> BigNumber -> BigNumber

instance euclidianRingBigNumber :: EuclideanRing BigNumber where
instance EuclideanRing BigNumber where
degree _ = 1
div = _divBigNumber
mod = _modBigNumber

instance bigNumberLModule :: LeftModule BigNumber Int where
instance LeftModule BigNumber Int where
mzeroL = embedInt 0
maddL = add
msubL = sub
mmulL a b = embedInt a * b

instance bigNumberRModule :: RightModule BigNumber Int where
instance RightModule BigNumber Int where
mzeroR = embedInt 0
maddR = add
msubR = sub
Expand All @@ -102,7 +101,7 @@ instance bigNumberRModule :: RightModule BigNumber Int where
class (Ring r, Ring a, LeftModule a r, RightModule a r) <= Algebra a r where
embed :: r -> a

instance embedInt' :: Algebra BigNumber Int where
instance Algebra BigNumber Int where
embed = embedInt

foreign import divide :: BigNumber -> BigNumber -> BigNumber
Expand Down Expand Up @@ -141,24 +140,18 @@ _decode str = case parseBigNumber Int.hexadecimal str of
Nothing -> Left $ "Failed to parse as BigNumber: " <> str
Just n -> Right n

instance decodeBigNumber :: Decode BigNumber where
decode x = do
instance ReadForeign BigNumber where
readImpl x = do
str <- readString x
either (fail <<< ForeignError) pure $ _decode str

instance readFBigNumber :: ReadForeign BigNumber where
readImpl = decode
instance WriteForeign BigNumber where
writeImpl = writeImpl <<< _encode

instance writeFBigNumber :: WriteForeign BigNumber where
writeImpl = encode

instance encodeBigNumber :: Encode BigNumber where
encode = encode <<< _encode

instance decodeJsonBigNumber :: A.DecodeJson BigNumber where
instance A.DecodeJson BigNumber where
decodeJson json = do
str <- A.decodeJson json
either (const <<< Left $ UnexpectedValue json) Right $ _decode str

instance encodeJsonBigNumber :: A.EncodeJson BigNumber where
instance A.EncodeJson BigNumber where
encodeJson = A.encodeJson <<< _encode
7 changes: 3 additions & 4 deletions src/Network/Ethereum/Core/HexString.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"use strict";
import BigNumber from "bn.js";

var BigNumber = require('bn.js');

exports.toBigNumber = function(str) {
export const toBigNumber = function(str) {
return new BigNumber(str, 16);
};

Expand All @@ -17,7 +16,7 @@ var signedIsNegative = function (value) {
return msb === '1';
};

exports.toBigNumberFromSignedHexString = function (value) {
export const toBigNumberFromSignedHexString = function (value) {
if (signedIsNegative(value)) {
return new BigNumber(value, 16).sub(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).subn(1);
}
Expand Down
Loading

0 comments on commit 52e6c01

Please sign in to comment.