| 1 | -- name: CreateQuote :one |
| 2 | -- It expires by the database's clock, the one LockQuote checks it with. |
| 3 | INSERT INTO fx_quotes (id, owner_id, sell_currency, sell_amount, buy_currency, |
| 4 | buy_amount, fee, rate, expires_at) |
| 5 | VALUES (@id, @owner_id, @sell_currency, @sell_amount, @buy_currency, |
| 6 | @buy_amount, @fee, @rate, now() + make_interval(secs => @ttl_seconds)) |
| 7 | RETURNING *; |
| 8 | |
| 9 | -- name: LockQuote :one |
| 10 | -- Two conversions with the same quote take turns: the second finds it used. |
| 11 | SELECT *, expires_at <= now() AS expired FROM fx_quotes |
| 12 | WHERE id = $1 AND owner_id = $2 |
| 13 | FOR UPDATE; |
| 14 | |
| 15 | -- name: UseQuote :one |
| 16 | UPDATE fx_quotes SET transaction_id = $2 |
| 17 | WHERE id = $1 |
| 18 | RETURNING *; |
| 19 | |
| 20 | -- name: GetWalletByCurrency :one |
| 21 | SELECT * FROM accounts |
| 22 | WHERE kind = 'wallet' AND owner_id = $1 AND currency = $2; |