-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SponsoredTransaction and ANS examples
- Loading branch information
1 parent
f481389
commit c528326
Showing
23 changed files
with
287 additions
and
74 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,20 @@ | ||
namespace Aptos.Examples; | ||
|
||
public class AptosNamesExample | ||
{ | ||
public static async Task Run() | ||
{ | ||
var aptos = new AptosClient(Networks.Mainnet); | ||
|
||
Console.WriteLine("=== Aptos Names Example ===\n"); | ||
|
||
var aptosName = "aaron.apt"; | ||
var nameAddress = "0xa746e980ae21949a4f084db7403430f00bce3c9a1da4101ffcf0bf45ebd35e7e"; | ||
|
||
var address = await aptos.Ans.GetAnsAddress(aptosName); | ||
var name = await aptos.Ans.GetAnsName(nameAddress); | ||
|
||
Console.WriteLine($"Address for {aptosName}: {address}"); | ||
Console.WriteLine($"Aptos name for {nameAddress}: {name}"); | ||
} | ||
} |
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
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
File renamed without changes.
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,40 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Aptos; | ||
|
||
public class SimulateTransferEd25519Example | ||
{ | ||
public static async Task Run() | ||
{ | ||
var aptos = new AptosClient(new AptosConfig(Networks.Devnet)); | ||
|
||
Console.WriteLine("=== Addresses ===\n"); | ||
|
||
var alice = Ed25519Account.Generate(); | ||
Console.WriteLine($"Alice: {alice.Address}"); | ||
|
||
Console.WriteLine("\n=== Funding accounts ===\n"); | ||
|
||
var aliceFundTxn = await aptos.Faucet.FundAccount(alice.Address, 100_000_000); | ||
Console.WriteLine($"Alice's fund transaction: {aliceFundTxn.Hash}"); | ||
|
||
Console.WriteLine("\n=== Building transaction ===\n"); | ||
|
||
var txn = await aptos.Transaction.Build( | ||
sender: alice.Address, | ||
data: new GenerateEntryFunctionPayloadData( | ||
function: "0x1::aptos_account::transfer_coins", | ||
typeArguments: ["0x1::aptos_coin::AptosCoin"], | ||
functionArguments: [alice.Address, "100000"] | ||
) | ||
); | ||
Console.WriteLine($"{JsonConvert.SerializeObject(txn)}"); | ||
|
||
Console.WriteLine("\n=== Simulating transaction ===\n"); | ||
|
||
var simulatedTxn = await aptos.Transaction.Simulate(new(txn, alice.PublicKey)); | ||
Console.WriteLine( | ||
$"Simulated Transaction: {JsonConvert.SerializeObject(simulatedTxn, Formatting.Indented)}" | ||
); | ||
} | ||
} |
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,69 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Aptos.Examples; | ||
|
||
public class SponsoredTransferEd25519Example | ||
{ | ||
public static async Task Run() | ||
{ | ||
var aptos = new AptosClient(new AptosConfig(Networks.Devnet)); | ||
|
||
Console.WriteLine("=== Addresses ===\n"); | ||
|
||
var alice = Ed25519Account.Generate(); | ||
var receiver = Ed25519Account.Generate(); | ||
var feePayer = Ed25519Account.Generate(); | ||
Console.WriteLine($"Alice: {alice.Address}"); | ||
Console.WriteLine($"Receiver: {receiver.Address}"); | ||
Console.WriteLine($"FeePayer: {feePayer.Address}"); | ||
|
||
Console.WriteLine("\n=== Funding accounts ===\n"); | ||
|
||
var aliceFundTxn = await aptos.Faucet.FundAccount(alice.Address, 100_000_000); | ||
var feePayerFundTxn = await aptos.Faucet.FundAccount(feePayer.Address, 100_000_000); | ||
Console.WriteLine($"Alice's fund transaction: {aliceFundTxn.Hash}"); | ||
Console.WriteLine($"FeePayer's fund transaction: {feePayerFundTxn.Hash}"); | ||
|
||
Console.WriteLine("\n=== Building transaction ===\n"); | ||
|
||
var txn = await aptos.Transaction.Build( | ||
sender: alice.Address, | ||
data: new GenerateEntryFunctionPayloadData( | ||
function: "0x1::aptos_account::transfer_coins", | ||
typeArguments: ["0x1::aptos_coin::AptosCoin"], | ||
functionArguments: [receiver.Address, "100000"] | ||
), | ||
// It's important to set this flag to true to enable sponsored transactions | ||
withFeePayer: true | ||
); | ||
Console.WriteLine($"{JsonConvert.SerializeObject(txn)}"); | ||
|
||
Console.WriteLine("\n=== Signing and submitting transaction ===\n"); | ||
|
||
var aliceSignature = alice.SignWithAuthenticator(txn); | ||
Console.WriteLine($"Alice has signed the transaction: {aliceSignature.BcsToHex()}"); | ||
|
||
var feePayerSignature = aptos.Transaction.SignAsFeePayer(feePayer, txn); | ||
Console.WriteLine($"FeePayer has signed the transaction: {feePayerSignature.BcsToHex()}"); | ||
|
||
var pendingTxn = await aptos.Transaction.SubmitTransaction( | ||
new(txn, aliceSignature, feePayerSignature) | ||
); | ||
Console.WriteLine($"Submitted transaction with hash: {pendingTxn.Hash}"); | ||
|
||
Console.WriteLine("Waiting for transaction..."); | ||
var committedTxn = await aptos.Transaction.WaitForTransaction(pendingTxn.Hash.ToString()); | ||
Console.WriteLine( | ||
$"Transaction {committedTxn.Hash} is {(committedTxn.Success ? "success" : "failure")}" | ||
); | ||
|
||
Console.WriteLine("\n=== Account Balance ===\n"); | ||
|
||
var aliceBalance = await aptos.Account.GetCoinBalance(alice.Address); | ||
var feePayerBalance = await aptos.Account.GetCoinBalance(feePayer.Address); | ||
var receiverBalance = await aptos.Account.GetCoinBalance(receiver.Address); | ||
Console.WriteLine($"Alice {alice.Address} has {aliceBalance?.Amount ?? 0} APT"); | ||
Console.WriteLine($"Receiver {receiver.Address} has {receiverBalance?.Amount ?? 0} APT"); | ||
Console.WriteLine($"FeePayer {feePayer.Address} has {feePayerBalance?.Amount ?? 0} APT"); | ||
} | ||
} |
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,27 @@ | ||
fragment AptosNameData on current_aptos_names { | ||
domain | ||
expiration_timestamp | ||
registered_address | ||
subdomain | ||
token_standard | ||
is_primary | ||
owner_address | ||
subdomain_expiration_policy | ||
domain_expiration_timestamp | ||
} | ||
|
||
query GetNames( | ||
$offset: Int | ||
$limit: Int | ||
$where: current_aptos_names_bool_exp | ||
$order_by: [current_aptos_names_order_by!] | ||
) { | ||
current_aptos_names( | ||
limit: $limit | ||
where: $where | ||
order_by: $order_by | ||
offset: $offset | ||
) { | ||
...AptosNameData | ||
} | ||
} |
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
Oops, something went wrong.