wallet.proto162 lines, 4.2 KB
| 1 | syntax = "proto3"; |
| 2 | |
| 3 | // Version 1 of the wallet's API. A breaking change means a new package, |
| 4 | // wallet.v2, served next to this one until every client has moved. |
| 5 | package wallet.v1; |
| 6 | |
| 7 | import "google/protobuf/timestamp.proto"; |
| 8 | |
| 9 | // WalletService is the wallet's API for our other services. Clients outside |
| 10 | // the cluster use the REST API, through the gateway. |
| 11 | service WalletService { |
| 12 | // CreateWallet opens a wallet for the caller, in one currency. A user has at |
| 13 | // most one wallet per currency. |
| 14 | rpc CreateWallet(CreateWalletRequest) returns (CreateWalletResponse); |
| 15 | // GetWallet returns a wallet. |
| 16 | rpc GetWallet(GetWalletRequest) returns (GetWalletResponse); |
| 17 | // GetBalance returns a wallet's balance, derived from its ledger entries. |
| 18 | rpc GetBalance(GetBalanceRequest) returns (GetBalanceResponse); |
| 19 | // Deposit adds money from outside to one of the caller's wallets, once per |
| 20 | // idempotency key, like Transfer. |
| 21 | rpc Deposit(DepositRequest) returns (DepositResponse); |
| 22 | // Transfer moves money between two wallets in the same currency. It runs |
| 23 | // once per idempotency key: a retry with the same key returns the first |
| 24 | // response, and moves nothing. |
| 25 | rpc Transfer(TransferRequest) returns (TransferResponse); |
| 26 | } |
| 27 | |
| 28 | // HoldService sets money aside for operations that take several steps, like |
| 29 | // a transfer saga. Its callers are our services, so requests name the user |
| 30 | // they act for. Every call is safe to repeat: the hold ID is the key. |
| 31 | service HoldService { |
| 32 | // Hold moves money out of a wallet into a hold, if the wallet can cover it. |
| 33 | rpc Hold(HoldRequest) returns (HoldResponse); |
| 34 | // Capture pays a hold into another wallet. |
| 35 | rpc Capture(CaptureRequest) returns (CaptureResponse); |
| 36 | // Release gives a hold back to its wallet. |
| 37 | rpc Release(ReleaseRequest) returns (ReleaseResponse); |
| 38 | // DescribeWallet returns anyone's wallet, and its status: what a transfer |
| 39 | // checks before it holds money. |
| 40 | rpc DescribeWallet(DescribeWalletRequest) returns (DescribeWalletResponse); |
| 41 | } |
| 42 | |
| 43 | // Money is an amount in minor units of a currency, like cents. Never a float. |
| 44 | message Money { |
| 45 | // 1050 is 10.50 USD. |
| 46 | int64 amount_minor = 1; |
| 47 | // An ISO 4217 code, like USD. |
| 48 | string currency = 2; |
| 49 | } |
| 50 | |
| 51 | // Wallet is a customer's account, in one currency. |
| 52 | message Wallet { |
| 53 | string id = 1; |
| 54 | string owner_id = 2; |
| 55 | string currency = 3; |
| 56 | google.protobuf.Timestamp create_time = 4; |
| 57 | // A frozen wallet can't send or receive money. |
| 58 | bool frozen = 5; |
| 59 | } |
| 60 | |
| 61 | message CreateWalletRequest { |
| 62 | string currency = 1; |
| 63 | } |
| 64 | |
| 65 | message CreateWalletResponse { |
| 66 | Wallet wallet = 1; |
| 67 | } |
| 68 | |
| 69 | message GetWalletRequest { |
| 70 | string wallet_id = 1; |
| 71 | } |
| 72 | |
| 73 | message GetWalletResponse { |
| 74 | Wallet wallet = 1; |
| 75 | } |
| 76 | |
| 77 | message GetBalanceRequest { |
| 78 | string wallet_id = 1; |
| 79 | } |
| 80 | |
| 81 | message GetBalanceResponse { |
| 82 | string wallet_id = 1; |
| 83 | Money balance = 2; |
| 84 | } |
| 85 | |
| 86 | message DepositRequest { |
| 87 | // Chosen by the caller, unique per deposit: 1 to 255 characters. |
| 88 | string idempotency_key = 1; |
| 89 | string wallet_id = 2; |
| 90 | Money amount = 3; |
| 91 | } |
| 92 | |
| 93 | message DepositResponse { |
| 94 | string transaction_id = 1; |
| 95 | string wallet_id = 2; |
| 96 | Money amount = 3; |
| 97 | google.protobuf.Timestamp create_time = 4; |
| 98 | } |
| 99 | |
| 100 | message TransferRequest { |
| 101 | // Chosen by the caller, unique per transfer: 1 to 255 characters. Send the |
| 102 | // same key again to retry safely. |
| 103 | string idempotency_key = 1; |
| 104 | string from_wallet_id = 2; |
| 105 | string to_wallet_id = 3; |
| 106 | Money amount = 4; |
| 107 | } |
| 108 | |
| 109 | message TransferResponse { |
| 110 | string transaction_id = 1; |
| 111 | string from_wallet_id = 2; |
| 112 | string to_wallet_id = 3; |
| 113 | Money amount = 4; |
| 114 | google.protobuf.Timestamp create_time = 5; |
| 115 | } |
| 116 | |
| 117 | // Hold is money set aside, until it's captured or released. |
| 118 | message Hold { |
| 119 | string id = 1; |
| 120 | string wallet_id = 2; |
| 121 | Money amount = 3; |
| 122 | // held, captured or released |
| 123 | string status = 4; |
| 124 | } |
| 125 | |
| 126 | message HoldRequest { |
| 127 | // Chosen by the caller, like a transfer's ID. |
| 128 | string hold_id = 1; |
| 129 | // The wallet must be theirs. |
| 130 | string owner_id = 2; |
| 131 | string wallet_id = 3; |
| 132 | Money amount = 4; |
| 133 | } |
| 134 | |
| 135 | message HoldResponse { |
| 136 | Hold hold = 1; |
| 137 | } |
| 138 | |
| 139 | message CaptureRequest { |
| 140 | string hold_id = 1; |
| 141 | string to_wallet_id = 2; |
| 142 | } |
| 143 | |
| 144 | message CaptureResponse { |
| 145 | Hold hold = 1; |
| 146 | } |
| 147 | |
| 148 | message ReleaseRequest { |
| 149 | string hold_id = 1; |
| 150 | } |
| 151 | |
| 152 | message ReleaseResponse { |
| 153 | Hold hold = 1; |
| 154 | } |
| 155 | |
| 156 | message DescribeWalletRequest { |
| 157 | string wallet_id = 1; |
| 158 | } |
| 159 | |
| 160 | message DescribeWalletResponse { |
| 161 | Wallet wallet = 1; |
| 162 | } |