-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/blocto/solana-go-sdk/client" | ||
"github.com/blocto/solana-go-sdk/common" | ||
"github.com/blocto/solana-go-sdk/program/stake" | ||
"github.com/blocto/solana-go-sdk/rpc" | ||
"github.com/blocto/solana-go-sdk/types" | ||
) | ||
|
||
// FUarP2p5EnxD66vVDL4PWRoWMzA56ZVHG24hpEDFShEz | ||
var feePayer, _ = types.AccountFromBase58("4TMFNY9ntAn3CHzguSAvDNLPRoQTaK3sWbQQXdDXaE6KWRBLufGL6PJdsD2koiEe3gGmMdRK3aAw7sikGNksHJrN") | ||
|
||
// 9aE476sH92Vz7DMPyq5WLPkrKWivxeuTKEFKd2sZZcde | ||
var alice, _ = types.AccountFromBase58("4voSPg3tYuWbKzimpQK9EbXHmuyy5fUrtXvpLDMLkmY6TRncaTHAKGD8jUg3maB5Jbrd9CkQg4qjJMyN6sQvnEF2") | ||
|
||
var aliceStakeAccountPubkey = common.PublicKeyFromString("oyRPx4Ejo11J6b4AGaCx9UXUvGzkEmZQoGxKqx4Yp4B") | ||
|
||
func main() { | ||
c := client.NewClient(rpc.LocalnetRPCEndpoint) | ||
|
||
res, err := c.GetLatestBlockhash(context.Background()) | ||
if err != nil { | ||
log.Fatalf("get recent block hash error, err: %v\n", err) | ||
} | ||
tx, err := types.NewTransaction(types.NewTransactionParam{ | ||
Message: types.NewMessage(types.NewMessageParam{ | ||
FeePayer: feePayer.PublicKey, | ||
RecentBlockhash: res.Blockhash, | ||
Instructions: []types.Instruction{ | ||
stake.Deactivate(stake.DeactivateParam{ | ||
Stake: aliceStakeAccountPubkey, | ||
Auth: alice.PublicKey, | ||
}), | ||
}, | ||
}), | ||
Signers: []types.Account{feePayer, alice}, | ||
}) | ||
if err != nil { | ||
log.Fatalf("generate tx error, err: %v\n", err) | ||
} | ||
|
||
txhash, err := c.SendTransaction(context.Background(), tx) | ||
if err != nil { | ||
log.Fatalf("send tx error, err: %v\n", err) | ||
} | ||
|
||
log.Println("txhash:", txhash) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/blocto/solana-go-sdk/client" | ||
"github.com/blocto/solana-go-sdk/common" | ||
"github.com/blocto/solana-go-sdk/program/stake" | ||
"github.com/blocto/solana-go-sdk/rpc" | ||
"github.com/blocto/solana-go-sdk/types" | ||
) | ||
|
||
// FUarP2p5EnxD66vVDL4PWRoWMzA56ZVHG24hpEDFShEz | ||
var feePayer, _ = types.AccountFromBase58("4TMFNY9ntAn3CHzguSAvDNLPRoQTaK3sWbQQXdDXaE6KWRBLufGL6PJdsD2koiEe3gGmMdRK3aAw7sikGNksHJrN") | ||
|
||
// 9aE476sH92Vz7DMPyq5WLPkrKWivxeuTKEFKd2sZZcde | ||
var alice, _ = types.AccountFromBase58("4voSPg3tYuWbKzimpQK9EbXHmuyy5fUrtXvpLDMLkmY6TRncaTHAKGD8jUg3maB5Jbrd9CkQg4qjJMyN6sQvnEF2") | ||
|
||
var aliceStakeAccountPubkey = common.PublicKeyFromString("oyRPx4Ejo11J6b4AGaCx9UXUvGzkEmZQoGxKqx4Yp4B") | ||
|
||
func main() { | ||
c := client.NewClient(rpc.LocalnetRPCEndpoint) | ||
|
||
// obtain a random voting account here, or you can use your own. please note that a voting account is required here, rather than an identity. | ||
voteAccountStatus, err := c.GetVoteAccounts(context.Background()) | ||
if err != nil { | ||
log.Fatalf("failed to get vote account status, err: %v", err) | ||
} | ||
if len(voteAccountStatus.Current) == 0 { | ||
log.Fatalf("there are no decent voting accounts") | ||
} | ||
delegatedVotePubkey := voteAccountStatus.Current[0].VotePubkey | ||
fmt.Println("delegated vote pubkey:", delegatedVotePubkey.String()) | ||
|
||
res, err := c.GetLatestBlockhash(context.Background()) | ||
if err != nil { | ||
log.Fatalf("get recent block hash error, err: %v\n", err) | ||
} | ||
tx, err := types.NewTransaction(types.NewTransactionParam{ | ||
Message: types.NewMessage(types.NewMessageParam{ | ||
FeePayer: feePayer.PublicKey, | ||
RecentBlockhash: res.Blockhash, | ||
Instructions: []types.Instruction{ | ||
stake.DelegateStake(stake.DelegateStakeParam{ | ||
Stake: aliceStakeAccountPubkey, | ||
Auth: alice.PublicKey, | ||
Vote: delegatedVotePubkey, | ||
}), | ||
}, | ||
}), | ||
Signers: []types.Account{feePayer, alice}, | ||
}) | ||
if err != nil { | ||
log.Fatalf("generate tx error, err: %v\n", err) | ||
} | ||
|
||
txhash, err := c.SendTransaction(context.Background(), tx) | ||
if err != nil { | ||
log.Fatalf("send tx error, err: %v\n", err) | ||
} | ||
|
||
log.Println("txhash:", txhash) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/blocto/solana-go-sdk/client" | ||
"github.com/blocto/solana-go-sdk/common" | ||
"github.com/blocto/solana-go-sdk/program/stake" | ||
"github.com/blocto/solana-go-sdk/program/system" | ||
"github.com/blocto/solana-go-sdk/rpc" | ||
"github.com/blocto/solana-go-sdk/types" | ||
) | ||
|
||
// FUarP2p5EnxD66vVDL4PWRoWMzA56ZVHG24hpEDFShEz | ||
var feePayer, _ = types.AccountFromBase58("4TMFNY9ntAn3CHzguSAvDNLPRoQTaK3sWbQQXdDXaE6KWRBLufGL6PJdsD2koiEe3gGmMdRK3aAw7sikGNksHJrN") | ||
|
||
// 9aE476sH92Vz7DMPyq5WLPkrKWivxeuTKEFKd2sZZcde | ||
var alice, _ = types.AccountFromBase58("4voSPg3tYuWbKzimpQK9EbXHmuyy5fUrtXvpLDMLkmY6TRncaTHAKGD8jUg3maB5Jbrd9CkQg4qjJMyN6sQvnEF2") | ||
|
||
var stakeAmountInLamports uint64 = 1_000_000_000 // 1 SOL | ||
|
||
func main() { | ||
c := client.NewClient(rpc.LocalnetRPCEndpoint) | ||
|
||
// create an stake account | ||
stakeAccount := types.NewAccount() | ||
fmt.Println("stake account:", stakeAccount.PublicKey.ToBase58()) | ||
|
||
// get rent | ||
rentExemptionBalance, err := c.GetMinimumBalanceForRentExemption( | ||
context.Background(), | ||
stake.AccountSize, | ||
) | ||
if err != nil { | ||
log.Fatalf("get min balacne for rent exemption, err: %v", err) | ||
} | ||
|
||
res, err := c.GetLatestBlockhash(context.Background()) | ||
if err != nil { | ||
log.Fatalf("get recent block hash error, err: %v\n", err) | ||
} | ||
|
||
tx, err := types.NewTransaction(types.NewTransactionParam{ | ||
Message: types.NewMessage(types.NewMessageParam{ | ||
FeePayer: feePayer.PublicKey, | ||
RecentBlockhash: res.Blockhash, | ||
Instructions: []types.Instruction{ | ||
system.CreateAccount(system.CreateAccountParam{ | ||
From: feePayer.PublicKey, | ||
New: stakeAccount.PublicKey, | ||
Owner: common.StakeProgramID, | ||
Lamports: rentExemptionBalance + stakeAmountInLamports, | ||
Space: stake.AccountSize, | ||
}), | ||
stake.Initialize(stake.InitializeParam{ | ||
Stake: stakeAccount.PublicKey, | ||
Auth: stake.Authorized{ | ||
Staker: alice.PublicKey, | ||
Withdrawer: alice.PublicKey, | ||
}, | ||
Lockup: stake.Lockup{}, | ||
}), | ||
}, | ||
}), | ||
Signers: []types.Account{feePayer, stakeAccount}, | ||
}) | ||
if err != nil { | ||
log.Fatalf("generate tx error, err: %v\n", err) | ||
} | ||
|
||
txhash, err := c.SendTransaction(context.Background(), tx) | ||
if err != nil { | ||
log.Fatalf("send tx error, err: %v\n", err) | ||
} | ||
|
||
log.Println("txhash:", txhash) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/blocto/solana-go-sdk/client" | ||
"github.com/blocto/solana-go-sdk/common" | ||
"github.com/blocto/solana-go-sdk/program/stake" | ||
"github.com/blocto/solana-go-sdk/rpc" | ||
"github.com/blocto/solana-go-sdk/types" | ||
) | ||
|
||
// FUarP2p5EnxD66vVDL4PWRoWMzA56ZVHG24hpEDFShEz | ||
var feePayer, _ = types.AccountFromBase58("4TMFNY9ntAn3CHzguSAvDNLPRoQTaK3sWbQQXdDXaE6KWRBLufGL6PJdsD2koiEe3gGmMdRK3aAw7sikGNksHJrN") | ||
|
||
// 9aE476sH92Vz7DMPyq5WLPkrKWivxeuTKEFKd2sZZcde | ||
var alice, _ = types.AccountFromBase58("4voSPg3tYuWbKzimpQK9EbXHmuyy5fUrtXvpLDMLkmY6TRncaTHAKGD8jUg3maB5Jbrd9CkQg4qjJMyN6sQvnEF2") | ||
|
||
var aliceStakeAccountPubkey = common.PublicKeyFromString("oyRPx4Ejo11J6b4AGaCx9UXUvGzkEmZQoGxKqx4Yp4B") | ||
|
||
func main() { | ||
c := client.NewClient(rpc.LocalnetRPCEndpoint) | ||
|
||
aliceStakeAccountInfo, err := c.GetAccountInfo(context.Background(), aliceStakeAccountPubkey.String()) | ||
if err != nil { | ||
log.Fatalf("failed to get stake account info, err: %v", err) | ||
} | ||
|
||
res, err := c.GetLatestBlockhash(context.Background()) | ||
if err != nil { | ||
log.Fatalf("get recent block hash error, err: %v\n", err) | ||
} | ||
tx, err := types.NewTransaction(types.NewTransactionParam{ | ||
Message: types.NewMessage(types.NewMessageParam{ | ||
FeePayer: feePayer.PublicKey, | ||
RecentBlockhash: res.Blockhash, | ||
Instructions: []types.Instruction{ | ||
stake.Withdraw(stake.WithdrawParam{ | ||
Stake: aliceStakeAccountPubkey, | ||
Auth: alice.PublicKey, | ||
To: alice.PublicKey, | ||
Lamports: aliceStakeAccountInfo.Lamports, // withdraw all | ||
}), | ||
}, | ||
}), | ||
Signers: []types.Account{feePayer, alice}, | ||
}) | ||
if err != nil { | ||
log.Fatalf("generate tx error, err: %v\n", err) | ||
} | ||
|
||
txhash, err := c.SendTransaction(context.Background(), tx) | ||
if err != nil { | ||
log.Fatalf("send tx error, err: %v\n", err) | ||
} | ||
|
||
log.Println("txhash:", txhash) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Deactivate (unstake) | ||
|
||
::: tip | ||
Activation requires waiting for 1 epoch. You can use `solana-test-validator --slot-per-epoch <SLOT>` for test. | ||
::: | ||
|
||
@[code](@/program/stake/deactivate/main.go) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Delegate (stake) | ||
|
||
::: tip | ||
Activation requires waiting for 1 epoch. You can use `solana-test-validator --slot-per-epoch <SLOT>` for test. | ||
::: | ||
|
||
@[code](@/program/stake/delegate-stake/main.go) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Initialize Account | ||
|
||
@[code](@/program/stake/initialize-account/main.go) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Withdraw | ||
|
||
@[code](@/program/stake/withdraw/main.go) |