-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ink): add typescript support for ink interactions (#752)
- Loading branch information
Showing
26 changed files
with
720 additions
and
53 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./add" | ||
export * from "./generate" | ||
export * from "./ink" | ||
export * from "./remove" | ||
export * from "./update" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { | ||
defaultConfig, | ||
papiFolder, | ||
readPapiConfig, | ||
writePapiConfig, | ||
} from "@/papiConfig" | ||
import { existsSync } from "node:fs" | ||
import * as fs from "node:fs/promises" | ||
import { join } from "node:path" | ||
import { CommonOptions } from "./commonOptions" | ||
import { generate } from "./generate" | ||
|
||
export interface InkAddOptions extends CommonOptions { | ||
key?: string | ||
} | ||
|
||
export const ink = { | ||
async add(file: string, options: InkAddOptions) { | ||
const metadata = JSON.parse(await fs.readFile(file, "utf-8")) | ||
// Remove wasm blob if it's there | ||
delete metadata.source?.wasm | ||
|
||
const key = options.key || metadata.contract.name | ||
const config = (await readPapiConfig(options.config)) ?? defaultConfig | ||
const inkConfig = (config.ink ||= {}) | ||
if (key in inkConfig) { | ||
console.warn(`Replacing existing ${key} config`) | ||
} | ||
|
||
const contractsFolder = join(papiFolder, "contracts") | ||
if (!existsSync(contractsFolder)) { | ||
await fs.mkdir(contractsFolder, { recursive: true }) | ||
} | ||
const fileName = join(contractsFolder, key + ".json") | ||
await fs.writeFile(fileName, JSON.stringify(metadata, null, 2)) | ||
|
||
inkConfig[key] = fileName | ||
await writePapiConfig(options.config, config) | ||
|
||
if (!options.skipCodegen) { | ||
generate({ | ||
config: options.config, | ||
}) | ||
} | ||
}, | ||
async remove(key: string, options: CommonOptions) { | ||
const config = (await readPapiConfig(options.config)) ?? defaultConfig | ||
const inkConfig = (config.ink ||= {}) | ||
if (!(key in inkConfig)) { | ||
console.log(`${key} contract not found in config`) | ||
return | ||
} | ||
|
||
const fileName = inkConfig[key] | ||
delete inkConfig[key] | ||
|
||
if (existsSync(fileName)) { | ||
await fs.rm(fileName) | ||
} | ||
|
||
await writePapiConfig(options.config, config) | ||
|
||
if (!options.skipCodegen) { | ||
generate({ | ||
config: options.config, | ||
}) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#!/usr/bin/env node | ||
|
||
import { getCli } from "./cli" | ||
import { add, generate, remove, update } from "./commands" | ||
import { add, generate, ink, remove, update } from "./commands" | ||
|
||
const program = getCli({ add, generate, remove, update }) | ||
const program = getCli({ add, generate, remove, update, ink }) | ||
program.parse() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "polkadot-api_ink", | ||
"types": "../dist/reexports/ink.d.ts", | ||
"module": "../dist/esm/reexports/ink.mjs", | ||
"import": "../dist/esm/reexports/ink.mjs", | ||
"browser": "../dist/esm/reexports/ink.mjs", | ||
"require": "../dist/reexports/ink.js", | ||
"default": "../dist/reexports/ink.js" | ||
} |
Oops, something went wrong.