forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C# SDK Add Transaction Manager and Smart Contract APIs (neo-project#1026
) * Add NEO SDK based on RPC client * add rpc interface methods for neo3 * update unit test * add unit test * Update TransactionHelper.cs Changed for neo 3.0, not final yet. * implement sdk rpc client methods * backup files * change class name * remove uncompleted modules for pull request * change json deserialize method with Neo JObject * modified JSON implementation, added FromJson() * more RPC change * PR correction * RPC module fix, remove newton.json * fix * fix getblock issue * PR correction * PR Correction * PR Correction: rename RPC models * PR Correction * resolve conflicts * Clean code * Clean code * Clean code * Clean code * Update RpcValidateAddressResult.cs * Clean code * PR correction * Move test file to the right place * Added SDK Transaction module. * Add SDK SmartContract module * height = count - 1 * Add sign function for TxManager * Add Deploy Contract * Add unit tests * Add Network Fee calculate for TxManager, add unit tests * adjust cosigners change * PR Correction * Remove empty line * Rename TxManager to TransactionManager * PR correction * PR correction * change namespace * Reorder methods * Remove TransactionContext * fix unit test * Remove `virtual` * Remove virtuals * Remove ScriptHash from KeyPair * Add comments * Add comments * Adjust to Neo_Contract_Create parameter * use default mainfest * fix unit test * Fix typo * use manifest as parameter * add cosigner for nep5 transfer * code clean * Update neo.UnitTests/Network/RPC/UT_RpcClient.cs Co-Authored-By: Shargon <shargon@gmail.com> * move MakeScript to VM.Helper * Add unit test for InteropInterface * PR Correction * Add unit test
- Loading branch information
1 parent
f76c037
commit 9c05432
Showing
16 changed files
with
1,112 additions
and
58 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,68 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using Neo.Network.RPC; | ||
using Neo.SmartContract; | ||
using Neo.SmartContract.Manifest; | ||
using Neo.SmartContract.Native; | ||
using Neo.VM; | ||
using Neo.Wallets; | ||
|
||
namespace Neo.UnitTests.Network.RPC | ||
{ | ||
[TestClass] | ||
public class UT_ContractClient | ||
{ | ||
Mock<RpcClient> rpcClientMock; | ||
KeyPair keyPair1; | ||
UInt160 sender; | ||
|
||
[TestInitialize] | ||
public void TestSetup() | ||
{ | ||
keyPair1 = new KeyPair(Wallet.GetPrivateKeyFromWIF("KyXwTh1hB76RRMquSvnxZrJzQx7h9nQP2PCRL38v6VDb5ip3nf1p")); | ||
sender = Contract.CreateSignatureRedeemScript(keyPair1.PublicKey).ToScriptHash(); | ||
rpcClientMock = UT_TransactionManager.MockRpcClient(sender, new byte[0]); | ||
} | ||
|
||
[TestMethod] | ||
public void TestMakeScript() | ||
{ | ||
byte[] testScript = NativeContract.GAS.Hash.MakeScript("balanceOf", UInt160.Zero); | ||
|
||
Assert.AreEqual("14000000000000000000000000000000000000000051c10962616c616e63654f66142582d1b275e86c8f0e93a9b2facd5fdb760976a168627d5b52", | ||
testScript.ToHexString()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestInvoke() | ||
{ | ||
byte[] testScript = NativeContract.GAS.Hash.MakeScript("balanceOf", UInt160.Zero); | ||
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.ByteArray, Value = "00e057eb481b".HexToBytes() }); | ||
|
||
ContractClient contractClient = new ContractClient(rpcClientMock.Object); | ||
var result = contractClient.TestInvoke(NativeContract.GAS.Hash, "balanceOf", UInt160.Zero); | ||
|
||
Assert.AreEqual(30000000000000L, (long)result.Stack[0].ToStackItem().GetBigInteger()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestDeployContract() | ||
{ | ||
byte[] script; | ||
var manifest = ContractManifest.CreateDefault(new byte[1].ToScriptHash()); | ||
manifest.Features = ContractFeatures.HasStorage | ContractFeatures.Payable; | ||
using (ScriptBuilder sb = new ScriptBuilder()) | ||
{ | ||
sb.EmitSysCall(InteropService.Neo_Contract_Create, new byte[1], manifest.ToString()); | ||
script = sb.ToArray(); | ||
} | ||
|
||
UT_TransactionManager.MockInvokeScript(rpcClientMock, script, new ContractParameter()); | ||
|
||
ContractClient contractClient = new ContractClient(rpcClientMock.Object); | ||
var result = contractClient.DeployContract(new byte[1], manifest, keyPair1); | ||
|
||
Assert.IsNotNull(result); | ||
} | ||
} | ||
} |
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,89 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using Neo.Network.RPC; | ||
using Neo.SmartContract; | ||
using Neo.SmartContract.Native; | ||
using Neo.VM; | ||
using Neo.Wallets; | ||
using System.Numerics; | ||
|
||
namespace Neo.UnitTests.Network.RPC | ||
{ | ||
[TestClass] | ||
public class UT_Nep5API | ||
{ | ||
Mock<RpcClient> rpcClientMock; | ||
KeyPair keyPair1; | ||
UInt160 sender; | ||
Nep5API nep5API; | ||
|
||
[TestInitialize] | ||
public void TestSetup() | ||
{ | ||
keyPair1 = new KeyPair(Wallet.GetPrivateKeyFromWIF("KyXwTh1hB76RRMquSvnxZrJzQx7h9nQP2PCRL38v6VDb5ip3nf1p")); | ||
sender = Contract.CreateSignatureRedeemScript(keyPair1.PublicKey).ToScriptHash(); | ||
rpcClientMock = UT_TransactionManager.MockRpcClient(sender, new byte[0]); | ||
nep5API = new Nep5API(rpcClientMock.Object); | ||
} | ||
|
||
[TestMethod] | ||
public void TestBalanceOf() | ||
{ | ||
byte[] testScript = NativeContract.GAS.Hash.MakeScript("balanceOf", UInt160.Zero); | ||
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(10000) }); | ||
|
||
var balance = nep5API.BalanceOf(NativeContract.GAS.Hash, UInt160.Zero); | ||
Assert.AreEqual(10000, (int)balance); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetName() | ||
{ | ||
byte[] testScript = NativeContract.GAS.Hash.MakeScript("name"); | ||
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.String, Value = NativeContract.GAS.Name }); | ||
|
||
var result = nep5API.Name(NativeContract.GAS.Hash); | ||
Assert.AreEqual(NativeContract.GAS.Name, result); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetSymbol() | ||
{ | ||
byte[] testScript = NativeContract.GAS.Hash.MakeScript("symbol"); | ||
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.String, Value = NativeContract.GAS.Symbol }); | ||
|
||
var result = nep5API.Symbol(NativeContract.GAS.Hash); | ||
Assert.AreEqual(NativeContract.GAS.Symbol, result); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetDecimals() | ||
{ | ||
byte[] testScript = NativeContract.GAS.Hash.MakeScript("decimals"); | ||
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(NativeContract.GAS.Decimals) }); | ||
|
||
var result = nep5API.Decimals(NativeContract.GAS.Hash); | ||
Assert.AreEqual(NativeContract.GAS.Decimals, (byte)result); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetTotalSupply() | ||
{ | ||
byte[] testScript = NativeContract.GAS.Hash.MakeScript("totalSupply"); | ||
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_00000000) }); | ||
|
||
var result = nep5API.TotalSupply(NativeContract.GAS.Hash); | ||
Assert.AreEqual(1_00000000, (int)result); | ||
} | ||
|
||
[TestMethod] | ||
public void TestTransfer() | ||
{ | ||
byte[] testScript = NativeContract.GAS.Hash.MakeScript("transfer", sender, UInt160.Zero, new BigInteger(1_00000000)); | ||
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter()); | ||
|
||
var result = nep5API.Transfer(NativeContract.GAS.Hash, keyPair1, UInt160.Zero, new BigInteger(1_00000000)); | ||
Assert.IsNotNull(result); | ||
} | ||
} | ||
} |
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 Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using Neo.Network.RPC; | ||
using Neo.SmartContract; | ||
using Neo.SmartContract.Native; | ||
using Neo.VM; | ||
using Neo.Wallets; | ||
using System.Numerics; | ||
|
||
namespace Neo.UnitTests.Network.RPC | ||
{ | ||
[TestClass] | ||
public class UT_PolicyAPI | ||
{ | ||
Mock<RpcClient> rpcClientMock; | ||
KeyPair keyPair1; | ||
UInt160 sender; | ||
PolicyAPI policyAPI; | ||
|
||
[TestInitialize] | ||
public void TestSetup() | ||
{ | ||
keyPair1 = new KeyPair(Wallet.GetPrivateKeyFromWIF("KyXwTh1hB76RRMquSvnxZrJzQx7h9nQP2PCRL38v6VDb5ip3nf1p")); | ||
sender = Contract.CreateSignatureRedeemScript(keyPair1.PublicKey).ToScriptHash(); | ||
rpcClientMock = UT_TransactionManager.MockRpcClient(sender, new byte[0]); | ||
policyAPI = new PolicyAPI(rpcClientMock.Object); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetMaxTransactionsPerBlock() | ||
{ | ||
byte[] testScript = NativeContract.Policy.Hash.MakeScript("getMaxTransactionsPerBlock"); | ||
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(512) }); | ||
|
||
var result = policyAPI.GetMaxTransactionsPerBlock(); | ||
Assert.AreEqual(512u, result); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetMaxBlockSize() | ||
{ | ||
byte[] testScript = NativeContract.Policy.Hash.MakeScript("getMaxBlockSize"); | ||
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1024u * 256u) }); | ||
|
||
var result = policyAPI.GetMaxBlockSize(); | ||
Assert.AreEqual(1024u * 256u, result); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetFeePerByte() | ||
{ | ||
byte[] testScript = NativeContract.Policy.Hash.MakeScript("getFeePerByte"); | ||
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1000) }); | ||
|
||
var result = policyAPI.GetFeePerByte(); | ||
Assert.AreEqual(1000L, result); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetBlockedAccounts() | ||
{ | ||
byte[] testScript = NativeContract.Policy.Hash.MakeScript("getBlockedAccounts"); | ||
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Array, Value = new[] { new ContractParameter { Type = ContractParameterType.Hash160, Value = UInt160.Zero } } }); | ||
|
||
var result = policyAPI.GetBlockedAccounts(); | ||
Assert.AreEqual(UInt160.Zero, result[0]); | ||
} | ||
} | ||
} |
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.