Example #1
0
        public async Task UT_PoolFactory_SetupAsync()
        {
            //var aggClient = new LyraAggregatedClient(networkId);
            //await aggClient.InitAsync();
            //client = aggClient;
            client = LyraRestClient.Create(networkId, "Windows", "UnitTest", "1.0");

            var w1 = Restore(testPrivateKey);
            await w1.SyncAsync(client);

            var balances = w1.GetLastSyncBlock().Balances;

            Assert.IsTrue(balances[LyraGlobal.OFFICIALTICKERCODE].ToBalanceDecimal() > 100000m, "Insufficient funds: LYR");

            // make sure we have 2 test token
            var genResult = await client.GetTokenGenesisBlockAsync(testPublicKey, testTokenA, await SignAPIAsync());

            if (genResult.ResultCode == APIResultCodes.TokenGenesisBlockNotFound)
            {
                var secs   = testTokenA.Split('/');
                var result = await w1.CreateTokenAsync(secs[1], secs[0], "", 8, 50000000000, true, "", "", "", Lyra.Core.Blocks.ContractTypes.Cryptocurrency, null);

                Assert.IsTrue(result.Successful(), "Failed to create token: " + result.ResultCode);
                await w1.SyncAsync(client);
            }

            genResult = await client.GetTokenGenesisBlockAsync(testPublicKey, testTokenB, await SignAPIAsync());

            if (genResult.ResultCode == APIResultCodes.TokenGenesisBlockNotFound)
            {
                var secs   = testTokenB.Split('/');
                var result = await w1.CreateTokenAsync(secs[1], secs[0], "", 8, 50000000000, true, "", "", "", Lyra.Core.Blocks.ContractTypes.Cryptocurrency, null);

                Assert.IsTrue(result.Successful(), "Failed to create token: " + result.ResultCode);
                await w1.SyncAsync(client);
            }

            var pool = await client.GetPoolAsync(LyraGlobal.OFFICIALTICKERCODE, testTokenA);

            if (pool.PoolAccountId == null)
            {
                var token0 = LyraGlobal.OFFICIALTICKERCODE;
                var token1 = testTokenA;

                var poolCreateResult = await w1.CreateLiquidatePoolAsync(token0, token1);

                await Task.Delay(3000);     // give consens network time to create it.

                Assert.IsTrue(poolCreateResult.ResultCode == APIResultCodes.Success, "Can't create pool for " + token1);
            }

            await w1.SyncAsync(client);

            balances = w1.GetLastSyncBlock().Balances;
            Assert.IsTrue(balances[testTokenA].ToBalanceDecimal() > 100000m, "Insufficient funds: " + testTokenA);
        }
        private async Task TestDepositWithdraw(Wallet dexWallet)
        {
            //// prepare dex
            //string lyrawalletfolder = Wallet.GetFullFolderName(networkId, "wallets");
            //var walletStore = new SecuredWalletStore(lyrawalletfolder);
            //var dexWallet = Wallet.Open(walletStore, "dex", "");
            //await genesisWallet.SendAsync(100000m, dexWallet.AccountId);
            //await Task.Delay(1000);
            //await dexWallet.SyncAsync(genesisWallet.RPC);
            //Assert.IsTrue(dexWallet.BaseBalance >= 100000m);

            // external token genesis
            var lsbret = await client.GetLastServiceBlockAsync();

            Assert.IsTrue(lsbret.Successful());
            var lsb = lsbret.GetBlock();

            var tgexists = await client.GetTokenGenesisBlockAsync(dexWallet.AccountId, "tether/TRX",
                                                                  Signatures.GetSignature(dexWallet.PrivateKey, lsb.Hash, dexWallet.AccountId));

            if (!tgexists.Successful())
            {
                var tokenGenesisResult = await dexWallet.CreateTokenAsync("TRX", "tether", "", 8, 0, false, dexWallet.AccountId,
                                                                          "", "", ContractTypes.Cryptocurrency, null);

                Assert.IsTrue(tokenGenesisResult.Successful(), "dex token genesis failed");
            }

            var dexws = await dexWallet.GetAllDexWalletsAsync(dexWallet.AccountId);

            Assert.IsNotNull(dexws, "DEX Wallet not setup.");
            var wcnt = dexws.Count(a => (a as IDexWallet).ExtSymbol == "TRX" && (a as IDexWallet).ExtProvider == "native");

            Assert.AreEqual(1, wcnt, $"wallet not created properly. created: {wcnt}");

            // must fail
            await dexWallet.SyncAsync(null);

            var getokretx = await dexWallet.DexGetTokenAsync((dexws.First() as TransactionBlock).AccountID, 500m);

            Assert.IsTrue(!getokretx.Successful(), "Should not success");

            // mint via dex account

            /*
             * var
             * var dexbrk1 = dexws.First() as TransactionBlock;
             * var mintRet = await dexWallet.DexMintTokenAsync(dexbrk1.AccountID, 1000m);
             * Assert.IsTrue(mintRet.Successful(), "Mint failed.");
             * await Task.Delay(1000);
             *
             * var brk1lstret = await client.GetLastBlockAsync(dexbrk1.AccountID);
             * Assert.IsTrue(brk1lstret.Successful());
             * var brk1mint = brk1lstret.GetBlock() as TokenMintBlock;
             * Assert.IsNotNull(brk1mint);
             *
             * if (networkId == "xunit")
             * {
             *  Assert.AreEqual(2, brk1mint.Height, "No mint block generated.");
             *  Assert.AreEqual(1000, brk1mint.Balances["tether/TRX"].ToBalanceDecimal());
             * }
             *
             * // get minted token to owner wallet
             * await dexWallet.SyncAsync(null);
             * var getokret = await dexWallet.DexGetTokenAsync(dexbrk1.AccountID, 500m);
             * Assert.IsTrue(getokret.Successful(), "error get ext token to own wallet");
             * await Task.Delay(1500);
             * await dexWallet.SyncAsync(null);
             * Assert.AreEqual(500m, dexWallet.GetLastSyncBlock().Balances["tether/TRX"].ToBalanceDecimal(), "Ext token amount error");
             *
             * // put external token to dex wallet
             * var putokret = await dexWallet.DexPutTokenAsync(dexbrk1.AccountID, "tether/TRX", 500m);
             * Assert.IsTrue(putokret.Successful(), "Put token error");
             * await Task.Delay(1500);
             * var brk1lstret2 = await client.GetLastBlockAsync(dexbrk1.AccountID);
             * Assert.IsTrue(brk1lstret2.Successful());
             * var brk1lastblk = brk1lstret2.GetBlock() as TransactionBlock;
             * if (networkId == "xunit")
             * {
             *  Assert.AreEqual(1000m, brk1lastblk.Balances["tether/TRX"].ToBalanceDecimal(), "brk1 ext tok balance error");
             * }
             *
             * // withdraw token to external blockchain
             * var wdwret = await dexWallet.DexWithdrawTokenAsync(dexbrk1.AccountID, "Txxxxxxxxx", 1000m);
             * Assert.IsTrue(wdwret.Successful(), "Error withdraw");
             * await Task.Delay(1500);
             * var brk1lstret3 = await client.GetLastBlockAsync(dexbrk1.AccountID);
             * Assert.IsTrue(brk1lstret3.Successful());
             * var brk1lastblk3 = brk1lstret3.GetBlock() as TokenBurnBlock;
             * if (networkId == "xunit")
             *  Assert.AreEqual(0m, brk1lastblk3.Balances["tether/TRX"].ToBalanceDecimal(), "brk1 ext tok burn error");
             *
             */
        }