@@ -20,15 +20,13 @@ import (
2020 "github.com/node-real/go-direct-route/example/utils"
2121)
2222
23+ // Minnet endpoint
2324var rpcEndPoint = "https://bsc-dataseed.binance.org"
24-
25- // Testnet endpoint
26- //var rpcEndPoint = "https://data-seed-prebsc-1-s1.binance.org:8545"
27-
2825var directRouteEndPoint = "https://api.nodereal.io/direct-route"
2926
3027// Testnet endpoint
31- //var directRouteEndPoint = "https://testnet-api.nodereal.io/direct-route"
28+ // var rpcEndPoint = "https://data-seed-prebsc-1-s1.binance.org:8545"
29+ // var directRouteEndPoint = "https://testnet-api.nodereal.io/direct-route"
3230
3331var account1 , _ = utils .FromHexKey ("input your private key1 here" )
3432var account2 , _ = utils .FromHexKey ("input your private key2 here" )
@@ -40,7 +38,7 @@ func getBundlePriceDemo() {
4038 if err != nil {
4139 log .Fatal (fmt .Sprintf ("failed to get bundle price %v" , err ))
4240 }
43- fmt .Printf ("get bundle price price %s\n " , price .String ())
41+ fmt .Printf ("get bundle price: %s, minmal gas price: %s\n " , price . BundlePrice . String (), price . MinimalGasPrice .String ())
4442}
4543
4644func getServiceStatus () {
@@ -60,10 +58,14 @@ the two transaction should be all success or all failed.
6058func sendBNBByBundleDemo () {
6159 directClient , _ := client .Dial (directRouteEndPoint )
6260 rpcClient , _ := ethclient .Dial (rpcEndPoint )
63- price , err := directClient .BundlePrice (context .Background ())
61+ bundlePrice , err := directClient .BundlePrice (context .Background ())
6462 if err != nil {
6563 log .Fatalf ("failed to get bundle price%v" , err )
6664 }
65+ price := bundlePrice .BundlePrice
66+ if price .Cmp (bundlePrice .MinimalGasPrice ) < 0 {
67+ price = bundlePrice .MinimalGasPrice
68+ }
6769
6870 n1 , _ := rpcClient .PendingNonceAt (context .Background (), account1 .Addr )
6971
@@ -125,7 +127,14 @@ we want the bundle been verified on chain during [now+20 second, now+80 second].
125127func sendBUSDByBundleDemo () {
126128 directClient , _ := client .Dial (directRouteEndPoint )
127129 rpcClient , _ := ethclient .Dial (rpcEndPoint )
128- price , _ := directClient .BundlePrice (context .Background ())
130+ bundlePrice , err := directClient .BundlePrice (context .Background ())
131+ if err != nil {
132+ log .Fatalf ("failed to get bundle price%v" , err )
133+ }
134+ price := bundlePrice .BundlePrice
135+ if price .Cmp (bundlePrice .MinimalGasPrice ) < 0 {
136+ price = bundlePrice .MinimalGasPrice
137+ }
129138
130139 n1 , _ := rpcClient .PendingNonceAt (context .Background (), account1 .Addr )
131140
@@ -185,6 +194,80 @@ func sendBUSDByBundleDemo() {
185194 }
186195}
187196
197+ func sendBNBByBundleWithDepositCoinbaseDemo () {
198+ directClient , _ := client .Dial (directRouteEndPoint )
199+ rpcClient , _ := ethclient .Dial (rpcEndPoint )
200+ bundlePrice , err := directClient .BundlePrice (context .Background ())
201+ if err != nil {
202+ log .Fatalf ("failed to get bundle price%v" , err )
203+ }
204+ price := bundlePrice .BundlePrice
205+ if price .Cmp (bundlePrice .MinimalGasPrice ) < 0 {
206+ price = bundlePrice .MinimalGasPrice
207+ }
208+ n1 , _ := rpcClient .PendingNonceAt (context .Background (), account1 .Addr )
209+
210+ chainId := big .NewInt (56 )
211+
212+ // testnet chain id
213+ //chainId := big.NewInt(97)
214+
215+ valueToTransfer := big .NewInt (100 * params .GWei )
216+ gasLimit := uint64 (70000 )
217+ depositCoinbaseABI , _ := abi .JSON (strings .NewReader (eabi .DEPOSIT_COINBASE_ABI ))
218+ data1 , _ := depositCoinbaseABI .Pack ("deposit" )
219+
220+ // testnet contract addr
221+ // depositCoinbaseAddr := common.HexToAddress("0xE7febD44315508a1100E1a06701e7e0Ae5e325Bc")
222+
223+ // mainnet contract addr
224+ depositCoinbaseAddr := common .HexToAddress ("0xB3BB00B9785f35D0BE13B2BD91C8e3742D9Ab03a" )
225+
226+ tx1 , hash1 , _ := utils .SignTransaction (account1 , depositCoinbaseAddr , valueToTransfer , data1 , n1 , gasLimit , price , chainId )
227+ tx2 , hash2 , _ := utils .SignTransaction (account1 , account2 .Addr , valueToTransfer , nil , n1 + 1 , gasLimit , price , chainId )
228+
229+ maxTime := uint64 (time .Now ().Unix () + 80 )
230+
231+ bundle := & client.SendBundleArgs {
232+ Txs : []string {hexutil .Encode (tx1 ), hexutil .Encode (tx2 )},
233+ MaxBlockNumber : "" ,
234+ MinTimestamp : nil ,
235+ MaxTimestamp : & maxTime ,
236+ RevertingTxHashes : nil ,
237+ }
238+ bundleHash , err := directClient .SendBundle (context .Background (), bundle )
239+ if err != nil {
240+ log .Fatalf ("failed to send bundle %v" , err )
241+ }
242+ fmt .Printf ("successfull send bundle, hash %v\n " , bundleHash )
243+
244+ queryBundle , err := directClient .GetBundleByHash (context .Background (), bundleHash )
245+ if err != nil || queryBundle == nil {
246+ log .Fatalf ("failed to query bundle %v" , err )
247+ }
248+
249+ bz , _ := json .Marshal (queryBundle )
250+ fmt .Printf ("The bundle is %s\n " , string (bz ))
251+
252+ found := false
253+ for i := 0 ; i < 42 ; i ++ {
254+ r1 , err1 := rpcClient .TransactionReceipt (context .Background (), hash1 )
255+ r2 , err2 := rpcClient .TransactionReceipt (context .Background (), hash2 )
256+ if r1 != nil && err1 == nil && r2 != nil && err2 == nil {
257+ found = true
258+ break
259+ }
260+ time .Sleep (3 * time .Second )
261+ }
262+ if found {
263+ fmt .Println ("bundle verified on chain" )
264+ } else {
265+ log .Fatalf ("bundle failed to be verified on chain or timeout" )
266+ }
267+ }
268+
188269func main () {
189- sendBNBByBundleDemo ()
270+ //sendBNBByBundleDemo()
271+ //sendBUSDByBundleDemo()
272+ sendBNBByBundleWithDepositCoinbaseDemo ()
190273}
0 commit comments