Skip to content

Commit

Permalink
pr correction
Browse files Browse the repository at this point in the history
  • Loading branch information
chenquanyu committed Nov 8, 2019
1 parent 4959114 commit 6abf3c2
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 24 deletions.
2 changes: 1 addition & 1 deletion neo.UnitTests/Network/RPC/UT_ContractClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void TestDeployContract()
UT_TransactionManager.MockInvokeScript(rpcClientMock, script, new ContractParameter());

ContractClient contractClient = new ContractClient(rpcClientMock.Object);
var result = contractClient.DeployContract(new byte[1], manifest, keyPair1);
var result = contractClient.CreateDeployContractTx(new byte[1], manifest, keyPair1);

Assert.IsNotNull(result);
}
Expand Down
16 changes: 8 additions & 8 deletions neo.UnitTests/Network/RPC/UT_WalletAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public class UT_WalletAPI
public void TestSetup()
{
keyPair1 = new KeyPair(Wallet.GetPrivateKeyFromWIF("KyXwTh1hB76RRMquSvnxZrJzQx7h9nQP2PCRL38v6VDb5ip3nf1p"));
address1 = Neo.Wallets.Helper.ToAddress(keyPair1.ToScriptHash());
sender = Contract.CreateSignatureRedeemScript(keyPair1.PublicKey).ToScriptHash();
address1 = Neo.Wallets.Helper.ToAddress(sender);
rpcClientMock = UT_TransactionManager.MockRpcClient(sender, new byte[0]);
walletAPI = new WalletAPI(rpcClientMock.Object);
}

[TestMethod]
public void TestGetUnclaimedGas()
{
byte[] testScript = NativeContract.NEO.Hash.MakeScript("unclaimedGas", keyPair1.ToScriptHash(), 99);
byte[] testScript = NativeContract.NEO.Hash.MakeScript("unclaimedGas", sender, 99);
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_10000000) });

var balance = walletAPI.GetUnclaimedGas(address1);
Expand All @@ -44,7 +44,7 @@ public void TestGetUnclaimedGas()
[TestMethod]
public void TestGetNeoBalance()
{
byte[] testScript = NativeContract.NEO.Hash.MakeScript("balanceOf", keyPair1.ToScriptHash());
byte[] testScript = NativeContract.NEO.Hash.MakeScript("balanceOf", sender);
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_00000000) });

var balance = walletAPI.GetNeoBalance(address1);
Expand All @@ -54,7 +54,7 @@ public void TestGetNeoBalance()
[TestMethod]
public void TestGetGasBalance()
{
byte[] testScript = NativeContract.GAS.Hash.MakeScript("balanceOf", keyPair1.ToScriptHash());
byte[] testScript = NativeContract.GAS.Hash.MakeScript("balanceOf", sender);
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_10000000) });

var balance = walletAPI.GetGasBalance(address1);
Expand All @@ -64,7 +64,7 @@ public void TestGetGasBalance()
[TestMethod]
public void TestGetTokenBalance()
{
byte[] testScript = UInt160.Zero.MakeScript("balanceOf", keyPair1.ToScriptHash());
byte[] testScript = UInt160.Zero.MakeScript("balanceOf", sender);
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_10000000) });

var balance = walletAPI.GetTokenBalance(UInt160.Zero.ToString(), address1);
Expand All @@ -74,10 +74,10 @@ public void TestGetTokenBalance()
[TestMethod]
public void TestClaimGas()
{
byte[] balanceScript = NativeContract.NEO.Hash.MakeScript("balanceOf", keyPair1.ToScriptHash());
byte[] balanceScript = NativeContract.NEO.Hash.MakeScript("balanceOf", sender);
UT_TransactionManager.MockInvokeScript(rpcClientMock, balanceScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_00000000) });

byte[] testScript = NativeContract.NEO.Hash.MakeScript("transfer", keyPair1.ToScriptHash(), keyPair1.ToScriptHash(), new BigInteger(1_00000000));
byte[] testScript = NativeContract.NEO.Hash.MakeScript("transfer", sender, sender, new BigInteger(1_00000000));
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_10000000) });

rpcClientMock.Setup(p => p.RpcSend("sendrawtransaction", It.IsAny<JObject>())).Returns(true);
Expand All @@ -92,7 +92,7 @@ public void TestTransfer()
byte[] decimalsScript = NativeContract.GAS.Hash.MakeScript("decimals");
UT_TransactionManager.MockInvokeScript(rpcClientMock, decimalsScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(8) });

byte[] testScript = NativeContract.GAS.Hash.MakeScript("transfer", keyPair1.ToScriptHash(), UInt160.Zero, NativeContract.GAS.Factor * 100);
byte[] testScript = NativeContract.GAS.Hash.MakeScript("transfer", sender, UInt160.Zero, NativeContract.GAS.Factor * 100);
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_10000000) });

rpcClientMock.Setup(p => p.RpcSend("sendrawtransaction", It.IsAny<JObject>())).Returns(true);
Expand Down
5 changes: 3 additions & 2 deletions neo/Network/RPC/ContractClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public RpcInvokeResult TestInvoke(UInt160 scriptHash, string operation, params o
/// <param name="key">sender KeyPair</param>
/// <param name="networkFee">transaction NetworkFee, set to be 0 if you don't need higher priority</param>
/// <returns></returns>
public Transaction DeployContract(byte[] contractScript, ContractManifest manifest, KeyPair key, long networkFee = 0)
public Transaction CreateDeployContractTx(byte[] contractScript, ContractManifest manifest, KeyPair key, long networkFee = 0)
{
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
Expand All @@ -53,7 +53,8 @@ public Transaction DeployContract(byte[] contractScript, ContractManifest manife
script = sb.ToArray();
}

Transaction tx = new TransactionManager(rpcClient, key.ToScriptHash())
UInt160 sender = Contract.CreateSignatureRedeemScript(key.PublicKey).ToScriptHash();
Transaction tx = new TransactionManager(rpcClient, sender)
.MakeTransaction(script, null, null, networkFee)
.AddSignature(key)
.Sign()
Expand Down
12 changes: 1 addition & 11 deletions neo/Network/RPC/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ public static UInt160 ToUInt160(this string account)
throw new FormatException();
}

/// <summary>
/// Get the verification script hash of KeyPair
/// </summary>
/// <param name="key">account KeyPair</param>
/// <returns></returns>
public static UInt160 ToScriptHash(this KeyPair key)
{
return Contract.CreateSignatureRedeemScript(key.PublicKey).ToScriptHash();
}

/// <summary>
/// Parse WIF or private key hex string to KeyPair
/// </summary>
Expand Down Expand Up @@ -81,7 +71,7 @@ public static KeyPair ToKeyPair(this string key)
/// <param name="amount">float value</param>
/// <param name="decimals">token decimals</param>
/// <returns></returns>
public static BigInteger ToBigInteger(this decimal amount, uint decimals)
internal static BigInteger ToBigInteger(this decimal amount, uint decimals)
{
BigInteger factor = BigInteger.Pow(10, (int)decimals);
var (numerator, denominator) = Fraction(amount);
Expand Down
3 changes: 2 additions & 1 deletion neo/Network/RPC/Nep5API.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Neo.Network.P2P.Payloads;
using Neo.Network.RPC.Models;
using Neo.SmartContract;
using Neo.VM;
using Neo.Wallets;
using System.Linq;
Expand Down Expand Up @@ -105,7 +106,7 @@ public RpcNep5TokenInfo GetTokenInfo(UInt160 scriptHash)
/// <returns></returns>
public Transaction CreateTransferTx(UInt160 scriptHash, KeyPair fromKey, UInt160 to, BigInteger amount, long networkFee = 0)
{
var sender = fromKey.ToScriptHash();
var sender = Contract.CreateSignatureRedeemScript(fromKey.PublicKey).ToScriptHash();
Cosigner[] cosigners = new[] { new Cosigner { Scopes = WitnessScope.CalledByEntry, Account = sender } };

byte[] script = scriptHash.MakeScript("transfer", sender, to, amount);
Expand Down
3 changes: 2 additions & 1 deletion neo/Network/RPC/WalletAPI.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Network.RPC.Models;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.Wallets;
Expand Down Expand Up @@ -113,7 +114,7 @@ public Transaction ClaimGas(string key)
/// <returns>The transaction sended</returns>
public Transaction ClaimGas(KeyPair keyPair)
{
UInt160 toHash = keyPair.ToScriptHash();
UInt160 toHash = Contract.CreateSignatureRedeemScript(keyPair.PublicKey).ToScriptHash();
BigInteger balance = nep5API.BalanceOf(NativeContract.NEO.Hash, toHash);
Transaction transaction = nep5API.CreateTransferTx(NativeContract.NEO.Hash, keyPair, toHash, balance);
rpcClient.SendRawTransaction(transaction);
Expand Down

0 comments on commit 6abf3c2

Please sign in to comment.