Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplementation v020 #8

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions .github/workflows/lint.yml

This file was deleted.

187 changes: 187 additions & 0 deletions complex/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package complex

import (
meta "go-sdk/metadata"
daPallet "go-sdk/metadata/pallets/data_availability"
prim "go-sdk/primitives"
)

type Block struct {
client *Client
Block RPCBlock
events prim.Option[EventRecords]
}

func NewBlock(client *Client, blockHash prim.H256) Block {
block := client.GetBlock(prim.NewSome(blockHash))
events, err := client.GetEvents(prim.NewSome(blockHash))
if err != nil {
panic(err)
}
return Block{
client: client,
Block: block,
events: prim.NewSome(events),
}
}

func (this *Block) TransactionBySigner(accountId meta.AccountId) []BlockTransaction {
var result = []BlockTransaction{}
for _, tx := range this.Block.Extrinsics {
if !sameSignature(&tx, accountId) {
continue
}
result = append(result, NewBlockTransaction(this.client, &tx))
}

return result
}

func (this *Block) TransactionByIndex(txIndex uint32) prim.Option[BlockTransaction] {
for _, tx := range this.Block.Extrinsics {
if tx.TxIndex == txIndex {
return prim.NewSome(NewBlockTransaction(this.client, &tx))
}
}

return prim.NewNone[BlockTransaction]()
}

func (this *Block) TransactionByHash(txHash prim.H256) prim.Option[BlockTransaction] {
for _, tx := range this.Block.Extrinsics {
if tx.TxHash == txHash {
return prim.NewSome(NewBlockTransaction(this.client, &tx))
}
}

return prim.NewNone[BlockTransaction]()
}

func (this *Block) TransactionByAppId(appId uint32) []BlockTransaction {
var result = []BlockTransaction{}
for _, tx := range this.Block.Extrinsics {
if tx.Signed.IsNone() {
continue
}
var signed = tx.Signed.Unwrap()
if signed.AppId != appId {
continue
}
result = append(result, NewBlockTransaction(this.client, &tx))
}

return result
}

func (this *Block) DataSubmissionBySigner(accountId meta.AccountId) []DataSubmission {
var result = []DataSubmission{}
for _, tx := range this.Block.Extrinsics {
if !sameSignature(&tx, accountId) {
continue
}

if res, ok := NewDataSubmission(&tx); ok {
result = append(result, res)
}

}

return result
}

func (this *Block) DataSubmissionByIndex(txIndex uint32) prim.Option[DataSubmission] {
for _, tx := range this.Block.Extrinsics {
if tx.TxIndex != txIndex {
continue
}
if res, ok := NewDataSubmission(&tx); ok {
return prim.NewSome(res)
}
}

return prim.NewNone[DataSubmission]()
}

func (this *Block) DataSubmissionByHash(txHash prim.H256) prim.Option[DataSubmission] {
for _, tx := range this.Block.Extrinsics {
if tx.TxHash != txHash {
continue
}
if res, ok := NewDataSubmission(&tx); ok {
return prim.NewSome(res)
}
}

return prim.NewNone[DataSubmission]()
}

func (this *Block) DataSubmissionByAppId(appId uint32) []DataSubmission {
var result = []DataSubmission{}
for _, tx := range this.Block.Extrinsics {
if tx.Signed.IsNone() {
continue
}
var signed = tx.Signed.Unwrap()
if signed.AppId != appId {
continue
}
if res, ok := NewDataSubmission(&tx); ok {
result = append(result, res)
}
}

return result
}

func (this *Block) Events(appId uint32) prim.Option[EventRecords] {
return this.events
}

func sameSignature(tx *prim.DecodedExtrinsic, accountId meta.AccountId) bool {
txAccountId := tx.Signed.UnwrapOrDefault().Address.Id.UnwrapOrDefault()
if accountId.Value != txAccountId {
return false
}

return true
}

type DataSubmission struct {
TxHash prim.H256
TxIndex uint32
Data []byte
TxSigner prim.MultiAddress
AppId uint32
}

func NewDataSubmission(tx *prim.DecodedExtrinsic) (DataSubmission, bool) {
ok := false
res := DataSubmission{}

callSubmitData := daPallet.CallSubmitData{}
if tx.Call.PalletIndex != callSubmitData.PalletIndex() {
return res, ok
}

if tx.Call.CallIndex != callSubmitData.CallIndex() {
return res, ok
}

// Data submission cannot be done without signed being set.
signed := tx.Signed.UnwrapOrDefault()

decoder := prim.NewDecoder(prim.FromHex(tx.Call.Fields.Value), 0)
if err := decoder.Decode(&callSubmitData); err != nil {
return res, false
}

res = DataSubmission{
TxHash: tx.TxHash,
TxIndex: tx.TxIndex,
Data: callSubmitData.Data,
TxSigner: signed.Address,
AppId: signed.AppId,
}

return res, true
}
63 changes: 63 additions & 0 deletions complex/block_transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package complex

import (
"go-sdk/interfaces"
prim "go-sdk/primitives"
)

type BlockTransaction struct {
client *Client
Extrinsic *prim.DecodedExtrinsic
palletName string
callName string
}

func NewBlockTransaction(client *Client, extrinsic *prim.DecodedExtrinsic) BlockTransaction {
names, err := client.Metadata().PalletCallName(extrinsic.Call.PalletIndex, extrinsic.Call.CallIndex)
if err != nil {
panic(err)
}

return BlockTransaction{
client: client,
Extrinsic: extrinsic,
palletName: names[0],
callName: names[1],
}
}

func (this *BlockTransaction) CallData(data interfaces.CallDataT) prim.Option[interface{}] {
return data.Decode(this.Extrinsic.Call)
}

func (this *BlockTransaction) PalletName() string {
return this.palletName
}

func (this *BlockTransaction) CallName() string {
return this.callName
}

func (this *BlockTransaction) PalletIndex() uint8 {
return this.Extrinsic.Call.PalletIndex
}

func (this *BlockTransaction) CallIndex() uint8 {
return this.Extrinsic.Call.CallIndex
}

func (this *BlockTransaction) TxHash() prim.H256 {
return this.Extrinsic.TxHash
}

func (this *BlockTransaction) TxIndex() uint32 {
return this.Extrinsic.TxIndex
}

func (this *BlockTransaction) Signed() prim.Option[prim.DecodedExtrinsicSigned] {
return this.Extrinsic.Signed
}

func (this *BlockTransaction) Fields() prim.AlreadyEncoded {
return this.Extrinsic.Call.Fields
}
Loading