@@ -43,6 +43,10 @@ type revision struct {
4343 journalIndex int
4444}
4545
46+ func Transfer (db vm.StateDB , sender , recipient common.Address , amount * big.Int ) {
47+ db .(* StateDB ).Transfer (sender , recipient , amount )
48+ }
49+
4650var _ vm.StateDB = & StateDB {}
4751
4852// StateDB structs within the ethereum protocol are used to store anything
@@ -382,11 +386,33 @@ func (s *StateDB) Context() sdk.Context {
382386 * SETTERS
383387 */
384388
389+ // Transfer from one account to another
390+ func (s * StateDB ) Transfer (sender , recipient common.Address , amount * big.Int ) {
391+ if amount .Sign () == 0 {
392+ return
393+ }
394+ if amount .Sign () < 0 {
395+ panic ("negative amount" )
396+ }
397+
398+ coins := sdk .NewCoins (sdk .NewCoin (s .evmDenom , sdkmath .NewIntFromBigIntMut (amount )))
399+ senderAddr := sdk .AccAddress (sender .Bytes ())
400+ recipientAddr := sdk .AccAddress (recipient .Bytes ())
401+ if err := s .ExecuteNativeAction (common.Address {}, nil , func (ctx sdk.Context ) error {
402+ return s .keeper .Transfer (ctx , senderAddr , recipientAddr , coins )
403+ }); err != nil {
404+ s .err = err
405+ }
406+ }
407+
385408// AddBalance adds amount to the account associated with addr.
386409func (s * StateDB ) AddBalance (addr common.Address , amount * big.Int ) {
387- if amount .Sign () < = 0 {
410+ if amount .Sign () = = 0 {
388411 return
389412 }
413+ if amount .Sign () < 0 {
414+ panic ("negative amount" )
415+ }
390416 coins := sdk.Coins {sdk .NewCoin (s .evmDenom , sdkmath .NewIntFromBigInt (amount ))}
391417 if err := s .ExecuteNativeAction (common.Address {}, nil , func (ctx sdk.Context ) error {
392418 return s .keeper .AddBalance (ctx , sdk .AccAddress (addr .Bytes ()), coins )
@@ -397,9 +423,12 @@ func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) {
397423
398424// SubBalance subtracts amount from the account associated with addr.
399425func (s * StateDB ) SubBalance (addr common.Address , amount * big.Int ) {
400- if amount .Sign () < = 0 {
426+ if amount .Sign () = = 0 {
401427 return
402428 }
429+ if amount .Sign () < 0 {
430+ panic ("negative amount" )
431+ }
403432 coins := sdk.Coins {sdk .NewCoin (s .evmDenom , sdkmath .NewIntFromBigInt (amount ))}
404433 if err := s .ExecuteNativeAction (common.Address {}, nil , func (ctx sdk.Context ) error {
405434 return s .keeper .SubBalance (ctx , sdk .AccAddress (addr .Bytes ()), coins )
@@ -408,6 +437,7 @@ func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) {
408437 }
409438}
410439
440+ // SetBalance is called by state override
411441func (s * StateDB ) SetBalance (addr common.Address , amount * big.Int ) {
412442 if err := s .ExecuteNativeAction (common.Address {}, nil , func (ctx sdk.Context ) error {
413443 return s .keeper .SetBalance (ctx , addr , amount , s .evmDenom )
0 commit comments