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

Update proto/hypergridssn/tx.proto and docs/static/openapi.yml to rem… #27

Merged
merged 1 commit into from
Jul 9, 2024
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
278 changes: 102 additions & 176 deletions api/hypergridssn/hypergridssn/tx.pulsar.go

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3656,8 +3656,6 @@ paths:
type: string
grid:
type: string
account:
type: string
slot:
type: string
hash:
Expand Down Expand Up @@ -6831,8 +6829,6 @@ definitions:
type: string
grid:
type: string
account:
type: string
slot:
type: string
hash:
Expand Down
5 changes: 2 additions & 3 deletions proto/hypergridssn/hypergridssn/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,8 @@ message MsgCreateGridInbox {
option (cosmos.msg.v1.signer) = "creator";
string creator = 1;
string grid = 2;
string account = 3;
string slot = 4;
string hash = 5;
string slot = 3;
string hash = 4;
}

message MsgCreateGridInboxResponse {
Expand Down
151 changes: 117 additions & 34 deletions tools/solana.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package tools

import (
"bytes"
"context"
"crypto/sha256"
"encoding/binary"
"fmt"
"os"
"strings"

"github.com/davecgh/go-spew/spew"
Expand Down Expand Up @@ -81,40 +82,80 @@ type InitializedParams struct {
AccountType uint32
}

// BorshEncode encodes the InstructionData using Borsh
func (d *SettleFeeBillParams) 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.FromID)
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.LittleEndian, d.EndID)
if err != nil {
return nil, err
}
billCount := uint64(len(d.Bills))
err = binary.Write(buf, binary.LittleEndian, billCount)
if err != nil {
return nil, err
}
for _, bill := range d.Bills {
err = binary.Write(buf, binary.LittleEndian, bill.Key[:])
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.LittleEndian, bill.Amount)
if err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}

const SonicFeeProgramID = "SonicFeeSet1ement11111111111111111111111111"
const L1InboxProgramID = "5XJ1wZkTwAw9mc5FbM3eBgAT83TKgtAGzKos9wVxC6my"

func sendSonicTx(rpcUrl string, programId string, accounts solana.AccountMetaSlice, instructionData []byte) (*solana.Signature, error) {
func getLocalPrivateKey() (solana.PrivateKey, error) {
//get home path "~/"
// home, err := os.UserHomeDir()
// if err != nil {
// // panic(err)
// return nil, err
// }
// Load the account that you will send funds FROM:
// accountFrom, err := solana.PrivateKeyFromSolanaKeygenFile(home + "/.config/solana/id.json")

// Load the account that you will send funds FROM:
accountFrom, err := solana.PrivateKeyFromBase58("5gA6JTpFziXu7py2j63arRUq1H29p6pcPMB74LaNuzcSqULPD6s1SZUS3UMPvFEE9oXmt1kk6ez3C6piTc3bwpJ6")
if err != nil {
// panic(err)
return nil, err
}
fmt.Println("accountFrom private key:", accountFrom)
fmt.Println("accountFrom public key:", accountFrom.PublicKey())

return accountFrom, nil
}

func sendSonicTx(rpcUrl string, programId string, accounts solana.AccountMetaSlice, instructionData []byte, signers []solana.PrivateKey) (*solana.Signature, error) {
// Create a new RPC client:
rpcClient := rpc.New(rpcUrl)

// Create a new WS client (used for confirming transactions)
//replace http or https with ws
rpcWsUrl := strings.Replace(rpcUrl, "http://", "ws://", 1)
rpcWsUrl = strings.Replace(rpcWsUrl, "https://", "wss://", 1)
rpcWsUrl = strings.Replace(rpcWsUrl, ":8899", ":8900", 1)

wsClient, err := ws.Connect(context.Background(), rpcWsUrl)
if err != nil {
// panic(err)
return nil, err
}

//get home path "~/"
home, err := os.UserHomeDir()
if err != nil {
// panic(err)
return nil, err
}

// Load the account that you will send funds FROM:
accountFrom, err := solana.PrivateKeyFromSolanaKeygenFile(home + "/.config/solana/id.json")
if err != nil {
// panic(err)
return nil, err
}
fmt.Println("accountFrom private key:", accountFrom)
fmt.Println("accountFrom public key:", accountFrom.PublicKey())

recent, err := rpcClient.GetRecentBlockhash(context.TODO(), rpc.CommitmentFinalized)
if err != nil {
// panic(err)
Expand All @@ -130,21 +171,25 @@ func sendSonicTx(rpcUrl string, programId string, accounts solana.AccountMetaSli
),
},
recent.Value.Blockhash,
solana.TransactionPayer(accountFrom.PublicKey()),
solana.TransactionPayer(signers[0].PublicKey()),
)
if err != nil {
// panic(err)
return nil, err
}

_, err = tx.Sign(
func(key solana.PublicKey) *solana.PrivateKey {
if accountFrom.PublicKey().Equals(key) {
return &accountFrom
_, err = tx.Sign(func(key solana.PublicKey) *solana.PrivateKey {
//check key is in signers
for _, signer := range signers {
if key.Equals(signer.PublicKey()) {
return &signer
}
return nil
},
)
}
// if accountFrom.PublicKey().Equals(key) {
// return &accountFrom
// }
return nil
})
if err != nil {
// panic(fmt.Errorf("unable to sign transaction: %w", err))
return nil, err
Expand All @@ -164,7 +209,6 @@ func sendSonicTx(rpcUrl string, programId string, accounts solana.AccountMetaSli
}
spew.Dump(sig)
return &sig, nil

}

func SendTxFeeSettlement(rpcUrl string, data_accounts []string, FromId uint64, EndID uint64, bills map[string]uint64) (*solana.Signature, error) {
Expand All @@ -185,7 +229,7 @@ func SendTxFeeSettlement(rpcUrl string, data_accounts []string, FromId uint64, E
}

// Serialize to bytes using Borsh
serializedData, err := borsh.Serialize(instructionData)
serializedData, err := instructionData.BorshEncode() // borsh.Serialize(instructionData)
if err != nil {
// panic(err)
return nil, err
Expand All @@ -195,8 +239,14 @@ func SendTxFeeSettlement(rpcUrl string, data_accounts []string, FromId uint64, E
for _, data_account := range data_accounts {
accounts = append(accounts, solana.NewAccountMeta(solana.MustPublicKeyFromBase58(data_account), true, false))
}
signer, err := getLocalPrivateKey()
if err != nil {
// panic(err)
return nil, err
}

return sendSonicTx(rpcUrl, SonicFeeProgramID, accounts, serializedData)
signers := []solana.PrivateKey{signer}
return sendSonicTx(rpcUrl, SonicFeeProgramID, accounts, serializedData, signers)
}

func InitializeDataAccount(rpcUrl string, owner string, data_account string, account_type uint32) (*solana.Signature, error) {
Expand All @@ -217,7 +267,14 @@ func InitializeDataAccount(rpcUrl string, owner string, data_account string, acc
solana.NewAccountMeta(solana.MustPublicKeyFromBase58(data_account), true, false),
}

return sendSonicTx(rpcUrl, SonicFeeProgramID, accounts, serializedData)
signer, err := getLocalPrivateKey()
if err != nil {
// panic(err)
return nil, err
}
signers := []solana.PrivateKey{signer}

return sendSonicTx(rpcUrl, SonicFeeProgramID, accounts, serializedData, signers)
}

type InboxProgrmParams struct {
Expand All @@ -236,7 +293,7 @@ func hashInstructionMethod(method string) [8]byte {
return hash
}

func SendTxInbox(rpcUrl string, data_account string, slot uint64, hash string) (*solana.Signature, error) {
func SendTxInbox(rpcUrl string, slot uint64, hash string) (*solana.Signature, *solana.PublicKey, error) {
instructionData := InboxProgrmParams{
Instruction: hashInstructionMethod("addblock"),
Slot: slot,
Expand All @@ -247,14 +304,40 @@ func SendTxInbox(rpcUrl string, data_account string, slot uint64, hash string) (
serializedData, err := borsh.Serialize(instructionData)
if err != nil {
// panic(err)
return nil, err
return nil, nil, err
}

//create a new keypair
data_account, err := solana.NewRandomPrivateKey()
if err != nil {
// panic(err)
return nil, nil, err
}
data_key := data_account.PublicKey()
fmt.Println("data_account:", data_key)

signer, err := getLocalPrivateKey()
if err != nil {
// panic(err)
return nil, nil, err
}

accounts := solana.AccountMetaSlice{
solana.NewAccountMeta(solana.MustPublicKeyFromBase58(data_account), true, false),
solana.NewAccountMeta(data_account.PublicKey(), true, true),
solana.NewAccountMeta(signer.PublicKey(), true, true),
solana.NewAccountMeta(solana.MustPublicKeyFromBase58("11111111111111111111111111111111"), false, false),
}

signers := []solana.PrivateKey{signer, data_account}

sig, err := sendSonicTx(rpcUrl, L1InboxProgramID, accounts, serializedData, signers)
if err != nil {
// panic(err)
return nil, nil, err
}
fmt.Println("signature: ", sig)

return sendSonicTx(rpcUrl, L1InboxProgramID, accounts, serializedData)
return sig, &data_key, nil
}

// func main() {
Expand Down
54 changes: 54 additions & 0 deletions tools/solana_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package tools

import (
"testing"

"github.com/davecgh/go-spew/spew"
)

func TestSendTxFeeSettlement(t *testing.T) {
// FromId := uint64(1)
// EndID := uint64(100)
// bills := map[string]uint64{
// "EyYFxQ2FRcSkR8rdvefEDNy69KWHi2xTzbuVKxuBVueS": 100,
// "AzVnQCpY2rqQmxJz6PQzxWj9HHQQc5qFuL89wvov6cL4": 101,
// }

// sig, err := SendTxFeeSettlement("http://localhost:8899", []string{"AzVnQCpY2rqQmxJz6PQzxWj9HHQQc5qFuL89wvov6cL4"}, FromId, EndID, bills)
// if err != nil {
// panic(err)
// }
// spew.Dump(sig)

sig, key, err := SendTxInbox("https://api.devnet.solana.com", 53893, "2YmCa9RBVN9CZjAcN3o431UhTEU8BwmaiGoCnbeK1Sgr")
if err != nil {
panic(err)
}
spew.Dump(sig)
spew.Dump(key)

// Bills := []SettlementBillParam{}
// // convert bills to []SettlementBillParam
// for key, value := range bills {
// Bills = append(Bills, SettlementBillParam{
// Key: solana.MustPublicKeyFromBase58(key),
// Amount: value,
// })
// }

// instructionData := SettleFeeBillParams{
// Instruction: 1,
// FromID: FromId,
// EndID: EndID,
// Bills: Bills,
// }

// // Serialize to bytes using Borsh
// // serializedData, err := borsh.Serialize(instructionData)
// serializedData, err := instructionData.BorshEncode()
// if err != nil {
// panic(err)
// }

// println("serializedData", serializedData)
}
4 changes: 2 additions & 2 deletions x/hypergridssn/keeper/msg_server_grid_inbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (k msgServer) CreateGridInbox(goCtx context.Context, msg *types.MsgCreateGr
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid slot")
}

sig, err := solana.SendTxInbox(base_layer_rpc, msg.Account, slot, msg.Hash)
sig, account, err := solana.SendTxInbox(base_layer_rpc, slot, msg.Hash)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, err.Error())
}
Expand All @@ -44,7 +44,7 @@ func (k msgServer) CreateGridInbox(goCtx context.Context, msg *types.MsgCreateGr
var gridInbox = types.GridInbox{
Creator: msg.Creator,
Grid: msg.Grid,
Account: msg.Account,
Account: account.String(),
Slot: msg.Slot,
Hash: msg.Hash,
}
Expand Down
4 changes: 2 additions & 2 deletions x/hypergridssn/module/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
},
{
RpcMethod: "CreateGridInbox",
Use: "create-grid-inbox [grid] [account] [slot] [hash]",
Use: "create-grid-inbox [grid] [slot] [hash]",
Short: "Create GridInbox",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "grid"}, {ProtoField: "account"}, {ProtoField: "slot"}, {ProtoField: "hash"}},
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "grid"}, {ProtoField: "slot"}, {ProtoField: "hash"}},
},
// this line is used by ignite scaffolding # autocli/tx
},
Expand Down
9 changes: 2 additions & 7 deletions x/hypergridssn/types/messages_grid_block_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ import (

var _ sdk.Msg = &MsgCreateGridBlockFee{}

func NewMsgCreateGridBlockFee(creator string, items []*GridBlockFeeItem) *MsgCreateGridBlockFee { //grid string, slot string, blockhash string, blocktime int32, fee string) *MsgCreateGridBlockFee {
func NewMsgCreateGridBlockFee(creator string, items []*GridBlockFeeItem) *MsgCreateGridBlockFee {
return &MsgCreateGridBlockFee{
Creator: creator,
// Grid: grid,
// Slot: slot,
// Blockhash: blockhash,
// Blocktime: blocktime,
// Fee: fee,
Items: items,
Items: items,
}
}

Expand Down
8 changes: 4 additions & 4 deletions x/hypergridssn/types/messages_grid_inbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

var _ sdk.Msg = &MsgCreateGridInbox{}

func NewMsgCreateGridInbox(creator string, grid string, account string, slot string, hash string) *MsgCreateGridInbox {
func NewMsgCreateGridInbox(creator string, grid string, slot string, hash string) *MsgCreateGridInbox {
return &MsgCreateGridInbox{
Creator: creator,
Grid: grid,
Account: account,
Slot: slot,
Hash: hash,
// Account: account,
Slot: slot,
Hash: hash,
}
}

Expand Down
Loading
Loading