Skip to content

Commit

Permalink
feat: add publish and mint to the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
abarmat committed Sep 27, 2022
1 parent 5e36583 commit 0ca43ff
Showing 1 changed file with 87 additions and 2 deletions.
89 changes: 87 additions & 2 deletions cli/commands/contracts/gns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { logger } from '../../logging'
import { sendTransaction } from '../../network'
import { loadEnv, CLIArgs, CLIEnvironment } from '../../env'
import { nameToNode } from './ens'
import { IPFS, pinMetadataToIPFS } from '../../helpers'
import { IPFS, pinMetadataToIPFS, buildSubgraphID, ensureGRTAllowance } from '../../helpers'

export const setDefaultName = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
const graphAccount = cliArgs.graphAccount
Expand Down Expand Up @@ -102,6 +102,44 @@ export const withdraw = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<v
await sendTransaction(cli.wallet, gns, 'withdraw', [subgraphID])
}

export const publishAndSignal = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
// parse args
const ipfs = cliArgs.ipfs
const subgraphDeploymentID = cliArgs.subgraphDeploymentID
const versionPath = cliArgs.versionPath
const subgraphPath = cliArgs.subgraphPath
const tokens = parseGRT(cliArgs.tokens)

// pin to IPFS
const subgraphDeploymentIDBytes = IPFS.ipfsHashToBytes32(subgraphDeploymentID)
const versionHashBytes = await pinMetadataToIPFS(ipfs, 'version', versionPath)
const subgraphHashBytes = await pinMetadataToIPFS(ipfs, 'subgraph', subgraphPath)

// craft transaction
const GNS = cli.contracts.GNS

// build publish tx
const publishTx = await GNS.populateTransaction.publishNewSubgraph(
subgraphDeploymentIDBytes,
versionHashBytes,
subgraphHashBytes,
)

// build mint tx
const subgraphID = buildSubgraphID(
cli.walletAddress,
await GNS.nextAccountSeqID(cli.walletAddress),
)
const mintTx = await GNS.populateTransaction.mintSignal(subgraphID, tokens, 0)

// ensure approval
await ensureGRTAllowance(cli.wallet, GNS.address, tokens, cli.contracts.GraphToken)

// send multicall transaction
logger.info(`Publishing and minting on new subgraph for ${cli.walletAddress}...`)
await sendTransaction(cli.wallet, GNS, 'multicall', [[publishTx.data, mintTx.data]])
}

export const gnsCommand = {
command: 'gns',
describe: 'GNS contract calls',
Expand Down Expand Up @@ -172,7 +210,7 @@ export const gnsCommand = {
})
.command({
command: 'publishNewVersion',
describe: 'Withdraw unlocked GRT',
describe: 'Publish a new subgraph version',
builder: (yargs: Argv) => {
return yargs
.option('subgraphID', {
Expand Down Expand Up @@ -307,6 +345,53 @@ export const gnsCommand = {
return withdraw(await loadEnv(argv), argv)
},
})
.command({
command: 'publishAndSignal',
describe: 'Publish a new subgraph and add initial signal',
builder: (yargs: Argv) => {
return yargs
.option('ipfs', {
description: 'ipfs endpoint. ex. https://api.thegraph.com/ipfs/',
type: 'string',
requiresArg: true,
demandOption: true,
})
.option('subgraphDeploymentID', {
description: 'subgraph deployment ID in base58',
type: 'string',
requiresArg: true,
demandOption: true,
})
.option('versionPath', {
description: ` filepath to metadata. With JSON format:\n
"description": "",
"label": ""`,
type: 'string',
requiresArg: true,
demandOption: true,
})
.option('subgraphPath', {
description: ` filepath to metadata. With JSON format:\n
"description": "",
"displayName": "",
"image": "",
"codeRepository": "",
"website": "",`,
type: 'string',
requiresArg: true,
demandOption: true,
})
.option('tokens', {
description: 'Amount of tokens to deposit',
type: 'string',
requiresArg: true,
demandOption: true,
})
},
handler: async (argv: CLIArgs): Promise<void> => {
return publishAndSignal(await loadEnv(argv), argv)
},
})
},
handler: (): void => {
yargs.showHelp()
Expand Down

0 comments on commit 0ca43ff

Please sign in to comment.