MCPcopy
hub / github.com/yeasy/blockchain_guide / Transfer

Method Transfer

11_app_dev/chaincode_example03.go:179–211  ·  view source on GitHub ↗

Transfer moves currency between companies.

(ctx contractapi.TransactionContextInterface, fromCompanyID string, toCompanyID string, amount string)

Source from the content-addressed store, hash-verified

177
178// Transfer moves currency between companies.
179func (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
213func (s *SmartContract) GetCenterBank(ctx contractapi.TransactionContextInterface) (*CenterBank, error) {
214 return readCenterBank(ctx)

Callers

nothing calls this directly

Calls 5

parseIDAndAmountFunction · 0.85
readCompanyFunction · 0.85
companyKeyFunction · 0.85
recordTransactionFunction · 0.85
putJSONFunction · 0.70

Tested by

no test coverage detected