Skip to content

Commit

Permalink
add withdraw/deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
youtpout committed Jan 31, 2023
1 parent c7d3125 commit b2d66e6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
55 changes: 55 additions & 0 deletions Assets/Sample/Scripts/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
using Nethereum.RPC.Eth.DTOs;
using Nethereum.RPC.HostWallet;
using Nethereum.Util;
using System;
using System.Collections.Generic;
using System.Numerics;
using TokenContract;
using Unity.Burst.CompilerServices;
using UnityEditor;
using UnityEngine;
using UnityEngine.Diagnostics;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using Web3Unity;
Expand All @@ -18,6 +21,8 @@ public class Sample : MonoBehaviour
protected Button btnCall;
protected Button btnSign;
protected Button btnSwitch;
protected Button btnDeposit;
protected Button btnWithdraw;
protected Label lblResult;
protected Label lblAccount;
protected VisualElement root;
Expand All @@ -30,6 +35,7 @@ public class Sample : MonoBehaviour
public GameObject cube;

private bool connected = false;
private bool getbalance = false;

// Start is called before the first frame update
async void Start()
Expand All @@ -41,6 +47,8 @@ async void Start()
btnCall = root.Q<Button>("btnCall");
btnSign = root.Q<Button>("btnSign");
btnSwitch = root.Q<Button>("btnSwitch");
btnDeposit = root.Q<Button>("btnDeposit");
btnWithdraw = root.Q<Button>("btnWithdraw");
lblResult = root.Q<Label>("lblResult");
lblAccount = root.Q<Label>("lblAccount");
veActions = root.Q<VisualElement>("veActions");
Expand All @@ -50,17 +58,58 @@ async void Start()
btnSign.clicked += BtnSign_clicked;
btnWallet.clicked += BtnWallet_clicked;
btnSwitch.clicked += BtnSwitch_clicked;
btnDeposit.clicked += BtnDeposit_clicked;
btnWithdraw.clicked += BtnWithdraw_clicked;

veActions.style.display = DisplayStyle.None;

Web3Connect.Instance.OnConnected += Instance_OnConnected;
}



private void Instance_OnConnected(object sender, string e)
{
Initialize();
}

private async void BtnDeposit_clicked()
{
// exemple call payable function
DepositFunction depositFunction = new DepositFunction()
{
AmountToSend = UnitConversion.Convert.ToWei(0.01m)
};
var deposit = await tokenService.DepositRequestAndWaitForReceiptAsync(depositFunction);
if (deposit.Succeeded())
{
lblResult.text = $"Matic depose on contract";
}
}

private async void BtnWithdraw_clicked()
{
WithdrawFunction function = new WithdrawFunction()
{
// put gas to prevent from estimate gas who can fail with walletconnect
Gas = 100000
};
// withdraw matic depose on contract
var withdraw = await tokenService.WithdrawRequestAsync(function);
lblResult.text = $"Withdraw tx hash {withdraw}";
}

private async void GetBalance()
{
if (Web3Connect.Instance.Web3 != null)
{
// request user balance, we can use classic nethereum function
var balance = await Web3Connect.Instance.Web3.Eth.GetBalance.SendRequestAsync(Web3Connect.Instance.AccountAddress);
var amount = UnitConversion.Convert.FromWei(balance.Value);
lblAccount.text = $"{Web3Connect.Instance.AccountAddress} {amount.ToString("F3")} matic";
}
}

private async void BtnMint_clicked()
{
// exemple mint 10 000 token
Expand Down Expand Up @@ -172,5 +221,11 @@ private void Initialize()
lblAccount.text = Web3Connect.Instance.AccountAddress;
veActions.style.display = DisplayStyle.Flex;
tokenService = new TokenContractService(tokenContractAddress);
if (!getbalance)
{
// prevent from multiple invoke repeating
getbalance = true;
InvokeRepeating("GetBalance", 0, 5);
}
}
}
4 changes: 3 additions & 1 deletion Assets/Sample/UI/Sample.uxml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
<ui:VisualElement name="veActions" style="flex-direction: row; justify-content: space-between; flex-wrap: wrap;">
<ui:Button text="Mint" display-tooltip-when-elided="true" name="btnMint" style="-unity-font: url(&apos;project://database/Library/unity%20default%20resources?fileID=10102&amp;guid=0000000000000000e000000000000000&amp;type=0#Arial&apos;); height: 50px; width: 300px; background-color: rgb(195, 134, 3); -unity-font-style: bold; font-size: 16px;" />
<ui:Button text="Switch Chain" display-tooltip-when-elided="true" name="btnSwitch" style="-unity-font: url(&apos;project://database/Library/unity%20default%20resources?fileID=10102&amp;guid=0000000000000000e000000000000000&amp;type=0#Arial&apos;); height: 50px; width: 300px; background-color: rgb(0, 106, 34); -unity-font-style: bold; font-size: 16px;" />
<ui:Button text="Sign" display-tooltip-when-elided="true" name="btnSign" style="-unity-font: url(&apos;project://database/Library/unity%20default%20resources?fileID=10102&amp;guid=0000000000000000e000000000000000&amp;type=0#Arial&apos;); height: 50px; width: 300px; background-color: rgb(0, 106, 34); -unity-font-style: bold; font-size: 16px;" />
<ui:Button text="Get Balance" display-tooltip-when-elided="true" name="btnCall" style="-unity-font: url(&apos;project://database/Library/unity%20default%20resources?fileID=10102&amp;guid=0000000000000000e000000000000000&amp;type=0#Arial&apos;); height: 50px; width: 300px; background-color: rgb(195, 134, 3); -unity-font-style: bold; font-size: 16px;" />
<ui:Button text="Sign" display-tooltip-when-elided="true" name="btnSign" style="-unity-font: url(&apos;project://database/Library/unity%20default%20resources?fileID=10102&amp;guid=0000000000000000e000000000000000&amp;type=0#Arial&apos;); height: 50px; width: 300px; background-color: rgb(0, 106, 34); -unity-font-style: bold; font-size: 16px;" />
<ui:Button text="Deposit 0.01" display-tooltip-when-elided="true" name="btnDeposit" style="-unity-font: url(&apos;project://database/Library/unity%20default%20resources?fileID=10102&amp;guid=0000000000000000e000000000000000&amp;type=0#Arial&apos;); height: 50px; width: 300px; background-color: rgb(0, 106, 34); -unity-font-style: bold; font-size: 16px;" />
<ui:Button text="Withdraw" display-tooltip-when-elided="true" name="btnWithdraw" style="-unity-font: url(&apos;project://database/Library/unity%20default%20resources?fileID=10102&amp;guid=0000000000000000e000000000000000&amp;type=0#Arial&apos;); height: 50px; width: 300px; background-color: rgb(195, 134, 3); -unity-font-style: bold; font-size: 16px;" />
</ui:VisualElement>
<ui:Label text="Result" display-tooltip-when-elided="true" name="lblResult" style="font-size: 20px;" />
</ui:VisualElement>
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ You can find the docs here : [Web3Library doc](https://be-technology.gitbook.io/

## Tip me
BTC bc1qrrvflnkh3447r6tcykjqe925pdud55lnhazs20

Eth/Bnb/Matic/Ftm/Avax... 0xB95c39ef569c5a6205C2F45F04bDA4efe9cAe5D3

## Contact me
Expand Down

0 comments on commit b2d66e6

Please sign in to comment.