Transfer moves currency between companies.
(ctx contractapi.TransactionContextInterface, fromCompanyID string, toCompanyID string, amount string)
| 177 | |
| 178 | // Transfer moves currency between companies. |
| 179 | func (s *SmartContract) Transfer(ctx contractapi.TransactionContextInterface, fromCompanyID string, toCompanyID string, amount string) (*Transaction, error) { |
| 180 | fromID, err := strconv.Atoi(fromCompanyID) |
| 181 | if err != nil { |
| 182 | return nil, fmt.Errorf("from company id must be an integer: %w", err) |
| 183 | } |
| 184 | toID, value, err := parseIDAndAmount(toCompanyID, amount) |
| 185 | if err != nil { |
| 186 | return nil, err |
| 187 | } |
| 188 | |
| 189 | from, err := readCompany(ctx, fromID) |
| 190 | if err != nil { |
| 191 | return nil, err |
| 192 | } |
| 193 | if from.Number < value { |
| 194 | return nil, fmt.Errorf("company %d balance is insufficient", fromID) |
| 195 | } |
| 196 | to, err := readCompany(ctx, toID) |
| 197 | if err != nil { |
| 198 | return nil, err |
| 199 | } |
| 200 | |
| 201 | from.Number -= value |
| 202 | to.Number += value |
| 203 | |
| 204 | if err := putJSON(ctx, companyKey(fromID), from); err != nil { |
| 205 | return nil, err |
| 206 | } |
| 207 | if err := putJSON(ctx, companyKey(toID), to); err != nil { |
| 208 | return nil, err |
| 209 | } |
| 210 | return recordTransaction(ctx, 2, fromID, 2, toID, value) |
| 211 | } |
| 212 | |
| 213 | func (s *SmartContract) GetCenterBank(ctx contractapi.TransactionContextInterface) (*CenterBank, error) { |
| 214 | return readCenterBank(ctx) |
nothing calls this directly
no test coverage detected