-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
34 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@restless/ethereum", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"author": "Piotr Szlachciak <[email protected]>", | ||
"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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { Either, Sanitizer } from '@restless/sanitizers' | ||
import { Result, Sanitizer } from '@restless/sanitizers' | ||
import { utils } from 'ethers' | ||
|
||
export const asBigNumber: Sanitizer<utils.BigNumber> = (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' }]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { Either, Sanitizer } from '@restless/sanitizers' | ||
import { Result, Sanitizer } from '@restless/sanitizers' | ||
import { utils } from 'ethers' | ||
|
||
export const asEthAddress: Sanitizer<string> = (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' }]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
import { Either } from '@restless/sanitizers' | ||
import { Result } from '@restless/sanitizers' | ||
import { expect } from 'chai' | ||
import { utils } from 'ethers' | ||
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' }]) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters