fx.sql.go167 lines, 4.3 KB
| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: fx.sql |
| 5 | |
| 6 | package walletdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/fiston-user/payments/internal/fx" |
| 13 | "github.com/google/uuid" |
| 14 | ) |
| 15 | |
| 16 | const createQuote = `-- name: CreateQuote :one |
| 17 | INSERT INTO fx_quotes (id, owner_id, sell_currency, sell_amount, buy_currency, |
| 18 | buy_amount, fee, rate, expires_at) |
| 19 | VALUES ($1, $2, $3, $4, $5, |
| 20 | $6, $7, $8, now() + make_interval(secs => $9)) |
| 21 | RETURNING id, owner_id, sell_currency, sell_amount, buy_currency, buy_amount, fee, rate, expires_at, created_at, transaction_id |
| 22 | ` |
| 23 | |
| 24 | type CreateQuoteParams struct { |
| 25 | ID uuid.UUID `json:"id"` |
| 26 | OwnerID uuid.UUID `json:"owner_id"` |
| 27 | SellCurrency string `json:"sell_currency"` |
| 28 | SellAmount int64 `json:"sell_amount"` |
| 29 | BuyCurrency string `json:"buy_currency"` |
| 30 | BuyAmount int64 `json:"buy_amount"` |
| 31 | Fee int64 `json:"fee"` |
| 32 | Rate fx.Rate `json:"rate"` |
| 33 | TtlSeconds float64 `json:"ttl_seconds"` |
| 34 | } |
| 35 | |
| 36 | // It expires by the database's clock, the one LockQuote checks it with. |
| 37 | func (q *Queries) CreateQuote(ctx context.Context, arg CreateQuoteParams) (FXQuote, error) { |
| 38 | row := q.db.QueryRow(ctx, createQuote, |
| 39 | arg.ID, |
| 40 | arg.OwnerID, |
| 41 | arg.SellCurrency, |
| 42 | arg.SellAmount, |
| 43 | arg.BuyCurrency, |
| 44 | arg.BuyAmount, |
| 45 | arg.Fee, |
| 46 | arg.Rate, |
| 47 | arg.TtlSeconds, |
| 48 | ) |
| 49 | var i FXQuote |
| 50 | err := row.Scan( |
| 51 | &i.ID, |
| 52 | &i.OwnerID, |
| 53 | &i.SellCurrency, |
| 54 | &i.SellAmount, |
| 55 | &i.BuyCurrency, |
| 56 | &i.BuyAmount, |
| 57 | &i.Fee, |
| 58 | &i.Rate, |
| 59 | &i.ExpiresAt, |
| 60 | &i.CreatedAt, |
| 61 | &i.TransactionID, |
| 62 | ) |
| 63 | return i, err |
| 64 | } |
| 65 | |
| 66 | const getWalletByCurrency = `-- name: GetWalletByCurrency :one |
| 67 | SELECT id, owner_id, name, currency, normal_balance, created_at, kind, frozen FROM accounts |
| 68 | WHERE kind = 'wallet' AND owner_id = $1 AND currency = $2 |
| 69 | ` |
| 70 | |
| 71 | type GetWalletByCurrencyParams struct { |
| 72 | OwnerID *uuid.UUID `json:"owner_id"` |
| 73 | Currency string `json:"currency"` |
| 74 | } |
| 75 | |
| 76 | func (q *Queries) GetWalletByCurrency(ctx context.Context, arg GetWalletByCurrencyParams) (Account, error) { |
| 77 | row := q.db.QueryRow(ctx, getWalletByCurrency, arg.OwnerID, arg.Currency) |
| 78 | var i Account |
| 79 | err := row.Scan( |
| 80 | &i.ID, |
| 81 | &i.OwnerID, |
| 82 | &i.Name, |
| 83 | &i.Currency, |
| 84 | &i.NormalBalance, |
| 85 | &i.CreatedAt, |
| 86 | &i.Kind, |
| 87 | &i.Frozen, |
| 88 | ) |
| 89 | return i, err |
| 90 | } |
| 91 | |
| 92 | const lockQuote = `-- name: LockQuote :one |
| 93 | SELECT id, owner_id, sell_currency, sell_amount, buy_currency, buy_amount, fee, rate, expires_at, created_at, transaction_id, expires_at <= now() AS expired FROM fx_quotes |
| 94 | WHERE id = $1 AND owner_id = $2 |
| 95 | FOR UPDATE |
| 96 | ` |
| 97 | |
| 98 | type LockQuoteParams struct { |
| 99 | ID uuid.UUID `json:"id"` |
| 100 | OwnerID uuid.UUID `json:"owner_id"` |
| 101 | } |
| 102 | |
| 103 | type LockQuoteRow struct { |
| 104 | ID uuid.UUID `json:"id"` |
| 105 | OwnerID uuid.UUID `json:"owner_id"` |
| 106 | SellCurrency string `json:"sell_currency"` |
| 107 | SellAmount int64 `json:"sell_amount"` |
| 108 | BuyCurrency string `json:"buy_currency"` |
| 109 | BuyAmount int64 `json:"buy_amount"` |
| 110 | Fee int64 `json:"fee"` |
| 111 | Rate fx.Rate `json:"rate"` |
| 112 | ExpiresAt time.Time `json:"expires_at"` |
| 113 | CreatedAt time.Time `json:"created_at"` |
| 114 | TransactionID *uuid.UUID `json:"transaction_id"` |
| 115 | Expired bool `json:"expired"` |
| 116 | } |
| 117 | |
| 118 | // Two conversions with the same quote take turns: the second finds it used. |
| 119 | func (q *Queries) LockQuote(ctx context.Context, arg LockQuoteParams) (LockQuoteRow, error) { |
| 120 | row := q.db.QueryRow(ctx, lockQuote, arg.ID, arg.OwnerID) |
| 121 | var i LockQuoteRow |
| 122 | err := row.Scan( |
| 123 | &i.ID, |
| 124 | &i.OwnerID, |
| 125 | &i.SellCurrency, |
| 126 | &i.SellAmount, |
| 127 | &i.BuyCurrency, |
| 128 | &i.BuyAmount, |
| 129 | &i.Fee, |
| 130 | &i.Rate, |
| 131 | &i.ExpiresAt, |
| 132 | &i.CreatedAt, |
| 133 | &i.TransactionID, |
| 134 | &i.Expired, |
| 135 | ) |
| 136 | return i, err |
| 137 | } |
| 138 | |
| 139 | const useQuote = `-- name: UseQuote :one |
| 140 | UPDATE fx_quotes SET transaction_id = $2 |
| 141 | WHERE id = $1 |
| 142 | RETURNING id, owner_id, sell_currency, sell_amount, buy_currency, buy_amount, fee, rate, expires_at, created_at, transaction_id |
| 143 | ` |
| 144 | |
| 145 | type UseQuoteParams struct { |
| 146 | ID uuid.UUID `json:"id"` |
| 147 | TransactionID *uuid.UUID `json:"transaction_id"` |
| 148 | } |
| 149 | |
| 150 | func (q *Queries) UseQuote(ctx context.Context, arg UseQuoteParams) (FXQuote, error) { |
| 151 | row := q.db.QueryRow(ctx, useQuote, arg.ID, arg.TransactionID) |
| 152 | var i FXQuote |
| 153 | err := row.Scan( |
| 154 | &i.ID, |
| 155 | &i.OwnerID, |
| 156 | &i.SellCurrency, |
| 157 | &i.SellAmount, |
| 158 | &i.BuyCurrency, |
| 159 | &i.BuyAmount, |
| 160 | &i.Fee, |
| 161 | &i.Rate, |
| 162 | &i.ExpiresAt, |
| 163 | &i.CreatedAt, |
| 164 | &i.TransactionID, |
| 165 | ) |
| 166 | return i, err |
| 167 | } |