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

add EIP1559 tx button #117

Merged
merged 3 commits into from
Jul 8, 2021
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
9 changes: 9 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ <h4 class="card-title">
>
Send
</button>

<button
class="btn btn-primary btn-lg btn-block mb-3"
id="sendEIP1559Button"
disabled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we switch to a network that supports EIP-1559 without connecting to an account new button is enabled state.

Screen Shot 2021-06-24 at 5 52 04 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Responded in latest commit :D thanks @NiranjanaBinoy

hidden
>
Send EIP 1559 Transaction
</button>
<hr />
<h4 class="card-title">
Contract
Expand Down
64 changes: 62 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const contractStatus = document.getElementById('contractStatus');

// Send Eth Section
const sendButton = document.getElementById('sendButton');
const sendEIP1559Button = document.getElementById('sendEIP1559Button');

// Send Tokens Section
const tokenAddress = document.getElementById('tokenAddress');
Expand Down Expand Up @@ -327,6 +328,23 @@ const initialize = async () => {
console.log(result);
};

sendEIP1559Button.onclick = async () => {
const result = await ethereum.request({
method: 'eth_sendTransaction',
params: [
{
from: accounts[0],
to: '0x2f318C334780961FB129D2a6c30D0763d9a5C970',
value: '0x29a2241af62c0000',
gasLimit: '0x5028',
maxFeePerGas: '0x2540be400',
maxPriorityFeePerGas: '0x3b9aca00',
},
],
});
console.log(result);
};

/**
* ERC20 Token
*/
Expand Down Expand Up @@ -965,6 +983,18 @@ const initialize = async () => {
chainIdDiv.innerHTML = chainId;
}

function handleEIP1559Support(supported) {
if (supported && Array.isArray(accounts) && accounts.length >= 1) {
sendEIP1559Button.disabled = false;
sendEIP1559Button.hidden = false;
sendButton.innerText = 'Send Legacy Transaction';
} else {
sendEIP1559Button.disabled = true;
sendEIP1559Button.hidden = true;
sendButton.innerText = 'Send';
}
}

function handleNewNetwork(networkId) {
networkDiv.innerHTML = networkId;
}
Expand All @@ -980,6 +1010,13 @@ const initialize = async () => {
method: 'net_version',
});
handleNewNetwork(networkId);

const block = await ethereum.request({
method: 'eth_getBlockByNumber',
params: ['latest', false],
});

handleEIP1559Support(block.baseFeePerGas !== undefined);
} catch (err) {
console.error(err);
}
Expand All @@ -991,9 +1028,32 @@ const initialize = async () => {
ethereum.autoRefreshOnNetworkChange = false;
getNetworkAndChainId();

ethereum.on('chainChanged', handleNewChain);
ethereum.autoRefreshOnNetworkChange = false;
getNetworkAndChainId();

ethereum.on('chainChanged', (chain) => {
handleNewChain(chain);
ethereum
.request({
method: 'eth_getBlockByNumber',
params: ['latest', false],
})
.then((block) => {
handleEIP1559Support(block.baseFeePerGas !== undefined);
});
});
ethereum.on('networkChanged', handleNewNetwork);
ethereum.on('accountsChanged', handleNewAccounts);
ethereum.on('accountsChanged', (newAccounts) => {
ethereum
.request({
method: 'eth_getBlockByNumber',
params: ['latest', false],
})
.then((block) => {
handleEIP1559Support(block.baseFeePerGas !== undefined);
});
handleNewAccounts(newAccounts);
});

try {
const newAccounts = await ethereum.request({
Expand Down