|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "log" |
| 6 | + "math" |
| 7 | + "math/big" |
| 8 | + |
| 9 | + "github.com/availproject/avail-go-sdk/src/config" |
| 10 | + "github.com/availproject/avail-go-sdk/src/sdk" |
| 11 | + "github.com/availproject/avail-go-sdk/src/sdk/types" |
| 12 | + "github.com/vedhavyas/go-subkey" |
| 13 | + |
| 14 | + "fmt" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + config, err := config.LoadConfig() |
| 19 | + if err != nil { |
| 20 | + fmt.Printf("cannot load config:%v", err) |
| 21 | + } |
| 22 | + api, err := sdk.NewSDK(config.ApiURL) |
| 23 | + if err != nil { |
| 24 | + fmt.Printf("cannot create api:%v", err) |
| 25 | + } |
| 26 | + |
| 27 | + amount := uint64(math.Pow(10, 18)) * 10 // send amount 10 AVAIL |
| 28 | + dest := "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty" |
| 29 | + keyringPair, err := sdk.KeyringFromSeed(config.Seed) |
| 30 | + if err != nil { |
| 31 | + panic(fmt.Sprintf("cannot create KeyPair:%v", err)) |
| 32 | + } |
| 33 | + _, pubkeyBytes, _ := subkey.SS58Decode(dest) |
| 34 | + hexString := subkey.EncodeHex(pubkeyBytes) |
| 35 | + destAddr, err := sdk.NewMultiAddressFromHexAccountID(hexString) |
| 36 | + if err != nil { |
| 37 | + log.Fatalf("Failed to create address from given hex: %v", err) |
| 38 | + } |
| 39 | + bondAmountUCompact := types.NewUCompactFromUInt(amount) |
| 40 | + |
| 41 | + ext, err := sdk.CreateExtrinsic(api, "Balances.transfer_keep_alive", keyringPair, 0, destAddr, bondAmountUCompact) |
| 42 | + if err != nil { |
| 43 | + log.Fatalf("Failed to create extrinsic: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + encodedExt, err := sdk.EncodeToHex(ext) |
| 47 | + if err != nil { |
| 48 | + log.Fatalf("Failed to encode extrinsic: %v", err) |
| 49 | + } |
| 50 | + fmt.Println("Encoded Extrinsic:", encodedExt) |
| 51 | + var paymentInfo map[string]interface{} |
| 52 | + err = api.Client.Call(&paymentInfo, "payment_queryInfo", encodedExt, nil) |
| 53 | + if err != nil { |
| 54 | + log.Fatalf("Failed to get payment info: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + // Format the weight |
| 58 | + weight := paymentInfo["weight"].(map[string]interface{}) |
| 59 | + weightJSON, err := json.Marshal(map[string]interface{}{ |
| 60 | + "refTime": weight["ref_time"], |
| 61 | + "proofSize": weight["proof_size"], |
| 62 | + }) |
| 63 | + if err != nil { |
| 64 | + log.Fatalf("Failed to marshal weight: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Format the partial fee |
| 68 | + partialFee, ok := new(big.Int).SetString(paymentInfo["partialFee"].(string), 10) |
| 69 | + if !ok { |
| 70 | + log.Fatalf("Failed to parse partialFee") |
| 71 | + } |
| 72 | + mAVAIL := new(big.Float).Quo(new(big.Float).SetInt(partialFee), big.NewFloat(1e15)) |
| 73 | + fmt.Printf("Transaction Fee for Balance Transfer:\n") |
| 74 | + fmt.Printf(" class=%s,\n", paymentInfo["class"]) |
| 75 | + fmt.Printf(" weight=%s,\n", string(weightJSON)) |
| 76 | + fmt.Printf(" partialFee=%.4f mAVAIL\n", mAVAIL) |
| 77 | +} |
0 commit comments