Skip to content

Commit

Permalink
00497 revisit compact transfer diagram (#595)
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Le Ponner <[email protected]>
  • Loading branch information
ericleponner authored May 15, 2023
1 parent adc1964 commit 3278f96
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/components/transfer_graphs/HbarTransferGraphC.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<!-- #2 : hbar amount -->
<div class="justify-end">
<HbarAmount v-if="i === 1"
v-bind:amount="hbarTransferLayout.netAmount"/>
v-bind:amount="hbarTransferLayout.destinationAmount"/>
</div>

<!-- #3 : arrow -->
Expand Down
23 changes: 13 additions & 10 deletions src/components/transfer_graphs/layout/HbarTransferLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
*/

import {compareTransferByAccount, NetworkNode, Transaction, Transfer} from "@/schemas/HederaSchemas";
import {isFeeTransfer, makeOperatorDescription} from "@/schemas/HederaUtils";
import {computeNetAmount} from "@/utils/TransactionTools";
import {makeOperatorDescription} from "@/schemas/HederaUtils";

export class HbarTransferLayout {

public readonly transaction: Transaction|undefined
public readonly nodes: NetworkNode[]
public readonly netAmount: number
public readonly destinationAmount: number
public readonly sources = Array<HbarTransferRow>()
public readonly destinations = Array<HbarTransferRow>()
public readonly rowCount: number
Expand All @@ -39,7 +39,6 @@ export class HbarTransferLayout {

this.transaction = transaction
this.nodes = nodes
this.netAmount = transaction ? computeNetAmount(transaction) : 0

if (this.transaction?.transfers) {
const negativeTransfers = new Array<Transfer>()
Expand All @@ -55,18 +54,18 @@ export class HbarTransferLayout {
positiveTransfers.sort(compareTransferByAccount)

for (const t of negativeTransfers) {
const payload = t.account === null || makeOperatorDescription(t.account, this.nodes) === null
this.sources.push(new HbarTransferRow(t, null, payload))
const isFee = isFeeTransfer(t, this.nodes)
this.sources.push(new HbarTransferRow(t, null, !isFee))
}
for (const t of positiveTransfers) {
const operator = t.account !== null ? makeOperatorDescription(t.account, this.nodes) : null
const payload = t.account === null || operator === null
this.destinations.push(new HbarTransferRow(t, operator ?? "Transfer", payload))
const isFee = isFeeTransfer(t, this.nodes)
const operator = t.account ? makeOperatorDescription(t.account, this.nodes) : null
this.destinations.push(new HbarTransferRow(t, operator ?? "Transfer", !isFee))
}
}

// Makes sure net amount is distributed across payload transfers
let remaining = this.netAmount
let remaining = this.transaction ? computeNetAmount(this.transaction) : 0
// First we remove amount from payload transfers
for (const r of this.destinations) {
if (r.payload) {
Expand Down Expand Up @@ -112,10 +111,14 @@ export class HbarTransferLayout {
}
}

this.destinationAmount = 0
for (const r of this.destinations) {
this.destinationAmount += r.transfer.amount
}

this.rowCount = Math.max(this.sources.length, this.destinations.length)
}


//
// Private
//
Expand Down
8 changes: 7 additions & 1 deletion src/schemas/HederaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
*/

import {AccountInfo, KeyType, NetworkNode, TokenInfo} from "@/schemas/HederaSchemas";
import {AccountInfo, KeyType, NetworkNode, TokenInfo, Transfer} from "@/schemas/HederaSchemas";
import {EntityID} from "@/utils/EntityID";
import {ethers} from "ethers";

Expand Down Expand Up @@ -112,6 +112,12 @@ export function makeOperatorDescription(accountId: string, nodes: NetworkNode[])
return result
}

export function isFeeTransfer(t: Transfer, nodes: NetworkNode[]): boolean {
return t.account !== null
&& t.amount > 0
&& makeOperatorDescription(t.account, nodes) !== null
}

const errorStringSelector = '0x08c379a0'
const panicUint256Selector = '0x4e487b71'

Expand Down
28 changes: 14 additions & 14 deletions tests/unit/transfer_graphs/layout/HbarTransferLayout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("HbarTransferLayout.vue", () => {
const fullLayout = new HbarTransferLayout(transaction, NETWORK_NODES)

expect(fullLayout.transaction).toBe(transaction)
expect(fullLayout.netAmount).toBe(0)
expect(fullLayout.destinationAmount).toBe(10)
expect(fullLayout.rowCount).toBe(2)
expect(fullLayout.sources.length).toBe(1)
expect(fullLayout.destinations.length).toBe(2)
Expand Down Expand Up @@ -85,7 +85,7 @@ describe("HbarTransferLayout.vue", () => {
const compactLayout = new HbarTransferLayout(transaction, NETWORK_NODES, false)

expect(compactLayout.transaction).toBe(transaction)
expect(compactLayout.netAmount).toBe(0)
expect(compactLayout.destinationAmount).toBe(0)
expect(compactLayout.rowCount).toBe(0)
expect(compactLayout.sources.length).toBe(0)
expect(compactLayout.destinations.length).toBe(0)
Expand All @@ -111,7 +111,7 @@ describe("HbarTransferLayout.vue", () => {
const fullLayout = new HbarTransferLayout(transaction, NETWORK_NODES)

expect(fullLayout.transaction).toBe(transaction)
expect(fullLayout.netAmount).toBe(90)
expect(fullLayout.destinationAmount).toBe(100)
expect(fullLayout.rowCount).toBe(3)
expect(fullLayout.sources.length).toBe(1)
expect(fullLayout.destinations.length).toBe(3)
Expand Down Expand Up @@ -147,7 +147,7 @@ describe("HbarTransferLayout.vue", () => {
const compactLayout = new HbarTransferLayout(transaction, NETWORK_NODES, false)

expect(compactLayout.transaction).toBe(transaction)
expect(compactLayout.netAmount).toBe(90)
expect(compactLayout.destinationAmount).toBe(90)
expect(compactLayout.rowCount).toBe(1)
expect(compactLayout.sources.length).toBe(1)
expect(compactLayout.destinations.length).toBe(1)
Expand Down Expand Up @@ -185,7 +185,7 @@ describe("HbarTransferLayout.vue", () => {
const fullLayout = new HbarTransferLayout(transaction, NETWORK_NODES)

expect(fullLayout.transaction).toBe(transaction)
expect(fullLayout.netAmount).toBe(90)
expect(fullLayout.destinationAmount).toBe(100)
expect(fullLayout.rowCount).toBe(4)
expect(fullLayout.sources.length).toBe(1)
expect(fullLayout.destinations.length).toBe(4)
Expand Down Expand Up @@ -227,7 +227,7 @@ describe("HbarTransferLayout.vue", () => {
const compactLayout = new HbarTransferLayout(transaction, NETWORK_NODES, false)

expect(compactLayout.transaction).toBe(transaction)
expect(compactLayout.netAmount).toBe(90)
expect(compactLayout.destinationAmount).toBe(90)
expect(compactLayout.rowCount).toBe(2)
expect(compactLayout.sources.length).toBe(1)
expect(compactLayout.destinations.length).toBe(2)
Expand Down Expand Up @@ -275,7 +275,7 @@ describe("HbarTransferLayout.vue", () => {
const fullLayout = new HbarTransferLayout(transaction, NETWORK_NODES)

expect(fullLayout.transaction).toBe(transaction)
expect(fullLayout.netAmount).toBe(0)
expect(fullLayout.destinationAmount).toBe(10)
expect(fullLayout.rowCount).toBe(2)
expect(fullLayout.sources.length).toBe(2)
expect(fullLayout.destinations.length).toBe(2)
Expand Down Expand Up @@ -312,7 +312,7 @@ describe("HbarTransferLayout.vue", () => {
const compactLayout = new HbarTransferLayout(transaction, NETWORK_NODES, false)

expect(compactLayout.transaction).toBe(transaction)
expect(compactLayout.netAmount).toBe(0)
expect(compactLayout.destinationAmount).toBe(0)
expect(compactLayout.rowCount).toBe(0)
expect(compactLayout.sources.length).toBe(0)
expect(compactLayout.destinations.length).toBe(0)
Expand All @@ -339,7 +339,7 @@ describe("HbarTransferLayout.vue", () => {
const fullLayout = new HbarTransferLayout(transaction, NETWORK_NODES)

expect(fullLayout.transaction).toBe(transaction)
expect(fullLayout.netAmount).toBe(90)
expect(fullLayout.destinationAmount).toBe(100)
expect(fullLayout.rowCount).toBe(3)
expect(fullLayout.sources.length).toBe(2)
expect(fullLayout.destinations.length).toBe(3)
Expand Down Expand Up @@ -381,7 +381,7 @@ describe("HbarTransferLayout.vue", () => {
const compactLayout = new HbarTransferLayout(transaction, NETWORK_NODES, false)

expect(compactLayout.transaction).toBe(transaction)
expect(compactLayout.netAmount).toBe(90)
expect(compactLayout.destinationAmount).toBe(90)
expect(compactLayout.rowCount).toBe(2)
expect(compactLayout.sources.length).toBe(2)
expect(compactLayout.destinations.length).toBe(1)
Expand Down Expand Up @@ -426,7 +426,7 @@ describe("HbarTransferLayout.vue", () => {
const fullLayout = new HbarTransferLayout(transaction, NETWORK_NODES)

expect(fullLayout.transaction).toBe(transaction)
expect(fullLayout.netAmount).toBe(90)
expect(fullLayout.destinationAmount).toBe(100)
expect(fullLayout.rowCount).toBe(4)
expect(fullLayout.sources.length).toBe(2)
expect(fullLayout.destinations.length).toBe(4)
Expand Down Expand Up @@ -475,7 +475,7 @@ describe("HbarTransferLayout.vue", () => {
const compactLayout = new HbarTransferLayout(transaction, NETWORK_NODES, false)

expect(compactLayout.transaction).toBe(transaction)
expect(compactLayout.netAmount).toBe(90)
expect(compactLayout.destinationAmount).toBe(90)
expect(compactLayout.rowCount).toBe(2)
expect(compactLayout.sources.length).toBe(2)
expect(compactLayout.destinations.length).toBe(2)
Expand Down Expand Up @@ -527,7 +527,7 @@ describe("HbarTransferLayout.vue", () => {
const fullLayout = new HbarTransferLayout(transaction, NETWORK_NODES)

expect(fullLayout.transaction).toBe(transaction)
expect(fullLayout.netAmount).toBe(2)
expect(fullLayout.destinationAmount).toBe(10)
expect(fullLayout.rowCount).toBe(3)
expect(fullLayout.sources.length).toBe(1)
expect(fullLayout.destinations.length).toBe(3)
Expand Down Expand Up @@ -563,7 +563,7 @@ describe("HbarTransferLayout.vue", () => {
const compactLayout = new HbarTransferLayout(transaction, NETWORK_NODES, false)

expect(compactLayout.transaction).toBe(transaction)
expect(compactLayout.netAmount).toBe(2)
expect(compactLayout.destinationAmount).toBe(2)
expect(compactLayout.rowCount).toBe(1)
expect(compactLayout.sources.length).toBe(1)
expect(compactLayout.destinations.length).toBe(1)
Expand Down

0 comments on commit 3278f96

Please sign in to comment.