From 0b767555aa4d47214918f89736c7886d4ccf5cd0 Mon Sep 17 00:00:00 2001 From: ZeneDeLuca Date: Fri, 14 Feb 2025 08:52:42 +0000 Subject: [PATCH] add init fee accounts in hypergrid-aide --- .gitignore | 3 ++ hypergrid-aide/main.go | 56 ++++++++++++++++++++++++++++++++- hypergrid-aide/tools/solana.go | 57 +++++++++++++++++++++++++++++++++- hypergrid-aide/tools/sys.go | 7 ++++- 4 files changed, 120 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 0a748ce..7c91b12 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,7 @@ release/ *.log *.ign hypergrid-aide/main +hypergrid-aide/.hypergrid-ssn hypergrid-aide/hypergrid-aide +hypergrid-aide/hypergrid-aide.gz +hypergrid-aide/hypergrid-aide.zip diff --git a/hypergrid-aide/main.go b/hypergrid-aide/main.go index 213ebcf..c652e23 100644 --- a/hypergrid-aide/main.go +++ b/hypergrid-aide/main.go @@ -193,7 +193,7 @@ func SettleFeeBill(cosmos tools.CosmosClient, account cosmosaccount.Account) { } if fromId == 0 && endId == 0 { - log.Fatal("fromId or endId is 0") + log.Fatal("fromId and endId is 0") return } @@ -224,6 +224,48 @@ func SettleFeeBill(cosmos tools.CosmosClient, account cosmosaccount.Account) { log.Println(res4) } +func InitFeeAccounts(cosmos tools.CosmosClient, account cosmosaccount.Account) { + res1, err1 := cosmos.QueryAllHypergridNodes() + if err1 != nil { + log.Fatal(err1) + } + + nodes := res1.HypergridNode + has_hssn := false + has_sonic_grid := false + for _, node := range nodes { + account_type := uint32(node.Role) + if account_type == 4 { + continue + } + //to make sure only one hssn account is created + if account_type == 1 { + if has_hssn { + continue + } + has_hssn = true + } + //to make sure only one sonic_grid account is created + if account_type == 2 { + if has_sonic_grid { + continue + } + has_sonic_grid = true + } + owner := node.DataAccount + if owner == "" { + owner = node.Pubkey + } + res4, err4 := tools.InitializeDataAccount(SOLANA_PRIVATE_KEY, SOLANA_SONIC_GRID_RPC, SonicFeeProgramID, SonicFeeDataAccountID, owner, account_type) + if err4 != nil { + log.Print(err4) + } + + log.Print("InitializeDataAccount:\n\n") + log.Println(res4) + } +} + func main() { log.SetFlags(log.Llongfile | log.Lmicroseconds | log.Ldate) //get program arguments @@ -311,6 +353,18 @@ func main() { log.Fatal(err) } SettleFeeBill(*cosmos, account) + case "initFeeAccount": + cosmos := tools.NewCosmosClient( + cosmosclient.WithNodeAddress(COSMOS_RPC_ENDPOINT), + cosmosclient.WithAddressPrefix(COSMOS_ADDRESS_PREFIX), + cosmosclient.WithHome(COSMOS_HOME), + cosmosclient.WithGas(strconv.FormatUint(COSMOS_GAS, 10)), + ) + account, err := cosmos.Account(COSMOS_KEY) + if err != nil { + log.Fatal(err) + } + InitFeeAccounts(*cosmos, account) default: fmt.Println("Usage: hypergrid-aide ") } diff --git a/hypergrid-aide/tools/solana.go b/hypergrid-aide/tools/solana.go index cd443e7..50363e0 100644 --- a/hypergrid-aide/tools/solana.go +++ b/hypergrid-aide/tools/solana.go @@ -169,7 +169,7 @@ func (s *SolanaClient) GetBlocks(start_slot uint64, limit uint64) ([]SolanaBlock blocks = append(blocks, SolanaBlock{ Blockhash: resp2.Blockhash.String(), Slot: block, - BlockTime: resp2.BlockTime.Time().Second(), + BlockTime: int(resp2.BlockTime.Time().Unix()), Fee: Fee, }) } @@ -462,3 +462,58 @@ func SendTxFeeSettlement(localPrivateKey string, rpcUrl string, SonicFeeProgramI signers := []solana.PrivateKey{signer} return sendSonicTx(rpcUrl, SonicFeeProgramID, accounts, serializedData, signers) } + +type InitializedParams struct { + Instruction uint32 + Owner solana.PublicKey + AccountType uint32 +} + +// BorshEncode encodes the InstructionData using Borsh +func (d *InitializedParams) BorshEncode() ([]byte, error) { + buf := new(bytes.Buffer) + err := binary.Write(buf, binary.LittleEndian, d.Instruction) + if err != nil { + return nil, err + } + err = binary.Write(buf, binary.LittleEndian, d.Owner[:]) + if err != nil { + return nil, err + } + err = binary.Write(buf, binary.LittleEndian, d.AccountType) + if err != nil { + return nil, err + } + + return buf.Bytes(), nil +} + +func InitializeDataAccount(localPrivateKey string, rpcUrl string, SonicFeeProgramID string, SonicFeeDataAccountID string, owner string /*data_account string,*/, account_type uint32) (*solana.Signature, error) { + instructionData := InitializedParams{ + Instruction: 0, + Owner: solana.MustPublicKeyFromBase58(owner), + AccountType: account_type, + } + + log.Println("instructionData:", instructionData) + + // Serialize to bytes using Borsh + serializedData, err := instructionData.BorshEncode() + if err != nil { + // panic(err) + return nil, err + } + + accounts := solana.AccountMetaSlice{ + solana.NewAccountMeta(solana.MustPublicKeyFromBase58(SonicFeeDataAccountID), true, false), + } + + signer, err := getLocalPrivateKey(localPrivateKey) + if err != nil { + // panic(err) + return nil, err + } + signers := []solana.PrivateKey{signer} + + return sendSonicTx(rpcUrl, SonicFeeProgramID, accounts, serializedData, signers) +} diff --git a/hypergrid-aide/tools/sys.go b/hypergrid-aide/tools/sys.go index 92eac79..1356e8e 100644 --- a/hypergrid-aide/tools/sys.go +++ b/hypergrid-aide/tools/sys.go @@ -6,6 +6,7 @@ import ( "log" "os" "strconv" + "strings" ) const AIDE_CONFIG_FILE = "/home/ubuntu/.hypergrid-ssn/last_slot.txt" @@ -37,7 +38,11 @@ func GetLastSentSlot() (uint64, error) { fmt.Println("read to fd fail", err) return 0, err } - last_sent_slot, err := strconv.ParseUint(string(fd), 10, 64) // 将fd从[]byte转换为string,然后转换为int + + //trim string fd + s := strings.Trim(string(fd), "\n") + + last_sent_slot, err := strconv.ParseUint(s, 10, 64) // 将fd从[]byte转换为string,然后转换为int if err != nil { fmt.Println("convert fd to int fail", err) return 0, err