Skip to content

Commit

Permalink
Merge pull request #3139 from dessaya/fix-empty-allowance
Browse files Browse the repository at this point in the history
fix(evm): moveAllowedFunds with numTokens = 0
  • Loading branch information
fijter authored Dec 11, 2023
2 parents cea343c + a7934d7 commit 5ab9d86
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
5 changes: 3 additions & 2 deletions packages/vm/core/evm/evmimpl/iscmagic_privileged.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ func (h *magicContractHandler) MoveAllowedFunds(
to common.Address,
allowance iscmagic.ISCAssets,
) {
taken := subtractFromAllowance(h.ctx, from, to, allowance.Unwrap())
assets := allowance.Unwrap()
subtractFromAllowance(h.ctx, from, to, assets)
h.ctx.Privileged().MustMoveBetweenAccounts(
isc.NewEthereumAddressAgentID(h.ctx.ChainID(), from),
isc.NewEthereumAddressAgentID(h.ctx.ChainID(), to),
taken,
assets,
)
}
5 changes: 3 additions & 2 deletions packages/vm/core/evm/evmimpl/iscmagic_sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ func (h *magicContractHandler) Allow(target common.Address, allowance iscmagic.I

// handler for ISCSandbox::takeAllowedFunds
func (h *magicContractHandler) TakeAllowedFunds(addr common.Address, allowance iscmagic.ISCAssets) {
taken := subtractFromAllowance(h.ctx, addr, h.caller.Address(), allowance.Unwrap())
assets := allowance.Unwrap()
subtractFromAllowance(h.ctx, addr, h.caller.Address(), assets)
h.ctx.Privileged().MustMoveBetweenAccounts(
isc.NewEthereumAddressAgentID(h.ctx.ChainID(), addr),
isc.NewEthereumAddressAgentID(h.ctx.ChainID(), h.caller.Address()),
taken,
assets,
)
}

Expand Down
12 changes: 2 additions & 10 deletions packages/vm/core/evm/evmimpl/iscmagic_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,18 @@ func withAllowance(ctx isc.Sandbox, from, to common.Address, f func(*isc.Assets)

var errFundsNotAllowed = coreerrors.Register("remaining allowance insufficient").Create()

func subtractFromAllowance(ctx isc.Sandbox, from, to common.Address, taken *isc.Assets) *isc.Assets {
func subtractFromAllowance(ctx isc.Sandbox, from, to common.Address, taken *isc.Assets) {
state := evm.ISCMagicSubrealm(ctx.State())
key := keyAllowance(from, to)

remaining := isc.MustAssetsFromBytes(state.Get(key))
if taken.IsEmpty() {
taken = remaining.Clone()
}

ok := remaining.Spend(taken)
if !ok {
if ok := remaining.Spend(taken); !ok {
panic(errFundsNotAllowed)
}
if remaining.IsEmpty() {
state.Del(key)
} else {
state.Set(key, remaining.Bytes())
}

return taken
}

// directory of ERC20 contract addresses by native token ID
Expand Down

0 comments on commit 5ab9d86

Please sign in to comment.