Skip to content

Commit 196aea9

Browse files
Added PaymentQueryFeeDetails rpc
1 parent 25e9395 commit 196aea9

File tree

6 files changed

+160
-10
lines changed

6 files changed

+160
-10
lines changed

main.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package main
22

3-
import (
4-
"github.com/availproject/avail-go-sdk/examples"
5-
)
3+
import ()
64

75
func main() {
8-
examples.Run()
6+
7+
}
8+
9+
func panic2(err error) {
10+
if err != nil {
11+
panic(err)
12+
}
913
}

metadata/types.go

+13
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ func (this Balance) ToHuman() string {
4646
var trailing = removeTrailingZeros(stringValue)
4747
if trailing == "" {
4848
result += "0"
49+
} else {
50+
missingPlaces := 18 - len(stringValue)
51+
52+
for i := 0; i < missingPlaces; i++ {
53+
result += "0"
54+
}
4955
}
56+
5057
return result + trailing + " Avail"
5158
}
5259

@@ -1585,3 +1592,9 @@ type AddressedMessage struct {
15851592
DestinationDomain uint32 `scale:"compact"`
15861593
Id uint64 `scale:"compact"`
15871594
}
1595+
1596+
type InclusionFee struct {
1597+
BaseFee Balance
1598+
LenFee Balance
1599+
AdjustedWeightFee Balance
1600+
}

sdk/rpc.go

+22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sdk
22

33
import (
4+
"fmt"
45
"strconv"
56

67
prim "github.com/availproject/avail-go-sdk/primitives"
@@ -14,6 +15,7 @@ type RPC struct {
1415
ChainSpec chainSpecRPC
1516
Kate kateRPC
1617
Author authorRPC
18+
Payment paymentRPC
1719
}
1820

1921
func newRPC(client *Client) RPC {
@@ -25,6 +27,7 @@ func newRPC(client *Client) RPC {
2527
ChainSpec: chainSpecRPC{client: client},
2628
Kate: kateRPC{client: client},
2729
Author: authorRPC{client: client},
30+
Payment: paymentRPC{client: client},
2831
}
2932
}
3033

@@ -36,6 +39,25 @@ func (this *RPCParams) Add(value string) {
3639
this.Values = append(this.Values, value)
3740
}
3841

42+
func (this *RPCParams) AddByteSlice(value []byte) {
43+
if len(value) == 0 {
44+
return
45+
}
46+
47+
res := "["
48+
for i, elem := range value {
49+
res += fmt.Sprintf("%v", elem)
50+
51+
if i < (len(value) - 1) {
52+
res += ","
53+
}
54+
}
55+
56+
res = res + "]"
57+
58+
this.Values = append(this.Values, res)
59+
}
60+
3961
func (this *RPCParams) AddH256(value prim.H256) {
4062
this.Add(value.ToRpcParam())
4163
}

sdk/rpc_payment.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package sdk
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"math/big"
7+
"strings"
8+
9+
"github.com/availproject/avail-go-sdk/metadata"
10+
prim "github.com/availproject/avail-go-sdk/primitives"
11+
)
12+
13+
type paymentRPC struct {
14+
client *Client
15+
}
16+
17+
func (this *paymentRPC) QueryFeeDetails(extrinsic string, at prim.Option[prim.H256]) (metadata.InclusionFee, error) {
18+
var params = &RPCParams{}
19+
20+
if !strings.HasPrefix(extrinsic, "0x") {
21+
extrinsic = "0x" + extrinsic
22+
}
23+
24+
params.Add("\"" + extrinsic + "\"")
25+
if at.IsSome() {
26+
params.AddH256(at.Unwrap())
27+
}
28+
29+
rawJson, err := this.client.Request("payment_queryFeeDetails", params.Build())
30+
if err != nil {
31+
return metadata.InclusionFee{}, err
32+
}
33+
34+
var mappedData map[string]interface{}
35+
if err := json.Unmarshal([]byte(rawJson), &mappedData); err != nil {
36+
return metadata.InclusionFee{}, err
37+
}
38+
39+
if mappedData["inclusionFee"] == nil {
40+
return metadata.InclusionFee{}, errors.New("Failed to find inclusionFee")
41+
}
42+
43+
jsonData := mappedData["inclusionFee"].(map[string]interface{})
44+
if jsonData["adjustedWeightFee"] == nil {
45+
return metadata.InclusionFee{}, errors.New("Failed to find adjustedWeightFee")
46+
}
47+
48+
if jsonData["baseFee"] == nil {
49+
return metadata.InclusionFee{}, errors.New("Failed to find baseFee")
50+
}
51+
52+
if jsonData["lenFee"] == nil {
53+
return metadata.InclusionFee{}, errors.New("Failed to find lenFee")
54+
}
55+
56+
res := metadata.InclusionFee{}
57+
58+
{
59+
lenFeeString := jsonData["lenFee"].(string)
60+
v := big.Int{}
61+
if _, ok := v.SetString(lenFeeString[2:], 16); !ok {
62+
return metadata.InclusionFee{}, errors.New("Failed to convert lenFee")
63+
}
64+
res.LenFee = metadata.NewBalanceFromBigInt(&v)
65+
}
66+
67+
{
68+
baseFeeString := jsonData["baseFee"].(string)
69+
v := big.Int{}
70+
if _, ok := v.SetString(baseFeeString[2:], 16); !ok {
71+
return metadata.InclusionFee{}, errors.New("Failed to convert baseFee")
72+
}
73+
res.BaseFee = metadata.NewBalanceFromBigInt(&v)
74+
}
75+
76+
{
77+
adjustedWeightFeeString := jsonData["adjustedWeightFee"].(string)
78+
v := big.Int{}
79+
if _, ok := v.SetString(adjustedWeightFeeString[2:], 16); !ok {
80+
return metadata.InclusionFee{}, errors.New("Failed to convert adjustedWeightFee")
81+
}
82+
res.AdjustedWeightFee = metadata.NewBalanceFromBigInt(&v)
83+
}
84+
85+
return res, err
86+
}

sdk/rpc_state.go

+12
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,15 @@ func (this *stateRPC) GetEvents(at prim.Option[prim.H256]) (string, error) {
7171

7272
return this.client.Request("state_getStorage", params.Build())
7373
}
74+
75+
/* func (this *stateRPC) Call(method string, data string, at prim.Option[prim.H256]) (string, error) {
76+
params := RPCParams{}
77+
params.Add("\"" + method + "\"")
78+
params.Add("\"" + data + "\"")
79+
if at.IsSome() {
80+
params.AddH256(at.Unwrap())
81+
}
82+
83+
return this.client.Request("state_call", params.Build())
84+
}
85+
*/

sdk/transaction.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,44 @@ const Finalization = uint8(0)
1919
const Inclusion = uint8(1)
2020

2121
type Transaction struct {
22-
Client *Client
22+
client *Client
2323
Payload metadata.Payload
2424
}
2525

2626
func NewTransaction(client *Client, payload metadata.Payload) Transaction {
2727
return Transaction{
28-
Client: client,
28+
client: client,
2929
Payload: payload,
3030
}
3131
}
3232

3333
func (this *Transaction) Execute(account subkey.KeyPair, options TransactionOptions) (prim.H256, error) {
34-
return TransactionSignAndSend(this.Client, account, this.Payload, options)
34+
return TransactionSignAndSend(this.client, account, this.Payload, options)
3535
}
3636

3737
func (this *Transaction) ExecuteAndWatch(account subkey.KeyPair, waitFor uint8, blockTimeout uint32, options TransactionOptions) (TransactionDetails, error) {
38-
return TransactionSignSendWatch(this.Client, account, this.Payload, waitFor, blockTimeout, options)
38+
return TransactionSignSendWatch(this.client, account, this.Payload, waitFor, blockTimeout, options)
3939
}
4040

4141
func (this *Transaction) ExecuteAndWatchFinalization(account subkey.KeyPair, options TransactionOptions) (TransactionDetails, error) {
42-
return TransactionSignSendWatch(this.Client, account, this.Payload, Finalization, 5, options)
42+
return TransactionSignSendWatch(this.client, account, this.Payload, Finalization, 5, options)
4343
}
4444

4545
func (this *Transaction) ExecuteAndWatchInclusion(account subkey.KeyPair, options TransactionOptions) (TransactionDetails, error) {
46-
return TransactionSignSendWatch(this.Client, account, this.Payload, Inclusion, 3, options)
46+
return TransactionSignSendWatch(this.client, account, this.Payload, Inclusion, 3, options)
47+
}
48+
49+
func (this *Transaction) PaymentQueryFeeDetails(account subkey.KeyPair, options TransactionOptions) (metadata.InclusionFee, error) {
50+
extra, additional, err := options.ToPrimitive(this.client, account.SS58Address(42))
51+
if err != nil {
52+
return metadata.InclusionFee{}, err
53+
}
54+
tx, err := prim.CreateSigned(this.Payload.Call, extra, additional, account)
55+
if err != nil {
56+
return metadata.InclusionFee{}, err
57+
}
58+
59+
return this.client.Rpc.Payment.QueryFeeDetails(tx.Value, prim.NewNone[prim.H256]())
4760
}
4861

4962
func TransactionSignAndSend(client *Client, account subkey.KeyPair, payload metadata.Payload, options TransactionOptions) (prim.H256, error) {

0 commit comments

Comments
 (0)