From 311a397e00fe763ec67ddadb93d36a14b52df6e5 Mon Sep 17 00:00:00 2001 From: Shivani Aggarwal Date: Sat, 3 Apr 2021 20:18:24 -0400 Subject: [PATCH 01/11] Balance TDD --- .../inbox-compose.component.spec.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/app/inbox/inbox-compose/inbox-compose.component.spec.ts b/src/app/inbox/inbox-compose/inbox-compose.component.spec.ts index e0f3232..1652470 100644 --- a/src/app/inbox/inbox-compose/inbox-compose.component.spec.ts +++ b/src/app/inbox/inbox-compose/inbox-compose.component.spec.ts @@ -47,6 +47,8 @@ describe('InboxComposeComponent', () => { const currentAccount = '0x92d8f10248c6a3953cc3692a894655ad05d61efb'; const currentAccount2 = '0x0123456789012345678901234567890123456789'; + const balance = '1.018 Eth'; + const ensName = 'mailchain.eth'; const addresses = [currentAccount, currentAccount2]; @@ -134,6 +136,18 @@ describe('InboxComposeComponent', () => { expect(component.model.body).toBe('') }) + + describe('handling the balance', () => { + it('should initialize a balance in the "balance" field using an available balance', async () => { + component.balance = balance + await component.ngOnInit(); + fixture.detectChanges() + expect(component.balance).toBe(balance) + }) + }); + + + describe('handling the envelope', () => { it('should initialize an envelope in the "envelope" field using an available envelop', async () => { envelopes = mailchainTestService.envelopeTypeMli() @@ -603,3 +617,5 @@ describe('InboxComposeComponent', () => { }); }); + + From 1e72e6ee900dba7130014c9a63c29a0320ee7c96 Mon Sep 17 00:00:00 2001 From: Shivani Aggarwal Date: Sat, 3 Apr 2021 20:19:32 -0400 Subject: [PATCH 02/11] Balance component implemenation --- src/app/inbox/inbox-compose/inbox-compose.component.html | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/app/inbox/inbox-compose/inbox-compose.component.html b/src/app/inbox/inbox-compose/inbox-compose.component.html index ba9237e..e71ff74 100644 --- a/src/app/inbox/inbox-compose/inbox-compose.component.html +++ b/src/app/inbox/inbox-compose/inbox-compose.component.html @@ -7,6 +7,14 @@

Compose New

+ +
+
+ Balance: +
+
+ +
From bcebd11d77e21cb07442ecf98c9eec4fb0f0fe2a Mon Sep 17 00:00:00 2001 From: Shivani Aggarwal Date: Sat, 3 Apr 2021 20:19:50 -0400 Subject: [PATCH 03/11] Balance variable define --- src/app/inbox/inbox-compose/inbox-compose.component.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app/inbox/inbox-compose/inbox-compose.component.ts b/src/app/inbox/inbox-compose/inbox-compose.component.ts index 4c3bef3..fb4b99f 100644 --- a/src/app/inbox/inbox-compose/inbox-compose.component.ts +++ b/src/app/inbox/inbox-compose/inbox-compose.component.ts @@ -53,6 +53,8 @@ export class InboxComposeComponent implements OnInit { public contentTypeSwitchLabel: string = "" public envelopeType public envelopeDescription + public balance + @ViewChild('editor') public editorComponent: CKEditorComponent; From c0df6fe428478134b75487d47068c175bd6f5876 Mon Sep 17 00:00:00 2001 From: Shivani Aggarwal Date: Sat, 3 Apr 2021 20:20:50 -0400 Subject: [PATCH 04/11] Fees variable --- src/app/inbox/inbox-compose/inbox-compose.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/inbox/inbox-compose/inbox-compose.component.ts b/src/app/inbox/inbox-compose/inbox-compose.component.ts index fb4b99f..0ed4dca 100644 --- a/src/app/inbox/inbox-compose/inbox-compose.component.ts +++ b/src/app/inbox/inbox-compose/inbox-compose.component.ts @@ -54,7 +54,7 @@ export class InboxComposeComponent implements OnInit { public envelopeType public envelopeDescription public balance - + public fees @ViewChild('editor') public editorComponent: CKEditorComponent; From 6537b214182b1a695300122be9463e7b69fc52d8 Mon Sep 17 00:00:00 2001 From: Shivani Aggarwal Date: Sat, 3 Apr 2021 20:21:06 -0400 Subject: [PATCH 05/11] Fees TDD --- .../inbox-compose/inbox-compose.component.spec.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/app/inbox/inbox-compose/inbox-compose.component.spec.ts b/src/app/inbox/inbox-compose/inbox-compose.component.spec.ts index 1652470..ebbd337 100644 --- a/src/app/inbox/inbox-compose/inbox-compose.component.spec.ts +++ b/src/app/inbox/inbox-compose/inbox-compose.component.spec.ts @@ -48,7 +48,7 @@ describe('InboxComposeComponent', () => { const currentAccount = '0x92d8f10248c6a3953cc3692a894655ad05d61efb'; const currentAccount2 = '0x0123456789012345678901234567890123456789'; const balance = '1.018 Eth'; - + const fees = '0.00008 Eth'; const ensName = 'mailchain.eth'; const addresses = [currentAccount, currentAccount2]; @@ -146,6 +146,14 @@ describe('InboxComposeComponent', () => { }) }); + describe('handling the fees', () => { + it('should initialize a Fees in the "fees" field', async () => { + component.fees = fees + await component.ngOnInit(); + fixture.detectChanges() + expect(component.fees).toBe(fees) + }) + }); describe('handling the envelope', () => { From d9ee6382eaa0fea10d15449be9d0f8148ba14547 Mon Sep 17 00:00:00 2001 From: Shivani Aggarwal Date: Sat, 3 Apr 2021 20:21:24 -0400 Subject: [PATCH 06/11] Fees component Imp --- src/app/inbox/inbox-compose/inbox-compose.component.html | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/app/inbox/inbox-compose/inbox-compose.component.html b/src/app/inbox/inbox-compose/inbox-compose.component.html index e71ff74..73343b9 100644 --- a/src/app/inbox/inbox-compose/inbox-compose.component.html +++ b/src/app/inbox/inbox-compose/inbox-compose.component.html @@ -14,7 +14,13 @@

Compose New

- + +
+
+ Fees: +
+
+ From d68a09704098a2f49b8b2449c652b9156e40773a Mon Sep 17 00:00:00 2001 From: Shivani Aggarwal Date: Tue, 6 Apr 2021 15:11:23 -0400 Subject: [PATCH 07/11] balance service --- src/app/services/mailchain/addresses/balance.service.spec.ts | 0 src/app/services/mailchain/addresses/balance.service.ts | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/app/services/mailchain/addresses/balance.service.spec.ts create mode 100644 src/app/services/mailchain/addresses/balance.service.ts diff --git a/src/app/services/mailchain/addresses/balance.service.spec.ts b/src/app/services/mailchain/addresses/balance.service.spec.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/app/services/mailchain/addresses/balance.service.ts b/src/app/services/mailchain/addresses/balance.service.ts new file mode 100644 index 0000000..e69de29 From c1176d7a0633d4b07808919e15c7af9c754b5de1 Mon Sep 17 00:00:00 2001 From: Shivani Aggarwal Date: Tue, 6 Apr 2021 20:09:00 -0400 Subject: [PATCH 08/11] Balance Service --- .../addresses/balance.service.spec.ts | 72 +++++++++++++++++ .../mailchain/addresses/balance.service.ts | 78 +++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/src/app/services/mailchain/addresses/balance.service.spec.ts b/src/app/services/mailchain/addresses/balance.service.spec.ts index e69de29..dc06b8d 100644 --- a/src/app/services/mailchain/addresses/balance.service.spec.ts +++ b/src/app/services/mailchain/addresses/balance.service.spec.ts @@ -0,0 +1,72 @@ +import { TestBed } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; + +import { BalanceService } from './balance.service'; +import { MailchainTestService } from 'src/app/test/test-helpers/mailchain-test.service'; +import { HttpHelpersService } from '../../helpers/http-helpers/http-helpers.service'; +import { ProtocolsServiceStub } from '../protocols/protocols.service.stub'; +import { ProtocolsService } from '../protocols/protocols.service'; +import { Observable, of } from 'rxjs'; + + +describe('BalanceService', () => { + + let balanceService: BalanceService; + let httpTestingController: HttpTestingController; + let mailchainTestService: MailchainTestService + let serverResponse + + const currentAccount = '0x92d8f10248c6a3953cc3692a894655ad05d61efb'; + const desiredUrl = `http://127.0.0.1:8080/api/addresses/` + currentAccount + `/balance?protocol=ethereum&network=mainnet` + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ + HttpHelpersService, + { provide: ProtocolsService, useClass: ProtocolsServiceStub }, + BalanceService, + ], + imports: [HttpClientTestingModule] + }); + + balanceService = TestBed.inject(BalanceService); + mailchainTestService = TestBed.inject(MailchainTestService); + httpTestingController = TestBed.inject(HttpTestingController); + + //serverResponse = mailchainTestService.senderAddressesEthereumObserveResponse() + // expectedAddresses = mailchainTestService.senderAddresses() + }); + + afterEach(() => { + httpTestingController.verify(); + }); + + + describe('initUrl', () => { + it('should initialize the url', () => { + balanceService.initUrl() + expect(balanceService['url']).toEqual('http://127.0.0.1:8080/api') + }); + }) + + it('should be created', () => { + expect(balanceService).toBeTruthy(); + }); + + describe('getBalance', () => { + beforeEach(() => { + let obs: Observable = of(mailchainTestService.senderAddressesEthereumObserveResponse()) + spyOn(balanceService, 'getBalance').and.returnValue( + obs.toPromise() + ) + }); + it('should get balance for addresses', async () => { + let result + await balanceService.getBalance(currentAccount, 'ethereum', 'mainnet').then(res => { + result = res + }); + expect(result).toEqual(mailchainTestService.senderAddressesEthereumObserveResponse()) + expect(balanceService.getBalance).toHaveBeenCalled() + }); + }) +}); diff --git a/src/app/services/mailchain/addresses/balance.service.ts b/src/app/services/mailchain/addresses/balance.service.ts index e69de29..cf5bd25 100644 --- a/src/app/services/mailchain/addresses/balance.service.ts +++ b/src/app/services/mailchain/addresses/balance.service.ts @@ -0,0 +1,78 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { LocalStorageServerService } from '../../helpers/local-storage-server/local-storage-server.service'; +import { HttpHelpersService } from '../../helpers/http-helpers/http-helpers.service'; + +@Injectable({ + providedIn: 'root' +}) +export class BalanceService { + + private url: string + + constructor( + private http: HttpClient, + private httpHelpersService: HttpHelpersService, + private localStorageServerService: LocalStorageServerService, + ) { + this.initUrl() + } + + /** + * Initialize URL from local storage + */ + async initUrl() { + this.url = `${this.localStorageServerService.getCurrentServerDetails()}/api` + } + + /** + * Get and return the balance for the address + */ + async getBalance(address, protocol, network) { + var httpOptions = this.httpHelpersService.getHttpOptions([ + ['protocol', protocol], + ['network', network] + ]) + + let res = await this.http.get( + this.url + `/addresses/` + address + `/balance`, + httpOptions + // TODO handle failure + ).toPromise(); + + return res["body"]["balance"] + } + + /** + * handleAddressFormatting + * @param address + * @param encoding "hex/0x-prefix" | "base58/plain" + */ + handleAddressFormatting(address, encoding) { + switch (encoding) { + case 'hex/0x-prefix': + return address.toLowerCase() + case 'base58/plain': + return address + default: + return address + } + } + + + /** + * Returns the balance response with status codes + */ + public getBalanceResponse(address, protocol, network) { + var httpOptions = this.httpHelpersService.getHttpOptions([ + ['protocol', protocol], + ['network', network] + ]) + + return this.http.get( + this.url + `/addresses/` + address + `/balance`, + httpOptions + ) + } + +} From 61b39b822e40482489238471da5575396e20accd Mon Sep 17 00:00:00 2001 From: Shivani Aggarwal Date: Fri, 9 Apr 2021 20:44:57 -0400 Subject: [PATCH 09/11] Mailchain inbox balance feature --- .../inbox-compose.component.html | 10 ++++--- .../inbox-compose.component.spec.ts | 6 ++--- .../inbox-compose/inbox-compose.component.ts | 27 +++++++++++++++++-- .../addresses/balance.service.spec.ts | 4 +-- .../test-helpers/mailchain-test.service.ts | 22 ++++++++++++++- 5 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/app/inbox/inbox-compose/inbox-compose.component.html b/src/app/inbox/inbox-compose/inbox-compose.component.html index 73343b9..d8c018d 100644 --- a/src/app/inbox/inbox-compose/inbox-compose.component.html +++ b/src/app/inbox/inbox-compose/inbox-compose.component.html @@ -10,16 +10,17 @@

Compose New

- Balance: + Balance:
-
+ @@ -34,7 +35,8 @@

Compose New

- diff --git a/src/app/inbox/inbox-compose/inbox-compose.component.spec.ts b/src/app/inbox/inbox-compose/inbox-compose.component.spec.ts index ebbd337..0abbcfa 100644 --- a/src/app/inbox/inbox-compose/inbox-compose.component.spec.ts +++ b/src/app/inbox/inbox-compose/inbox-compose.component.spec.ts @@ -47,8 +47,8 @@ describe('InboxComposeComponent', () => { const currentAccount = '0x92d8f10248c6a3953cc3692a894655ad05d61efb'; const currentAccount2 = '0x0123456789012345678901234567890123456789'; - const balance = '1.018 Eth'; - const fees = '0.00008 Eth'; + const balance = ''; + const fees = ''; const ensName = 'mailchain.eth'; const addresses = [currentAccount, currentAccount2]; @@ -139,7 +139,6 @@ describe('InboxComposeComponent', () => { describe('handling the balance', () => { it('should initialize a balance in the "balance" field using an available balance', async () => { - component.balance = balance await component.ngOnInit(); fixture.detectChanges() expect(component.balance).toBe(balance) @@ -148,7 +147,6 @@ describe('InboxComposeComponent', () => { describe('handling the fees', () => { it('should initialize a Fees in the "fees" field', async () => { - component.fees = fees await component.ngOnInit(); fixture.detectChanges() expect(component.fees).toBe(fees) diff --git a/src/app/inbox/inbox-compose/inbox-compose.component.ts b/src/app/inbox/inbox-compose/inbox-compose.component.ts index 0ed4dca..2f62b58 100644 --- a/src/app/inbox/inbox-compose/inbox-compose.component.ts +++ b/src/app/inbox/inbox-compose/inbox-compose.component.ts @@ -7,6 +7,8 @@ import { PublicKeyService } from 'src/app/services/mailchain/public-key/public-k import { AddressesService } from 'src/app/services/mailchain/addresses/addresses.service'; import { EnvelopeService } from 'src/app/services/mailchain/envelope/envelope.service'; import { NameserviceService } from 'src/app/services/mailchain/nameservice/nameservice.service'; +import { BalanceService } from 'src/app/services/mailchain/addresses/balance.service'; + import { Subject, of, Observable } from 'rxjs'; import { debounceTime, distinctUntilChanged, mergeMap } from "rxjs/operators"; @@ -34,6 +36,8 @@ export class InboxComposeComponent implements OnInit { public model = new Mail() public fromAddresses: Array = [] public envelopes: Array = [] + public balance: string = "" + public fees: string = "" public sendMessagesDisabled: boolean = false; private subscription @@ -53,8 +57,6 @@ export class InboxComposeComponent implements OnInit { public contentTypeSwitchLabel: string = "" public envelopeType public envelopeDescription - public balance - public fees @ViewChild('editor') public editorComponent: CKEditorComponent; @@ -65,6 +67,7 @@ export class InboxComposeComponent implements OnInit { private addressesService: AddressesService, private envelopeService: EnvelopeService, private nameserviceService: NameserviceService, + private balanceService: BalanceService, private modalService: BsModalService, ) { this.initMail() @@ -78,6 +81,7 @@ export class InboxComposeComponent implements OnInit { this.model.from = "" this.model.subject = "" this.model.body = "" + } /** @@ -120,6 +124,24 @@ export class InboxComposeComponent implements OnInit { this.envelopes = await this.envelopeService.getEnvelope(); } + + /** + * Sets the balance + */ + + public async updateBalanceforaddress(address) { + this.balance = await this.balanceService.getBalance(address, this.currentProtocol, this.currentNetwork); + + } + + private async setBalance() { + if (this.currentAccount != undefined) { + + this.balance = await this.balanceService.getBalance(this.currentAccount, this.currentProtocol, this.currentNetwork); + } + } + + /** * Returns the identicon for the an address * @param address the address @@ -330,6 +352,7 @@ export class InboxComposeComponent implements OnInit { this.setFirstEnvelopeInEnvelopeDropdown() this.handleReplyFields() this.setupRecipientAddressLookupSubscription() + await this.setBalance() } /** diff --git a/src/app/services/mailchain/addresses/balance.service.spec.ts b/src/app/services/mailchain/addresses/balance.service.spec.ts index dc06b8d..f70f40e 100644 --- a/src/app/services/mailchain/addresses/balance.service.spec.ts +++ b/src/app/services/mailchain/addresses/balance.service.spec.ts @@ -55,7 +55,7 @@ describe('BalanceService', () => { describe('getBalance', () => { beforeEach(() => { - let obs: Observable = of(mailchainTestService.senderAddressesEthereumObserveResponse()) + let obs: Observable = of(mailchainTestService.senderBalanceEthereumObserveResponse()) spyOn(balanceService, 'getBalance').and.returnValue( obs.toPromise() ) @@ -65,7 +65,7 @@ describe('BalanceService', () => { await balanceService.getBalance(currentAccount, 'ethereum', 'mainnet').then(res => { result = res }); - expect(result).toEqual(mailchainTestService.senderAddressesEthereumObserveResponse()) + expect(result).toEqual(mailchainTestService.senderBalanceEthereumObserveResponse()) expect(balanceService.getBalance).toHaveBeenCalled() }); }) diff --git a/src/app/test/test-helpers/mailchain-test.service.ts b/src/app/test/test-helpers/mailchain-test.service.ts index be39c85..999b5cb 100644 --- a/src/app/test/test-helpers/mailchain-test.service.ts +++ b/src/app/test/test-helpers/mailchain-test.service.ts @@ -139,6 +139,26 @@ export class MailchainTestService { } } + public senderBalanceEthereumObserveResponse() { + return { + "headers": { + "normalizedNames": {}, + "lazyUpdate": null + }, + "status": 200, + "statusText": "OK", + "url": "http://127.0.0.1:8080/api/addresses/0x92d8f10248c6a3953cc3692a894655ad05d61efb/balance?protocol=ethereum&network=mainnet", + "ok": true, + "type": 4, + "body": { + "balance": 2183846200000000000, + "unit": "wei" + } + + } + } + + public senderAddressEthereumServerResponse(): any { return { "addresses": [ @@ -313,7 +333,7 @@ export class MailchainTestService { } public protocolsServerResponse(): any { - return {"protocols":[{"name":"ethereum","networks":[{"name":"goerli","id":"","nameservice-domain-enabled":true,"nameservice-address-enabled":true},{"name":"kovan","id":"","nameservice-domain-enabled":true,"nameservice-address-enabled":true},{"name":"mainnet","id":"","nameservice-domain-enabled":true,"nameservice-address-enabled":true},{"name":"rinkeby","id":"","nameservice-domain-enabled":true,"nameservice-address-enabled":true},{"name":"ropsten","id":"","nameservice-domain-enabled":true,"nameservice-address-enabled":true}]},{"name":"substrate","networks":[{"name":"edgeware-beresheet","id":"7","nameservice-domain-enabled":false,"nameservice-address-enabled":false},{"name":"edgeware-local","id":"7","nameservice-domain-enabled":false,"nameservice-address-enabled":false},{"name":"edgeware-mainnet","id":"7","nameservice-domain-enabled":false,"nameservice-address-enabled":false}]}]} + return { "protocols": [{ "name": "ethereum", "networks": [{ "name": "goerli", "id": "", "nameservice-domain-enabled": true, "nameservice-address-enabled": true }, { "name": "kovan", "id": "", "nameservice-domain-enabled": true, "nameservice-address-enabled": true }, { "name": "mainnet", "id": "", "nameservice-domain-enabled": true, "nameservice-address-enabled": true }, { "name": "rinkeby", "id": "", "nameservice-domain-enabled": true, "nameservice-address-enabled": true }, { "name": "ropsten", "id": "", "nameservice-domain-enabled": true, "nameservice-address-enabled": true }] }, { "name": "substrate", "networks": [{ "name": "edgeware-beresheet", "id": "7", "nameservice-domain-enabled": false, "nameservice-address-enabled": false }, { "name": "edgeware-local", "id": "7", "nameservice-domain-enabled": false, "nameservice-address-enabled": false }, { "name": "edgeware-mainnet", "id": "7", "nameservice-domain-enabled": false, "nameservice-address-enabled": false }] }] } } From 9a9008895ef7324f47d1f2914fd496cc77ed00ff Mon Sep 17 00:00:00 2001 From: Shivani Aggarwal Date: Sat, 26 Jun 2021 20:31:04 -0400 Subject: [PATCH 10/11] feat: Balance functionality --- .../inbox-compose.component.html | 7 ++- .../inbox-compose.component.spec.ts | 23 ++++---- .../inbox-compose/inbox-compose.component.ts | 17 +++++- .../addresses/balance.service.stub.ts | 58 +++++++++++++++++++ .../mailchain/addresses/balance.service.ts | 28 ++++----- .../services/mailchain/mailchain.service.ts | 29 ++++++++-- .../test-helpers/mailchain-test.service.ts | 37 ++++++++++++ 7 files changed, 161 insertions(+), 38 deletions(-) create mode 100644 src/app/services/mailchain/addresses/balance.service.stub.ts diff --git a/src/app/inbox/inbox-compose/inbox-compose.component.html b/src/app/inbox/inbox-compose/inbox-compose.component.html index e00b36e..86695f2 100644 --- a/src/app/inbox/inbox-compose/inbox-compose.component.html +++ b/src/app/inbox/inbox-compose/inbox-compose.component.html @@ -10,10 +10,11 @@

Compose New

- Balance: + Balance:
+