Skip to content

Commit

Permalink
release 0.7.4 (#188)
Browse files Browse the repository at this point in the history
* Fix/tx stall (#178)

* fixed bug where txStall event would be fired when the transaction has not stalled

* added check for status approved

* Fix race condition (#175)

* Fix/promievent (#180)

* fixed bug where txStall event would be fired when the transaction has not stalled

* added check for status approved

* resolve with promiEvent

* cloned inner object from contract method call (#182)

* Fix Duplicate Transaction Check (#184)

* added extra check for confirmed

* dont remomve from queue

* add on confirmation method (#186)

* Test truffle contract decoration (#176)

* Test contract decoration matches snapshot

* Update yarn.lock

* Improve Contract test coverage

* Classify existing Contract tests as unit tests

* Run contract tests with multiple web3 versions

* Clear localStorage between contract tests

* Improve comment

* Remove redundant comment

* Mock websocket in contract test

* Remove mock-socket from contract tests

* Only snapshot test a subset of decoratedContract

* Remove redundant logic

* Mock the web3 websocket connection

* Cleanup

* Use a complex ABI for contract decoration tests

* Refactor contract delegation for web3 >beta.37

* Improve comments

* Hardcode userAgent in tests

* Setup a ganache instance for use in the test env

* Improve ganache error handling

* Create web3.js test structure

* - Add test for web3Func bigNumber
- Add test for web3Func gasPrice

* Handle restarting ganache when jest is watching

* Add test for web3Funcs.contractGas

* Set ganache account generation to be deterministic

* - Add test for web3Func txGas
- Add test for web3Func balance
- Add test for web3Func accounts
- Add test for web3Func txReceipt

* Create ganacheConfig.js

* Deploy contract to testing ganache chain

* Remove redundant console.log

* Add truffle-contract dependency

* Change the contract deployed to test ganache chain

* Test estimating gas of a truffle method call

* Reduce logic duplication

* Improve comments

* Explicitly end tests once ganache is shutdown

* Fix estimateGas on truffle contract methods

* Test truffle contract decoration

* Update snapshot

* return promievent frim sendTransaction (#187)

* update to version 0.7.4
  • Loading branch information
cmeisl authored May 16, 2019
1 parent de4038f commit cea237c
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 118 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ yarn add bnc-assist
#### Script Tag

The library uses [semantic versioning](https://semver.org/spec/v2.0.0.html).
The current version is 0.7.3.
The current version is 0.7.4.
There are minified and non-minified versions.
Put this script at the top of your `<head>`

```html
<script src="https://assist.blocknative.com/0-7-3/assist.js"></script>
<script src="https://assist.blocknative.com/0-7-4/assist.js"></script>

<!-- OR... -->

<script src="https://assist.blocknative.com/0-7-3/assist.min.js"></script>
<script src="https://assist.blocknative.com/0-7-4/assist.min.js"></script>
```

### Initialize the Library
Expand Down Expand Up @@ -130,6 +130,18 @@ To speed things up, you can decorate the contract inline:
var myContract = assistInstance.Contract(new web3.eth.Contract(abi, address))
```

### `promiEvent`

If using web3 versions 1.0 and you would like to listen for the events triggered on the `promiEvent` that is normally returned from a transaction call, Assist returns the `promiEvent`, but it is wrapped in an object to prevent it from resolving internally in Assist.

```javascript
const { promiEvent } = await decoratedContract.myMethod(param).send(txOptions)

promiEvent.on('receipt', () => {
// ...
})
```

## API Reference

### Config
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bnc-assist",
"version": "0.7.3",
"version": "0.7.4",
"description": "Blocknative Assist js library for Dapp developers",
"main": "lib/assist.min.js",
"scripts": {
Expand Down
76 changes: 72 additions & 4 deletions src/__tests__/js/contract/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`using web3 0.20.6 Contract function is called it doesn't fail and returns the expected decorated contract 1`] = `
exports[`using web3 0.20.6 Contract is called with a truffle contract it doesn't fail and returns the expected decorated contract 1`] = `
Object {
"BatchRequest": undefined,
"_jsonInterface": undefined,
"abiModel": undefined,
"events": Object {
"contract": null,
},
"givenProvider": undefined,
"methods": Object {
"convert(uint256,uint256)": [Function],
"subtract(uint256,uint256)": [Function],
},
"options": undefined,
}
`;

exports[`using web3 0.20.6 Contract is called with a web3 contract it doesn't fail and returns the expected decorated contract 1`] = `
Object {
"Approval": [Function],
"Burn": [Function],
Expand Down Expand Up @@ -684,7 +701,24 @@ Object {
}
`;

exports[`using web3 1.0.0-beta.37 Contract function is called it doesn't fail and returns the expected decorated contract 1`] = `
exports[`using web3 1.0.0-beta.37 Contract is called with a truffle contract it doesn't fail and returns the expected decorated contract 1`] = `
Object {
"BatchRequest": undefined,
"_jsonInterface": undefined,
"abiModel": undefined,
"events": Object {
"contract": null,
},
"givenProvider": undefined,
"methods": Object {
"convert(uint256,uint256)": [Function],
"subtract(uint256,uint256)": [Function],
},
"options": undefined,
}
`;

exports[`using web3 1.0.0-beta.37 Contract is called with a web3 contract it doesn't fail and returns the expected decorated contract 1`] = `
Object {
"BatchRequest": [Function],
"_jsonInterface": Array [
Expand Down Expand Up @@ -2061,7 +2095,24 @@ Object {
}
`;

exports[`using web3 1.0.0-beta.46 Contract function is called it doesn't fail and returns the expected decorated contract 1`] = `
exports[`using web3 1.0.0-beta.46 Contract is called with a truffle contract it doesn't fail and returns the expected decorated contract 1`] = `
Object {
"BatchRequest": undefined,
"_jsonInterface": undefined,
"abiModel": undefined,
"events": Object {
"contract": null,
},
"givenProvider": undefined,
"methods": Object {
"convert(uint256,uint256)": [Function],
"subtract(uint256,uint256)": [Function],
},
"options": undefined,
}
`;

exports[`using web3 1.0.0-beta.46 Contract is called with a web3 contract it doesn't fail and returns the expected decorated contract 1`] = `
Object {
"BatchRequest": [Function],
"_jsonInterface": undefined,
Expand Down Expand Up @@ -7614,7 +7665,24 @@ Object {
}
`;

exports[`using web3 1.0.0-beta.55 Contract function is called it doesn't fail and returns the expected decorated contract 1`] = `
exports[`using web3 1.0.0-beta.55 Contract is called with a truffle contract it doesn't fail and returns the expected decorated contract 1`] = `
Object {
"BatchRequest": undefined,
"_jsonInterface": undefined,
"abiModel": undefined,
"events": Object {
"contract": null,
},
"givenProvider": undefined,
"methods": Object {
"convert(uint256,uint256)": [Function],
"subtract(uint256,uint256)": [Function],
},
"options": undefined,
}
`;

exports[`using web3 1.0.0-beta.55 Contract is called with a web3 contract it doesn't fail and returns the expected decorated contract 1`] = `
Object {
"BatchRequest": [Function],
"_jsonInterface": undefined,
Expand Down
199 changes: 121 additions & 78 deletions src/__tests__/js/contract/index.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import truffleContract from 'truffle-contract'
import { Server } from 'mock-socket'
import abi from '~/__tests__/res/dstoken.json'
import da from '~/js'
import * as web3Helpers from '~/js/helpers/web3'
import { initialState, updateState } from '~/js/helpers/state'
import { state, initialState, updateState } from '~/js/helpers/state'
import convertLibJson from '~/__tests__/res/ConvertLib.json'
import { convertLibAddress, port } from '../../../../internals/ganacheConfig'

const multidepRequire = require('multidep')('multidep.json')

// truffle contracts require an old web3
const Web3v0p20 = multidepRequire('web3', '0.20.6')

const someAddress = '0x0000000000000000000000000000000000000000'

const getTruffleContract = async () => {
const contractDef = truffleContract(convertLibJson)
contractDef.setProvider(
new Web3v0p20.providers.HttpProvider(`http://localhost:${port}`)
)
return contractDef.at(convertLibAddress)
}

const getWeb3Contract = async web3 =>
web3.eth.contract
? web3.eth.contract(abi).at(someAddress) // 0.20
: new web3.eth.Contract(abi, someAddress) // 1.0.0-beta

// multidep docs: https://github.com/joliss/node-multidep
multidepRequire.forEachVersion('web3', (version, Web3) => {
describe(`using web3 ${version}`, () => {
describe('Contract function is called', () => {
describe('Contract is called', () => {
let assistInstance
let web3
let contract
Expand All @@ -21,92 +40,116 @@ multidepRequire.forEachVersion('web3', (version, Web3) => {
beforeEach(() => {
mockServer = new Server(fakeURL)
web3 = new Web3(fakeURL)
contract = web3.eth.contract
? web3.eth.contract(abi).at(someAddress)
: new web3.eth.Contract(abi, someAddress)
assistInstance = da.init(config)
})
afterEach(() => {
mockServer.close()
})
test(`it doesn't fail and returns the expected decorated contract`, () => {
const assistInstance = da.init({ dappId: '123', web3, networkId: '1' })
const decoratedContract = assistInstance.Contract(contract)
const contracts = [
['truffle', getTruffleContract],
['web3', getWeb3Contract]
]
contracts.forEach(([name, getContract]) => {
describe(`with a ${name} contract`, () => {
beforeEach(async () => {
if (name === 'truffle') state.config.truffleContract = true
contract = await getContract(web3)
})
afterEach(() => {
if (name === 'truffle') state.config.truffleContract = false
})
test(`it doesn't fail and returns the expected decorated contract`, () => {
const assistInstance = da.init({
dappId: '123',
web3,
networkId: '1'
})
const decoratedContract = assistInstance.Contract(contract)

if (decoratedContract.methods) {
// test web3js v1.0.0-beta.X
expect({
givenProvider: decoratedContract.givenProvider,
BatchRequest: decoratedContract.BatchRequest,
options: decoratedContract.options,
methods: decoratedContract.methods,
abiModel: decoratedContract.abiModel,
_jsonInterface: decoratedContract._jsonInterface,
events: {
...decoratedContract.events,
contract: null
if (decoratedContract.methods) {
// test web3js v1.0.0-beta.X
expect({
givenProvider: decoratedContract.givenProvider,
BatchRequest: decoratedContract.BatchRequest,
options: decoratedContract.options,
methods: decoratedContract.methods,
abiModel: decoratedContract.abiModel,
_jsonInterface: decoratedContract._jsonInterface,
events: {
...decoratedContract.events,
contract: null
}
}).toMatchSnapshot()
} else {
// test web3js v0.20.X
expect({
...decoratedContract,
_eth: null
}).toMatchSnapshot()
}
}).toMatchSnapshot()
} else {
// test web3js v0.20.X
expect({
...decoratedContract,
_eth: null
}).toMatchSnapshot()
}
})
describe(`when user doesn't have a validApiKey`, () => {
beforeEach(() => {
updateState({ validApiKey: false })
})
test('should throw the expected error', () => {
expect(() => {
assistInstance.Contract(abi, someAddress)
}).toThrowError('Your API key is not valid')
})
})
describe(`when state.supportedNetwork is falsy`, () => {
beforeEach(() => {
updateState({ supportedNetwork: null })
})
test('should throw the expected error', () => {
expect(() => {
assistInstance.Contract(abi, someAddress)
}).toThrowError('This network is not supported')
})
})
describe('when user is on mobile and mobile is not blocked', () => {
beforeEach(() => {
updateState({ mobileDevice: true, config: { mobileBlocked: false } })
})
test('it should return the contract unmodified', () => {
const decoratedContract = assistInstance.Contract(contract)
expect(decoratedContract).toEqual(contract)
})
})
describe('when state.web3Instance is falsy', () => {
beforeEach(() => {
updateState({ web3Instance: undefined })
})
describe('and window.web3 exists', () => {
beforeEach(() => {
window.web3 = web3
})
afterEach(() => {
delete window.web3
describe(`when user doesn't have a validApiKey`, () => {
beforeEach(() => {
updateState({ validApiKey: false })
})
test('should throw the expected error', () => {
expect(() => {
assistInstance.Contract(abi, someAddress)
}).toThrowError('Your API key is not valid')
})
})
test('configureWeb3 should be called', () => {
const configureWeb3Mock = jest.spyOn(web3Helpers, 'configureWeb3')
assistInstance.Contract(contract)
expect(configureWeb3Mock).toHaveBeenCalledTimes(1)
configureWeb3Mock.mockRestore()
describe(`when state.supportedNetwork is falsy`, () => {
beforeEach(() => {
updateState({ supportedNetwork: null })
})
test('should throw the expected error', () => {
expect(() => {
assistInstance.Contract(abi, someAddress)
}).toThrowError('This network is not supported')
})
})
})
describe('and window.web3 is falsy', () => {
test('it should throw the expected error', () => {
expect(() => {
assistInstance.Contract(contract)
}).toThrowError('A web3 instance is needed to decorate contract')
describe('when user is on mobile and mobile is not blocked', () => {
beforeEach(() => {
updateState({
mobileDevice: true,
config: { mobileBlocked: false }
})
})
test('it should return the contract unmodified', () => {
const decoratedContract = assistInstance.Contract(contract)
expect(decoratedContract).toEqual(contract)
})
})
describe('when state.web3Instance is falsy', () => {
beforeEach(() => {
updateState({ web3Instance: undefined })
})
describe('and window.web3 exists', () => {
beforeEach(() => {
window.web3 = web3
})
afterEach(() => {
delete window.web3
})
test('configureWeb3 should be called', () => {
const configureWeb3Mock = jest.spyOn(
web3Helpers,
'configureWeb3'
)
assistInstance.Contract(contract)
expect(configureWeb3Mock).toHaveBeenCalledTimes(1)
configureWeb3Mock.mockRestore()
})
})
describe('and window.web3 is falsy', () => {
test('it should throw the expected error', () => {
expect(() => {
assistInstance.Contract(contract)
}).toThrowError(
'A web3 instance is needed to decorate contract'
)
})
})
})
})
})
Expand Down
Loading

0 comments on commit cea237c

Please sign in to comment.