Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add transferFrom and approve to input address for ERC20 contract #271

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ <h4 class="card-title">
Transfer Tokens
</button>

<div class="form-group">
<label>Approve to Address</label>
<input
class="form-control"
id="approveTo"
value="0x9bc5baF874d2DA8D216aE9f137804184EE5AfEF4"
/>
</div>

<button
class="btn btn-primary btn-lg btn-block mb-3"
id="approveTokens"
Expand All @@ -351,6 +360,28 @@ <h4 class="card-title">
Approve Tokens
</button>

<div class="form-group">
<label>Transfer From</label>
<input
class="form-control"
id="transferFromSenderInput"
/>
</div>

<div class="form-group">
<label>Transfer To</label>
<input
class="form-control"
id="transferFromRecipientInput"
/>
</div>
<button
class="btn btn-primary btn-lg btn-block mb-3"
id="transferFromTokens"
disabled
>
Transfer From Tokens
</button>
<button
class="btn btn-primary btn-lg btn-block mb-3"
id="transferTokensWithoutGas"
Expand Down Expand Up @@ -1180,4 +1211,4 @@ <h4 class="card-title">

<script src="main.js" defer></script>
</body>
</html>
</html>
38 changes: 37 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,19 @@ const sendEIP1559Button = document.getElementById('sendEIP1559Button');

// Send Tokens Section
const decimalUnitsInput = document.getElementById('tokenDecimals');
const approveTokensToInput = document.getElementById('approveTo');
const transferFromSenderInput = document.getElementById(
'transferFromSenderInput',
);
const transferFromRecipientInput = document.getElementById(
'transferFromRecipientInput',
);
const tokenSymbol = 'TST';
const tokenAddresses = document.getElementById('tokenAddresses');
const createToken = document.getElementById('createToken');
const watchAssets = document.getElementById('watchAssets');
const transferTokens = document.getElementById('transferTokens');
const transferFromTokens = document.getElementById('transferFromTokens');
const approveTokens = document.getElementById('approveTokens');
const transferTokensWithoutGas = document.getElementById(
'transferTokensWithoutGas',
Expand Down Expand Up @@ -271,9 +279,13 @@ const allConnectedButtons = [
sendButton,
createToken,
decimalUnitsInput,
approveTokensToInput,
watchAssets,
transferTokens,
transferFromTokens,
approveTokens,
transferFromRecipientInput,
transferFromSenderInput,
transferTokensWithoutGas,
approveTokensWithoutGas,
getEncryptionKeyButton,
Expand Down Expand Up @@ -806,9 +818,13 @@ const updateContractElements = () => {
tokenAddresses.innerHTML = hstContract ? hstContract.address : '';
watchAssets.disabled = false;
transferTokens.disabled = false;
transferFromTokens.disabled = false;
approveTokens.disabled = false;
transferTokensWithoutGas.disabled = false;
approveTokensWithoutGas.disabled = false;
transferFromSenderInput.disabled = false;
approveTokensToInput.disabled = false;
transferFromRecipientInput.disabled = false;
}
};

Expand Down Expand Up @@ -1424,9 +1440,13 @@ const initializeFormElements = () => {
.join(', ');
watchAssets.disabled = false;
transferTokens.disabled = false;
transferFromTokens.disabled = false;
approveTokens.disabled = false;
transferTokensWithoutGas.disabled = false;
approveTokensWithoutGas.disabled = false;
approveTokensToInput.disabled = false;
transferFromSenderInput.disabled = false;
transferFromRecipientInput.disabled = false;
};

watchAssets.onclick = async () => {
Expand Down Expand Up @@ -1469,7 +1489,7 @@ const initializeFormElements = () => {

approveTokens.onclick = async () => {
const result = await hstContract.approve(
'0x9bc5baF874d2DA8D216aE9f137804184EE5AfEF4',
approveTokensToInput.value,
`${7 * 10 ** decimalUnitsInput.value}`,
{
from: accounts[0],
Expand All @@ -1480,6 +1500,22 @@ const initializeFormElements = () => {
console.log('result', result);
};

transferFromTokens.onclick = async () => {
const result = await hstContract.transferFrom(
transferFromSenderInput.value,
transferFromRecipientInput.value,
decimalUnitsInput.value === '0'
? 1
: `${1.5 * 10 ** decimalUnitsInput.value}`,
{
from: accounts[0],
gasLimit: '95000',
gasPrice: '20000000000',
},
);
console.log('result', result);
};

transferTokensWithoutGas.onclick = async () => {
const result = await hstContract.transfer(
'0x2f318C334780961FB129D2a6c30D0763d9a5C970',
Expand Down
Loading