Skip to content

Commit

Permalink
added check overflow for compiler and button for finding network fee …
Browse files Browse the repository at this point in the history
…optimized
  • Loading branch information
vncoelho committed Nov 21, 2023
1 parent 52e6b18 commit 2215e3e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 48 deletions.
56 changes: 53 additions & 3 deletions assets/eco-scripts/contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ function drawParametersTable() {

var paramInput = document.createElement('input');
paramInput.setAttribute('id', "paramInput" + p);
paramInput.setAttribute("class", "form-control");
paramInput.setAttribute("class", "form-control");
paramInput.placeholder = method.parameters[p].name + "(" + method.parameters[p].type + ")";
txRow.insertCell(-1).appendChild(paramInput);
} //Finishes loop that draws each relayed transaction
Expand Down Expand Up @@ -389,15 +389,18 @@ function invokeFunctionWithParams(contractHash, method, params, relay = false) {

var jsonForInvokingFunction = {
"jsonrpc": "2.0",
"id": 5,
"id": 1,
"method": "invokefunction",
"params": invokeparams
};

goToTabAndClick("nav-rpc");
var jsonToCallStringified = JSON.stringify(jsonForInvokingFunction);
$("#txtRPCJson").val(jsonToCallStringified);
rawRpcCall(true, -1, relay);

var fillNextTx = true;
var saveID = -1;
rawRpcCall(fillNextTx, saveID, relay);
}

function invokeFunction() {
Expand Down Expand Up @@ -443,6 +446,53 @@ function invokeFunction() {
invokeFunctionWithParams(CONTRACTS_TO_LIST[getCurrentSelectedContract()].hash, method.name, params);
}

function findNetworkFee() {
if (!checkIfWalletIsConnected())
return;
var sysFee = Math.ceil(getFixed8Integer($("#sys_fee")[0].value));
var validUntil = Number($("#valid_until")[0].value);
var script = $("#tx_script")[0].value;

var netFee = Math.ceil(getFixed8Integer($("#net_fee")[0].value));

const tx = new Neon.tx.Transaction({
signers: [{
account: ECO_WALLET[CONNECTED_WALLET_ID].account.scriptHash,
scopes: Neon.tx.WitnessScope.CalledByEntry,
},],
validUntilBlock: validUntil,
script: script,
systemFee: sysFee,
networkFee: netFee,
})

signTx(tx);
console.log(tx)

var rawTX = Neon.u.hex2base64(tx.serialize(true));
console.log(rawTX)
var jsonForInvokingFunction = {
"jsonrpc": "2.0",
"id": 10,
"method": "calculatenetworkfee",
"params": [rawTX]
};

var jsonToCallStringified = JSON.stringify(jsonForInvokingFunction);
console.log(jsonToCallStringified)
$.post(
BASE_PATH_CLI, // Gets the URL to sent the post to
jsonToCallStringified, // Serializes form data in standard format
function (netFeeResult) {
console.log(netFeeResult);
console.log("calculated fee is:" + netFeeResult..result.networkfee);
$("#net_fee")[0].value = netFeeResult.result.networkfee / getFixed8();
},
"json" // The format the response should be in
).fail(function () {
$("#txtRPCJsonOut").val("failed to invoke network!");
}); //End of POST for search
}

function getNativeContractIndexByName(contractName) {
for (nc = 0; nc < NATIVE_CONTRACTS.length; nc++)
Expand Down
67 changes: 36 additions & 31 deletions assets/eco-scripts/rawRpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ function fillRealTxFromInvokeFunction() {
});
return;
}

$("#sys_fee")[0].value = invokeResult.result.gasconsumed / getFixed8();
$("#net_fee")[0].value = 10000000 / getFixed8();
$("#net_fee")[0].value = 20000000 / getFixed8();
$("#tx_script")[0].value = Neon.u.base642hex(invokeResult.result.script);
$("#valid_until")[0].value = Neon.tx.Transaction.MAX_TRANSACTION_LIFESPAN + LAST_BEST_HEIGHT_NEOCLI - 1;
drawSigners();
Expand Down Expand Up @@ -125,8 +126,6 @@ function signAndRelay(blinkRelay = true) {
}
var signerHash = ECO_WALLET[CONNECTED_WALLET_ID].account.scriptHash;



invokeWithParametersDAPI(CONNECTED_DAPI_WALLET_OBJ, netFee, dapiParams.params[0], dapiParams.params[1], dapiParams.params[2], signerHash);

return;
Expand All @@ -150,33 +149,7 @@ function signAndRelay(blinkRelay = true) {
networkFee: netFee,
})

if (isMultiSig(CONNECTED_WALLET_ID)) {
var publicKeys = Neon.wallet.getPublicKeysFromVerificationScript(Neon.u.base642hex(ECO_WALLET[CONNECTED_WALLET_ID].account.contract.script))
var threshold = Neon.wallet.getSigningThresholdFromVerificationScript(Neon.u.base642hex(ECO_WALLET[CONNECTED_WALLET_ID].account.contract.script))
var signingAccountsIDs = getMultiSigSignersID(publicKeys, threshold);
if (signingAccountsIDs.length < threshold) {
swal({
title: "Current Wallet is connected to a multisig!",
text: "Your wallet has " + signingAccountsIDs.length + " accounts to sign. But required is " + threshold,
icon: "error",
button: "Ok!",
timer: 5500,
});
}

for (sa = 0; sa < signingAccountsIDs.length; sa++)
tx.sign(ECO_WALLET[signingAccountsIDs[sa]].account, NETWORK_MAGIC)

const multisigWitness = Neon.tx.Witness.buildMultiSig(
tx.serialize(false),
tx.witnesses,
ECO_WALLET[CONNECTED_WALLET_ID].account
);

tx.witnesses = [multisigWitness];
} else {
tx.sign(ECO_WALLET[CONNECTED_WALLET_ID].account, NETWORK_MAGIC)
}
signTx(tx);

var relayedTX = [];
relayedTX.push({
Expand All @@ -194,9 +167,11 @@ function signAndRelay(blinkRelay = true) {
console.log(tx)
console.log(tx.serialize(true))
var rawTX = Neon.u.hex2base64(tx.serialize(true));


var jsonForInvokingFunction = {
"jsonrpc": "2.0",
"id": 5,
"id": 2,
"method": "sendrawtransaction",
"params": [rawTX]
};
Expand All @@ -206,4 +181,34 @@ function signAndRelay(blinkRelay = true) {
rawRpcCall(false, RELAYED_TXS.length - 1, false, blinkRelay);
//var client = new Neon.rpc.RPCClient(BASE_PATH_CLI);
//RELAYED_TXS.push(client.sendRawTransaction(tx));
}

function signTx(tx){
if (isMultiSig(CONNECTED_WALLET_ID)) {
var publicKeys = Neon.wallet.getPublicKeysFromVerificationScript(Neon.u.base642hex(ECO_WALLET[CONNECTED_WALLET_ID].account.contract.script))
var threshold = Neon.wallet.getSigningThresholdFromVerificationScript(Neon.u.base642hex(ECO_WALLET[CONNECTED_WALLET_ID].account.contract.script))
var signingAccountsIDs = getMultiSigSignersID(publicKeys, threshold);
if (signingAccountsIDs.length < threshold) {
swal({
title: "Current Wallet is connected to a multisig!",
text: "Your wallet has " + signingAccountsIDs.length + " accounts to sign. But required is " + threshold,
icon: "error",
button: "Ok!",
timer: 5500,
});
}

for (sa = 0; sa < signingAccountsIDs.length; sa++)
tx.sign(ECO_WALLET[signingAccountsIDs[sa]].account, NETWORK_MAGIC)

const multisigWitness = Neon.tx.Witness.buildMultiSig(
tx.serialize(false),
tx.witnesses,
ECO_WALLET[CONNECTED_WALLET_ID].account
);

tx.witnesses = [multisigWitness];
} else {
tx.sign(ECO_WALLET[CONNECTED_WALLET_ID].account, NETWORK_MAGIC)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ RUN (cd /neo-devpack-dotnet/src/Neo.Compiler.CSharp && dotnet build)
#COPY file.csproj /neo-devpack-dotnet/templates/Template.NEP17.CSharp/Template.NEP17.CSharp.csproj

RUN mkdir /neo-devpack-dotnet/src/Template.CSharpNeoCompiler
COPY file.csproj /neo-devpack-dotnet/src/Template.CSharpNeoCompiler/Template.CSharpNeoCompiler.csproj
COPY file.csproj /neo-devpack-dotnet/src/Template.CSharpNeoCompiler/NeoCompilerContractGenerated.csproj


ADD entrypoint.sh /entrypoint.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ cat /tmp/output.txt | base64 -w 0 >> /tmp/return.txt

# Add new output related to the AVM, ABI and MANIFEST
echo -n "\", \"nef\": \"" >> /tmp/return.txt
if [ -f /neo-devpack-dotnet/src/Template.CSharpNeoCompiler/bin/sc/Template.CSharpNeoCompiler.nef ]; then
cat /neo-devpack-dotnet/src/Template.CSharpNeoCompiler/bin/sc/Template.CSharpNeoCompiler.nef | base64 -w 0 >> /tmp/return.txt
if [ -f /neo-devpack-dotnet/src/Template.CSharpNeoCompiler/bin/sc/NeoCompilerContractGenerated.nef ]; then
cat /neo-devpack-dotnet/src/Template.CSharpNeoCompiler/bin/sc/NeoCompilerContractGenerated.nef | base64 -w 0 >> /tmp/return.txt
fi
#echo -n "\", \"abi\":\"" >> /tmp/return.txt
#if [ -f /neo-devpack-dotnet/src/Template.CSharpNeoCompiler/bin/sc/Template.CSharpNeoCompiler.abi.json ]; then
# cat /neo-devpack-dotnet/src/Template.CSharpNeoCompiler/bin/sc/Template.CSharpNeoCompiler.abi.json | base64 -w 0 >> /tmp/return.txt
#fi
echo -n "\", \"manifest\":\"" >> /tmp/return.txt
if [ -f /neo-devpack-dotnet/src/Template.CSharpNeoCompiler/bin/sc/Template.CSharpNeoCompiler.manifest.json ]; then
cat /neo-devpack-dotnet/src/Template.CSharpNeoCompiler/bin/sc/Template.CSharpNeoCompiler.manifest.json | base64 -w 0 >> /tmp/return.txt
if [ -f /neo-devpack-dotnet/src/Template.CSharpNeoCompiler/bin/sc/NeoCompilerContractGenerated.manifest.json ]; then
cat /neo-devpack-dotnet/src/Template.CSharpNeoCompiler/bin/sc/NeoCompilerContractGenerated.manifest.json | base64 -w 0 >> /tmp/return.txt
fi
echo -n "\"}" >> /tmp/return.txt

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<BaseNameArgument Condition="'$(AssemblyName)' != ''">--base-name $(AssemblyName)</BaseNameArgument>
<BaseNameArgument Condition="'$(AssemblyName)' == ''">--base-name $(MSBuildProjectName)</BaseNameArgument>
<BaseNameArgument Condition="'$(AssemblyName)' == ''">--base-name $(MSBuildProjectName)</BaseNameArgument>
<CheckedArgument Condition="'$(CheckForOverflowUnderflow)' == 'true'">--checked</CheckedArgument>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

Expand All @@ -13,7 +14,7 @@
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Message Text="Start NeoContract converter, Source File: $(TargetPath) - Project Path $(ProjectPath)" Importance="high">
</Message>
<Exec Command="/neo-devpack-dotnet/src/Neo.Compiler.CSharp/bin/$(ConfigurationName)/net7.0/nccs $(BaseNameArgument) &quot;$(ProjectPath)&quot;" />
<Exec Command="/neo-devpack-dotnet/src/Neo.Compiler.CSharp/bin/$(ConfigurationName)/net7.0/nccs $(CheckedArgument) $(BaseNameArgument) &quot;$(ProjectPath)&quot;" />
</Target>

</Project>
26 changes: 19 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,15 @@ <h4 class="green-title">
<div class="col-md-6">
<div class="form-group">
<select id="native_contracts" name="native_contracts"
onchange="createNativeManifest()" class="form-select checkedOption" size="8"></select>
onchange="createNativeManifest()" class="form-select checkedOption"
size="8"></select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<select id="local_contracts" name="local_contracts"
onchange="createLocalManifest()" class="form-select checkedOption" size="8"></select>
onchange="createLocalManifest()" class="form-select checkedOption"
size="8"></select>
</div>
</div>
</div>
Expand Down Expand Up @@ -513,12 +515,13 @@ <h4 class="green-title">Request from Neo C# nodes (RPC-JSON API)</h4>
Method (view <a href="https://docs.neo.org/en-us/node/cli/apigen.html"
target="_blank">Official API</a> or put cursor on top for
tooltip)</label> -->
<select id="rpcMethodSelectionBox" class="form-select checkedOption" onchange="frmRPCJson()"
size="3">
<select id="rpcMethodSelectionBox" class="form-select checkedOption"
onchange="frmRPCJson()" size="3">
</select>
</div>
<div class="col-md-6">
<button id="rawRpcPostCallButton" onclick="rawRpcCall()" style="width: 100%" class="btn btn-default btn-1">
<button id="rawRpcPostCallButton" onclick="rawRpcCall()" style="width: 100%"
class="btn btn-default btn-1">
<i class="fas fa-phone"></i> Post to RPC
</button>
</div>
Expand Down Expand Up @@ -551,6 +554,15 @@ <h4 class="green-title">Request from Neo C# nodes (RPC-JSON API)</h4>
</div>
</div>

<div class="row mb-2">
<div class="col-md-12">
<button type="button" class="btn btn-default btn-1 btn-1-darker"
onclick="findNetworkFee()">
<i class="fa-solid fa-bug-slash"></i> Optimize Net. Fee
</button>
</div>
</div>

<div class="row mb-2">
<div class="col-md-12">
<label for="tx_script">Script</label>
Expand Down Expand Up @@ -747,8 +759,8 @@ <h4 class="green-title">Transfer NEP17 Tokens
</div>
<div class="row mb-2">
<div class="col-12">
<button type="button" onclick="createNep17Tx()" id="createTransferButton" style="width: 100%"
class="btn btn-default btn-1">
<button type="button" onclick="createNep17Tx()" id="createTransferButton"
style="width: 100%" class="btn btn-default btn-1">
<i class="fa-solid fa-money-bill-transfer"></i> Create Transfer
</button>
</div>
Expand Down

0 comments on commit 2215e3e

Please sign in to comment.