-
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.
- Loading branch information
1 parent
4afac37
commit 13dcba9
Showing
4 changed files
with
209 additions
and
17 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,64 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Numerics; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Aptos.Examples; | ||
|
||
public class ComplexViewFunctionExample | ||
{ | ||
[JsonConverter(typeof(CurrentEpochProposalCountsConverter))] | ||
public class CurrentEpochProposalCounts(ulong successfulProposals, ulong failedProposals) | ||
{ | ||
public ulong? SuccessfulProposals = successfulProposals; | ||
|
||
public ulong? FailedProposals = failedProposals; | ||
} | ||
|
||
public class CurrentEpochProposalCountsConverter : JsonConverter<CurrentEpochProposalCounts> | ||
{ | ||
public override bool CanWrite => false; | ||
|
||
public override CurrentEpochProposalCounts? ReadJson( | ||
JsonReader reader, | ||
Type objectType, | ||
CurrentEpochProposalCounts? existingValue, | ||
bool hasExistingValue, | ||
JsonSerializer serializer | ||
) | ||
{ | ||
// Load the result of the View function which would be ["100", "100"] since the return type is (u64,u64). | ||
// We then deserialize the return value into a CurrentEpochProposalCounts. | ||
var jArrayObject = JArray.Load(reader); | ||
return new CurrentEpochProposalCounts( | ||
ulong.Parse(jArrayObject[0].ToString()), | ||
ulong.Parse(jArrayObject[0].ToString()) | ||
); | ||
} | ||
|
||
public override void WriteJson( | ||
JsonWriter writer, | ||
CurrentEpochProposalCounts? value, | ||
JsonSerializer serializer | ||
) => throw new NotImplementedException(); | ||
} | ||
|
||
public static async Task Run() | ||
{ | ||
var aptos = new AptosClient(new AptosConfig(Networks.Devnet)); | ||
|
||
// Complex typed view function - We will deserialize the return value into a custom class by using a JsonConverter. | ||
// This is only recommended if the return type is complex and cannot be easily deserialized | ||
// using the default deserializer (multiple return types). | ||
var proposals = await aptos.View<CurrentEpochProposalCounts>( | ||
new GenerateViewFunctionPayloadData( | ||
function: "0x1::stake::get_current_epoch_proposal_counts", | ||
functionArguments: [(ulong)0] | ||
) | ||
); | ||
|
||
Console.WriteLine( | ||
$"Successful Proposals: {proposals.SuccessfulProposals} and Failed Proposals: {proposals.FailedProposals}" | ||
); | ||
} | ||
} |
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,46 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Numerics; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Aptos.Examples; | ||
|
||
public class ViewFunctionExample | ||
{ | ||
public class Option<T>(List<T> vec) | ||
{ | ||
[JsonProperty("vec")] | ||
public List<T> Vec = vec; | ||
} | ||
|
||
public static async Task Run() | ||
{ | ||
var aptos = new AptosClient(new AptosConfig(Networks.Devnet)); | ||
|
||
// Regular view function | ||
var values = await aptos.View( | ||
new GenerateViewFunctionPayloadData( | ||
function: "0x1::coin::name", | ||
functionArguments: [], | ||
typeArguments: ["0x1::aptos_coin::AptosCoin"] | ||
) | ||
); | ||
// Returns an array of return values. In this array, we will get "["Aptos Coin]" | ||
Console.WriteLine($"Coin name: {JsonConvert.SerializeObject(values)}"); | ||
|
||
// Typed view function - If you know the type of the return values, you can pass | ||
// the type arguments to the view function to get the correct | ||
// return values. You can create your own deserializable values | ||
// to pass into this function. Its important to make sure that | ||
// the return type is a List. | ||
var typedValues = await aptos.View<List<Option<String>>>( | ||
new GenerateViewFunctionPayloadData( | ||
function: "0x1::coin::supply", | ||
functionArguments: [], | ||
typeArguments: ["0x1::aptos_coin::AptosCoin"] | ||
) | ||
); | ||
// Returns an array of return values. In this array, we will get "[{"vec":["18447254572092493002"]}]" | ||
Console.WriteLine($"Coin supply: {JsonConvert.SerializeObject(typedValues)}"); | ||
} | ||
} |
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