Skip to content

Commit

Permalink
Merge pull request #7 from primevprotocol/precompile-updates
Browse files Browse the repository at this point in the history
precompile updates
  • Loading branch information
shaspitz authored Dec 14, 2023
2 parents e0fd766 + 8067974 commit 0d63eca
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions core/vm/contracts_with_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,26 @@ func (c *mint) RequiredGas(input []byte) uint64 {
return 100
}

// Predetermined create2 address of whitelist contract with exclusive mint/burn privileges.
// See: https://github.com/primevprotocol/contracts/blob/ecfd53d43770201da9c7a697be1fb03e5e554e4e/scripts/DeployScripts.s.sol
const whitelistCreate2Addr = "0xe57ee51bcb0914EC666703F923e0433d8c4d70b1"

func (c *mint) Run(input []byte, ctx *precompileContext) ([]byte, error) {

// TODO: filter out non-allowed callers
_ = common.BytesToAddress(input[0:32]) // From
if ctx.caller != common.HexToAddress(whitelistCreate2Addr) {
return nil, fmt.Errorf("Error parsing transfer: caller not whitelisted")
}

to := common.BytesToAddress(input[32:64])
mintTo := common.BytesToAddress(input[0:32])

var parsed bool
value, parsed := math.ParseBig256(hexutil.Encode(input[64:96]))
value, parsed := math.ParseBig256(hexutil.Encode(input[32:64]))
if !parsed {
return nil, fmt.Errorf("Error parsing transfer: unable to parse value from " + hexutil.Encode(input[64:96]))
}

// Mint case: Create native token out of thin air
ctx.evm.StateDB.AddBalance(to, value)
// Create native token out of thin air
ctx.evm.StateDB.AddBalance(mintTo, value)

return input, nil
}
Expand All @@ -106,22 +111,22 @@ func (c *burn) RequiredGas(input []byte) uint64 {

func (c *burn) Run(input []byte, ctx *precompileContext) ([]byte, error) {

// TODO: filter out non-allowed callers
_ = common.BytesToAddress(input[0:32]) // From
if ctx.caller != common.HexToAddress(whitelistCreate2Addr) {
return nil, fmt.Errorf("Error parsing transfer: caller not whitelisted")
}

// Address to get their tokens burned
addrReqTokenBurn := common.BytesToAddress(input[32:64])
burnFrom := common.BytesToAddress(input[0:32])

var parsed bool
value, parsed := math.ParseBig256(hexutil.Encode(input[64:96]))
value, parsed := math.ParseBig256(hexutil.Encode(input[32:64]))
if !parsed {
return nil, fmt.Errorf("Error parsing transfer: unable to parse value from " + hexutil.Encode(input[64:96]))
}

if !ctx.CanTransfer(ctx.evm.StateDB, addrReqTokenBurn, value) {
if !ctx.CanTransfer(ctx.evm.StateDB, burnFrom, value) {
return nil, ErrInsufficientBalance
}
ctx.evm.StateDB.SubBalance(addrReqTokenBurn, value)
ctx.evm.StateDB.SubBalance(burnFrom, value)

return input, nil
}

0 comments on commit 0d63eca

Please sign in to comment.