|
| 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 | +} |
0 commit comments