Skip to content

Commit 6280868

Browse files
Added Tip abd Mortality to Tx Options.
1 parent 7d37939 commit 6280868

6 files changed

+103
-28
lines changed

examples/data_submission.go

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func RunDataSubmission() {
5555
// Transaction Details
5656
fmt.Println(fmt.Sprintf(`Block Hash: %v, Block Index: %v, Tx Hash: %v, Tx Index: %v`, res.BlockHash.ToHexWith0x(), res.BlockNumber, res.TxHash.ToHexWith0x(), res.TxIndex))
5757

58+
AssertTrue(res.Events.IsSome(), "Failed to decode events.")
5859
events = res.Events.Unwrap()
5960
event2 := SDK.EventFindFirst(events, daPallet.EventDataSubmitted{}).Unwrap()
6061

examples/transaction_options.go

+64-6
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@ import (
88
)
99

1010
func RunTransactionOptions() {
11-
runAppId()
12-
runNonce()
11+
RunTransactionOptionsAppId()
12+
RunTransactionOptionsNonce()
13+
RunTransactionOptionsMortality()
14+
RunTransactionOptionsTip()
1315

1416
fmt.Println("RunTransactionOptions finished correctly.")
1517
}
1618

17-
func runAppId() {
19+
func RunTransactionOptionsAppId() {
1820
sdk, err := SDK.NewSDK(SDK.LocalEndpoint)
1921
PanicOnError(err)
2022

21-
// Setting AppId and Executing Transaction
23+
// Setting AppId
2224
appId := uint32(5)
23-
tx := sdk.Tx.DataAvailability.SubmitData([]byte("Hello World"))
2425
options := SDK.NewTransactionOptions().WithAppId(appId)
26+
27+
// Executing Transaction
28+
tx := sdk.Tx.DataAvailability.SubmitData([]byte("Hello World"))
2529
res, err := tx.ExecuteAndWatchInclusion(SDK.Account.Alice(), options)
2630
PanicOnError(err)
2731

@@ -34,9 +38,11 @@ func runAppId() {
3438
genTx := block.TransactionByHash(res.TxHash).UnsafeUnwrap()
3539
foundAppId := genTx.Signed().UnsafeUnwrap().AppId
3640
AssertEq(appId, foundAppId, "App Ids are not the same")
41+
42+
fmt.Println("RunTransactionOptionsAppId finished correctly.")
3743
}
3844

39-
func runNonce() {
45+
func RunTransactionOptionsNonce() {
4046
sdk, err := SDK.NewSDK(SDK.LocalEndpoint)
4147
PanicOnError(err)
4248

@@ -64,4 +70,56 @@ func runNonce() {
6470
newNonce, err := SDK.Account.Nonce(sdk.Client, metadata.NewAccountIdFromKeyPair(acc))
6571
PanicOnError(err)
6672
AssertEq(newNonce, currentNonce+1, "New nonce and old nonce + 1 are not the same.")
73+
74+
fmt.Println("RunTransactionOptionsNonce finished correctly.")
75+
}
76+
77+
func RunTransactionOptionsMortality() {
78+
sdk, err := SDK.NewSDK(SDK.LocalEndpoint)
79+
PanicOnError(err)
80+
81+
// Setting Mortality
82+
mortality := uint32(16)
83+
options := SDK.NewTransactionOptions().WithMortality(mortality).WithAppId(1)
84+
85+
// Executing Transaction
86+
tx := sdk.Tx.DataAvailability.SubmitData([]byte("Hello World"))
87+
res, err := tx.ExecuteAndWatchInclusion(SDK.Account.Alice(), options)
88+
PanicOnError(err)
89+
AssertTrue(res.IsSuccessful().Unwrap(), "Transaction is supposed to succeed")
90+
91+
block, err := SDK.NewBlock(sdk.Client, res.BlockHash)
92+
PanicOnError(err)
93+
94+
// Checking if the Mortality is the same as the one expected
95+
genTx := block.TransactionByHash(res.TxHash).UnsafeUnwrap()
96+
actualMortality := uint32(genTx.Signed().UnsafeUnwrap().Era.Period)
97+
AssertEq(actualMortality, mortality, "Moartalities are not the same.")
98+
99+
fmt.Println("RunTransactionOptionsMortality finished correctly.")
100+
}
101+
102+
func RunTransactionOptionsTip() {
103+
sdk, err := SDK.NewSDK(SDK.LocalEndpoint)
104+
PanicOnError(err)
105+
106+
// Setting Tip
107+
tip := SDK.OneAvail()
108+
options := SDK.NewTransactionOptions().WithTip(tip).WithAppId(1)
109+
110+
// Executing Transaction
111+
tx := sdk.Tx.DataAvailability.SubmitData([]byte("Hello World"))
112+
res, err := tx.ExecuteAndWatchInclusion(SDK.Account.Alice(), options)
113+
PanicOnError(err)
114+
AssertTrue(res.IsSuccessful().Unwrap(), "Transaction is supposed to succeed")
115+
116+
block, err := SDK.NewBlock(sdk.Client, res.BlockHash)
117+
PanicOnError(err)
118+
119+
// Checking if the Tip is the same as the one expected
120+
genTx := block.TransactionByHash(res.TxHash).UnsafeUnwrap()
121+
actualTip := genTx.Signed().UnsafeUnwrap().Tip
122+
AssertEq(metadata.Balance{Value: actualTip}, tip, "Tips are not the same.")
123+
124+
fmt.Println("RunTransactionOptionsTip finished correctly.")
67125
}

main.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

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

75
func main() {
86
examples.Run()

primitives/extrinsics_decoded.go

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package primitives
33
import (
44
"errors"
55

6+
"github.com/itering/scale.go/utiles/uint128"
67
"golang.org/x/crypto/blake2b"
78
)
89

@@ -18,6 +19,8 @@ type DecodedExtrinsicSigned struct {
1819
Signature MultiSignature
1920
Nonce uint32
2021
AppId uint32
22+
Tip uint128.Uint128
23+
Era Era
2124
}
2225

2326
func NewDecodedExtrinsic(extrinsic EncodedExtrinsic, txIndex uint32) (DecodedExtrinsic, error) {
@@ -67,6 +70,8 @@ func NewDecodedExtrinsic(extrinsic EncodedExtrinsic, txIndex uint32) (DecodedExt
6770
Signature: multiSignature,
6871
Nonce: extra.Nonce,
6972
AppId: extra.AppId,
73+
Tip: extra.Tip,
74+
Era: extra.Era,
7075
}
7176

7277
signedPart.Set(signedData)

primitives/extrinsics_payload.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ func (this *Call) Decode(decoder *Decoder) error {
5050

5151
// Do not change the order of field members.
5252
type Era struct {
53-
period uint64
54-
phase uint64
53+
Period uint64
54+
Phase uint64
5555
}
5656

5757
func (this *Era) EncodeTo(dest *string) {
58-
quantizeFactor := math.Max(float64(this.period>>12), 1)
59-
trailingZeros := bits.TrailingZeros16(uint16(this.period))
60-
encoded := uint16(float64(this.phase)/quantizeFactor)<<4 | uint16(math.Min(15, math.Max(1, float64(trailingZeros-1))))
58+
quantizeFactor := math.Max(float64(this.Period>>12), 1)
59+
trailingZeros := bits.TrailingZeros16(uint16(this.Period))
60+
encoded := uint16(float64(this.Phase)/quantizeFactor)<<4 | uint16(math.Min(15, math.Max(1, float64(trailingZeros-1))))
6161

6262
first := byte(encoded & 0xff)
6363
second := byte(encoded >> 8)
@@ -87,9 +87,9 @@ func (this *Era) Decode(decoder *Decoder) error {
8787
trailingZeros := uint16(encoded&0xF) + 1 // Lower 4 bits + 1
8888
quantizedPhase := encoded >> 4 // Upper 12 bits
8989

90-
quantizeFactor := math.Max(float64(this.period>>12), 1)
91-
this.phase = uint64(float64(quantizedPhase) * quantizeFactor)
92-
this.period = uint64(uint16(1 << trailingZeros))
90+
quantizeFactor := math.Max(float64(this.Period>>12), 1)
91+
this.Phase = uint64(float64(quantizedPhase) * quantizeFactor)
92+
this.Period = uint64(uint16(1 << trailingZeros))
9393

9494
return nil
9595
}
@@ -109,8 +109,8 @@ func NewEra(period uint64, blockNumber uint64) Era {
109109
quantized_phase := float64(phase) / quantize_factor * quantize_factor
110110

111111
return Era{
112-
period: calPeriod,
113-
phase: uint64(quantized_phase),
112+
Period: calPeriod,
113+
Phase: uint64(quantized_phase),
114114
}
115115
}
116116

sdk/transaction_options.go

+22-9
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ package sdk
33
import (
44
"github.com/itering/scale.go/utiles/uint128"
55

6-
"math/big"
7-
6+
"github.com/availproject/avail-go-sdk/metadata"
87
prim "github.com/availproject/avail-go-sdk/primitives"
98
)
109

1110
type TransactionOptions struct {
12-
AppId prim.Option[uint32]
13-
Nonce prim.Option[uint32]
11+
AppId prim.Option[uint32]
12+
Nonce prim.Option[uint32]
13+
Mortality prim.Option[uint32]
14+
Tip prim.Option[metadata.Balance]
1415
}
1516

1617
func NewTransactionOptions() TransactionOptions {
1718
return TransactionOptions{
18-
AppId: prim.NewNone[uint32](),
19-
Nonce: prim.NewNone[uint32](),
19+
AppId: prim.NewNone[uint32](),
20+
Nonce: prim.NewNone[uint32](),
21+
Mortality: prim.NewNone[uint32](),
22+
Tip: prim.NewNone[metadata.Balance](),
2023
}
2124
}
2225

@@ -30,6 +33,16 @@ func (this TransactionOptions) WithNonce(value uint32) TransactionOptions {
3033
return this
3134
}
3235

36+
func (this TransactionOptions) WithMortality(value uint32) TransactionOptions {
37+
this.Mortality = prim.NewSome(value)
38+
return this
39+
}
40+
41+
func (this TransactionOptions) WithTip(value metadata.Balance) TransactionOptions {
42+
this.Tip = prim.NewSome(value)
43+
return this
44+
}
45+
3346
func (this *TransactionOptions) ToPrimitive(client *Client, accountAddress string) (prim.Extra, prim.Additional, error) {
3447
genesisHash, err := client.Rpc.ChainSpec.V1GenesisHash()
3548
if err != nil {
@@ -59,7 +72,7 @@ func (this *TransactionOptions) ToPrimitive(client *Client, accountAddress strin
5972

6073
extra := prim.Extra{}
6174
extra.AppId = this.AppId.UnwrapOr(uint32(0))
62-
extra.Tip = uint128.FromBig(big.NewInt(0))
75+
extra.Tip = this.Tip.UnwrapOr(metadata.Balance{Value: uint128.Zero}).Value
6376
if this.Nonce.IsNone() {
6477
extra.Nonce, err = client.Rpc.System.AccountNextIndex(accountAddress)
6578
if err != nil {
@@ -68,7 +81,7 @@ func (this *TransactionOptions) ToPrimitive(client *Client, accountAddress strin
6881
} else {
6982
extra.Nonce = this.Nonce.Unwrap()
7083
}
71-
extra.Era = prim.NewEra(32, uint64(forBlockNumber))
84+
extra.Era = prim.NewEra(uint64(this.Mortality.UnwrapOr(32)), uint64(forBlockNumber))
7285

7386
return extra, additional, nil
7487
}
@@ -84,7 +97,7 @@ func RegenerateEra(client *Client, extra *prim.Extra, additional *prim.Additiona
8497
}
8598

8699
additional.ForkHash = forkHash
87-
extra.Era = prim.NewEra(32, uint64(header.Number))
100+
extra.Era = prim.NewEra(extra.Era.Period, uint64(header.Number))
88101

89102
return nil
90103
}

0 commit comments

Comments
 (0)