-
Notifications
You must be signed in to change notification settings - Fork 90
/
main.go
172 lines (140 loc) · 5.11 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"encoding/base64"
"flag"
"fmt"
"math/big"
"time"
"github.com/anyswap/CrossChain-Router/v3/common"
"github.com/anyswap/CrossChain-Router/v3/log"
"github.com/anyswap/CrossChain-Router/v3/mpc"
"github.com/anyswap/CrossChain-Router/v3/params"
"github.com/anyswap/CrossChain-Router/v3/tokens"
"github.com/anyswap/CrossChain-Router/v3/tokens/solana"
routerprog "github.com/anyswap/CrossChain-Router/v3/tokens/solana/programs/router"
"github.com/anyswap/CrossChain-Router/v3/tokens/solana/programs/token"
solanatools "github.com/anyswap/CrossChain-Router/v3/tokens/solana/tools"
"github.com/anyswap/CrossChain-Router/v3/tokens/solana/types"
"github.com/mr-tron/base58"
)
var (
bridge = solana.NewCrossChainBridge()
paramConfigFile string
paramChainID string
paramPublicKey string
paramPriKey string
owner string
// ownerAta string
tokenProgramID string
routerContract string
mpcConfig *mpc.Config
chainID = big.NewInt(0)
payer types.PublicKey
)
func main() {
initAll()
if paramPriKey != "" {
payer = types.MustPrivateKeyFromBase58(paramPriKey).PublicKey()
} else {
payer = types.MustPublicKeyFromBase58(paramPublicKey)
}
payerAddr := payer.String()
fmt.Printf("payer: %v\n", payerAddr)
b1, _ := bridge.GetBalance(payerAddr)
fmt.Printf("payer sol: %v\n", b1)
tokenPubkey := types.MustPublicKeyFromBase58(tokenProgramID)
fmt.Printf("token programid: %v\n", tokenPubkey.String())
ownerPubkey := types.MustPublicKeyFromBase58(owner)
fmt.Printf("owner address: %v\n", ownerPubkey.String())
ownerAtaPublicKey, bump, err := types.PublicKeyFindProgramAddress([][]byte{ownerPubkey.ToSlice(), token.TokenProgramID.ToSlice(), tokenPubkey.ToSlice()}, types.ATAProgramID)
if err != nil {
log.Fatalf("PublicKeyFindProgramAddress error %v", err)
}
fmt.Printf("ownerAtaPublicKey bump:%v address:%v\n", uint8(bump), ownerAtaPublicKey)
routerContractPubkey := types.MustPublicKeyFromBase58(routerContract)
fmt.Printf("router address: %v\n", routerContractPubkey.String())
createMintAccount := routerprog.NewCreateATAInstruction(payer, ownerPubkey, tokenPubkey, ownerAtaPublicKey)
createMintAccount.RouterProgramID = routerContractPubkey
instructions := []types.TransactionInstruction{createMintAccount}
m, _ := createMintAccount.Data()
fmt.Printf("instructions0 length %v %v \n", len(m), base58.Encode(m))
resp, err := bridge.GetLatestBlockhash()
if err != nil {
log.Fatalf("GetLatestBlockhash error %v", err)
}
blockHash := resp.Value.Blockhash
fmt.Printf("blockHash: %v %v\n", resp.Value.LastValidBlockHeight, blockHash)
tx, err := types.NewTransaction(instructions, blockHash, types.TransactionPayer(payer))
if err != nil {
log.Fatalf("NewTransaction error %v", err)
}
signer := &solanatools.Signer{
PublicKey: paramPublicKey,
PrivateKey: paramPriKey,
}
signData, _ := tx.Message.Serialize()
fmt.Printf("sign: %v %v\n", len(signData), base64.StdEncoding.EncodeToString(signData))
for _, a := range tx.Message.AccountKeys {
fmt.Println(a.String())
}
txHash := solanatools.SignAndSend(mpcConfig, bridge, []*solanatools.Signer{signer}, tx)
fmt.Printf("tx send success: %v\n", txHash)
var txm *types.TransactionWithMeta
for i := 0; i < 10; i++ {
txResult, _ := bridge.GetTransaction(txHash)
if txResult != nil {
txm, _ = txResult.(*types.TransactionWithMeta)
break
}
fmt.Println("get tx status ...")
time.Sleep(5 * time.Second)
}
fmt.Printf("tx comfired success at slot: %v BlockTime: %v status: %v\n", uint64(txm.Slot), txm.BlockTime, txm.Meta.IsStatusOk())
result, err := bridge.GetAccountInfo(ownerAtaPublicKey.String(), "jsonParsed")
if err != nil {
log.Fatalf("GetAccountInfo error %v", err)
}
fmt.Println("ownerAta address:", ownerAtaPublicKey)
fmt.Println("accountInfo", result.Value.Data)
}
func initAll() {
initFlags()
initConfig()
initBridge()
}
func initFlags() {
flag.StringVar(¶mConfigFile, "config", "", "config file to init mpc and gateway")
flag.StringVar(¶mChainID, "chainID", "", "chain id")
flag.StringVar(¶mPublicKey, "pubkey", "", "signer solana address")
flag.StringVar(¶mPriKey, "priKey", "", "signer base58 priKey")
flag.StringVar(&owner, "routerAccount", "", "routerAccount address")
flag.StringVar(&tokenProgramID, "token", "", "token address")
flag.StringVar(&routerContract, "router", "", "router address")
flag.Parse()
if paramChainID != "" {
cid, err := common.GetBigIntFromStr(paramChainID)
if err != nil {
log.Fatal("wrong param chainID", "err", err)
}
chainID = cid
}
log.Info("init flags finished")
}
func initConfig() {
config := params.LoadRouterConfig(paramConfigFile, true, false)
mpcConfig = mpc.InitConfig(config.MPC, true)
log.Info("init config finished, IsFastMPC: %v", mpcConfig.IsFastMPC)
}
func initBridge() {
cfg := params.GetRouterConfig()
apiAddrs := cfg.Gateways[chainID.String()]
if len(apiAddrs) == 0 {
log.Fatal("gateway not found for chain ID", "chainID", chainID)
}
apiAddrsExt := cfg.GatewaysExt[chainID.String()]
bridge.SetGatewayConfig(&tokens.GatewayConfig{
APIAddress: apiAddrs,
APIAddressExt: apiAddrsExt,
})
log.Info("init bridge finished")
}