From 0ca43ff536a8b80b1d8684ec93244456b4d4965b Mon Sep 17 00:00:00 2001 From: Ariel Barmat Date: Wed, 21 Sep 2022 12:04:35 -0300 Subject: [PATCH] feat: add publish and mint to the CLI --- cli/commands/contracts/gns.ts | 89 ++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/cli/commands/contracts/gns.ts b/cli/commands/contracts/gns.ts index 8d8ae94ee..89c71e92f 100644 --- a/cli/commands/contracts/gns.ts +++ b/cli/commands/contracts/gns.ts @@ -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 => { const graphAccount = cliArgs.graphAccount @@ -102,6 +102,44 @@ export const withdraw = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise => { + // 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', @@ -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', { @@ -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 => { + return publishAndSignal(await loadEnv(argv), argv) + }, + }) }, handler: (): void => { yargs.showHelp()