Skip to content

Commit 81ad2e3

Browse files
fix: update withdraw amount to reflect fee deduction in issuance tests
1 parent 60b8814 commit 81ad2e3

31 files changed

+326
-300
lines changed

internal/domain/entity/issuance.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7-
"github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
7+
. "github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
88
"github.com/holiman/uint256"
99
)
1010

@@ -28,11 +28,11 @@ type Issuance struct {
2828
Title string `json:"title,omitempty" gorm:"not null"`
2929
Description string `json:"description,omitempty" gorm:"not null"`
3030
Promotion string `json:"promotion,omitempty" gorm:"not null"`
31-
Token types.Address `json:"token,omitempty" gorm:"types:text;not null"`
32-
CreatorAddress types.Address `json:"creator_address,omitempty" gorm:"types:text;not null"`
33-
CollateralAddress types.Address `json:"collateral_address,omitempty" gorm:"types:text;not null"`
31+
Token Address `json:"token,omitempty" gorm:"types:text;not null"`
32+
CreatorAddress Address `json:"creator_address,omitempty" gorm:"types:text;not null"`
33+
CollateralAddress Address `json:"collateral_address,omitempty" gorm:"types:text;not null"`
3434
CollateralAmount *uint256.Int `json:"collateral_amount,omitempty" gorm:"types:text;not null"`
35-
BadgeAddress types.Address `json:"badge_address,omitempty" gorm:"types:text;not null"`
35+
BadgeAddress Address `json:"badge_address,omitempty" gorm:"types:text;not null"`
3636
DebtIssued *uint256.Int `json:"debt_issued,omitempty" gorm:"types:text;not null"`
3737
MaxInterestRate *uint256.Int `json:"max_interest_rate,omitempty" gorm:"types:text;not null"`
3838
TotalObligation *uint256.Int `json:"total_obligation,omitempty" gorm:"types:text;not null;default:0"`
@@ -45,7 +45,7 @@ type Issuance struct {
4545
UpdatedAt int64 `json:"updated_at,omitempty" gorm:"default:0"`
4646
}
4747

48-
func NewIssuance(title string, description string, promotion string, token types.Address, creatorAddress types.Address, collateralAddress types.Address, collateralAmount *uint256.Int, badgeAddress types.Address, debtIssued *uint256.Int, maxInterestRate *uint256.Int, closesAt int64, maturityAt int64, createdAt int64) (*Issuance, error) {
48+
func NewIssuance(title string, description string, promotion string, token Address, creatorAddress Address, collateralAddress Address, collateralAmount *uint256.Int, badgeAddress Address, debtIssued *uint256.Int, maxInterestRate *uint256.Int, closesAt int64, maturityAt int64, createdAt int64) (*Issuance, error) {
4949
issuance := &Issuance{
5050
Title: title,
5151
Description: description,
@@ -79,19 +79,19 @@ func (a *Issuance) validate() error {
7979
if a.Promotion == "" {
8080
return fmt.Errorf("%w: promotion cannot be empty", ErrInvalidIssuance)
8181
}
82-
if a.Token == (types.Address{}) {
82+
if a.Token == (Address{}) {
8383
return fmt.Errorf("%w: invalid token address", ErrInvalidIssuance)
8484
}
85-
if a.CreatorAddress == (types.Address{}) {
85+
if a.CreatorAddress == (Address{}) {
8686
return fmt.Errorf("%w: invalid creator address", ErrInvalidIssuance)
8787
}
88-
if a.CollateralAddress == (types.Address{}) {
88+
if a.CollateralAddress == (Address{}) {
8989
return fmt.Errorf("%w: invalid collateral address", ErrInvalidIssuance)
9090
}
9191
if a.CollateralAmount.Sign() == 0 {
9292
return fmt.Errorf("%w: collateral amount cannot be zero", ErrInvalidIssuance)
9393
}
94-
if a.BadgeAddress == (types.Address{}) {
94+
if a.BadgeAddress == (Address{}) {
9595
return fmt.Errorf("%w: invalid badge address", ErrInvalidIssuance)
9696
}
9797
if a.DebtIssued.Sign() == 0 {

internal/domain/entity/order.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7-
"github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
7+
. "github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
88
"github.com/holiman/uint256"
99
)
1010

@@ -26,23 +26,23 @@ const (
2626
)
2727

2828
type Order struct {
29-
Id uint `json:"id" gorm:"primaryKey"`
30-
IssuanceId uint `json:"issuance_id" gorm:"not null;index"`
31-
InvestorAddress types.Address `json:"investor_address,omitempty" gorm:"not null"`
32-
Amount *uint256.Int `json:"amount,omitempty" gorm:"types:text;not null"`
33-
InterestRate *uint256.Int `json:"interest_rate,omitempty" gorm:"types:text;not null"`
34-
State OrderState `json:"state,omitempty" gorm:"types:text;not null"`
35-
CreatedAt int64 `json:"created_at,omitempty" gorm:"not null"`
36-
UpdatedAt int64 `json:"updated_at,omitempty" gorm:"default:0"`
29+
Id uint `json:"id" gorm:"primaryKey"`
30+
IssuanceId uint `json:"issuance_id" gorm:"not null;index"`
31+
InvestorAddress Address `json:"investor_address,omitempty" gorm:"not null"`
32+
Amount *uint256.Int `json:"amount,omitempty" gorm:"types:text;not null"`
33+
InterestRate *uint256.Int `json:"interest_rate,omitempty" gorm:"types:text;not null"`
34+
State OrderState `json:"state,omitempty" gorm:"types:text;not null"`
35+
CreatedAt int64 `json:"created_at,omitempty" gorm:"not null"`
36+
UpdatedAt int64 `json:"updated_at,omitempty" gorm:"default:0"`
3737
}
3838

39-
func NewOrder(issuanceId uint, investorAddress types.Address, amount *uint256.Int, interestRate *uint256.Int, createdAt int64) (*Order, error) {
39+
func NewOrder(issuanceId uint, investorAddress Address, amount *uint256.Int, interestRate *uint256.Int, state OrderState, createdAt int64) (*Order, error) {
4040
order := &Order{
4141
IssuanceId: issuanceId,
4242
InvestorAddress: investorAddress,
4343
Amount: amount,
4444
InterestRate: interestRate,
45-
State: OrderStatePending,
45+
State: state,
4646
CreatedAt: createdAt,
4747
}
4848
if err := order.validate(); err != nil {
@@ -55,7 +55,7 @@ func (b *Order) validate() error {
5555
if b.IssuanceId == 0 {
5656
return fmt.Errorf("%w: Issuance ID cannot be zero", ErrInvalidOrder)
5757
}
58-
if b.InvestorAddress == (types.Address{}) {
58+
if b.InvestorAddress == (Address{}) {
5959
return fmt.Errorf("%w: investor address cannot be empty", ErrInvalidOrder)
6060
}
6161
if b.Amount.Sign() <= 0 {

internal/domain/entity/user.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7-
"github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
7+
. "github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
88
)
99

1010
var (
@@ -24,13 +24,13 @@ const (
2424
type User struct {
2525
Id uint `json:"id" gorm:"primaryKey"`
2626
Role UserRole `json:"role,omitempty" gorm:"not null"`
27-
Address types.Address `json:"address,omitempty" gorm:"types:text;uniqueIndex;not null"`
27+
Address Address `json:"address,omitempty" gorm:"types:text;uniqueIndex;not null"`
2828
SocialAccounts []*SocialAccount `json:"social_accounts,omitempty" gorm:"foreignKey:UserId;constraint:OnDelete:CASCADE"`
2929
CreatedAt int64 `json:"created_at,omitempty" gorm:"not null"`
3030
UpdatedAt int64 `json:"updated_at,omitempty" gorm:"default:0"`
3131
}
3232

33-
func NewUser(role string, address types.Address, createdAt int64) (*User, error) {
33+
func NewUser(role string, address Address, createdAt int64) (*User, error) {
3434
user := &User{
3535
Role: UserRole(role),
3636
SocialAccounts: []*SocialAccount{},
@@ -50,7 +50,7 @@ func (u *User) validate() error {
5050
if u.Role != UserRoleAdmin && u.Role != UserRoleCreator && u.Role != UserRoleInvestor && u.Role != UserRoleVerifier {
5151
return fmt.Errorf("%w: invalid role", ErrInvalidUser)
5252
}
53-
if u.Address == (types.Address{}) {
53+
if u.Address == (Address{}) {
5454
return fmt.Errorf("%w: address cannot be empty", ErrInvalidUser)
5555
}
5656
if u.CreatedAt == 0 {

internal/infra/repository/repository.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package repository
22

33
import (
44
"github.com/2025-2A-T20-G91-INTERNO/src/rollup/internal/domain/entity"
5-
types "github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
5+
. "github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
66
)
77

88
type IssuanceRepository interface {
99
CreateIssuance(issuance *entity.Issuance) (*entity.Issuance, error)
10-
FindIssuancesByCreatorAddress(creator types.Address) ([]*entity.Issuance, error)
11-
FindIssuancesByInvestorAddress(investor types.Address) ([]*entity.Issuance, error)
10+
FindIssuancesByCreatorAddress(creator Address) ([]*entity.Issuance, error)
11+
FindOngoingIssuanceByCreatorAddress(creator Address) (*entity.Issuance, error)
12+
FindIssuancesByInvestorAddress(investor Address) ([]*entity.Issuance, error)
1213
FindIssuanceById(id uint) (*entity.Issuance, error)
1314
FindAllIssuances() ([]*entity.Issuance, error)
1415
UpdateIssuance(Issuance *entity.Issuance) (*entity.Issuance, error)
@@ -19,7 +20,7 @@ type OrderRepository interface {
1920
FindOrderById(id uint) (*entity.Order, error)
2021
FindOrdersByIssuanceId(id uint) ([]*entity.Order, error)
2122
FindOrdersByState(issuanceId uint, state string) ([]*entity.Order, error)
22-
FindOrdersByInvestorAddress(investor types.Address) ([]*entity.Order, error)
23+
FindOrdersByInvestorAddress(investor Address) ([]*entity.Order, error)
2324
FindAllOrders() ([]*entity.Order, error)
2425
UpdateOrder(order *entity.Order) (*entity.Order, error)
2526
DeleteOrder(id uint) error
@@ -35,9 +36,9 @@ type SocialAccountRepository interface {
3536
type UserRepository interface {
3637
CreateUser(user *entity.User) (*entity.User, error)
3738
FindUsersByRole(role string) ([]*entity.User, error)
38-
FindUserByAddress(address types.Address) (*entity.User, error)
39+
FindUserByAddress(address Address) (*entity.User, error)
3940
FindAllUsers() ([]*entity.User, error)
40-
DeleteUser(address types.Address) error
41+
DeleteUser(address Address) error
4142
}
4243

4344
type Repository interface {

internal/infra/repository/sqlite/issuance.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55

66
"github.com/2025-2A-T20-G91-INTERNO/src/rollup/internal/domain/entity"
7-
"github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
7+
. "github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
88
"gorm.io/gorm"
99
)
1010

@@ -16,49 +16,63 @@ func (r *SQLiteRepository) CreateIssuance(input *entity.Issuance) (*entity.Issua
1616
}
1717

1818
func (r *SQLiteRepository) FindIssuanceById(id uint) (*entity.Issuance, error) {
19-
var Issuance entity.Issuance
19+
var issuance entity.Issuance
2020
if err := r.Db.
2121
Preload("Orders").
22-
First(&Issuance, id).Error; err != nil {
22+
First(&issuance, id).Error; err != nil {
2323
if err == gorm.ErrRecordNotFound {
2424
return nil, entity.ErrIssuanceNotFound
2525
}
2626
return nil, fmt.Errorf("failed to find issuance by id: %w", err)
2727
}
28-
return &Issuance, nil
28+
return &issuance, nil
2929
}
3030

3131
func (r *SQLiteRepository) FindAllIssuances() ([]*entity.Issuance, error) {
32-
var Issuances []*entity.Issuance
32+
var issuance []*entity.Issuance
3333
if err := r.Db.
3434
Preload("Orders").
35-
Find(&Issuances).Error; err != nil {
35+
Find(&issuance).Error; err != nil {
3636
return nil, fmt.Errorf("failed to find all issuances: %w", err)
3737
}
38-
return Issuances, nil
38+
return issuance, nil
3939
}
4040

41-
func (r *SQLiteRepository) FindIssuancesByInvestorAddress(investor types.Address) ([]*entity.Issuance, error) {
42-
var Issuances []*entity.Issuance
41+
func (r *SQLiteRepository) FindIssuancesByInvestorAddress(investor Address) ([]*entity.Issuance, error) {
42+
var issuance []*entity.Issuance
4343
if err := r.Db.
4444
Joins("JOIN orders ON orders.issuance_id = issuances.id").
4545
Where("orders.investor_address = ?", investor).
4646
Preload("Orders").
47-
Find(&Issuances).Error; err != nil {
47+
Find(&issuance).Error; err != nil {
4848
return nil, fmt.Errorf("failed to find Issuances by investor: %w", err)
4949
}
50-
return Issuances, nil
50+
return issuance, nil
5151
}
5252

53-
func (r *SQLiteRepository) FindIssuancesByCreatorAddress(creator types.Address) ([]*entity.Issuance, error) {
54-
var Issuances []*entity.Issuance
53+
func (r *SQLiteRepository) FindIssuancesByCreatorAddress(creator Address) ([]*entity.Issuance, error) {
54+
var issuance []*entity.Issuance
5555
if err := r.Db.
5656
Where("creator_address = ?", creator).
5757
Preload("Orders").
58-
Find(&Issuances).Error; err != nil {
58+
Find(&issuance).Error; err != nil {
5959
return nil, fmt.Errorf("failed to find issuances by creator: %w", err)
6060
}
61-
return Issuances, nil
61+
return issuance, nil
62+
}
63+
64+
func (r *SQLiteRepository) FindOngoingIssuanceByCreatorAddress(creator Address) (*entity.Issuance, error) {
65+
var issuance entity.Issuance
66+
if err := r.Db.
67+
Where("creator_address = ? AND state = ?", creator, entity.IssuanceStateOngoing).
68+
Preload("Orders").
69+
First(&issuance).Error; err != nil {
70+
if err == gorm.ErrRecordNotFound {
71+
return nil, entity.ErrIssuanceNotFound
72+
}
73+
return nil, fmt.Errorf("failed to find ongoing issuance by creator: %w", err)
74+
}
75+
return &issuance, nil
6276
}
6377

6478
func (r *SQLiteRepository) UpdateIssuance(input *entity.Issuance) (*entity.Issuance, error) {

internal/infra/repository/sqlite/order.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55

66
"github.com/2025-2A-T20-G91-INTERNO/src/rollup/internal/domain/entity"
7-
"github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
7+
. "github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
88
"gorm.io/gorm"
99
)
1010

@@ -44,7 +44,7 @@ func (r *SQLiteRepository) FindOrdersByState(issuanceId uint, state string) ([]*
4444
return orders, nil
4545
}
4646

47-
func (r *SQLiteRepository) FindOrdersByInvestorAddress(investor types.Address) ([]*entity.Order, error) {
47+
func (r *SQLiteRepository) FindOrdersByInvestorAddress(investor Address) ([]*entity.Order, error) {
4848
var orders []*entity.Order
4949
if err := r.Db.Where("investor_address = ?", investor).Find(&orders).Error; err != nil {
5050
return nil, fmt.Errorf("failed to find orders by investor: %w", err)

internal/infra/repository/sqlite/sqlite.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
"github.com/2025-2A-T20-G91-INTERNO/src/rollup/configs"
1616
"github.com/2025-2A-T20-G91-INTERNO/src/rollup/internal/domain/entity"
17-
types "github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
17+
. "github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
1818
)
1919

2020
type SQLiteRepository struct {
@@ -73,7 +73,7 @@ func NewSQLiteRepository(ctx context.Context, conn string) (*SQLiteRepository, e
7373

7474
configs.SetDefaults()
7575

76-
var adminAddr, verifierAddr types.Address
76+
var adminAddr, verifierAddr Address
7777

7878
if isMemory {
7979
a, err := configs.GetAdminAddressTest()
@@ -82,31 +82,31 @@ func NewSQLiteRepository(ctx context.Context, conn string) (*SQLiteRepository, e
8282
} else if err == configs.ErrNotDefined {
8383
return nil, fmt.Errorf("rollup_ADMIN_ADDRESS_TEST is required for the rollup service: %w", err)
8484
}
85-
adminAddr = types.HexToAddress(a.Hex())
85+
adminAddr = HexToAddress(a.Hex())
8686

8787
v, err := configs.GetVerifierAddressTest()
8888
if err != nil && err != configs.ErrNotDefined {
8989
return nil, fmt.Errorf("failed to get rollup_VERIFIER_ADDRESS_TEST: %w", err)
9090
} else if err == configs.ErrNotDefined {
9191
return nil, fmt.Errorf("rollup_VERIFIER_ADDRESS_TEST is required for the rollup service: %w", err)
9292
}
93-
verifierAddr = types.HexToAddress(v.Hex())
93+
verifierAddr = HexToAddress(v.Hex())
9494
} else {
9595
a, err := configs.GetAdminAddress()
9696
if err != nil && err != configs.ErrNotDefined {
9797
return nil, fmt.Errorf("failed to get rollup_ADMIN_ADDRESS: %w", err)
9898
} else if err == configs.ErrNotDefined {
9999
return nil, fmt.Errorf("rollup_ADMIN_ADDRESS is required for the rollup service: %w", err)
100100
}
101-
adminAddr = types.HexToAddress(a.Hex())
101+
adminAddr = HexToAddress(a.Hex())
102102

103103
v, err := configs.GetVerifierAddress()
104104
if err != nil && err != configs.ErrNotDefined {
105105
return nil, fmt.Errorf("failed to get rollup_VERIFIER_ADDRESS: %w", err)
106106
} else if err == configs.ErrNotDefined {
107107
return nil, fmt.Errorf("rollup_VERIFIER_ADDRESS is required for the rollup service: %w", err)
108108
}
109-
verifierAddr = types.HexToAddress(v.Hex())
109+
verifierAddr = HexToAddress(v.Hex())
110110
}
111111

112112
baseTime := time.Now().Unix()

internal/infra/repository/sqlite/user.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55

66
"github.com/2025-2A-T20-G91-INTERNO/src/rollup/internal/domain/entity"
7-
"github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
7+
. "github.com/2025-2A-T20-G91-INTERNO/src/rollup/pkg/types"
88
"gorm.io/gorm"
99
)
1010

@@ -15,7 +15,7 @@ func (r *SQLiteRepository) CreateUser(input *entity.User) (*entity.User, error)
1515
return input, nil
1616
}
1717

18-
func (r *SQLiteRepository) FindUserByAddress(address types.Address) (*entity.User, error) {
18+
func (r *SQLiteRepository) FindUserByAddress(address Address) (*entity.User, error) {
1919
var user entity.User
2020
if err := r.Db.Preload("SocialAccounts").Where("address = ?", address).First(&user).Error; err != nil {
2121
if err == gorm.ErrRecordNotFound {
@@ -42,7 +42,7 @@ func (r *SQLiteRepository) FindAllUsers() ([]*entity.User, error) {
4242
return users, nil
4343
}
4444

45-
func (r *SQLiteRepository) DeleteUser(address types.Address) error {
45+
func (r *SQLiteRepository) DeleteUser(address Address) error {
4646
res := r.Db.Where("address = ?", address).Delete(&entity.User{})
4747
if res.Error != nil {
4848
return fmt.Errorf("failed to delete user: %w", res.Error)

0 commit comments

Comments
 (0)