How to integrate Ethereum with Triangle

November 2, 2022

Overview

In this guide, we'll walk through how to setup Triangle and interact with the Ethereum blockchain.

Installation

To get started, you'll need to install the Triangle Node.js library:

npm install triangle

You may also use our REST API directly to make requests.

Authentication

Now that you have the Triangle Node.js library installed, you'll need to authenticate using an API secret key. You can view and manage your API keys in the Triangle Dashboard.

1
2
3
import Triangle from "triangle";

const triangle = new Triangle("secret_abc123");

Add an account

Let's go over what Accounts are in Triangle. An account is a way to reference a wallet that was created outside of Triangle. This gives you read-only controls to make it easy for you to request and receive data about any address on any blockchain.

Here is an example of how to create a reference to an address:

1
2
3
4
5
const account = await triangle.accounts.add({
  address: "0xA1B2c3d4e5F6A7B8c9D0E1F2A3b4C5D6E7f8A9B0",
  name: "My Account",
  network: "ethereum_goerli",
});

After creating a reference to an address, you may use the available methods to retrieve an account's balance, list an account's NFTs, list an account's tokens, and list an account's transactions.

Create a vault

Let's go over what Vaults are in Triangle. A vault is used to generate keys to create a group of wallets. You can think of this just like how Ledger or any other consumer wallet generates a single key that is used to create many wallets.

Typically, you'll create a vault during the user sign up flow of your app so that you can make the relationship between their account and this vault.

Here is an example of how to create a vault:

1
2
3
const vault = await triangle.vaults.create({
  name: "My Vault",
});

Create a wallet

Let's go over what Wallets are in Triangle. A wallet is an just like an externally-owned account (EOA). You can use it just like any other wallet to sign and send any transaction to the network.

Here is an example of how to create a wallet:

1
2
3
4
5
const wallet = await triangle.wallets.create({
  name: "My Ethereum Wallet",
  network: "ethereum_goerli",
  vault: vault.id,
});

Then, you can view the address of the wallet like this:

1
console.log(wallet.address); // 0xA1B2c3d4e5F6A7B8c9D0E1F2A3b4C5D6E7f8A9B0

After creating a wallet, you may use the available methods to retrieve a wallet's balance, send native currency, list a wallet's NFTs, send an NFT, list a wallet's tokens, and send a token.

Send an arbitrary transaction

In addition, you can sign/send any arbitrary transaction or sign arbitrary messages.

Here is an example of how to create an arbitrary transaction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { ethers } from "ethers";

const provider = ethers.getDefaultProvider("goerli");
const feeData = await provider.getFeeData();
const tx = {
  chainId: (await provider.getNetwork()).chainId,
  gasLimit: 21000,
  maxFeePerGas: feeData.maxFeePerGas,
  maxPriorityFeePerGas: feeData.maxPriorityFeePerGas,
  nonce: await provider.getTransactionCount(wallet.address, "latest"),
  to: "0xA1B2c3d4e5F6A7B8c9D0E1F2A3b4C5D6E7f8A9B0",
  type: 2,
  value: ethers.utils.parseEther("0.123"),
};

const serialized = ethers.utils.serializeTransaction(tx);
console.log(serialized); // 0x...

Then, you can take the serialized transaction and send it:

1
2
3
4
const transaction = await triangle.wallets.sendTransaction(
  wallet.id,
  { serialized }
);

Or, you can take the serialized transaction, sign it, and then send the raw transaction through your own means (ex. a relayer for gasless transactions):

1
2
3
4
5
6
7
8
9
10
let transaction = await triangle.wallets.signTransaction(
  wallet.id,
  { serialized }
);
console.log(transaction.raw); // 0x...

transaction = await triangle.wallets.sendRawTransaction(
  wallet.id,
  { transaction: transaction.id }
);

Here is an example of how to sign an arbitrary message:

1
2
3
4
const message = await triangle.wallets.signMessage(
  wallet.id,
  { message: "hello world" }
);

Conclusion

That's it!