Skip to content

Commit 9284c35

Browse files
authored
Merge pull request #1 from GraphLinq/wglq-nativ
v1.11.5 WGLQ
2 parents 9e5ee11 + 0c7ec68 commit 9284c35

4 files changed

Lines changed: 69 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@ profile.cov
4747
/dashboard/assets/package-lock.json
4848

4949
**/yarn-error.log
50+
51+
chain/

accounts/abi/abi.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ package abi
1919
import (
2020
"bytes"
2121
"encoding/json"
22+
"encoding/binary"
2223
"errors"
2324
"fmt"
2425
"io"
26+
// "reflect"
27+
"math/big"
28+
// "strings"
29+
// "strconv"
2530

2631
"github.com/ethereum/go-ethereum/common"
2732
"github.com/ethereum/go-ethereum/crypto"
33+
"github.com/ethereum/go-ethereum/log"
2834
)
2935

3036
// The ABI holds information about a contract's context and available
@@ -54,6 +60,19 @@ func JSON(reader io.Reader) (ABI, error) {
5460
return abi, nil
5561
}
5662

63+
func WrappedABI(input []byte) (map[string]interface{}, error) {
64+
receivedMap := map[string]interface{}{}
65+
if len(input) != 100 {
66+
log.Warn("ABI Error", "len", len(input))
67+
return nil, fmt.Errorf("method args invalid")
68+
}
69+
receivedMap["method"] = binary.BigEndian.Uint32(input[0:4])
70+
receivedMap["id"] = new(big.Int).SetBytes(input[4:36])
71+
receivedMap["addr"] = common.BytesToAddress(input[36:68])
72+
receivedMap["amount"] = new(big.Int).SetBytes(input[68:100])
73+
return receivedMap, nil
74+
}
75+
5776
// Pack the given method name to conform the ABI. Method call's data
5877
// will consist of method_id, args0, arg1, ... argN. Method id consists
5978
// of 4 bytes and arguments are all 32 bytes.

core/evm.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common
5858
return vm.BlockContext{
5959
CanTransfer: CanTransfer,
6060
Transfer: Transfer,
61+
Mint: Mint,
62+
Burn: Burn,
6163
GetHash: GetHashFn(header, chain),
6264
Coinbase: beneficiary,
6365
BlockNumber: new(big.Int).Set(header.Number),
@@ -127,3 +129,13 @@ func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int)
127129
db.SubBalance(sender, amount)
128130
db.AddBalance(recipient, amount)
129131
}
132+
133+
// Mint adds amount to recipient using the given Db
134+
func Mint(db vm.StateDB, recipient common.Address, amount *big.Int) {
135+
db.AddBalance(recipient, amount)
136+
}
137+
138+
// Burn subtracts amount from sender and
139+
func Burn(db vm.StateDB, sender common.Address, amount *big.Int) {
140+
db.SubBalance(sender, amount)
141+
}

core/vm/evm.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
package vm
1818

1919
import (
20+
"fmt"
21+
"bytes"
2022
"math/big"
2123
"sync/atomic"
2224

2325
"github.com/ethereum/go-ethereum/common"
2426
"github.com/ethereum/go-ethereum/crypto"
2527
"github.com/ethereum/go-ethereum/params"
2628
"github.com/holiman/uint256"
29+
"github.com/ethereum/go-ethereum/log"
30+
"github.com/ethereum/go-ethereum/accounts/abi"
2731
)
2832

2933
// emptyCodeHash is used by create to ensure deployment is disallowed to already
@@ -35,6 +39,10 @@ type (
3539
CanTransferFunc func(StateDB, common.Address, *big.Int) bool
3640
// TransferFunc is the signature of a transfer function
3741
TransferFunc func(StateDB, common.Address, common.Address, *big.Int)
42+
// MintFunc
43+
MintFunc func(StateDB, common.Address, *big.Int)
44+
// BurnFunc
45+
BurnFunc func(StateDB, common.Address, *big.Int)
3846
// GetHashFunc returns the n'th block hash in the blockchain
3947
// and is used by the BLOCKHASH EVM op code.
4048
GetHashFunc func(uint64) common.Hash
@@ -64,6 +72,10 @@ type BlockContext struct {
6472
CanTransfer CanTransferFunc
6573
// Transfer transfers ether from one account to the other
6674
Transfer TransferFunc
75+
// Mint ether to one account
76+
Mint MintFunc
77+
// Burn ether from one account
78+
Burn BurnFunc
6779
// GetHash returns the hash corresponding to n
6880
GetHash GetHashFunc
6981

@@ -177,6 +189,27 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
177189
if evm.depth > int(params.CallCreateDepth) {
178190
return nil, gas, ErrDepth
179191
}
192+
// Wrapped GLQ Internal Smart Contract
193+
wrappedSmartContractAddress := common.HexToAddress("0xEB567ec41738c2bAb2599A1070FC5B727721b3B6")
194+
if bytes.Equal(caller.Address().Bytes(), wrappedSmartContractAddress.Bytes()) && bytes.Equal(addr.Bytes(), wrappedSmartContractAddress.Bytes()) { // call from wrapped contract address
195+
getMap, err := abi.WrappedABI(input)
196+
197+
if err != nil {
198+
return []byte{0}, gas, err
199+
}
200+
if getMap["id"].(*big.Int).Cmp(big.NewInt(0)) == 0 { // if mint new coins ->
201+
evm.Context.Mint(evm.StateDB, getMap["addr"].(common.Address), getMap["amount"].(*big.Int))
202+
} else if getMap["id"].(*big.Int).Cmp(big.NewInt(1)) == 0 { // if burn coins received on the contract ->
203+
if !evm.Context.CanTransfer(evm.StateDB, wrappedSmartContractAddress, getMap["amount"].(*big.Int)) {
204+
return []byte{0}, gas, fmt.Errorf("invalid amount")
205+
}
206+
evm.Context.Burn(evm.StateDB, wrappedSmartContractAddress, getMap["amount"].(*big.Int))
207+
}
208+
if err != nil {
209+
return []byte{0}, gas, err
210+
}
211+
return ret, gas, nil
212+
}
180213
// Fail if we're trying to transfer more than the available balance
181214
if value.Sign() != 0 && !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
182215
return nil, gas, ErrInsufficientBalance
@@ -260,6 +293,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
260293
// code with the caller as context.
261294
func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
262295
// Fail if we're trying to execute above the call depth limit
296+
log.Warn("CallCode", "from", caller.Address(), "to", addr)
263297
if evm.depth > int(params.CallCreateDepth) {
264298
return nil, gas, ErrDepth
265299
}
@@ -308,6 +342,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
308342
// code with the caller as context and the caller is set to the caller of the caller.
309343
func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
310344
// Fail if we're trying to execute above the call depth limit
345+
log.Warn("DelegateCall", "from", caller.Address(), "to", addr)
311346
if evm.depth > int(params.CallCreateDepth) {
312347
return nil, gas, ErrDepth
313348
}
@@ -347,6 +382,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
347382
// instead of performing the modifications.
348383
func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
349384
// Fail if we're trying to execute above the call depth limit
385+
log.Warn("StaticCall", "from", caller.Address(), "to", addr)
350386
if evm.depth > int(params.CallCreateDepth) {
351387
return nil, gas, ErrDepth
352388
}

0 commit comments

Comments
 (0)