Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from INFURA/docs-feedback
Browse files Browse the repository at this point in the history
Added a few more comments to the example scripts
  • Loading branch information
meronym authored Dec 3, 2020
2 parents 84b801d + a901c77 commit 36b6127
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 49 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Ethereum transactions demo
# Infura transactions demo

In this repository you'll find a collection of code samples that show you how to interact with Ethereum using:
ITX makes transaction sending easy. It takes care of the edge cases of getting a transaction mined, so you don't have to.

- [The standard approach](standard-transactions): here you'll learn how to use the two most popular Javascript libraries: [web3.js](https://github.com/ethereum/web3.js/) and [ethers.js](https://github.com/ethers-io/ethers.js/).
This repository contains a comparison of how traditional compares to using ITX. You'll find a collection of code samples that show you how to interact with Ethereum using:

- [Infura Transactions](infura-transactions): here you'll find examples for using the new [Infura Transactions](https://infura.io/docs/ethereum#tag/Transactions) service
- [The standard approach](standard-transactions): you're probably already familiar with this approach. Transactions are sent directly from a user wallet with the help of a popular library like [web3.js](https://github.com/ethereum/web3.js/) or [ethers.js](https://github.com/ethers-io/ethers.js/).

- [Infura Transactions](infura-transactions): here you'll find examples for using the new [Infura Transactions](https://infura.io/docs/ethereum#tag/Transactions) service.
2 changes: 2 additions & 0 deletions infura-transactions/balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ async function main() {
console.log(`Signer public address: ${signer.address}`);

// Check your existing ITX balance
// balance is added by sending eth to the deposit address: 0x015C7C7A7D65bbdb117C573007219107BD7486f9
// balance is deducted everytime you send a relay transaction
const balance = await itx.send('relay_getBalance', [signer.address]);
console.log(`Current ITX balance: ${balance}`);
}
Expand Down
80 changes: 36 additions & 44 deletions infura-transactions/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,10 @@ const { ethers } = require('ethers');
const fs = require('fs');
const { abi } = JSON.parse(fs.readFileSync('Demo.json'));

// This function signs a relay request using the signer's private key
// Final signature of the form keccak256("\x19Ethereum Signed Message:\n" + len((to + data + gas + chainId)) + (to + data + gas + chainId)))
// Where (to + data + gas + chainId) represents the RLP encoded concatenation of these fields.
async function signRequest(signer, tx) {
const relayTransactionHash = ethers.utils.keccak256(
ethers.utils.defaultAbiCoder.encode(
['address', 'bytes', 'uint', 'uint'],
[tx.to, tx.data, tx.gas, 4] // Rinkeby chainId is 4
)
);
return await signer.signMessage(ethers.utils.arrayify(relayTransactionHash));
}

const wait = milliseconds => {
return new Promise(resolve => setTimeout(resolve, milliseconds));
const wait = (milliseconds) => {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
};

// This function waits for an ITX job to be successfully mined
async function waitTransaction(itx, relayTransactionHash) {
let mined = false;

while (!mined) {
const statusResponse = await itx.send('relay_getTransactionStatus', [relayTransactionHash]);

for (let i = 0; i < statusResponse.length; i++) {
const hashes = statusResponse[i];
const receipt = await itx.getTransactionReceipt(hashes['ethTxHash']);
if (receipt && receipt.confirmations && receipt.confirmations > 1) {
mined = true;
return receipt;
}
}
await wait(1000);
}
}

async function main() {
// Make sure we're using the right network
if (process.env.ETHEREUM_NETWORK !== 'rinkeby') {
Expand All @@ -55,10 +23,7 @@ async function main() {
);

// Create a signing account from a private key
const signer = new ethers.Wallet(
'0xc5e8f61d1ab959b397eecc0a37a6517b8e67a0e7cf1f4bce5591f3ed80199122',
itx
);
const signer = new ethers.Wallet(process.env.SIGNER_PRIVATE_KEY, itx);

// Create a contract interface
const iface = new ethers.utils.Interface(abi);
Expand All @@ -73,20 +38,47 @@ async function main() {
gas: '100000',
};

// Sign the relay request
const signature = await signRequest(signer, tx);
// Sign a relay request using the signer's private key
// Final signature of the form keccak256("\x19Ethereum Signed Message:\n" + len((to + data + gas + chainId)) + (to + data + gas + chainId)))
// Where (to + data + gas + chainId) represents the RLP encoded concatenation of these fields.
// ITX will check the from address of this signature and deduct balance according to the gas used by the transaction
const relayTransactionHash = ethers.utils.keccak256(
ethers.utils.defaultAbiCoder.encode(
['address', 'bytes', 'uint', 'uint'],
[tx.to, tx.data, tx.gas, 4] // Rinkeby chainId is 4
)
);
const signature = await signer.signMessage(ethers.utils.arrayify(relayTransactionHash));

// Relay the transaction through ITX
const relayTransactionHash = await itx.send('relay_sendTransaction', [tx, signature]);
console.log(`ITX relay transaction hash: ${relayTransactionHash}`);

// Waiting for the corresponding Ethereum transaction to be mined
// We poll the relay_getTransactionStatus method for status updates
// ITX bumps the gas price of your transaction until it's mined,
// causing a new transaction hash to be created each time it happens.
// relay_getTransactionStatus returns a list of these transaction hashes
// which can then be used to poll Infura for their transaction receipts
console.log('Waiting to be mined...');
const receipt = await waitTransaction(itx, relayTransactionHash);
while (true) {
// fetch the latest ethereum transaction hashes
const statusResponse = await itx.send('relay_getTransactionStatus', [relayTransactionHash]);

// The transaction is now on chain!
console.log(`Ethereum transaction hash: ${receipt.transactionHash}`);
console.log(`Mined in block ${receipt.blockNumber}`);
// check each of these hashes to see if their receipt exists and
// has confirmations
for (let i = 0; i < statusResponse.length; i++) {
const hashes = statusResponse[i];
const receipt = await itx.getTransactionReceipt(hashes['ethTxHash']);
if (receipt && receipt.confirmations && receipt.confirmations > 1) {
// The transaction is now on chain!
console.log(`Ethereum transaction hash: ${receipt.transactionHash}`);
console.log(`Mined in block ${receipt.blockNumber}`);
return;
}
}
await wait(1000);
}
}

require('dotenv').config();
Expand Down
3 changes: 2 additions & 1 deletion infura-transactions/deposit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ async function main() {
// Send Ether to the ITX deposit contract
// ITX will register the deposit after 10 confirmations
// and credit the gas tank associated with your signer address
// you can view your balance at any time by calling relay_getBalance
const depositTx = await signer.sendTransaction({
// Rinkeby address of the ITX deposit contract
// Address of the ITX deposit contract
to: '0x015C7C7A7D65bbdb117C573007219107BD7486f9',
// The amount of ether you want to deposit in your ITX gas tank
value: ethers.utils.parseUnits('0.1', 'ether'),
Expand Down

0 comments on commit 36b6127

Please sign in to comment.