diff --git a/docs/n3/develop/tool/sdk/transaction.md b/docs/n3/develop/tool/sdk/transaction.md index 4b55a68..24ad327 100644 --- a/docs/n3/develop/tool/sdk/transaction.md +++ b/docs/n3/develop/tool/sdk/transaction.md @@ -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")) @@ -75,6 +75,7 @@ using Neo.SmartContract.Native; using Neo.VM; using Neo.Wallets; using System; +using Utility = Neo.Network.RPC.Utility; namespace ConsoleApp1 { @@ -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; @@ -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); @@ -135,6 +139,7 @@ using Neo.SmartContract.Native; using Neo.VM; using Neo.Wallets; using System; +using Utility = Neo.Network.RPC.Utility; namespace ConsoleApp1 { @@ -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); @@ -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; @@ -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; @@ -206,22 +218,28 @@ namespace ConsoleApp1 KeyPair key2 = Utility.GetKeyPair("L1bQBbZWnKbPkpHM3jXWD3E5NwK7nui2eWHYXVZPy3t8jSFF1Qj3"); KeyPair key3 = Utility.GetKeyPair("KwrJfYyc7KWfZG5h97SYfcCQyW4jRw1njmHo48kZhZmuQWeTtUHM"); + // add the KeyPairs to IReadOnlyCollection + IReadOnlyCollection keys = new List() + { + 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); @@ -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; @@ -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 + IReadOnlyCollection keys = new List() + { + 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(); @@ -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.