Skip to content

Commit e501fa3

Browse files
Reimplementation v020 (#8)
* Reimplementation v020 * Added constants for existing networks. * Removed linter
1 parent b866b38 commit e501fa3

File tree

103 files changed

+4758
-4980
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+4758
-4980
lines changed

.github/workflows/lint.yml

-27
This file was deleted.

complex/block.go

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package complex
2+
3+
import (
4+
meta "go-sdk/metadata"
5+
daPallet "go-sdk/metadata/pallets/data_availability"
6+
prim "go-sdk/primitives"
7+
)
8+
9+
type Block struct {
10+
client *Client
11+
Block RPCBlock
12+
events prim.Option[EventRecords]
13+
}
14+
15+
func NewBlock(client *Client, blockHash prim.H256) Block {
16+
block := client.GetBlock(prim.NewSome(blockHash))
17+
events, err := client.GetEvents(prim.NewSome(blockHash))
18+
if err != nil {
19+
panic(err)
20+
}
21+
return Block{
22+
client: client,
23+
Block: block,
24+
events: prim.NewSome(events),
25+
}
26+
}
27+
28+
func (this *Block) TransactionBySigner(accountId meta.AccountId) []BlockTransaction {
29+
var result = []BlockTransaction{}
30+
for _, tx := range this.Block.Extrinsics {
31+
if !sameSignature(&tx, accountId) {
32+
continue
33+
}
34+
result = append(result, NewBlockTransaction(this.client, &tx))
35+
}
36+
37+
return result
38+
}
39+
40+
func (this *Block) TransactionByIndex(txIndex uint32) prim.Option[BlockTransaction] {
41+
for _, tx := range this.Block.Extrinsics {
42+
if tx.TxIndex == txIndex {
43+
return prim.NewSome(NewBlockTransaction(this.client, &tx))
44+
}
45+
}
46+
47+
return prim.NewNone[BlockTransaction]()
48+
}
49+
50+
func (this *Block) TransactionByHash(txHash prim.H256) prim.Option[BlockTransaction] {
51+
for _, tx := range this.Block.Extrinsics {
52+
if tx.TxHash == txHash {
53+
return prim.NewSome(NewBlockTransaction(this.client, &tx))
54+
}
55+
}
56+
57+
return prim.NewNone[BlockTransaction]()
58+
}
59+
60+
func (this *Block) TransactionByAppId(appId uint32) []BlockTransaction {
61+
var result = []BlockTransaction{}
62+
for _, tx := range this.Block.Extrinsics {
63+
if tx.Signed.IsNone() {
64+
continue
65+
}
66+
var signed = tx.Signed.Unwrap()
67+
if signed.AppId != appId {
68+
continue
69+
}
70+
result = append(result, NewBlockTransaction(this.client, &tx))
71+
}
72+
73+
return result
74+
}
75+
76+
func (this *Block) DataSubmissionBySigner(accountId meta.AccountId) []DataSubmission {
77+
var result = []DataSubmission{}
78+
for _, tx := range this.Block.Extrinsics {
79+
if !sameSignature(&tx, accountId) {
80+
continue
81+
}
82+
83+
if res, ok := NewDataSubmission(&tx); ok {
84+
result = append(result, res)
85+
}
86+
87+
}
88+
89+
return result
90+
}
91+
92+
func (this *Block) DataSubmissionByIndex(txIndex uint32) prim.Option[DataSubmission] {
93+
for _, tx := range this.Block.Extrinsics {
94+
if tx.TxIndex != txIndex {
95+
continue
96+
}
97+
if res, ok := NewDataSubmission(&tx); ok {
98+
return prim.NewSome(res)
99+
}
100+
}
101+
102+
return prim.NewNone[DataSubmission]()
103+
}
104+
105+
func (this *Block) DataSubmissionByHash(txHash prim.H256) prim.Option[DataSubmission] {
106+
for _, tx := range this.Block.Extrinsics {
107+
if tx.TxHash != txHash {
108+
continue
109+
}
110+
if res, ok := NewDataSubmission(&tx); ok {
111+
return prim.NewSome(res)
112+
}
113+
}
114+
115+
return prim.NewNone[DataSubmission]()
116+
}
117+
118+
func (this *Block) DataSubmissionByAppId(appId uint32) []DataSubmission {
119+
var result = []DataSubmission{}
120+
for _, tx := range this.Block.Extrinsics {
121+
if tx.Signed.IsNone() {
122+
continue
123+
}
124+
var signed = tx.Signed.Unwrap()
125+
if signed.AppId != appId {
126+
continue
127+
}
128+
if res, ok := NewDataSubmission(&tx); ok {
129+
result = append(result, res)
130+
}
131+
}
132+
133+
return result
134+
}
135+
136+
func (this *Block) Events(appId uint32) prim.Option[EventRecords] {
137+
return this.events
138+
}
139+
140+
func sameSignature(tx *prim.DecodedExtrinsic, accountId meta.AccountId) bool {
141+
txAccountId := tx.Signed.UnwrapOrDefault().Address.Id.UnwrapOrDefault()
142+
if accountId.Value != txAccountId {
143+
return false
144+
}
145+
146+
return true
147+
}
148+
149+
type DataSubmission struct {
150+
TxHash prim.H256
151+
TxIndex uint32
152+
Data []byte
153+
TxSigner prim.MultiAddress
154+
AppId uint32
155+
}
156+
157+
func NewDataSubmission(tx *prim.DecodedExtrinsic) (DataSubmission, bool) {
158+
ok := false
159+
res := DataSubmission{}
160+
161+
callSubmitData := daPallet.CallSubmitData{}
162+
if tx.Call.PalletIndex != callSubmitData.PalletIndex() {
163+
return res, ok
164+
}
165+
166+
if tx.Call.CallIndex != callSubmitData.CallIndex() {
167+
return res, ok
168+
}
169+
170+
// Data submission cannot be done without signed being set.
171+
signed := tx.Signed.UnwrapOrDefault()
172+
173+
decoder := prim.NewDecoder(prim.FromHex(tx.Call.Fields.Value), 0)
174+
if err := decoder.Decode(&callSubmitData); err != nil {
175+
return res, false
176+
}
177+
178+
res = DataSubmission{
179+
TxHash: tx.TxHash,
180+
TxIndex: tx.TxIndex,
181+
Data: callSubmitData.Data,
182+
TxSigner: signed.Address,
183+
AppId: signed.AppId,
184+
}
185+
186+
return res, true
187+
}

complex/block_transaction.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package complex
2+
3+
import (
4+
"go-sdk/interfaces"
5+
prim "go-sdk/primitives"
6+
)
7+
8+
type BlockTransaction struct {
9+
client *Client
10+
Extrinsic *prim.DecodedExtrinsic
11+
palletName string
12+
callName string
13+
}
14+
15+
func NewBlockTransaction(client *Client, extrinsic *prim.DecodedExtrinsic) BlockTransaction {
16+
names, err := client.Metadata().PalletCallName(extrinsic.Call.PalletIndex, extrinsic.Call.CallIndex)
17+
if err != nil {
18+
panic(err)
19+
}
20+
21+
return BlockTransaction{
22+
client: client,
23+
Extrinsic: extrinsic,
24+
palletName: names[0],
25+
callName: names[1],
26+
}
27+
}
28+
29+
func (this *BlockTransaction) CallData(data interfaces.CallDataT) prim.Option[interface{}] {
30+
return data.Decode(this.Extrinsic.Call)
31+
}
32+
33+
func (this *BlockTransaction) PalletName() string {
34+
return this.palletName
35+
}
36+
37+
func (this *BlockTransaction) CallName() string {
38+
return this.callName
39+
}
40+
41+
func (this *BlockTransaction) PalletIndex() uint8 {
42+
return this.Extrinsic.Call.PalletIndex
43+
}
44+
45+
func (this *BlockTransaction) CallIndex() uint8 {
46+
return this.Extrinsic.Call.CallIndex
47+
}
48+
49+
func (this *BlockTransaction) TxHash() prim.H256 {
50+
return this.Extrinsic.TxHash
51+
}
52+
53+
func (this *BlockTransaction) TxIndex() uint32 {
54+
return this.Extrinsic.TxIndex
55+
}
56+
57+
func (this *BlockTransaction) Signed() prim.Option[prim.DecodedExtrinsicSigned] {
58+
return this.Extrinsic.Signed
59+
}
60+
61+
func (this *BlockTransaction) Fields() prim.AlreadyEncoded {
62+
return this.Extrinsic.Call.Fields
63+
}

0 commit comments

Comments
 (0)