-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathneovm_contract.go
113 lines (103 loc) · 3.01 KB
/
neovm_contract.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright 2019 the DNA Dev team
// Copyright 2018 the Ontology Dev team
//
package DNA_go_sdk
import (
"encoding/hex"
"fmt"
"time"
sdkcom "github.com/DNAProject/DNA-go-sdk/common"
"github.com/DNAProject/DNA/common"
"github.com/DNAProject/DNA/core/payload"
"github.com/DNAProject/DNA/core/types"
httpcom "github.com/DNAProject/DNA/http/base/common"
)
type NeoVMContract struct {
dnaSkd *DNASdk
}
func newNeoVMContract(dnaSkd *DNASdk) *NeoVMContract {
return &NeoVMContract{
dnaSkd: dnaSkd,
}
}
func (this *NeoVMContract) NewDeployNeoVMCodeTransaction(gasPrice, gasLimit uint64, contract *payload.DeployCode) *types.MutableTransaction {
tx := &types.MutableTransaction{
Version: sdkcom.VERSION_TRANSACTION,
TxType: types.Deploy,
Nonce: uint32(time.Now().Unix()),
Payload: contract,
GasPrice: gasPrice,
GasLimit: gasLimit,
Sigs: make([]types.Sig, 0, 0),
}
return tx
}
//DeploySmartContract Deploy smart contract to dna
func (this *NeoVMContract) DeployNeoVMSmartContract(
gasPrice,
gasLimit uint64,
singer *Account,
needStorage bool,
code,
name,
version,
author,
email,
desc string) (common.Uint256, error) {
invokeCode, err := hex.DecodeString(code)
if err != nil {
return common.UINT256_EMPTY, fmt.Errorf("code hex decode error:%s", err)
}
deployCode, err := payload.NewDeployCode(invokeCode, payload.NEOVM_TYPE, name, version, author, email, desc)
if err != nil {
return common.UINT256_EMPTY, fmt.Errorf("build deployCode err: %s", err)
}
tx := this.NewDeployNeoVMCodeTransaction(gasPrice, gasLimit, deployCode)
err = this.dnaSkd.SignToTransaction(tx, singer)
if err != nil {
return common.Uint256{}, err
}
txHash, err := this.dnaSkd.SendTransaction(tx)
if err != nil {
return common.Uint256{}, fmt.Errorf("SendRawTransaction error:%s", err)
}
return txHash, nil
}
func (this *NeoVMContract) NewNeoVMInvokeTransaction(
gasPrice,
gasLimit uint64,
contractAddress common.Address,
params []interface{},
) (*types.MutableTransaction, error) {
invokeCode, err := httpcom.BuildNeoVMInvokeCode(contractAddress, params)
if err != nil {
return nil, err
}
return this.dnaSkd.NewInvokeTransaction(gasPrice, gasLimit, invokeCode), nil
}
func (this *NeoVMContract) InvokeNeoVMContract(
gasPrice,
gasLimit uint64,
signer *Account,
contractAddress common.Address,
params []interface{}) (common.Uint256, error) {
tx, err := this.NewNeoVMInvokeTransaction(gasPrice, gasLimit, contractAddress, params)
if err != nil {
return common.UINT256_EMPTY, fmt.Errorf("NewNeoVMInvokeTransaction error:%s", err)
}
err = this.dnaSkd.SignToTransaction(tx, signer)
if err != nil {
return common.UINT256_EMPTY, err
}
return this.dnaSkd.SendTransaction(tx)
}
func (this *NeoVMContract) PreExecInvokeNeoVMContract(
contractAddress common.Address,
params []interface{}) (*sdkcom.PreExecResult, error) {
tx, err := this.NewNeoVMInvokeTransaction(0, 0, contractAddress, params)
if err != nil {
return nil, err
}
return this.dnaSkd.PreExecTransaction(tx)
}