Skip to content

Commit

Permalink
fix: api address contract endpoint to use creatorAddress field instea…
Browse files Browse the repository at this point in the history
…d of a separate call (#115)

# What ❔

Api contract endpoint to use `creatorAddress` field instead of a
separate DB call to fetch txReceipt.

## Why ❔

We don't need a DB call to fetch contract creator address.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [X] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [X] Tests for the changes have been added / updated.
  • Loading branch information
vasyl-ivanchuk authored Dec 7, 2023
1 parent b897a78 commit 9adb589
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 36 deletions.
21 changes: 2 additions & 19 deletions packages/api/src/address/address.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { BalanceService } from "../balance/balance.service";
import { BlockService } from "../block/block.service";
import { LogService } from "../log/log.service";
import { TransactionService } from "../transaction/transaction.service";
import { TransactionReceiptService } from "../transaction/transactionReceipt.service";
import { TransactionReceipt } from "../transaction/entities/transactionReceipt.entity";
import { Log } from "../log/log.entity";
import { Token } from "../token/token.entity";
import { PagingOptionsWithMaxItemsLimitDto } from "../common/dtos";
Expand All @@ -28,7 +26,6 @@ describe("AddressController", () => {
let logServiceMock: LogService;
let balanceServiceMock: BalanceService;
let transactionServiceMock: TransactionService;
let transactionReceiptServiceMock: TransactionReceiptService;
let transferServiceMock: TransferService;
const blockchainAddress = "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
const normalizedAddress = "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF";
Expand All @@ -38,7 +35,6 @@ describe("AddressController", () => {
serviceMock = mock<AddressService>();
blockServiceMock = mock<BlockService>();
transactionServiceMock = mock<TransactionService>();
transactionReceiptServiceMock = mock<TransactionReceiptService>();
logServiceMock = mock<LogService>();
balanceServiceMock = mock<BalanceService>();
transferServiceMock = mock<TransferService>();
Expand All @@ -58,10 +54,6 @@ describe("AddressController", () => {
provide: TransactionService,
useValue: transactionServiceMock,
},
{
provide: TransactionReceiptService,
useValue: transactionReceiptServiceMock,
},
{
provide: LogService,
useValue: logServiceMock,
Expand Down Expand Up @@ -104,9 +96,8 @@ describe("AddressController", () => {

describe("when contract address exists", () => {
const transactionHash = "transactionHash";
const transactionFrom = "transactionFrom";
const creatorAddress = "creatorAddress";
const totalTxCount = 20;
let transactionReceipt;
const addressBalances = {
balances: {
"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF": {
Expand All @@ -128,21 +119,13 @@ describe("AddressController", () => {
bytecode: "0x123",
createdInBlockNumber: 30,
creatorTxHash: transactionHash,
creatorAddress: transactionFrom,
creatorAddress,
};
transactionReceipt = mock<TransactionReceipt>({ from: transactionFrom });
(serviceMock.findOne as jest.Mock).mockResolvedValue(addressRecord);
(transactionReceiptServiceMock.findOne as jest.Mock).mockResolvedValue(transactionReceipt);
(transactionServiceMock.count as jest.Mock).mockResolvedValue(totalTxCount);
(balanceServiceMock.getBalances as jest.Mock).mockResolvedValue(addressBalances);
});

it("queries creatorAddress value from transaction receipt", async () => {
await controller.getAddress(blockchainAddress);
expect(transactionReceiptServiceMock.findOne).toHaveBeenCalledTimes(1);
expect(transactionReceiptServiceMock.findOne).toHaveBeenCalledWith(transactionHash, ["from"]);
});

it("queries totalTransactions value from transaction receipt repo with formatted contractAddress", async () => {
await controller.getAddress(blockchainAddress);
expect(transactionServiceMock.count).toHaveBeenCalledTimes(1);
Expand Down
10 changes: 2 additions & 8 deletions packages/api/src/address/address.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { formatHexAddress, buildDateFilter } from "../common/utils";
import { AddressService } from "./address.service";
import { BlockService } from "../block/block.service";
import { TransactionService } from "../transaction/transaction.service";
import { TransactionReceiptService } from "../transaction/transactionReceipt.service";
import { BalanceService } from "../balance/balance.service";
import { AddressType, ContractDto, AccountDto, TokenAddressDto } from "./dtos";
import { LogDto } from "../log/log.dto";
Expand All @@ -37,7 +36,6 @@ export class AddressController {
private readonly addressService: AddressService,
private readonly blockService: BlockService,
private readonly transactionService: TransactionService,
private readonly transactionReceiptService: TransactionReceiptService,
private readonly logService: LogService,
private readonly balanceService: BalanceService,
private readonly transferService: TransferService
Expand Down Expand Up @@ -66,11 +64,7 @@ export class AddressController {
]);

if (addressRecord?.bytecode.length > 2) {
const [txReceipt, totalTransactions] = await Promise.all([
this.transactionReceiptService.findOne(addressRecord.creatorTxHash, ["from"]),
this.transactionService.count({ "from|to": formatHexAddress(address) }),
]);

const totalTransactions = await this.transactionService.count({ "from|to": formatHexAddress(address) });
return {
type: AddressType.Contract,
...addressRecord,
Expand All @@ -79,7 +73,7 @@ export class AddressController {
createdInBlockNumber: addressRecord.createdInBlockNumber,
creatorTxHash: addressRecord.creatorTxHash,
totalTransactions,
creatorAddress: txReceipt.from,
creatorAddress: addressRecord.creatorAddress,
};
}

Expand Down
10 changes: 5 additions & 5 deletions packages/api/test/address.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ describe("AddressController (e2e)", () => {
blockNumber: 106,
bytecode: "0x000012",
createdInBlockNumber: 10,
creatorAddress: "0xc7e0220d02D549C4846A6EC31D89C3B670eBE355",
creatorAddress: "0x91d0a23f34e535e44Df8Ba84c53a0945cf0eEB60",
creatorTxHash: "0x8a008b8dbbc18035e56370abb820e736b705d68d6ac12b203603db8d9ea87e15",
totalTransactions: 4,
type: "contract",
Expand Down Expand Up @@ -844,7 +844,7 @@ describe("AddressController (e2e)", () => {
blockNumber: 106,
bytecode: "0x000012",
createdInBlockNumber: 10,
creatorAddress: "0xc7e0220d02D549C4846A6EC31D89C3B670eBE355",
creatorAddress: "0x91d0a23f34e535e44Df8Ba84c53a0945cf0eEB60",
creatorTxHash: "0x8a008b8dbbc18035e56370abb820e736b705d68d6ac12b203603db8d9ea87e15",
totalTransactions: 4,
type: "contract",
Expand Down Expand Up @@ -903,7 +903,7 @@ describe("AddressController (e2e)", () => {
blockNumber: 106,
bytecode: "0x000012",
createdInBlockNumber: 10,
creatorAddress: "0xc7e0220d02D549C4846A6EC31D89C3B670eBE355",
creatorAddress: "0x91d0a23f34e535e44Df8Ba84c53a0945cf0eEB60",
creatorTxHash: "0x8a008b8dbbc18035e56370abb820e736b705d68d6ac12b203603db8d9ea87e15",
totalTransactions: 4,
type: "contract",
Expand Down Expand Up @@ -962,7 +962,7 @@ describe("AddressController (e2e)", () => {
blockNumber: 106,
bytecode: "0x000012",
createdInBlockNumber: 10,
creatorAddress: "0xc7e0220d02D549C4846A6EC31D89C3B670eBE355",
creatorAddress: "0x91d0a23f34e535e44Df8Ba84c53a0945cf0eEB60",
creatorTxHash: "0x8a008b8dbbc18035e56370abb820e736b705d68d6ac12b203603db8d9ea87e15",
totalTransactions: 4,
type: "contract",
Expand All @@ -982,7 +982,7 @@ describe("AddressController (e2e)", () => {
blockNumber: 10,
bytecode: "0x000012",
createdInBlockNumber: 10,
creatorAddress: "0xc7e0220d02D549C4846A6EC31D89C3B670eBE355",
creatorAddress: "0x91d0a23f34e535e44Df8Ba84c53a0945cf0eEB60",
creatorTxHash: "0x8a008b8dbbc18035e56370abb820e736b705d68d6ac12b203603db8d9ea87e15",
totalTransactions: 0,
type: "contract",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ Feature: Main Page
# When I click by text "Show more transactions ->"
Then Element with "text" "in" should be "visible"

@id382 @mainnet
Scenario: Verify label "self" for Account info on Account page
Given I go to page "/address/0x94124252B5D343AB6E950A15982599ee1AADE660"
#@id382 @mainnet
#Scenario: Verify label "self" for Account info on Account page
#Given I go to page "/address/0x94124252B5D343AB6E950A15982599ee1AADE660"
# When I click by text "Show more transactions ->"
Then Element with "text" "self" should be "visible"
#Then Element with "text" "self" should be "visible"

@id580 @id578 @id619 @testnet @testnetSmokeSuite
Scenario Outline: Verify label "<label name>" for method column on Contract page
Expand Down

0 comments on commit 9adb589

Please sign in to comment.