Skip to content

Commit

Permalink
fix: use path.join to manage path concat
Browse files Browse the repository at this point in the history
  • Loading branch information
abarmat committed Sep 27, 2022
1 parent 000810b commit 5e36583
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions cli/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fs from 'fs'
import path from 'path'
import * as dotenv from 'dotenv'

import { utils, providers, Wallet } from 'ethers'
import { utils, BigNumber, BigNumberish, Signer } from 'ethers'
import ipfsHttpClient from 'ipfs-http-client'
import inquirer from 'inquirer'

Expand All @@ -15,6 +16,8 @@ import {
jsonToSubgraphMetadata,
jsonToVersionMetadata,
} from './metadata'
import { solidityKeccak256 } from 'ethers/lib/utils'
import { GraphToken } from '../build/types/GraphToken'

dotenv.config()

Expand Down Expand Up @@ -46,20 +49,24 @@ export class IPFS {
export const pinMetadataToIPFS = async (
ipfs: string,
type: string,
path?: string, // Only pass path or metadata, not both
filepath?: string, // Only pass path or metadata, not both
metadata?: SubgraphMetadata | VersionMetadata,
): Promise<string> => {
if (metadata == undefined && path != undefined) {
if (metadata == undefined && filepath != undefined) {
if (type == 'subgraph') {
metadata = jsonToSubgraphMetadata(JSON.parse(fs.readFileSync(__dirname + path).toString()))
metadata = jsonToSubgraphMetadata(
JSON.parse(fs.readFileSync(path.join(__dirname, filepath)).toString()),
)
logger.info('Meta data:')
logger.info(` Subgraph Description: ${metadata.description}`)
logger.info(`Subgraph Display Name: ${metadata.displayName}`)
logger.info(` Subgraph Image: ${metadata.image}`)
logger.info(` Subgraph Code Repository: ${metadata.codeRepository}`)
logger.info(` Subgraph Website: ${metadata.website}`)
} else if (type == 'version') {
metadata = jsonToVersionMetadata(JSON.parse(fs.readFileSync(__dirname + path).toString()))
metadata = jsonToVersionMetadata(
JSON.parse(fs.readFileSync(path.join(__dirname, filepath)).toString()),
)
logger.info('Meta data:')
logger.info(` Version Description: ${metadata.description}`)
logger.info(` Version Label: ${metadata.label}`)
Expand Down Expand Up @@ -104,3 +111,23 @@ export const confirm = async (message: string, skip: boolean): Promise<boolean>
}
return true
}

export const buildSubgraphID = (account: string, seqID: BigNumber): string =>
solidityKeccak256(['address', 'uint256'], [account, seqID])

export const ensureGRTAllowance = async (
owner: Signer,
spender: string,
amount: BigNumberish,
grt: GraphToken,
): Promise<void> => {
const ownerAddress = await owner.getAddress()
const allowance = await grt.allowance(ownerAddress, spender)
const allowTokens = BigNumber.from(amount).sub(allowance)
if (allowTokens.gt(0)) {
console.log(
`\nApproving ${spender} to spend ${allowTokens} tokens on ${ownerAddress} behalf...`,
)
await grt.connect(owner).approve(spender, amount)
}
}

0 comments on commit 5e36583

Please sign in to comment.