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 transaction construction examples for latest changes #33

Merged
merged 4 commits into from
Feb 22, 2024
Merged
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
62 changes: 45 additions & 17 deletions docs/n3/develop/tool/sdk/transaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
`RpcClient` encapsulates the transaction construction module, which allows you to construct transactions in Neo N3 with specific parameters and methods to personalize your functions. This document introduces the relevant methods.

:::note
If you use SDK to construct a transaction that requires a signature, you need to ensure that the RpcClient obeject and the network it is connected to are configured the same way, or the transaction constructed by the SDK will not be validated in the blockchain. To do so, load Neo-CLI config.json when constructing the RpcClient object, for example:
If you use SDK to construct a transaction that requires a signature, you need to ensure that the RpcClient object and the network it is connected to are configured the same way, or the transaction constructed by the SDK will not be validated in the blockchain. To do so, load Neo-CLI config.json when constructing the RpcClient object, for example:
:::
>
> RpcClient client = new RpcClient(new Uri("http://localhost:20332"), null, null, ProtocolSettings.Load("config.json"))
Expand Down Expand Up @@ -75,6 +75,7 @@ using Neo.SmartContract.Native;
using Neo.VM;
using Neo.Wallets;
using System;
using Utility = Neo.Network.RPC.Utility;

namespace ConsoleApp1
{
Expand All @@ -88,8 +89,11 @@ namespace ConsoleApp1

private static async Task TestNep17Transfer()
{
// get the protocol settings of the network
ProtocolSettings protocolSettings = ProtocolSettings.Load("/path/to/config.testnet.json");

// choose a neo node with rpc opened
RpcClient client = new RpcClient("http://127.0.0.1:10332");
RpcClient client = new RpcClient(new Uri("http://127.0.0.1:10332"), null, null, protocolSettings);
// get the KeyPair of your account, which will pay the system and network fee
KeyPair sendKey = Utility.GetKeyPair("L53tg72Az8QhYUAyyqTQ3LaXMXBE3S9mJGGZVKHBryZxya7prwhZ");
UInt160 sender = Contract.CreateSignatureContract(sendKey.PublicKey).ScriptHash;
Expand All @@ -98,15 +102,15 @@ namespace ConsoleApp1
Signer[] cosigners = new[] { new Signer { Scopes = WitnessScope.CalledByEntry, Account = sender } };

// get the scripthash of the account you want to transfer to
UInt160 receiver = Utility.GetScriptHash("NirHUAteaMr6CqWuAAMaEUScPcS3FDKebM");
UInt160 receiver = Utility.GetScriptHash("NirHUAteaMr6CqWuAAMaEUScPcS3FDKebM", protocolSettings);

// construct the script, in this example, we will transfer 1024 NEO to receiver
UInt160 scriptHash = NativeContract.NEO.Hash;
byte[] script = scriptHash.MakeScript("transfer", sender, receiver, 1024);
byte[] script = scriptHash.MakeScript("transfer", sender, receiver, 1024, null);

// initialize the TransactionManagerFactory with rpc client and magic
// fill in the TransactionManager with the script and cosigners
TransactionManager txManager = await new TransactionManagerFactory(client, 5195086)
TransactionManager txManager = await new TransactionManagerFactory(client)
.MakeTransactionAsync(script, cosigners).ConfigureAwait(false);
// add signature and sign transaction with the added signature
Transaction tx = await txManager.AddSignature(sendKey).SignAsync().ConfigureAwait(false);
Expand Down Expand Up @@ -135,6 +139,7 @@ using Neo.SmartContract.Native;
using Neo.VM;
using Neo.Wallets;
using System;
using Utility = Neo.Network.RPC.Utility;

namespace ConsoleApp1
{
Expand All @@ -148,17 +153,20 @@ namespace ConsoleApp1

private static async Task TestNep17Transfer()
{
// get the protocol settings of the network
ProtocolSettings protocolSettings = ProtocolSettings.Load("/path/to/config.testnet.json");

// choose a neo node with rpc opened
RpcClient client = new RpcClient("http://127.0.0.1:10332");
RpcClient client = new RpcClient(new Uri("http://127.0.0.1:10332"), null, null, protocolSettings);
// get the KeyPair of your account, which will pay the system and network fee
KeyPair sendKey = Utility.GetKeyPair("L53tg72Az8QhYUAyyqTQ3LaXMXBE3S9mJGGZVKHBryZxya7prwhZ");

// get the scripthash of the account you want to transfer to
UInt160 receiver = Utility.GetScriptHash("NirHUAteaMr6CqWuAAMaEUScPcS3FDKebM");
UInt160 receiver = Utility.GetScriptHash("NirHUAteaMr6CqWuAAMaEUScPcS3FDKebM", protocolSettings);

// use WalletAPI to create and send the transfer transaction
WalletAPI walletAPI = new WalletAPI(client);
Transaction tx = await walletAPI.TransferAsync(NativeContract.NEO.Hash, sendKey, receiver, 1024).ConfigureAwait(false);
Transaction tx = await walletAPI.TransferAsync(NativeContract.NEO.Hash, sendKey, receiver, 1024, null).ConfigureAwait(false);

// print a message after the transaction is on chain
WalletAPI neoAPI = new WalletAPI(client);
Expand All @@ -175,6 +183,7 @@ The following example implements a function that transfers 10 GAS to a multi-sig

```cs
using Neo;
using Neo.Cryptography.ECC;
using Neo.Network.P2P.Payloads;
using Neo.Network.RPC;
using Neo.SmartContract;
Expand All @@ -196,8 +205,11 @@ namespace ConsoleApp1

private static async Task TestToMultiTransfer()
{
// get the protocol settings of the network
ProtocolSettings protocolSettings = ProtocolSettings.Load("/path/to/config.testnet.json");

// choose a neo node with rpc opened
RpcClient client = new RpcClient("http://127.0.0.1:10332");
RpcClient client = new RpcClient(new Uri("http://127.0.0.1:10332"), null, null, protocolSettings);
// get the KeyPair of your account, which will pay the system and network fee
KeyPair sendKey = Utility.GetKeyPair("L53tg72Az8QhYUAyyqTQ3LaXMXBE3S9mJGGZVKHBryZxya7prwhZ");
UInt160 sender = Contract.CreateSignatureContract(sendKey.PublicKey).ScriptHash;
Expand All @@ -206,22 +218,28 @@ namespace ConsoleApp1
KeyPair key2 = Utility.GetKeyPair("L1bQBbZWnKbPkpHM3jXWD3E5NwK7nui2eWHYXVZPy3t8jSFF1Qj3");
KeyPair key3 = Utility.GetKeyPair("KwrJfYyc7KWfZG5h97SYfcCQyW4jRw1njmHo48kZhZmuQWeTtUHM");

// add the KeyPairs to IReadOnlyCollection<ECPoint>
IReadOnlyCollection<ECPoint> keys = new List<ECPoint>()
{
sendKey.PublicKey, key2.PublicKey, key3.PublicKey
};

// create multi-signatures contract, this contract needs at least 2 of 3 KeyPairs to sign
Contract multiContract = Contract.CreateMultiSigContract(2, sendKey.PublicKey, key2.PublicKey, key3.PublicKey);
Contract multiContract = Contract.CreateMultiSigContract(2, keys);
// get the scripthash of the multi-signature Contract
UInt160 multiAccount = multiContract.Script.ToScriptHash();

// construct the script, in this example, we will transfer 1024 GAS to multi-sign account
// in contract parameter, the amount type is BigInteger, so we need to muliply the contract factor
UInt160 scriptHash = NativeContract.GAS.Hash;
byte[] script = scriptHash.MakeScript("transfer", sender, multiAccount, 1024 * NativeContract.GAS.Factor);
byte[] script = scriptHash.MakeScript("transfer", sender, multiAccount, 1024 * NativeContract.GAS.Factor, null);

// add Signers, which is a collection of scripthashs that need to be signed
Signer[] cosigners = new[] { new Signer { Scopes = WitnessScope.CalledByEntry, Account = sender } };

// initialize the TransactionManager with rpc client and magic
// fill the script and cosigners
TransactionManager txManager = await new TransactionManagerFactory(client, 5195086)
TransactionManager txManager = await new TransactionManagerFactory(client)
.MakeTransactionAsync(script, cosigners).ConfigureAwait(false);
// add signature and sign transaction with the added signature
Transaction tx = await txManager.AddSignature(sendKey).SignAsync().ConfigureAwait(false);
Expand All @@ -245,6 +263,7 @@ The following example implements a function that transfers 1024 GAS from a multi

```cs
using Neo;
using Neo.Cryptography.ECC;
using Neo.Network.P2P.Payloads;
using Neo.Network.RPC;
using Neo.SmartContract;
Expand All @@ -266,16 +285,25 @@ namespace ConsoleApp1

private static async Task TestFromMultiTransfer()
{
// get the protocol settings of the network
ProtocolSettings protocolSettings = ProtocolSettings.Load("/path/to/config.testnet.json");

// choose a neo node with rpc opened
RpcClient client = new RpcClient("http://127.0.0.1:10332");
RpcClient client = new RpcClient(new Uri("http://127.0.0.1:10332"), null, null, protocolSettings);

// get the KeyPair of your account
KeyPair receiverKey = Utility.GetKeyPair("L53tg72Az8QhYUAyyqTQ3LaXMXBE3S9mJGGZVKHBryZxya7prwhZ");
KeyPair key2 = Utility.GetKeyPair("L1bQBbZWnKbPkpHM3jXWD3E5NwK7nui2eWHYXVZPy3t8jSFF1Qj3");
KeyPair key3 = Utility.GetKeyPair("KwrJfYyc7KWfZG5h97SYfcCQyW4jRw1njmHo48kZhZmuQWeTtUHM");

// add the KeyPairs to IReadOnlyCollection<ECPoint>
IReadOnlyCollection<ECPoint> keys = new List<ECPoint>()
{
receiverKey.PublicKey, key2.PublicKey, key3.PublicKey
};

// create multi-signature contract, this contract needs at least 2 of 3 KeyPairs to sign
Contract multiContract = Contract.CreateMultiSigContract(2, receiverKey.PublicKey, key2.PublicKey, key3.PublicKey);
Contract multiContract = Contract.CreateMultiSigContract(2, keys);
// get the scripthash of the multi-signature Contract
UInt160 multiAccount = multiContract.Script.ToScriptHash();

Expand All @@ -284,17 +312,17 @@ namespace ConsoleApp1
// construct the script, in this example, we will transfer 1024 GAS to multi-sign account
// in contract parameter, the amount type is BigInteger, so we need to muliply the contract factor
UInt160 scriptHash = NativeContract.GAS.Hash;
byte[] script = scriptHash.MakeScript("transfer", multiAccount, receiver, 1024 * NativeContract.GAS.Factor);
byte[] script = scriptHash.MakeScript("transfer", multiAccount, receiver, 1024 * NativeContract.GAS.Factor, null);

// add Signers, which is a collection of scripthashs that need to be signed
Signer[] cosigners = new[] { new Signer { Scopes = WitnessScope.CalledByEntry, Account = multiAccount } };

// initialize the TransactionManager with rpc client and magic
// fill the script and cosigners
TransactionManager txManager = await new TransactionManagerFactory(client, 5195086)
TransactionManager txManager = await new TransactionManagerFactory(client)
.MakeTransactionAsync(script, cosigners).ConfigureAwait(false);
// add signature and sign transaction with the added signature
Transaction tx = await txManager.AddMultiSig(new KeyPair[]{receiverKey, key2}, 2, receiverKey.PublicKey, key2.PublicKey, key3.PublicKey)
Transaction tx = await txManager.AddMultiSig(new KeyPair[] { receiverKey, key2 }, 2, receiverKey.PublicKey, key2.PublicKey, key3.PublicKey)
.SignAsync().ConfigureAwait(false);

// broadcasts the transaction over the Neo network.
Expand Down