Commit a9f2f570 authored by Conner Fromknecht's avatar Conner Fromknecht

feat: use generic Teleport struct, remove CompletedTeleport

parent bf1cc8f4
...@@ -44,11 +44,12 @@ type Disbursement struct { ...@@ -44,11 +44,12 @@ type Disbursement struct {
ConfirmationInfo ConfirmationInfo
} }
// CompletedTeleport represents an L1 deposit that has been disbursed on L2. The // Teleport represents the combination of an L1 deposit and its disbursement on
// struct also hold info about the L1 and L2 txns involved. // L2. Disburment will be nil if the L2 disbursement has not occurred.
type CompletedTeleport struct { type Teleport struct {
Deposit Deposit
Disbursement Disbursement
Disbursement *Disbursement
} }
const createDepositsTable = ` const createDepositsTable = `
...@@ -384,46 +385,19 @@ ORDER BY id DESC ...@@ -384,46 +385,19 @@ ORDER BY id DESC
// CompletedTeleports returns the set of all deposits that have also been // CompletedTeleports returns the set of all deposits that have also been
// disbursed. // disbursed.
func (d *Database) CompletedTeleports() ([]CompletedTeleport, error) { func (d *Database) CompletedTeleports() ([]Teleport, error) {
rows, err := d.conn.Query(completedTeleportsQuery) rows, err := d.conn.Query(completedTeleportsQuery)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close() defer rows.Close()
var teleports []CompletedTeleport var teleports []Teleport
for rows.Next() { for rows.Next() {
var teleport CompletedTeleport teleport, err := scanTeleport(rows)
var addressStr string
var amountStr string
var depTxnHashStr string
var disTxnHashStr string
err = rows.Scan(
&teleport.ID,
&addressStr,
&amountStr,
&teleport.Disbursement.Success,
&depTxnHashStr,
&teleport.Deposit.BlockNumber,
&teleport.Deposit.BlockTimestamp,
&disTxnHashStr,
&teleport.Disbursement.BlockNumber,
&teleport.Disbursement.BlockTimestamp,
)
if err != nil { if err != nil {
return nil, err return nil, err
} }
amount, ok := new(big.Int).SetString(amountStr, 10)
if !ok {
return nil, fmt.Errorf("unable to parse amount %v", amount)
}
teleport.Address = common.HexToAddress(addressStr)
teleport.Amount = amount
teleport.Deposit.TxnHash = common.HexToHash(depTxnHashStr)
teleport.Deposit.BlockTimestamp = teleport.Deposit.BlockTimestamp.Local()
teleport.Disbursement.TxnHash = common.HexToHash(disTxnHashStr)
teleport.Disbursement.BlockTimestamp = teleport.Disbursement.BlockTimestamp.Local()
teleports = append(teleports, teleport) teleports = append(teleports, teleport)
} }
if err := rows.Err(); err != nil { if err := rows.Err(); err != nil {
...@@ -433,6 +407,59 @@ func (d *Database) CompletedTeleports() ([]CompletedTeleport, error) { ...@@ -433,6 +407,59 @@ func (d *Database) CompletedTeleports() ([]CompletedTeleport, error) {
return teleports, nil return teleports, nil
} }
func scanTeleport(rows *sql.Rows) (Teleport, error) {
var teleport Teleport
var addressStr string
var amountStr string
var depTxnHashStr string
var disTxnHashStr *string
var disBlockNumber *uint64
var disBlockTimestamp *time.Time
var success *bool
err := rows.Scan(
&teleport.ID,
&addressStr,
&amountStr,
&success,
&depTxnHashStr,
&teleport.Deposit.BlockNumber,
&teleport.Deposit.BlockTimestamp,
&disTxnHashStr,
&disBlockNumber,
&disBlockTimestamp,
)
if err != nil {
return Teleport{}, err
}
amount, ok := new(big.Int).SetString(amountStr, 10)
if !ok {
return Teleport{}, fmt.Errorf("unable to parse amount %v", amount)
}
teleport.Address = common.HexToAddress(addressStr)
teleport.Amount = amount
teleport.Deposit.TxnHash = common.HexToHash(depTxnHashStr)
teleport.Deposit.BlockTimestamp = teleport.Deposit.BlockTimestamp.Local()
hasDisbursement := success != nil &&
disTxnHashStr != nil &&
disBlockNumber != nil &&
disBlockTimestamp != nil
if hasDisbursement {
teleport.Disbursement = &Disbursement{
ConfirmationInfo: ConfirmationInfo{
TxnHash: common.HexToHash(*disTxnHashStr),
BlockNumber: *disBlockNumber,
BlockTimestamp: disBlockTimestamp.Local(),
},
Success: *success,
}
}
return teleport, nil
}
// PendingTx encapsulates the metadata stored about published disbursement txs. // PendingTx encapsulates the metadata stored about published disbursement txs.
type PendingTx struct { type PendingTx struct {
// Txhash is the tx hash of the disbursement tx. // Txhash is the tx hash of the disbursement tx.
......
...@@ -301,7 +301,7 @@ func TestUpsertDisbursement(t *testing.T) { ...@@ -301,7 +301,7 @@ func TestUpsertDisbursement(t *testing.T) {
) )
require.Nil(t, err) require.Nil(t, err)
expTeleports := []db.CompletedTeleport{ expTeleports := []db.Teleport{
{ {
Deposit: db.Deposit{ Deposit: db.Deposit{
ID: 1, ID: 1,
...@@ -313,7 +313,7 @@ func TestUpsertDisbursement(t *testing.T) { ...@@ -313,7 +313,7 @@ func TestUpsertDisbursement(t *testing.T) {
BlockTimestamp: testTimestamp, BlockTimestamp: testTimestamp,
}, },
}, },
Disbursement: db.Disbursement{ Disbursement: &db.Disbursement{
Success: false, Success: false,
ConfirmationInfo: db.ConfirmationInfo{ ConfirmationInfo: db.ConfirmationInfo{
TxnHash: tempDisTxnHash, TxnHash: tempDisTxnHash,
...@@ -334,7 +334,7 @@ func TestUpsertDisbursement(t *testing.T) { ...@@ -334,7 +334,7 @@ func TestUpsertDisbursement(t *testing.T) {
err = d.UpsertDisbursement(1, disTxnHash, disBlockNumber, testTimestamp, true) err = d.UpsertDisbursement(1, disTxnHash, disBlockNumber, testTimestamp, true)
require.Nil(t, err) require.Nil(t, err)
expTeleports = []db.CompletedTeleport{ expTeleports = []db.Teleport{
{ {
Deposit: db.Deposit{ Deposit: db.Deposit{
ID: 1, ID: 1,
...@@ -346,7 +346,7 @@ func TestUpsertDisbursement(t *testing.T) { ...@@ -346,7 +346,7 @@ func TestUpsertDisbursement(t *testing.T) {
BlockTimestamp: testTimestamp, BlockTimestamp: testTimestamp,
}, },
}, },
Disbursement: db.Disbursement{ Disbursement: &db.Disbursement{
Success: true, Success: true,
ConfirmationInfo: db.ConfirmationInfo{ ConfirmationInfo: db.ConfirmationInfo{
TxnHash: disTxnHash, TxnHash: disTxnHash,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment