Skip to content

Commit

Permalink
[TX] return only unique addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Bozhytskyi committed Jul 2, 2021
1 parent 6a13bfa commit 8a60c4c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions types/v1/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,17 @@ func (t *Tx) GetAddresses() []string {
switch t.Metadata.(type) {
case *Transfer, *ContractCall:
if len(t.Inputs) > 0 || len(t.Outputs) > 0 {
uniqueAddresses := make(map[string]struct{})
for _, input := range t.Inputs {
addresses = append(addresses, input.Address)
uniqueAddresses[input.Address] = struct{}{}
}

for _, output := range t.Outputs {
addresses = append(addresses, output.Address)
uniqueAddresses[output.Address] = struct{}{}
}

for address := range uniqueAddresses {
addresses = append(addresses, address)
}

return addresses
Expand Down
11 changes: 11 additions & 0 deletions types/v1/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ func TestTx_GetAddresses(t *testing.T) {
},
expected: []string{},
},
{
name: "utxo",
tx: Tx{
From: "from_utxo",
To: "from_utxo",
Inputs: []TxOutput{{Address: "from_utxo"}},
Outputs: []TxOutput{{Address: "from_utxo"}, {Address: "to_utxo"}},
Metadata: &Transfer{},
},
expected: []string{"from_utxo", "to_utxo"},
},
}

for _, tc := range tests {
Expand Down

0 comments on commit 8a60c4c

Please sign in to comment.