Skip to content

Commit

Permalink
More tools
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Apr 27, 2024
1 parent 0b9c4d7 commit d84f4db
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
14 changes: 12 additions & 2 deletions rpc/client.go
Expand Up @@ -29,8 +29,10 @@ import (
"github.com/klauspost/compress/gzhttp"
)

var ErrNotFound = errors.New("not found")
var ErrNotConfirmed = errors.New("not confirmed")
var (
ErrNotFound = errors.New("not found")
ErrNotConfirmed = errors.New("not confirmed")
)

type Client struct {
rpcURL string
Expand Down Expand Up @@ -139,3 +141,11 @@ func (cl *Client) RPCCallBatch(
) (jsonrpc.RPCResponses, error) {
return cl.rpcClient.CallBatch(ctx, requests)
}

func NewBoolean(b bool) *bool {
return &b
}

func NewTransactionVersion(v uint64) *uint64 {
return &v
}
75 changes: 75 additions & 0 deletions transaction.go
Expand Up @@ -698,3 +698,78 @@ func (tx *Transaction) VerifySignatures() error {

return nil
}

func (tx *Transaction) NumWriteableAccounts() int {
return countWriteableAccounts(tx)
}

func (tx *Transaction) NumSigners() int {
return countSigners(tx)
}

func countSigners(tx *Transaction) (count int) {
if tx == nil {
return -1
}
return tx.Message.Signers().Len()
}

func (tx *Transaction) NumReadonlyAccounts() int {
return countReadonlyAccounts(tx)
}

func countReadonlyAccounts(tx *Transaction) (count int) {
if tx == nil {
return -1
}
return int(tx.Message.Header.NumReadonlyUnsignedAccounts) + int(tx.Message.Header.NumReadonlySignedAccounts)
}

func countWriteableAccounts(tx *Transaction) (count int) {
if tx == nil {
return -1
}
if !tx.Message.IsVersioned() {
metas, err := tx.Message.AccountMetaList()
if err != nil {
return -1
}
for _, meta := range metas {
if meta.IsWritable {
count++
}
}
return count
}
numStatisKeys := len(tx.Message.AccountKeys)
statisKeys := tx.Message.AccountKeys
h := tx.Message.Header
for _, key := range statisKeys {
accIndex, ok := getStaticAccountIndex(tx, key)
if !ok {
continue
}
index := int(accIndex)
is := false
if index >= int(h.NumRequiredSignatures) {
// unsignedAccountIndex < numWritableUnsignedAccounts
is = index-int(h.NumRequiredSignatures) < (numStatisKeys-int(h.NumRequiredSignatures))-int(h.NumReadonlyUnsignedAccounts)
} else {
is = index < int(h.NumRequiredSignatures-h.NumReadonlySignedAccounts)
}
if is {
count++
}
}
count += tx.Message.NumWritableLookups()
return count
}

func getStaticAccountIndex(tx *Transaction, key PublicKey) (int, bool) {
for idx, a := range tx.Message.AccountKeys {
if a.Equals(key) {
return (idx), true
}
}
return -1, false
}

0 comments on commit d84f4db

Please sign in to comment.