Skip to content

Commit

Permalink
Merge pull request #53 from mirrorworld-universe/aide
Browse files Browse the repository at this point in the history
add init fee accounts in hypergrid-aide
  • Loading branch information
ZeneDeLuca authored Feb 14, 2025
2 parents 461f080 + 0b76755 commit e988a2c
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
56 changes: 55 additions & 1 deletion hypergrid-aide/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 <command>")
}
Expand Down
57 changes: 56 additions & 1 deletion hypergrid-aide/tools/solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand Down Expand Up @@ -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)
}
7 changes: 6 additions & 1 deletion hypergrid-aide/tools/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"strconv"
"strings"
)

const AIDE_CONFIG_FILE = "/home/ubuntu/.hypergrid-ssn/last_slot.txt"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e988a2c

Please sign in to comment.