-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
services: wrap transactions with fee bump for sponsored accounts (#20)
What This PR adds the POST /tx/create-fee-bump endpoint. Also, the AccountModel was created. Why Implement the fee bump functionality.
- Loading branch information
1 parent
6e43cc6
commit f41bed0
Showing
16 changed files
with
991 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package data | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/stellar/wallet-backend/internal/db" | ||
) | ||
|
||
type AccountModel struct { | ||
DB db.ConnectionPool | ||
} | ||
|
||
func (m *AccountModel) Insert(ctx context.Context, address string) error { | ||
const query = `INSERT INTO accounts (stellar_address) VALUES ($1) ON CONFLICT DO NOTHING` | ||
_, err := m.DB.ExecContext(ctx, query, address) | ||
if err != nil { | ||
return fmt.Errorf("inserting address %s: %w", address, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *AccountModel) Delete(ctx context.Context, address string) error { | ||
const query = `DELETE FROM accounts WHERE stellar_address = $1` | ||
_, err := m.DB.ExecContext(ctx, query, address) | ||
if err != nil { | ||
return fmt.Errorf("deleting address %s: %w", address, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *AccountModel) Exists(ctx context.Context, address string) (bool, error) { | ||
const query = `SELECT EXISTS(SELECT stellar_address FROM accounts WHERE stellar_address = $1)` | ||
var exists bool | ||
err := m.DB.GetContext(ctx, &exists, query, address) | ||
if err != nil { | ||
return false, fmt.Errorf("checking if account %s exists: %w", address, err) | ||
} | ||
|
||
return exists, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package data | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/stellar/go/keypair" | ||
"github.com/stellar/wallet-backend/internal/db" | ||
"github.com/stellar/wallet-backend/internal/db/dbtest" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAccountModelInsert(t *testing.T) { | ||
dbt := dbtest.Open(t) | ||
defer dbt.Close() | ||
|
||
dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN) | ||
require.NoError(t, err) | ||
defer dbConnectionPool.Close() | ||
|
||
m := &AccountModel{ | ||
DB: dbConnectionPool, | ||
} | ||
|
||
ctx := context.Background() | ||
address := keypair.MustRandom().Address() | ||
err = m.Insert(ctx, address) | ||
require.NoError(t, err) | ||
|
||
var dbAddress sql.NullString | ||
err = m.DB.GetContext(ctx, &dbAddress, "SELECT stellar_address FROM accounts LIMIT 1") | ||
require.NoError(t, err) | ||
|
||
assert.True(t, dbAddress.Valid) | ||
assert.Equal(t, address, dbAddress.String) | ||
} | ||
|
||
func TestAccountModelDelete(t *testing.T) { | ||
dbt := dbtest.Open(t) | ||
defer dbt.Close() | ||
|
||
dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN) | ||
require.NoError(t, err) | ||
defer dbConnectionPool.Close() | ||
|
||
m := &AccountModel{ | ||
DB: dbConnectionPool, | ||
} | ||
|
||
ctx := context.Background() | ||
address := keypair.MustRandom().Address() | ||
result, err := m.DB.ExecContext(ctx, "INSERT INTO accounts (stellar_address) VALUES ($1)", address) | ||
require.NoError(t, err) | ||
rowAffected, err := result.RowsAffected() | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), rowAffected) | ||
|
||
err = m.Delete(ctx, address) | ||
require.NoError(t, err) | ||
|
||
var dbAddress sql.NullString | ||
err = m.DB.GetContext(ctx, &dbAddress, "SELECT stellar_address FROM accounts LIMIT 1") | ||
assert.ErrorIs(t, err, sql.ErrNoRows) | ||
} | ||
|
||
func TestAccountModelExists(t *testing.T) { | ||
dbt := dbtest.Open(t) | ||
defer dbt.Close() | ||
|
||
dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN) | ||
require.NoError(t, err) | ||
defer dbConnectionPool.Close() | ||
|
||
m := &AccountModel{ | ||
DB: dbConnectionPool, | ||
} | ||
|
||
ctx := context.Background() | ||
address := keypair.MustRandom().Address() | ||
|
||
exists, err := m.Exists(ctx, address) | ||
require.NoError(t, err) | ||
assert.False(t, exists) | ||
|
||
result, err := m.DB.ExecContext(ctx, "INSERT INTO accounts (stellar_address) VALUES ($1)", address) | ||
require.NoError(t, err) | ||
rowAffected, err := result.RowsAffected() | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), rowAffected) | ||
|
||
exists, err = m.Exists(ctx, address) | ||
require.NoError(t, err) | ||
assert.True(t, exists) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.