Skip to content

Commit

Permalink
Merge pull request #274 from TeknoPT/dev-clean-up
Browse files Browse the repository at this point in the history
Fix to the Script and Move DEX Contract to a future development.
  • Loading branch information
Relfos authored Oct 11, 2023
2 parents 04cbf0d + d660325 commit b76b921
Show file tree
Hide file tree
Showing 26 changed files with 291 additions and 48 deletions.
3 changes: 0 additions & 3 deletions DOCKER/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ COPY DOCKER/bin /app/bin

RUN apt-get update && apt-get install -y curl inotify-tools libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev libzstd-dev libc6-dev libicu-dev libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev libzstd-dev librocksdb-dev libc6-dev libsnappy-dev libicu-dev screen bash vim net-tools ca-certificates openssl libssl-dev nano

#RUN curl -sSL https://dot.net/v1/dotnet-install.sh | /bin/bash /dev/stdin -Channel 6.0 -Runtime dotnet -InstallDir /usr/share/dotnet \
# && ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet

EXPOSE 7077 7078 7079 7080 26056 26156 26256 26356 26057 26157 26257 26357

RUN chmod +x /app/wrapper.sh
Expand Down
3 changes: 2 additions & 1 deletion DOCKER/DockerfileTestnet
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ COPY DOCKER/bin /app/bin

RUN apt-get update; apt-get install -y inotify-tools libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev libzstd-dev libc6-dev libicu-dev libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev libzstd-dev librocksdb-dev libc6-dev libsnappy-dev libicu-dev screen bash vim net-tools ca-certificates openssl libssl-dev libgdiplus libx11-dev nano

EXPOSE 7077 7078 7079 7080 26056 26156 26256 26356 26057 26157 26257 26357
#EXPOSE 7077 7078 7079 7080 26056 26156 26256 26356 26057 26157 26257 26357
EXPOSE 7078 7079 7080 26156 26256 26356 26157 26257 26357

RUN chmod +x /app/wrapper-testnet.sh
ENTRYPOINT ["/app/wrapper-testnet.sh"]
15 changes: 15 additions & 0 deletions DOCKER/DockerfileTestnetDebug
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#--platform=linux/x86_64
FROM mcr.microsoft.com/dotnet/aspnet:6.0 as runtime
WORKDIR /app

# Copy everything
COPY DOCKER/wrapper-testnet-debug.sh /app/wrapper-testnet-debug.sh
COPY DOCKER/bin /app/bin

RUN apt-get update; apt-get install -y libc6-dev libsnappy-dev libicu-dev screen bash vim net-tools ca-certificates openssl libssl-dev librocksdb-dev nano

#EXPOSE 7077 7078 7079 7080 26056 26156 26256 26356 26057 26157 26257 26357
EXPOSE 7078 7079 7080 26156 26256 26356 26157 26257 26357

RUN chmod +x /app/wrapper-testnet-debug.sh
ENTRYPOINT ["/app/wrapper-testnet-debug.sh"]
34 changes: 34 additions & 0 deletions DOCKER/wrapper-testnet-debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
# reset all nodes
TROOT=/app/testnet
DIR="/app/testnet/node0/data/"

if [ -d "$DIR" ]; then
# Take action if $DIR exists. #
echo "Tendermint run..."
else
TMHOME=/app/testnet/node0 /app/bin/tendermint unsafe-reset-all
TMHOME=/app/testnet/node1 /app/bin/tendermint unsafe-reset-all
TMHOME=/app/testnet/node2 /app/bin/tendermint unsafe-reset-all
TMHOME=/app/testnet/node3 /app/bin/tendermint unsafe-reset-all
fi

# Clear old screens
screen -ls | grep 'node' | grep '(Detached)' | awk '{print $1}' | xargs -I % -t screen -X -S % quit
screen -wipe
#pkill -f "tendermint"

# Move config files
cp $TROOT/node0/config_node0.json $TROOT/node0/publish/config.json
cp $TROOT/node1/config_node1.json $TROOT/node1/publish/config.json
cp $TROOT/node2/config_node2.json $TROOT/node2/publish/config.json
cp $TROOT/node3/config_node3.json $TROOT/node3/publish/config.json

# start all tendermint sessions
#screen -S node0p -dm bash -c 'cd /app/testnet/node0/publish/; dotnet phantasma-node.dll; exec sh'
screen -S node1p -dm bash -c 'cd /app/testnet/node1/publish/; dotnet phantasma-node.dll; exec sh'
screen -S node2p -dm bash -c 'cd /app/testnet/node2/publish/; dotnet phantasma-node.dll; exec sh'
screen -S node3p -dm bash -c 'cd /app/testnet/node3/publish/; dotnet phantasma-node.dll; exec sh'

#screen -rd node0p
/bin/bash
10 changes: 6 additions & 4 deletions NodeExample/runTenderminNode.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
TROOT=`pwd`
NODENAME="node"
TMHOME=$TROOT/$NODENAME/
TENDERMINTPATH=./tendermint/tendermint # Change this to `./tendermint/tendermint

TENDERMINTPATH=./bin/tendermint # Change this to `./tendermint/tendermint
echo $TMHOME
# Setup node if not setuped previously
DIR="./node/data/"
if [ -d "$DIR" ]; then
echo "Node already configured"
else
$TENDERMINTPATH --home "$(pwd)/$NODENAME/" init
$TENDERMINTPATH --home "$TMHOME" unsafe-reset-all
#$TENDERMINTPATH --home "$TMHOME" init
fi

# start tendermint
# --proxy_app "tcp://127.0.0.1:26558" is used to setup the proxy_app (Phantasma.Node) ip address / port that is going to list to
# --p2p.seeds "node_ID@ip_address:node_port" -> can be used as a parameter to provide the seeds other way is going into the config.toml file.
$TENDERMINTPATH --home "$(pwd)/$NODENAME/" node
$TENDERMINTPATH --home "$TMHOME" node
echo $TENDERMINTPATH --home "$TMHOME" node
43 changes: 43 additions & 0 deletions Phantasma.Business/src/Blockchain/Contracts/Native/DexContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public bool IsSupportedToken(string symbol)
/// <param name="amount"></param>
public void DepositTokens(Address from, string symbol, BigInteger amount)
{
if (Runtime.ProtocolVersion >= 16)
{
Runtime.Expect(false, "This method is not available in this version of the DEX");
}
Runtime.Expect(Runtime.IsWitness(from), "invalid witness");
Runtime.Expect(from.IsUser, "address must be user address");

Expand All @@ -97,6 +101,10 @@ public void DepositTokens(Address from, string symbol, BigInteger amount)
/// <param name="amount"></param>
public void SwapTokens(Address from, string fromSymbol, string toSymbol, BigInteger amount)
{
if (Runtime.ProtocolVersion >= 16)
{
Runtime.Expect(false, "This method is not available in this version of the DEX");
}
Runtime.Expect(_DEXversion >= 1, "call migrateV3 first");
Runtime.Expect(Runtime.IsWitness(from), "invalid witness");
Runtime.Expect(amount > 0, $"invalid amount, need to be higher than 0 | {amount}");
Expand Down Expand Up @@ -410,6 +418,11 @@ public void Migrate()
/// </summary>
public void MigrateToV3()
{
if (Runtime.ProtocolVersion >= 16)
{
Runtime.Expect(false, "This method is not available in this version of the DEX");
}

Runtime.Expect(_DEXversion == 0, "Migration failed, wrong version");

var existsLP = Runtime.TokenExists(DomainSettings.LiquidityTokenSymbol);
Expand Down Expand Up @@ -1312,6 +1325,11 @@ private bool PoolExists(string symbol0, string symbol1)
/// <param name="amount1">Amount for Symbol1</param>
public void CreatePool(Address from, string symbol0, BigInteger amount0, string symbol1, BigInteger amount1)
{
if (Runtime.ProtocolVersion >= 16)
{
Runtime.Expect(false, "This method is not available in this version of the DEX");
}

ValidatePoolCreation(from, symbol0, amount0, symbol1, amount1);

var token0Info = Runtime.GetToken(symbol0);
Expand Down Expand Up @@ -1451,6 +1469,11 @@ private void MintLPTokensAndTransferToPool(Address from, string symbol0, BigInte
/// <param name="amount1">Amount for Symbol1</param>
public void AddLiquidity(Address from, string symbol0, BigInteger amount0, string symbol1, BigInteger amount1)
{
if (Runtime.ProtocolVersion >= 16)
{
Runtime.Expect(false, "This method is not available in this version of the DEX");
}

ValidateInputs(from, symbol0, amount0, symbol1, amount1);

var token0Info = Runtime.GetToken(symbol0);
Expand Down Expand Up @@ -1638,6 +1661,11 @@ private void UpdateLiquidity(Address from, string symbol0, string symbol1, BigIn
/// <param name="amount1">Amount for Symbol1</param>
public void AddLiquidityV1(Address from, string symbol0, BigInteger amount0, string symbol1, BigInteger amount1)
{
if (Runtime.ProtocolVersion >= 16)
{
Runtime.Expect(false, "This method is not available in this version of the DEX");
}

// Check input
Runtime.Expect(Runtime.IsWitness(from), "invalid witness");
Runtime.Expect(amount0 >= 0, "invalid amount 0");
Expand Down Expand Up @@ -1787,6 +1815,11 @@ public void AddLiquidityV1(Address from, string symbol0, BigInteger amount0, str
/// <param name="amount1">Amount for Symbol1</param>
public void RemoveLiquidity(Address from, string symbol0, BigInteger amount0, string symbol1, BigInteger amount1)
{
if (Runtime.ProtocolVersion >= 16)
{
Runtime.Expect(false, "This method is not available in this version of the DEX");
}

ValidateInputs(from, symbol0, amount0, symbol1, amount1);

// Check if user has LP Token
Expand Down Expand Up @@ -2208,6 +2241,11 @@ private void DistributeFee(BigInteger totalFeeAmount, Pool pool, string symbolDi
/// <param name="symbol1"></param>
public void ClaimFees(Address from, string symbol0, string symbol1)
{
if (Runtime.ProtocolVersion >= 16)
{
Runtime.Expect(false, "This method is not available in this version of the DEX");
}

if ( Runtime.ProtocolVersion >= 14) Runtime.Expect(_DEXversion >= 1, " This method is not available in this version of the DEX");
Runtime.Expect(Runtime.IsWitness(from), "invalid witness");

Expand Down Expand Up @@ -2429,6 +2467,11 @@ private void DeleteNFT(Address from, string symbol0, BigInteger amount0, string

public void BurnNFT(Address from, BigInteger nftID)
{
if (Runtime.ProtocolVersion >= 16)
{
Runtime.Expect(false, "This method is not available in this version of the DEX");
}

if ( Runtime.ProtocolVersion >= 14) Runtime.Expect(_DEXversion >= 1, " This method is not available in this version of the DEX");

if (!Runtime.NFTExists(DomainSettings.LiquidityTokenSymbol, nftID))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ private void ValidateExchangeParameters(string id, string name, BigInteger total
public void CreateExchange(Address from, string id, string name, BigInteger totalFee,
BigInteger feePercentForExchange, BigInteger feePercentForPool)
{
if (Runtime.ProtocolVersion >= 15)
if (Runtime.ProtocolVersion >= 16)
{
Runtime.Expect(false, "exchange not supported on this protocol version");
Runtime.Expect(false, "This method is not available in this version of the DEX");
return;
}

Expand Down Expand Up @@ -152,9 +152,9 @@ public void CreateExchange(Address from, string id, string name, BigInteger tota
public void EditExchange(Address from, string id, string name, BigInteger totalFee,
BigInteger feePercentForExchange, BigInteger feePercentForPool)
{
if (Runtime.ProtocolVersion >= 15)
if (Runtime.ProtocolVersion >= 16)
{
Runtime.Expect(false, "exchange not supported on this protocol version");
Runtime.Expect(false, "This method is not available in this version of the DEX");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public void Initialize(Address from)
}

Runtime.Expect(Runtime.IsWitness(from), "invalid witness");
Runtime.Expect(Runtime.IsKnownValidator(from), "invalid validator");
if ( Runtime.HasGenesis )
Runtime.Expect(Runtime.IsKnownValidator(from), "invalid validator");
_currentEnergyRatioDivisor = DefaultEnergyRatioDivisor; // used as 1/500, will initially generate 0.002 per staked token
}

Expand Down
5 changes: 3 additions & 2 deletions Phantasma.Business/src/Blockchain/Nexus/Nexus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ private Transaction NexusCreateTx(PhantasmaKeys owner, Timestamp genesisTime, in
// Generate extra KCAL in simnet only
if (Name == DomainSettings.NexusSimnet)
{
sb.MintTokens(DomainSettings.FuelTokenSymbol, owner.Address, owner.Address, UnitConversion.ToBigInteger(1000000, DomainSettings.FuelTokenDecimals));
sb.MintTokens(DomainSettings.StakingTokenSymbol, owner.Address, owner.Address, UnitConversion.ToBigInteger(10000000, DomainSettings.StakingTokenDecimals));
sb.MintTokens(DomainSettings.FuelTokenSymbol, owner.Address, owner.Address, UnitConversion.ToBigInteger(10000000, DomainSettings.FuelTokenDecimals));
}

sb.CallContract(NativeContractKind.Validator, nameof(ValidatorContract.SetValidator), owner.Address, new BigInteger(0), ValidatorType.Primary);
Expand Down Expand Up @@ -415,7 +416,7 @@ public void BeginInitialize(IRuntime vm, Address owner)
CreateToken(storage, DomainSettings.FuelTokenSymbol, DomainSettings.FuelTokenName, owner, 0, DomainSettings.FuelTokenDecimals, TokenFlags.Fungible | TokenFlags.Transferable | TokenFlags.Divisible | TokenFlags.Burnable | TokenFlags.Fuel, tokenScript, abi);
CreateToken(storage, DomainSettings.RewardTokenSymbol, DomainSettings.RewardTokenName, owner, 0, 0, TokenFlags.Transferable | TokenFlags.Burnable, tokenScript, abi);
CreateToken(storage, DomainSettings.FiatTokenSymbol, DomainSettings.FiatTokenName, owner, 0, DomainSettings.FiatTokenDecimals, TokenFlags.Fungible | TokenFlags.Transferable | TokenFlags.Divisible | TokenFlags.Fiat, tokenScript, abi);
var lpABI = ContractInterface.FromBytes(Base16.Decode(NEW_LP_CONTRACT_ABI));
//var lpABI = ContractInterface.FromBytes(Base16.Decode(NEW_LP_CONTRACT_ABI));
//CreateToken(storage, "LP", "Phantasma Liquidity Provider", owner, 0, 0, TokenFlags.Transferable | TokenFlags.Mintable | TokenFlags.Swappable | TokenFlags.Burnable, Base16.Decode(NEW_LP_CONTRACT_PVM), lpABI );

CreateToken(storage, "NEO", "NEO", owner, UnitConversion.ToBigInteger(100000000, 0), 0, TokenFlags.Fungible | TokenFlags.Transferable | TokenFlags.Finite, tokenScript, abi);
Expand Down
17 changes: 10 additions & 7 deletions Phantasma.Business/src/Blockchain/Nexus/NexusValidators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
using Phantasma.Core.Domain.Contract.Enums;
using Phantasma.Core.Domain.Contract.Validator.Enums;
using Phantasma.Core.Domain.Contract.Validator.Structs;
using Phantasma.Core.Domain.Interfaces;
using Phantasma.Core.Domain.Validation;
using Phantasma.Core.Storage.Context;
using Phantasma.Core.Storage.Context.Structs;
using Phantasma.Core.Types.Structs;

namespace Phantasma.Business.Blockchain;
Expand Down Expand Up @@ -84,6 +86,7 @@ public bool IsSecondaryValidator(Address address, Timestamp timestamp)
// this returns true for both active and waiting
public bool IsKnownValidator(Address address, Timestamp timestamp)
{
if (!HasGenesis()) return true;
var result = GetValidatorType(address, timestamp);
return result != ValidatorType.Invalid && result != ValidatorType.Proposed;
}
Expand Down Expand Up @@ -165,15 +168,15 @@ public ValidatorEntry GetValidator(StorageContext storage, string tAddress)
var lastBlockHash = this.RootChain.GetLastBlockHash();
var lastBlock = this.RootChain.GetBlockByHash(lastBlockHash);
// TODO use builtin methods instead of doing this directly
var validatorEntryVmObject = RootChain.InvokeContractAtTimestamp(storage, lastBlock.Timestamp,
/*var validatorEntryVmObject = RootChain.InvokeContractAtTimestamp(storage, lastBlock.Timestamp,
NativeContractKind.Validator,
nameof(ValidatorContract.GetCurrentValidator), tAddress);
return validatorEntryVmObject.AsStruct<ValidatorEntry>();
/*
var valueMapKey = Encoding.UTF8.GetBytes($".{validatorContractName}._validators");
var validators = new StorageMap(valueMapKey, storage);
return validatorEntryVmObject.AsStruct<ValidatorEntry>();*/

var valueMapKey = NativeContract.GetKeyForField(NativeContractKind.Validator, "_validators", true);
var validators = new StorageList(valueMapKey, storage);

foreach (var validator in validators.AllValues<ValidatorEntry>())
foreach (var validator in validators.All<ValidatorEntry>())
{
if (validator.address.TendermintAddress == tAddress)
{
Expand All @@ -186,7 +189,7 @@ public ValidatorEntry GetValidator(StorageContext storage, string tAddress)
address = Address.Null,
type = ValidatorType.Invalid,
election = new Timestamp(0)
};*/
};
}

#endregion
Expand Down
8 changes: 7 additions & 1 deletion Phantasma.Business/src/Blockchain/Oracle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Phantasma.Business.Blockchain.Contracts;
using Phantasma.Business.Blockchain.Contracts.Native;
using Phantasma.Core;
using Phantasma.Core.Cryptography;
Expand All @@ -21,6 +22,8 @@
using Phantasma.Core.Domain.Serializer;
using Phantasma.Core.Domain.Structs;
using Phantasma.Core.Numerics;
using Phantasma.Core.Storage.Context;
using Phantasma.Core.Storage.Context.Structs;
using Phantasma.Core.Types;
using Phantasma.Core.Types.Structs;
using Serilog;
Expand Down Expand Up @@ -142,7 +145,10 @@ public virtual T Read<T>(Timestamp time, string url) where T : class
BigInteger exchangeVersion = 0;
try
{
exchangeVersion = Nexus.RootChain.InvokeContractAtTimestamp(Nexus.RootStorage, time, NativeContractKind.Exchange, nameof(ExchangeContract.GetDexVersion)).AsNumber();
var keyForField = NativeContract.GetKeyForField(NativeContractKind.Exchange, "_DEXversion", true);
var storageValue = new StorageValue(keyForField, Nexus.RootStorage);
exchangeVersion = storageValue.Get<BigInteger>();
//exchangeVersion = Nexus.RootChain.InvokeContractAtTimestamp(Nexus.RootStorage, time, NativeContractKind.Exchange, nameof(ExchangeContract.GetDexVersion)).AsNumber();
}
catch ( Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ protected void SetInitialBalance(Address address)
simulator.GenerateTransfer(owner, address, nexus.RootChain, DomainSettings.FuelTokenSymbol, initialFuel);
simulator.GenerateTransfer(owner, address, nexus.RootChain, DomainSettings.StakingTokenSymbol, initialAmount);
simulator.EndBlock();
Assert.True(simulator.LastBlockWasSuccessful(), simulator.FailedTxReason);

simulator.BeginBlock();
simulator.GenerateTransfer(owner, address, nexus.RootChain, DomainSettings.FuelTokenSymbol, initialFuel);
simulator.GenerateTransfer(owner, address, nexus.RootChain, DomainSettings.StakingTokenSymbol, 100000000000);
simulator.EndBlock();
Assert.True(simulator.LastBlockWasSuccessful());
Assert.True(simulator.LastBlockWasSuccessful(), simulator.FailedTxReason);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void TestInflation()
phantomOrg.Address, simulator.CurrentTime);
// Initial Supply - 171462300000000
var tokenSupplySOUL = simulator.Nexus.RootChain.GetTokenSupply(simulator.Nexus.RootStorage, "SOUL");
Assert.Equal(171462300000000, tokenSupplySOUL);
Assert.Equal(1171462300000000, tokenSupplySOUL);
var InflationPerYear = 133;

var currentSupply = tokenSupplySOUL;
Expand Down
5 changes: 3 additions & 2 deletions Phantasma.Business/tests/Blockchain/ExtCallsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void TestDeployContractInsideContract()
simulator.BeginBlock();
simulator.GenerateTransfer(owner, contract.Address, nexus.RootChain, DomainSettings.FuelTokenSymbol, initialFuel);
simulator.EndBlock();

Assert.True(simulator.LastBlockWasSuccessful(), simulator.FailedTxReason);

simulator.BeginBlock();
var tx = simulator.GenerateCustomTransaction(user, ProofOfWork.Minimal,
Expand Down Expand Up @@ -114,7 +114,7 @@ public void TestDeployContractInsideContract()
}


[Fact]
[Fact(Skip = "TODO")]
public void TestDeployToken()
{
var contractName = "mintcontract";
Expand All @@ -125,6 +125,7 @@ public void TestDeployToken()
// Send Tokens to Contract
simulator.BeginBlock();
simulator.GenerateTransfer(owner, contract.Address, nexus.RootChain, DomainSettings.FuelTokenSymbol, initialFuel);
simulator.GenerateTransfer(owner, contract.Address, nexus.RootChain, DomainSettings.StakingTokenSymbol, UnitConversion.ToBigInteger(20000, DomainSettings.StakingTokenDecimals));
simulator.EndBlock();
Assert.True(simulator.LastBlockWasSuccessful(), simulator.FailedTxReason);

Expand Down
Loading

0 comments on commit b76b921

Please sign in to comment.