From c4eb98608054b31d827559833318705567743a0f Mon Sep 17 00:00:00 2001 From: Piotr Szlachciak Date: Thu, 8 Aug 2019 10:21:26 +0200 Subject: [PATCH] Update ethereum package (#21) --- ethereum/README.md | 16 ++++++++-------- ethereum/package.json | 6 +++--- ethereum/src/asBigNumber.ts | 6 +++--- ethereum/src/asEthAddress.ts | 6 +++--- ethereum/test/asBigNumber.test.ts | 14 +++++++------- ethereum/test/asEthAddress.test.ts | 12 ++++++------ ethereum/yarn.lock | 8 ++++---- 7 files changed, 34 insertions(+), 34 deletions(-) diff --git a/ethereum/README.md b/ethereum/README.md index 7af1147..e0e1b03 100644 --- a/ethereum/README.md +++ b/ethereum/README.md @@ -29,10 +29,10 @@ yarn add @restless/ethereum Accepts any string that represents a valid ethereum address. Checks the checksum if present. Returns a normalized representation. ```javascript -asEthAddress('0xA5fE...f213') // RIGHT 0xA5fE...f213 -asEthAddress('0xa5fe...f213') // RIGHT 0xA5fE...f213 -asEthAddress('bla bla bla') // LEFT 'expected: ethereum address' -asBigNumber(123) // LEFT 'expected: ethereum address' +asEthAddress('0xA5fE...f213', 'path') // Result.ok(0xA5fE...f213) +asEthAddress('0xa5fe...f213', 'path') // Result.ok(0xA5fE...f213) +asEthAddress('bla bla bla', 'path') // Result.error([{expected: 'ethereum address', path: 'path'}]) +asEthAddress(123, 'path') // Result.error([{expected: 'ethereum address', path: 'path'}]) ``` ### `asBigNumber` @@ -40,8 +40,8 @@ asBigNumber(123) // LEFT 'expected: ethereum address' Accepts any string or number that represents an integer. Converts the integer to a [BigNumber](https://docs.ethers.io/ethers.js/html/api-utils.html#big-numbers). ```javascript -asBigNumber('123') // RIGHT BigNumber(123) -asBigNumber(456) // RIGHT BigNumber(456) -asBigNumber(1.5) // LEFT 'expected: big number' -asBigNumber(true) // LEFT 'expected: big number' +asBigNumber('123', 'path') // Result.ok(BigNumber(123)) +asBigNumber(456, 'path') // Result.ok(BigNumber(456)) +asBigNumber(1.5, 'path') // Result.error([{expected: 'big number', path: 'path'}]) +asBigNumber(true, 'path') // Result.error([{expected: 'big number', path: 'path'}]) ``` diff --git a/ethereum/package.json b/ethereum/package.json index c29cc47..3edfedc 100644 --- a/ethereum/package.json +++ b/ethereum/package.json @@ -1,6 +1,6 @@ { "name": "@restless/ethereum", - "version": "0.1.1", + "version": "0.2.0", "author": "Piotr Szlachciak ", "description": "Ethereum utilities for restless", "license": "Unlicense", @@ -31,8 +31,8 @@ "lint": "tslint --project tsconfig.json \"src/**/*.ts\" \"test/**/*.ts\"" }, "dependencies": { - "ethers": "^4.0.0", - "@restless/sanitizers": "^0.1.0" + "@restless/sanitizers": "^0.2.0", + "ethers": "^4.0.0" }, "devDependencies": { "@types/chai": "^4.1.7", diff --git a/ethereum/src/asBigNumber.ts b/ethereum/src/asBigNumber.ts index 99548cc..6d47999 100644 --- a/ethereum/src/asBigNumber.ts +++ b/ethereum/src/asBigNumber.ts @@ -1,11 +1,11 @@ -import { Either, Sanitizer } from '@restless/sanitizers' +import { Result, Sanitizer } from '@restless/sanitizers' import { utils } from 'ethers' export const asBigNumber: Sanitizer = (value, path) => { try { if (typeof value === 'string' || typeof value === 'number') { - return Either.right(utils.bigNumberify(value)) + return Result.ok(utils.bigNumberify(value)) } } catch {} // tslint:disable-line - return Either.left([{ path, expected: 'big number' }]) + return Result.error([{ path, expected: 'big number' }]) } diff --git a/ethereum/src/asEthAddress.ts b/ethereum/src/asEthAddress.ts index 30e2a59..2c63d68 100644 --- a/ethereum/src/asEthAddress.ts +++ b/ethereum/src/asEthAddress.ts @@ -1,11 +1,11 @@ -import { Either, Sanitizer } from '@restless/sanitizers' +import { Result, Sanitizer } from '@restless/sanitizers' import { utils } from 'ethers' export const asEthAddress: Sanitizer = (value, path) => { try { if (typeof value === 'string') { - return Either.right(utils.getAddress(value)) + return Result.ok(utils.getAddress(value)) } } catch {} // tslint:disable-line - return Either.left([{ path, expected: 'ethereum address' }]) + return Result.error([{ path, expected: 'ethereum address' }]) } diff --git a/ethereum/test/asBigNumber.test.ts b/ethereum/test/asBigNumber.test.ts index 56b25d2..179ecef 100644 --- a/ethereum/test/asBigNumber.test.ts +++ b/ethereum/test/asBigNumber.test.ts @@ -1,4 +1,4 @@ -import { Either } from '@restless/sanitizers' +import { Result } from '@restless/sanitizers' import { expect } from 'chai' import { utils } from 'ethers' import { asBigNumber } from '../src/asBigNumber' @@ -6,37 +6,37 @@ import { asBigNumber } from '../src/asBigNumber' describe('asBigNumber', () => { it('sanitizes strings that are valid big numbers', async () => { const result = asBigNumber('1234', '') - expect(result).to.deep.equal(Either.right(utils.bigNumberify('1234'))) + expect(result).to.deep.equal(Result.ok(utils.bigNumberify('1234'))) }) it('sanitizes hex strings that are valid big numbers', async () => { const result = asBigNumber('0xaBf3', '') - expect(result).to.deep.equal(Either.right(utils.bigNumberify('0xaBf3'))) + expect(result).to.deep.equal(Result.ok(utils.bigNumberify('0xaBf3'))) }) it('sanitizes numbers', async () => { const result = asBigNumber(1234, '') - expect(result).to.deep.equal(Either.right(utils.bigNumberify(1234))) + expect(result).to.deep.equal(Result.ok(utils.bigNumberify(1234))) }) it('does not accept non integers', async () => { const result = asBigNumber(1.234567, 'path') expect(result).to.deep.equal( - Either.left([{ path: 'path', expected: 'big number' }]) + Result.error([{ path: 'path', expected: 'big number' }]) ) }) it('does not accept non big number strings', async () => { const result = asBigNumber('bla bla bla', 'path') expect(result).to.deep.equal( - Either.left([{ path: 'path', expected: 'big number' }]) + Result.error([{ path: 'path', expected: 'big number' }]) ) }) it('does not accept types other than string or number', async () => { const result = asBigNumber(true, 'path') expect(result).to.deep.equal( - Either.left([{ path: 'path', expected: 'big number' }]) + Result.error([{ path: 'path', expected: 'big number' }]) ) }) }) diff --git a/ethereum/test/asEthAddress.test.ts b/ethereum/test/asEthAddress.test.ts index 5a9dd57..a1be069 100644 --- a/ethereum/test/asEthAddress.test.ts +++ b/ethereum/test/asEthAddress.test.ts @@ -1,4 +1,4 @@ -import { Either } from '@restless/sanitizers' +import { Result } from '@restless/sanitizers' import { expect } from 'chai' import { Wallet } from 'ethers' import { asEthAddress } from '../src/asEthAddress' @@ -7,20 +7,20 @@ describe('asEthAddress', () => { it('sanitizes strings that are valid ethereum addresses', async () => { const { address } = Wallet.createRandom() const result = asEthAddress(address, '') - expect(result).to.deep.equal(Either.right(address)) + expect(result).to.deep.equal(Result.ok(address)) }) it('keeps the string normalized', async () => { const { address } = Wallet.createRandom() const result = asEthAddress(address.toLowerCase(), '') - expect(result).to.deep.equal(Either.right(address)) + expect(result).to.deep.equal(Result.ok(address)) }) it('does not accept invalid ethereum addresses', async () => { const INVALID_ADDRESS = '0x7a4EdC939038dbB58befD7028b1625DFf2ca58b8' const result = asEthAddress(INVALID_ADDRESS, 'path') expect(result).to.deep.equal( - Either.left([{ path: 'path', expected: 'ethereum address' }]) + Result.error([{ path: 'path', expected: 'ethereum address' }]) ) }) @@ -28,14 +28,14 @@ describe('asEthAddress', () => { const NOT_AN_ADDRESS = 'bla bla bla' const result = asEthAddress(NOT_AN_ADDRESS, 'path') expect(result).to.deep.equal( - Either.left([{ path: 'path', expected: 'ethereum address' }]) + Result.error([{ path: 'path', expected: 'ethereum address' }]) ) }) it('does not accept non strings', async () => { const result = asEthAddress(123, 'path') expect(result).to.deep.equal( - Either.left([{ path: 'path', expected: 'ethereum address' }]) + Result.error([{ path: 'path', expected: 'ethereum address' }]) ) }) }) diff --git a/ethereum/yarn.lock b/ethereum/yarn.lock index 48d25b2..dff39c8 100644 --- a/ethereum/yarn.lock +++ b/ethereum/yarn.lock @@ -18,10 +18,10 @@ esutils "^2.0.2" js-tokens "^4.0.0" -"@restless/sanitizers@^0.1.0": - version "0.1.0" - resolved "https://registry.yarnpkg.com/@restless/sanitizers/-/sanitizers-0.1.0.tgz#7b1af5bbb15cabb3975b33a3539f04f295637f20" - integrity sha512-aMxomLCELnv115RMW4Xksn5By6WmVdqQK0GdZDa09sUdsHpoVP8/lyAtcAZY9amP91rEtkL7oksp3hrQ0pNftg== +"@restless/sanitizers@^0.2.0": + version "0.2.0" + resolved "https://registry.yarnpkg.com/@restless/sanitizers/-/sanitizers-0.2.0.tgz#f66ba872f8215a869fcf1290f5547c06b5a7eef5" + integrity sha512-b11WcVDC+xFn8p5uKhNwKv5La+L9+I4kjQDIyiWC4HIgwhcTtP5RT/7UJPjSpV+9k0Dr6BtVUnwEueS5kI3pZQ== "@types/chai@^4.1.7": version "4.1.7"