1
1
import fs from 'fs'
2
+ import path from 'path'
2
3
import * as dotenv from 'dotenv'
3
4
4
- import { utils , providers , Wallet } from 'ethers'
5
+ import { utils , BigNumber , BigNumberish , Signer } from 'ethers'
5
6
import ipfsHttpClient from 'ipfs-http-client'
6
7
import inquirer from 'inquirer'
7
8
@@ -15,6 +16,8 @@ import {
15
16
jsonToSubgraphMetadata ,
16
17
jsonToVersionMetadata ,
17
18
} from './metadata'
19
+ import { solidityKeccak256 } from 'ethers/lib/utils'
20
+ import { GraphToken } from '../build/types/GraphToken'
18
21
19
22
dotenv . config ( )
20
23
@@ -46,20 +49,24 @@ export class IPFS {
46
49
export const pinMetadataToIPFS = async (
47
50
ipfs : string ,
48
51
type : string ,
49
- path ?: string , // Only pass path or metadata, not both
52
+ filepath ?: string , // Only pass path or metadata, not both
50
53
metadata ?: SubgraphMetadata | VersionMetadata ,
51
54
) : Promise < string > => {
52
- if ( metadata == undefined && path != undefined ) {
55
+ if ( metadata == undefined && filepath != undefined ) {
53
56
if ( type == 'subgraph' ) {
54
- metadata = jsonToSubgraphMetadata ( JSON . parse ( fs . readFileSync ( __dirname + path ) . toString ( ) ) )
57
+ metadata = jsonToSubgraphMetadata (
58
+ JSON . parse ( fs . readFileSync ( path . join ( __dirname , filepath ) ) . toString ( ) ) ,
59
+ )
55
60
logger . info ( 'Meta data:' )
56
61
logger . info ( ` Subgraph Description: ${ metadata . description } ` )
57
62
logger . info ( `Subgraph Display Name: ${ metadata . displayName } ` )
58
63
logger . info ( ` Subgraph Image: ${ metadata . image } ` )
59
64
logger . info ( ` Subgraph Code Repository: ${ metadata . codeRepository } ` )
60
65
logger . info ( ` Subgraph Website: ${ metadata . website } ` )
61
66
} else if ( type == 'version' ) {
62
- metadata = jsonToVersionMetadata ( JSON . parse ( fs . readFileSync ( __dirname + path ) . toString ( ) ) )
67
+ metadata = jsonToVersionMetadata (
68
+ JSON . parse ( fs . readFileSync ( path . join ( __dirname , filepath ) ) . toString ( ) ) ,
69
+ )
63
70
logger . info ( 'Meta data:' )
64
71
logger . info ( ` Version Description: ${ metadata . description } ` )
65
72
logger . info ( ` Version Label: ${ metadata . label } ` )
@@ -104,3 +111,23 @@ export const confirm = async (message: string, skip: boolean): Promise<boolean>
104
111
}
105
112
return true
106
113
}
114
+
115
+ export const buildSubgraphID = ( account : string , seqID : BigNumber ) : string =>
116
+ solidityKeccak256 ( [ 'address' , 'uint256' ] , [ account , seqID ] )
117
+
118
+ export const ensureGRTAllowance = async (
119
+ owner : Signer ,
120
+ spender : string ,
121
+ amount : BigNumberish ,
122
+ grt : GraphToken ,
123
+ ) : Promise < void > => {
124
+ const ownerAddress = await owner . getAddress ( )
125
+ const allowance = await grt . allowance ( ownerAddress , spender )
126
+ const allowTokens = BigNumber . from ( amount ) . sub ( allowance )
127
+ if ( allowTokens . gt ( 0 ) ) {
128
+ console . log (
129
+ `\nApproving ${ spender } to spend ${ allowTokens } tokens on ${ ownerAddress } behalf...` ,
130
+ )
131
+ await grt . connect ( owner ) . approve ( spender , amount )
132
+ }
133
+ }
0 commit comments