Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
feat(Eth): handle pending and confirmed eth txs from websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
Thore3 committed Dec 12, 2017
1 parent 1128b39 commit dea69e2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/eth/eth-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ class EthAccount {
return txns;
}

updateFromIncomingTx (tx) {
if (tx.type === 'confirmed') {
this.fetchBalance();
} else if (tx.type === 'pending') {
let isSent = EthWalletTx.fromJSON(tx).isFromAccount(this);
let adjustBalance = (bal, val) => isSent ? bal.sub(val) : bal.add(val);
this.setData({
balance: adjustBalance(this.wei, toBigNumber(tx.value)).toString(),
nonce: isSent ? this.nonce + 1 : this.nonce
});
}
}

updateTxs (ethWallet) {
this.txs.forEach(tx => tx.update(ethWallet));
}
Expand Down
2 changes: 1 addition & 1 deletion src/eth/eth-socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class EthSocket extends StableSocket {
static accountMessageHandler (ethWallet, account, legacyAccount) {
return pipe(JSON.parse, (data) => {
if (data.op === OP_ACCOUNT_SUB && data.account === account.address) {

This comment has been minimized.

Copy link
@Vlisa228

Vlisa228 Jun 12, 2018

updateFromIncomingTx

account.setData(data);
account.updateFromIncomingTx(data.tx);
account.appendTransaction(data.tx).update(ethWallet);
if (legacyAccount && legacyAccount.isCorrectAddress(data.tx.from)) {
legacyAccount.setData({ balance: '0' });
Expand Down
31 changes: 31 additions & 0 deletions tests/eth/eth-account.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,37 @@ describe('EthAccount', () => {
})
})

describe('.updateFromIncomingTx()', () => {
it('should fetch account data if the tx is confirmed', () => {
spyOn(account, 'fetchBalance')
account.updateFromIncomingTx({ type: 'confirmed' })

This comment has been minimized.

Copy link
@Vlisa228

Vlisa228 Jun 12, 2018

updateFromIncomingTx

expect(account.balance).toEqual('0')
expect(account.nonce).toEqual(0)
expect(account.fetchBalance).toHaveBeenCalled()
})

it('should adjust the balance and nonce if the tx is sent / pending', () => {
account.setData({ nonce: 0, balance: '10000000000000000' })
account.updateFromIncomingTx({ type: 'pending', value: '10000000000000000', from: '0xD70073f72621FB90060Ac257f38cF2FF566Ea6bB' })
expect(account.balance).toEqual('0')
expect(account.nonce).toEqual(1)
})

it('should adjust the balance and nonce if the tx is received / pending', () => {
account.updateFromIncomingTx({ type: 'pending', value: '10000000000000000', to: '0xD70073f72621FB90060Ac257f38cF2FF566Ea6bB', from: '0xasdf' })
expect(account.balance).toEqual('0.01')
expect(account.nonce).toEqual(0)
})

it('should do nothing if the type is not confirmed or pending', () => {
spyOn(account, 'fetchBalance')
account.updateFromIncomingTx({ type: 'contract' })
expect(account.balance).toEqual('0')
expect(account.nonce).toEqual(0)
expect(account.fetchBalance).not.toHaveBeenCalled()
})
})

describe('.toJSON()', () => {
it('should serialize to json', () => {
let expected = '{"label":"Test Account","archived":false,"correct":false,"addr":"0xD70073f72621FB90060Ac257f38cF2FF566Ea6bB"}'
Expand Down
5 changes: 3 additions & 2 deletions tests/eth/eth-socket.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('EthSocket', () => {
setData () {},
fetchTransaction () {},
appendTransaction () { return { update () {} } },
updateFromIncomingTx () {},
isCorrectAddress (a) { return a === address }
})

Expand Down Expand Up @@ -68,9 +69,9 @@ describe('EthSocket', () => {
describe('.accountMessageHandler()', () => {
it('should call .setData on message', () => {
let handler = EthSocket.accountMessageHandler(ethWallet, account)
spyOn(account, 'setData')
spyOn(account, 'updateFromIncomingTx')
handler(balanceResponse)
expect(account.setData).toHaveBeenCalledWith(jasmine.objectContaining({ balance: '1000', nonce: 1 }))
expect(account.updateFromIncomingTx).toHaveBeenCalledWith(jasmine.objectContaining(JSON.parse(balanceResponse).tx))
})

it('should call .appendTransaction with the tx object', () => {
Expand Down

0 comments on commit dea69e2

Please sign in to comment.