Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(gre): add wallet methods #716

Merged
merged 3 commits into from
Sep 22, 2022
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
14 changes: 10 additions & 4 deletions gre/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,18 @@ Returns an object with all the named accounts available in the network. Named ac
'0xf1135bFF22512FF2A585b8d4489426CE660f204c'
```

The accounts are initialized from the graph config file but if the correct mnemonic or private key is provided via hardhat network configuration then they will be fully capable of signing transactions.
The accounts are initialized from the graph config file but if the correct mnemonic or private key is provided via hardhat network configuration then they will be fully capable of signing transactions. Accounts are already connected to the network provider.

**Account management: getTestAccounts**
Returns an object with accounts which can be used for testing/interacting with the protocol. These are obtained from hardhat's network configuration using the provided mnemonic or private key.
Returns an object with accounts which can be used for testing/interacting with the protocol. These are obtained from hardhat's network configuration using the provided mnemonic or private key. Accounts are already connected to the network provider.

**Account management: getDeployer**
Returns an object with the would-be deployer account. The deployer is by convention the first (index 0) account derived from the mnemonic or private key provided via hardhat network configuration.
Returns an object with the would-be deployer account. The deployer is by convention the first (index 0) account derived from the mnemonic or private key provided via hardhat network configuration. Deployer account is already connected to the network provider.

It's important to note that the deployer is not a named account as it's derived from the provided mnemonic so it won't necessarily match the actual deployer for a given deployment. It's the account that would be used to deploy the protocol with the current configuration. It's not possible at the moment to recover the actual deployer account from a deployed protocol.
It's important to note that the deployer is not a named account as it's derived from the provided mnemonic so it won't necessarily match the actual deployer for a given deployment. It's the account that would be used to deploy the protocol with the current configuration. It's not possible at the moment to recover the actual deployer account from a deployed protocol.

**Account management: getWallets**
Returns an object with wallets derived from the mnemonic or private key provided via hardhat network configuration. These wallets are not connected to a provider.

**Account management: getWallet**
Returns a wallet derived from the mnemonic or private key provided via hardhat network configuration that matches a given address. This wallet is not connected to a provider.
31 changes: 31 additions & 0 deletions gre/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { EthersProviderWrapper } from '@nomiclabs/hardhat-ethers/internal/ethers-provider-wrapper'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
import { derivePrivateKeys } from 'hardhat/internal/core/providers/util'
import { Wallet } from 'ethers'
import { getItemValue, readConfig } from '../cli/config'
import { AccountNames, NamedAccounts } from './type-extensions'
import { getNetworkName } from './config'
import { HttpNetworkHDAccountsConfig, NetworksConfig } from 'hardhat/types'

const namedAccountList: AccountNames[] = [
'arbitrator',
Expand Down Expand Up @@ -53,3 +57,30 @@ export async function getTestAccounts(
return !blacklist.includes(s.address)
})
}

export async function getWallets(
networks: NetworksConfig,
chainId: number,
mainNetworkName: string,
): Promise<Wallet[]> {
const networkName = getNetworkName(networks, chainId, mainNetworkName)
const accounts = networks[networkName].accounts
const mnemonic = (accounts as HttpNetworkHDAccountsConfig).mnemonic

if (mnemonic) {
const privateKeys = derivePrivateKeys(mnemonic, "m/44'/60'/0'/0/", 0, 20, '')
return privateKeys.map((privateKey) => new Wallet(privateKey))
}

return []
}

export async function getWallet(
networks: NetworksConfig,
chainId: number,
mainNetworkName: string,
address: string,
): Promise<Wallet | undefined> {
const wallets = await getWallets(networks, chainId, mainNetworkName)
return wallets.find((w) => w.address === address)
}
2 changes: 1 addition & 1 deletion gre/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ function getNetworkConfig(
}
}

function getNetworkName(
export function getNetworkName(
networks: NetworksConfig,
chainId: number,
mainNetworkName: string,
Expand Down
20 changes: 19 additions & 1 deletion gre/gre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import {
GraphRuntimeEnvironmentOptions,
} from './type-extensions'
import { getChains, getProviders, getAddressBookPath, getGraphConfigPaths } from './config'
import { getDeployer, getNamedAccounts, getTestAccounts } from './accounts'
import { getDeployer, getNamedAccounts, getTestAccounts, getWallet, getWallets } from './accounts'
import { logDebug, logWarn } from './logger'
import path from 'path'
import { EthersProviderWrapper } from '@nomiclabs/hardhat-ethers/internal/ethers-provider-wrapper'
import { Wallet } from 'ethers'

// Graph Runtime Environment (GRE) extensions for the HRE

Expand Down Expand Up @@ -53,12 +54,23 @@ extendEnvironment((hre: HardhatRuntimeEnvironment) => {
isHHL1,
)

// Wallet functions
const l1GetWallets = () => getWallets(hre.config.networks, l1ChainId, hre.network.name)
const l1GetWallet = (address: string) =>
getWallet(hre.config.networks, l1ChainId, hre.network.name, address)
const l2GetWallets = () => getWallets(hre.config.networks, l2ChainId, hre.network.name)
const l2GetWallet = (address: string) =>
getWallet(hre.config.networks, l2ChainId, hre.network.name, address)

// Build the Graph Runtime Environment (GRE)
const l1Graph: GraphNetworkEnvironment | null = buildGraphNetworkEnvironment(
l1ChainId,
l1Provider,
l1GraphConfigPath,
addressBookPath,
isHHL1,
l1GetWallets,
l1GetWallet,
)

const l2Graph: GraphNetworkEnvironment | null = buildGraphNetworkEnvironment(
Expand All @@ -67,6 +79,8 @@ extendEnvironment((hre: HardhatRuntimeEnvironment) => {
l2GraphConfigPath,
addressBookPath,
isHHL1,
l2GetWallets,
l2GetWallet,
)

const gre: GraphRuntimeEnvironment = {
Expand All @@ -88,6 +102,8 @@ function buildGraphNetworkEnvironment(
graphConfigPath: string | undefined,
addressBookPath: string,
isHHL1: boolean,
getWallets: () => Promise<Wallet[]>,
getWallet: (address: string) => Promise<Wallet>,
): GraphNetworkEnvironment | null {
if (graphConfigPath === undefined) {
logWarn(
Expand Down Expand Up @@ -116,5 +132,7 @@ function buildGraphNetworkEnvironment(
getDeployer: lazyFunction(() => () => getDeployer(provider)),
getNamedAccounts: lazyFunction(() => () => getNamedAccounts(provider, graphConfigPath)),
getTestAccounts: lazyFunction(() => () => getTestAccounts(provider, graphConfigPath)),
getWallets: lazyFunction(() => () => getWallets()),
getWallet: lazyFunction(() => (address: string) => getWallet(address)),
}
}
85 changes: 85 additions & 0 deletions gre/test/accounts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import chai, { expect } from 'chai'
import chaiAsPromised from 'chai-as-promised'
import { ethers } from 'ethers'
import { GraphRuntimeEnvironment } from '../type-extensions'
import { useEnvironment } from './helpers'

chai.use(chaiAsPromised)

const mnemonic = 'pumpkin orient can short never warm truth legend cereal tourist craft skin'

describe('GRE usage > account management', function () {
// Tests that loop through all the wallets take more than the default timeout
this.timeout(10_000)

useEnvironment('graph-config', 'hardhat')

let graph: GraphRuntimeEnvironment

beforeEach(function () {
graph = this.hre.graph()
})

describe('getWallets', function () {
it('should return 20 wallets', async function () {
const wallets = await graph.getWallets()
expect(wallets.length).to.equal(20)
})

it('should derive wallets from hardhat config mnemonic', async function () {
const wallets = await graph.getWallets()

for (let i = 0; i < wallets.length; i++) {
const derived = ethers.Wallet.fromMnemonic(mnemonic, `m/44'/60'/0'/0/${i}`)
expect(wallets[i].address).to.equal(derived.address)
}
})

it('should return wallets capable of signing messages', async function () {
const wallets = await graph.getWallets()

for (const wallet of wallets) {
expect(wallet.signMessage('test')).to.eventually.be.fulfilled
}
})

it('should return wallets not connected to a provider', async function () {
const wallets = await graph.getWallets()

for (const wallet of wallets) {
expect(wallet.provider).to.be.null
}
})
})

describe('getWallet', function () {
it('should return wallet if provided address can be derived from mnemonic', async function () {
for (let i = 0; i < 20; i++) {
const derived = ethers.Wallet.fromMnemonic(mnemonic, `m/44'/60'/0'/0/${i}`)
const wallet = await graph.getWallet(derived.address)
expect(wallet.address).to.equal(derived.address)
}
})

it('should return wallet capable of signing messages', async function () {
for (let i = 0; i < 20; i++) {
const derived = ethers.Wallet.fromMnemonic(mnemonic, `m/44'/60'/0'/0/${i}`)
const wallet = await graph.getWallet(derived.address)
expect(wallet.signMessage('test')).to.eventually.be.fulfilled
}
})

it('should return wallet not connected to a provider', async function () {
for (let i = 0; i < 20; i++) {
const derived = ethers.Wallet.fromMnemonic(mnemonic, `m/44'/60'/0'/0/${i}`)
const wallet = await graph.getWallet(derived.address)
expect(wallet.provider).to.be.null
}
})

it('should return undefined if provided address cant be derived from mnemonic', async function () {
const wallet = await graph.getWallet('0x0000000000000000000000000000000000000000')
expect(wallet).to.be.undefined
})
})
})
3 changes: 3 additions & 0 deletions gre/test/fixture-projects/graph-config/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module.exports = {
networks: {
hardhat: {
chainId: 1337,
accounts: {
mnemonic: 'pumpkin orient can short never warm truth legend cereal tourist craft skin',
},
},
mainnet: {
chainId: 1,
Expand Down
3 changes: 3 additions & 0 deletions gre/type-extensions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AddressBook } from '../cli/address-book'
import { NetworkContracts } from '../cli/contracts'

import { EthersProviderWrapper } from '@nomiclabs/hardhat-ethers/internal/ethers-provider-wrapper'
import { Wallet } from 'ethers'

export interface GraphRuntimeEnvironmentOptions {
addressBook?: string
Expand Down Expand Up @@ -32,6 +33,8 @@ export interface GraphNetworkEnvironment {
getNamedAccounts: () => Promise<NamedAccounts>
getTestAccounts: () => Promise<SignerWithAddress[]>
getDeployer: () => Promise<SignerWithAddress>
getWallets: () => Promise<Wallet[]>
getWallet: (address: string) => Promise<Wallet>
}

export interface GraphRuntimeEnvironment extends GraphNetworkEnvironment {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@typechain/ethers-v5": "^7.0.0",
"@typechain/hardhat": "^2.0.0",
"@types/bs58": "^4.0.1",
"@types/chai-as-promised": "^7.1.5",
"@types/dotenv": "^8.2.0",
"@types/glob": "^7.2.0",
"@types/inquirer": "^7.3.1",
Expand All @@ -42,6 +43,7 @@
"@urql/core": "^2.1.3",
"bignumber.js": "^9.0.0",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"cli-table": "^0.3.6",
"dotenv": "^9.0.0",
"eslint": "^7.24.0",
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,13 @@
dependencies:
base-x "^3.0.6"

"@types/chai-as-promised@^7.1.5":
version "7.1.5"
resolved "https://registry.yarnpkg.com/@types/chai-as-promised/-/chai-as-promised-7.1.5.tgz#6e016811f6c7a64f2eed823191c3a6955094e255"
integrity sha512-jStwss93SITGBwt/niYrkf2C+/1KTeZCZl1LaeezTlqppAKeoQC7jxyqYuP72sxBGKCIbw7oHgbYssIRzT5FCQ==
dependencies:
"@types/chai" "*"

"@types/chai@*":
version "4.3.1"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.3.1.tgz#e2c6e73e0bdeb2521d00756d099218e9f5d90a04"
Expand Down Expand Up @@ -3320,6 +3327,13 @@ cbor@^8.0.0:
dependencies:
nofilter "^3.1.0"

chai-as-promised@^7.1.1:
version "7.1.1"
resolved "https://registry.yarnpkg.com/chai-as-promised/-/chai-as-promised-7.1.1.tgz#08645d825deb8696ee61725dbf590c012eb00ca0"
integrity sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA==
dependencies:
check-error "^1.0.2"

chai@^4.3.4:
version "4.3.6"
resolved "https://registry.yarnpkg.com/chai/-/chai-4.3.6.tgz#ffe4ba2d9fa9d6680cc0b370adae709ec9011e9c"
Expand Down