-
Notifications
You must be signed in to change notification settings - Fork 13
/
processor.go
31 lines (28 loc) · 1017 Bytes
/
processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package virtualmachine
import "github.com/orbs-network/go-experiment/types/services/statestorage"
func (s *service) processTransfer(fromUsername string, toUsername string, amount int32) (int32, error) {
fromBalance, err := s.stateStorage.ReadKey(&statestorage.ReadKeyInput{Key: fromUsername})
if err != nil {
return 0, err
}
toBalance, err := s.stateStorage.ReadKey(&statestorage.ReadKeyInput{Key: toUsername})
if err != nil {
return 0, err
}
_, err = s.stateStorage.WriteKey(&statestorage.WriteKeyInput{Key: fromUsername, Value: fromBalance.Value - amount})
if err != nil {
return 0, err
}
_, err = s.stateStorage.WriteKey(&statestorage.WriteKeyInput{Key: toUsername, Value: toBalance.Value + amount})
if err != nil {
return 0, err
}
return fromBalance.Value - amount, nil
}
func (s *service) processGetBalance(username string) (int32, error) {
balance, err := s.stateStorage.ReadKey(&statestorage.ReadKeyInput{Key: username})
if err != nil {
return 0, err
}
return balance.Value, nil
}