-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package chain | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/CESSProject/cess-go-sdk/core/pattern" | ||
"github.com/CESSProject/cess-go-sdk/utils" | ||
"github.com/centrifuge/go-substrate-rpc-client/v4/types" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input types.Bytes, value types.U256, gasLimit types.U64, maxFeePerGas types.U256, accessList []pattern.AccessInfo) (string, error) { | ||
c.lock.Lock() | ||
defer func() { | ||
c.lock.Unlock() | ||
if err := recover(); err != nil { | ||
log.Println(utils.RecoverError(err)) | ||
} | ||
}() | ||
|
||
var ( | ||
txhash string | ||
accountInfo types.AccountInfo | ||
) | ||
|
||
if !c.GetChainState() { | ||
return txhash, fmt.Errorf("chainSDK.UploadDeclaration(): GetChainState(): %v", pattern.ERR_RPC_CONNECTION) | ||
} | ||
|
||
var nonce types.Option[types.U256] | ||
nonce.SetNone() | ||
|
||
var maxPriorityFeePerGas types.Option[types.U256] | ||
maxPriorityFeePerGas.SetNone() | ||
|
||
call, err := types.NewCall(c.metadata, pattern.TX_EVM_CALL, source, target, input, value, gasLimit, maxFeePerGas, maxPriorityFeePerGas, nonce, accessList) | ||
if err != nil { | ||
err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.EVM, err) | ||
c.SetChainState(false) | ||
return txhash, err | ||
} | ||
|
||
ext := types.NewExtrinsic(call) | ||
|
||
key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) | ||
if err != nil { | ||
err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.EVM, err) | ||
c.SetChainState(false) | ||
return txhash, err | ||
} | ||
|
||
ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) | ||
if err != nil { | ||
err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.EVM, err) | ||
c.SetChainState(false) | ||
return txhash, err | ||
} | ||
|
||
if !ok { | ||
keyStr, _ := utils.NumsToByteStr(key, map[string]bool{}) | ||
return txhash, fmt.Errorf( | ||
"chain rpc.state.GetStorageLatest[%v]: %v", | ||
keyStr, | ||
pattern.ERR_RPC_EMPTY_VALUE, | ||
) | ||
} | ||
|
||
o := types.SignatureOptions{ | ||
BlockHash: c.genesisHash, | ||
Era: types.ExtrinsicEra{IsMortalEra: false}, | ||
GenesisHash: c.genesisHash, | ||
Nonce: types.NewUCompactFromUInt(uint64(accountInfo.Nonce)), | ||
SpecVersion: c.runtimeVersion.SpecVersion, | ||
Tip: types.NewUCompactFromUInt(0), | ||
TransactionVersion: c.runtimeVersion.TransactionVersion, | ||
} | ||
|
||
err = ext.Sign(c.keyring, o) | ||
if err != nil { | ||
err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.EVM, err) | ||
c.SetChainState(false) | ||
return txhash, err | ||
} | ||
|
||
sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) | ||
if err != nil { | ||
if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { | ||
o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) | ||
err = ext.Sign(c.keyring, o) | ||
if err != nil { | ||
return txhash, errors.Wrap(err, "[Sign]") | ||
} | ||
sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) | ||
if err != nil { | ||
err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.EVM, err) | ||
c.SetChainState(false) | ||
return txhash, err | ||
} | ||
} else { | ||
err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.EVM, err) | ||
c.SetChainState(false) | ||
return txhash, err | ||
} | ||
} | ||
defer sub.Unsubscribe() | ||
|
||
timeout := time.NewTimer(c.packingTime) | ||
defer timeout.Stop() | ||
|
||
for { | ||
select { | ||
case status := <-sub.Chan(): | ||
if status.IsInBlock { | ||
txhash = status.AsInBlock.Hex() | ||
return txhash, err | ||
} | ||
case err = <-sub.Err(): | ||
return txhash, errors.Wrap(err, "[sub]") | ||
case <-timeout.C: | ||
return txhash, pattern.ERR_RPC_TIMEOUT | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"fmt" | ||
"log" | ||
"math/big" | ||
"time" | ||
|
||
cess "github.com/CESSProject/cess-go-sdk" | ||
"github.com/CESSProject/cess-go-sdk/chain" | ||
"github.com/CESSProject/cess-go-sdk/core/pattern" | ||
"github.com/centrifuge/go-substrate-rpc-client/v4/types" | ||
) | ||
|
||
// Substrate well-known mnemonic: | ||
// | ||
// https://github.com/substrate-developer-hub/substrate-developer-hub.github.io/issues/613 | ||
var MY_MNEMONIC = "head achieve piano online exhaust bulk trust vote inflict room keen maximum" | ||
|
||
var RPC_ADDRS = []string{ | ||
//devnet | ||
"wss://devnet-rpc.cess.cloud/ws/", | ||
//testnet | ||
// "wss://testnet-rpc0.cess.cloud/ws/", | ||
// "wss://testnet-rpc1.cess.cloud/ws/", | ||
// "wss://testnet-rpc2.cess.cloud/ws/", | ||
} | ||
|
||
func main() { | ||
// 1. new sdk | ||
sdk, err := NewSDK() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var ( | ||
source types.H160 | ||
target types.H160 | ||
input types.Bytes | ||
value types.U256 | ||
gasLimit types.U64 | ||
maxFeePerGas types.U256 | ||
accessList []pattern.AccessInfo | ||
) | ||
|
||
s_h160, err := hex.DecodeString("1e3e1c69dfbd27d398e92da4844a9abdc2786ac0") | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
source = types.NewH160(s_h160) | ||
|
||
t_h160, err := hex.DecodeString("7352188979857675C3aD1AA6662326ebD6DDBf6d") | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
target = types.NewH160(t_h160) | ||
|
||
input_string, err := hex.DecodeString("a9059cbb00000000000000000000000085cdaca43a76c8ab769b974c2cf7306980742a310000000000000000000000000000000000000000000000000de0b6b3a7640000") | ||
input = input_string | ||
|
||
value = types.NewU256(*big.NewInt(0)) | ||
|
||
gasLimit = 3000000 | ||
|
||
maxFeePerGas = types.NewU256(*big.NewInt(500000000)) | ||
|
||
block_hash, err := sdk.SendEvmCall(source, target, input, value, gasLimit, maxFeePerGas, accessList) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
fmt.Printf("%s", block_hash) | ||
} | ||
|
||
func NewSDK() (*chain.ChainClient, error) { | ||
return cess.New( | ||
context.Background(), | ||
cess.ConnectRpcAddrs(RPC_ADDRS), | ||
cess.Mnemonic(MY_MNEMONIC), | ||
cess.TransactionTimeout(time.Second*10), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters