@sentio/sdk 1.7.14 → 1.7.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{base-processor-template.d.ts → lib/base-processor-template.d.ts} +0 -0
- package/{base-processor-template.js → lib/base-processor-template.js} +0 -0
- package/{base-processor-template.js.map → lib/base-processor-template.js.map} +0 -0
- package/{base-processor.d.ts → lib/base-processor.d.ts} +0 -0
- package/{base-processor.js → lib/base-processor.js} +0 -0
- package/{base-processor.js.map → lib/base-processor.js.map} +0 -0
- package/{bind-options.d.ts → lib/bind-options.d.ts} +0 -0
- package/{bind-options.js → lib/bind-options.js} +0 -0
- package/{bind-options.js.map → lib/bind-options.js.map} +0 -0
- package/{binds.d.ts → lib/binds.d.ts} +0 -0
- package/{binds.js → lib/binds.js} +0 -0
- package/{binds.js.map → lib/binds.js.map} +0 -0
- package/{builtin → lib/builtin}/erc20/index.d.ts +0 -0
- package/{builtin → lib/builtin}/erc20/index.js +0 -0
- package/{builtin → lib/builtin}/erc20/index.js.map +0 -0
- package/{builtin → lib/builtin}/erc20/test-utils.d.ts +0 -0
- package/{builtin → lib/builtin}/erc20/test-utils.js +0 -0
- package/{builtin → lib/builtin}/erc20/test-utils.js.map +0 -0
- package/{builtin → lib/builtin}/internal/Erc20.d.ts +0 -0
- package/{builtin → lib/builtin}/internal/Erc20.js +0 -0
- package/{builtin → lib/builtin}/internal/Erc20.js.map +0 -0
- package/lib/builtin/internal/Weth9.d.ts +221 -0
- package/lib/builtin/internal/Weth9.js +3 -0
- package/lib/builtin/internal/Weth9.js.map +1 -0
- package/{builtin → lib/builtin}/internal/common.d.ts +0 -0
- package/{builtin → lib/builtin}/internal/common.js +0 -0
- package/{builtin → lib/builtin}/internal/common.js.map +0 -0
- package/{builtin → lib/builtin}/internal/erc20_processor.d.ts +0 -0
- package/{builtin → lib/builtin}/internal/erc20_processor.js +0 -0
- package/{builtin → lib/builtin}/internal/erc20_processor.js.map +0 -0
- package/{builtin → lib/builtin}/internal/factories/Erc20__factory.d.ts +0 -0
- package/{builtin → lib/builtin}/internal/factories/Erc20__factory.js +0 -0
- package/{builtin → lib/builtin}/internal/factories/Erc20__factory.js.map +0 -0
- package/lib/builtin/internal/factories/Weth9__factory.d.ts +45 -0
- package/lib/builtin/internal/factories/Weth9__factory.js +297 -0
- package/lib/builtin/internal/factories/Weth9__factory.js.map +1 -0
- package/{builtin → lib/builtin}/internal/factories/index.d.ts +1 -0
- package/{builtin → lib/builtin}/internal/factories/index.js +3 -1
- package/lib/builtin/internal/factories/index.js.map +1 -0
- package/{builtin → lib/builtin}/internal/index.d.ts +2 -0
- package/{builtin → lib/builtin}/internal/index.js +3 -1
- package/lib/builtin/internal/index.js.map +1 -0
- package/lib/builtin/internal/weth9_processor.d.ts +50 -0
- package/lib/builtin/internal/weth9_processor.js +295 -0
- package/lib/builtin/internal/weth9_processor.js.map +1 -0
- package/lib/builtin/weth9/index.d.ts +2 -0
- package/lib/builtin/weth9/index.js +22 -0
- package/lib/builtin/weth9/index.js.map +1 -0
- package/lib/builtin/weth9/test-utils.d.ts +6 -0
- package/lib/builtin/weth9/test-utils.js +57 -0
- package/lib/builtin/weth9/test-utils.js.map +1 -0
- package/{chain-config.d.ts → lib/chain-config.d.ts} +0 -0
- package/{chain-config.js → lib/chain-config.js} +0 -0
- package/{chain-config.js.map → lib/chain-config.js.map} +0 -0
- package/{cli → lib/cli}/build.d.ts +0 -0
- package/{cli → lib/cli}/build.js +14 -0
- package/lib/cli/build.js.map +1 -0
- package/{cli → lib/cli}/cli.d.ts +0 -0
- package/{cli → lib/cli}/cli.js +0 -0
- package/{cli → lib/cli}/cli.js.map +0 -0
- package/{cli → lib/cli}/config.d.ts +0 -0
- package/{cli → lib/cli}/config.js +0 -0
- package/{cli → lib/cli}/config.js.map +0 -0
- package/{cli → lib/cli}/key.d.ts +0 -0
- package/{cli → lib/cli}/key.js +0 -0
- package/{cli → lib/cli}/key.js.map +0 -0
- package/{cli → lib/cli}/solana-code-gen.d.ts +0 -0
- package/{cli → lib/cli}/solana-code-gen.js +0 -0
- package/{cli → lib/cli}/solana-code-gen.js.map +0 -0
- package/{cli → lib/cli}/upload.d.ts +0 -0
- package/{cli → lib/cli}/upload.js +0 -0
- package/{cli → lib/cli}/upload.js.map +0 -0
- package/{cli → lib/cli}/webpack.config.js +0 -0
- package/{context.d.ts → lib/context.d.ts} +0 -0
- package/{context.js → lib/context.js} +0 -0
- package/{context.js.map → lib/context.js.map} +0 -0
- package/{contract-namer.d.ts → lib/contract-namer.d.ts} +0 -0
- package/{contract-namer.js → lib/contract-namer.js} +0 -0
- package/{contract-namer.js.map → lib/contract-namer.js.map} +0 -0
- package/{error.d.ts → lib/error.d.ts} +0 -0
- package/{error.js → lib/error.js} +0 -0
- package/{error.js.map → lib/error.js.map} +0 -0
- package/{gen → lib/gen}/builtin.d.ts +0 -0
- package/{gen → lib/gen}/builtin.js +0 -0
- package/{gen → lib/gen}/builtin.js.map +0 -0
- package/{gen → lib/gen}/google/protobuf/empty.d.ts +0 -0
- package/{gen → lib/gen}/google/protobuf/empty.js +0 -0
- package/{gen → lib/gen}/google/protobuf/empty.js.map +0 -0
- package/{gen → lib/gen}/processor/protos/processor.d.ts +0 -0
- package/{gen → lib/gen}/processor/protos/processor.js +0 -0
- package/{gen → lib/gen}/processor/protos/processor.js.map +0 -0
- package/{generic-processor.d.ts → lib/generic-processor.d.ts} +0 -0
- package/{generic-processor.js → lib/generic-processor.js} +0 -0
- package/{generic-processor.js.map → lib/generic-processor.js.map} +0 -0
- package/{index.d.ts → lib/index.d.ts} +0 -0
- package/{index.js → lib/index.js} +0 -0
- package/{index.js.map → lib/index.js.map} +0 -0
- package/lib/loader.d.ts +5 -0
- package/lib/loader.js +30 -0
- package/lib/loader.js.map +1 -0
- package/{meter.d.ts → lib/meter.d.ts} +0 -0
- package/{meter.js → lib/meter.js} +0 -0
- package/{meter.js.map → lib/meter.js.map} +0 -0
- package/{numberish.d.ts → lib/numberish.d.ts} +0 -0
- package/{numberish.js → lib/numberish.js} +0 -0
- package/{numberish.js.map → lib/numberish.js.map} +0 -0
- package/{numberish.test.d.ts → lib/numberish.test.d.ts} +0 -0
- package/{numberish.test.js → lib/numberish.test.js} +0 -0
- package/{numberish.test.js.map → lib/numberish.test.js.map} +0 -0
- package/{processor-runner.d.ts → lib/processor-runner.d.ts} +0 -0
- package/{processor-runner.js → lib/processor-runner.js} +2 -27
- package/lib/processor-runner.js.map +1 -0
- package/{processor-state.d.ts → lib/processor-state.d.ts} +0 -0
- package/{processor-state.js → lib/processor-state.js} +0 -0
- package/{processor-state.js.map → lib/processor-state.js.map} +0 -0
- package/{promise-or-void.d.ts → lib/promise-or-void.d.ts} +0 -0
- package/{promise-or-void.js → lib/promise-or-void.js} +0 -0
- package/{promise-or-void.js.map → lib/promise-or-void.js.map} +0 -0
- package/{provider.d.ts → lib/provider.d.ts} +0 -0
- package/{provider.js → lib/provider.js} +0 -0
- package/{provider.js.map → lib/provider.js.map} +0 -0
- package/{release.config.js → lib/release.config.js} +0 -0
- package/{service.d.ts → lib/service.d.ts} +2 -1
- package/{service.js → lib/service.js} +6 -3
- package/lib/service.js.map +1 -0
- package/{solana → lib/solana}/builtin/index.d.ts +0 -0
- package/{solana → lib/solana}/builtin/index.js +0 -0
- package/{solana → lib/solana}/builtin/index.js.map +0 -0
- package/{solana → lib/solana}/builtin/spl-token-processor.d.ts +0 -0
- package/{solana → lib/solana}/builtin/spl-token-processor.js +0 -0
- package/{solana → lib/solana}/builtin/spl-token-processor.js.map +0 -0
- package/{solana → lib/solana}/builtin/types.d.ts +0 -0
- package/{solana → lib/solana}/builtin/types.js +0 -0
- package/{solana → lib/solana}/builtin/types.js.map +0 -0
- package/{solana → lib/solana}/builtin/wormhole-processor.d.ts +0 -0
- package/{solana → lib/solana}/builtin/wormhole-processor.js +0 -0
- package/{solana → lib/solana}/builtin/wormhole-processor.js.map +0 -0
- package/{solana-processor.d.ts → lib/solana-processor.d.ts} +0 -0
- package/{solana-processor.js → lib/solana-processor.js} +0 -0
- package/{solana-processor.js.map → lib/solana-processor.js.map} +0 -0
- package/{target-ethers-sentio → lib/target-ethers-sentio}/codegen.d.ts +0 -0
- package/{target-ethers-sentio → lib/target-ethers-sentio}/codegen.js +0 -0
- package/lib/target-ethers-sentio/codegen.js.map +1 -0
- package/{target-ethers-sentio → lib/target-ethers-sentio}/index.d.ts +0 -0
- package/{target-ethers-sentio → lib/target-ethers-sentio}/index.js +0 -0
- package/lib/target-ethers-sentio/index.js.map +1 -0
- package/{test → lib/test}/codegen.test.d.ts +0 -0
- package/{test → lib/test}/codegen.test.js +1 -1
- package/{test → lib/test}/codegen.test.js.map +1 -1
- package/{test → lib/test}/erc20-template.d.ts +0 -0
- package/{test → lib/test}/erc20-template.js +0 -0
- package/{test → lib/test}/erc20-template.js.map +0 -0
- package/{test → lib/test}/erc20-template.test.d.ts +0 -0
- package/{test → lib/test}/erc20-template.test.js +1 -3
- package/lib/test/erc20-template.test.js.map +1 -0
- package/{test → lib/test}/erc20.d.ts +0 -0
- package/{test → lib/test}/erc20.js +0 -0
- package/{test → lib/test}/erc20.js.map +0 -0
- package/{test → lib/test}/erc20.test.d.ts +0 -0
- package/{test → lib/test}/erc20.test.js +1 -3
- package/lib/test/erc20.test.js.map +1 -0
- package/{test → lib/test}/generic-processor.test.d.ts +0 -0
- package/{test → lib/test}/generic-processor.test.js +3 -3
- package/lib/test/generic-processor.test.js.map +1 -0
- package/{test → lib/test}/index.d.ts +0 -0
- package/{test → lib/test}/index.js +0 -0
- package/{test → lib/test}/index.js.map +0 -0
- package/{test → lib/test}/metric-utils.d.ts +0 -0
- package/{test → lib/test}/metric-utils.js +0 -0
- package/{test → lib/test}/metric-utils.js.map +0 -0
- package/{test → lib/test}/mirrorworld.d.ts +0 -0
- package/{test → lib/test}/mirrorworld.js +0 -0
- package/{test → lib/test}/mirrorworld.js.map +0 -0
- package/{test → lib/test}/solana.test.d.ts +0 -0
- package/{test → lib/test}/solana.test.js +3 -3
- package/lib/test/solana.test.js.map +1 -0
- package/{test → lib/test}/test-processor-server.d.ts +1 -1
- package/{test → lib/test}/test-processor-server.js +3 -8
- package/lib/test/test-processor-server.js.map +1 -0
- package/{test → lib/test}/types/game_wallet.d.ts +0 -0
- package/{test → lib/test}/types/game_wallet.js +0 -0
- package/{test → lib/test}/types/game_wallet.js.map +0 -0
- package/{test → lib/test}/types/game_wallet_processor.d.ts +0 -0
- package/{test → lib/test}/types/game_wallet_processor.js +0 -0
- package/{test → lib/test}/types/game_wallet_processor.js.map +0 -0
- package/{test → lib/test}/wormhole-token-bridge.d.ts +0 -0
- package/{test → lib/test}/wormhole-token-bridge.js +0 -0
- package/{test → lib/test}/wormhole-token-bridge.js.map +0 -0
- package/{utils → lib/utils}/chainmap.d.ts +0 -0
- package/{utils → lib/utils}/chainmap.js +0 -0
- package/{utils → lib/utils}/chainmap.js.map +0 -0
- package/{utils → lib/utils}/convert.d.ts +0 -0
- package/{utils → lib/utils}/convert.js +0 -0
- package/{utils → lib/utils}/convert.js.map +0 -0
- package/{utils → lib/utils}/index.d.ts +0 -0
- package/{utils → lib/utils}/index.js +0 -0
- package/{utils → lib/utils}/index.js.map +0 -0
- package/package.json +47 -6
- package/src/abis/erc20.json +198 -0
- package/src/abis/weth9.json +153 -0
- package/src/base-processor-template.ts +92 -0
- package/src/base-processor.ts +128 -0
- package/src/bind-options.ts +41 -0
- package/src/binds.ts +73 -0
- package/src/builtin/erc20/index.ts +6 -0
- package/src/builtin/erc20/test-utils.ts +49 -0
- package/src/builtin/internal/Erc20.ts +356 -0
- package/src/builtin/internal/Weth9.ts +480 -0
- package/src/builtin/internal/common.ts +46 -0
- package/src/builtin/internal/erc20_processor.ts +295 -0
- package/src/builtin/internal/factories/Erc20__factory.ts +216 -0
- package/src/builtin/internal/factories/Weth9__factory.ts +297 -0
- package/src/builtin/internal/factories/index.ts +5 -0
- package/src/builtin/internal/index.ts +8 -0
- package/src/builtin/internal/weth9_processor.ts +403 -0
- package/src/builtin/weth9/index.ts +6 -0
- package/src/builtin/weth9/test-utils.ts +89 -0
- package/src/chain-config.ts +6 -0
- package/src/cli/build.ts +196 -0
- package/src/cli/cli.ts +217 -0
- package/src/cli/config.ts +43 -0
- package/src/cli/key.ts +43 -0
- package/src/cli/solana-code-gen.ts +16 -0
- package/src/cli/upload.ts +86 -0
- package/src/cli/webpack.config.js +42 -0
- package/src/context.ts +93 -0
- package/src/contract-namer.ts +17 -0
- package/src/error.ts +43 -0
- package/src/gen/builtin.ts +22 -0
- package/src/gen/google/protobuf/empty.ts +70 -0
- package/src/gen/processor/protos/processor.ts +3071 -0
- package/src/generic-processor.ts +38 -0
- package/src/index.ts +19 -0
- package/src/loader.ts +24 -0
- package/src/meter.ts +122 -0
- package/src/numberish.test.ts +82 -0
- package/src/numberish.ts +99 -0
- package/src/processor-runner.ts +49 -0
- package/src/processor-state.ts +24 -0
- package/src/promise-or-void.ts +1 -0
- package/src/provider.ts +74 -0
- package/src/service.ts +439 -0
- package/src/solana/builtin/index.ts +2 -0
- package/src/solana/builtin/spl-token-processor.ts +164 -0
- package/src/solana/builtin/types.ts +268 -0
- package/src/solana/builtin/wormhole-processor.ts +178 -0
- package/src/solana-processor.ts +105 -0
- package/src/target-ethers-sentio/codegen.ts +289 -0
- package/src/target-ethers-sentio/index.ts +50 -0
- package/src/target-ethers-sentio/tsconfig.json +9 -0
- package/src/test/abis/evm/anyswapRouter.json +490 -0
- package/src/test/abis/solana/mirrorworld.json +1392 -0
- package/src/test/codegen.test.ts +26 -0
- package/src/test/erc20-template.test.ts +37 -0
- package/src/test/erc20-template.ts +25 -0
- package/src/test/erc20.test.ts +90 -0
- package/src/test/erc20.ts +48 -0
- package/src/test/generic-processor.test.ts +70 -0
- package/src/test/index.ts +1 -0
- package/src/test/metric-utils.ts +49 -0
- package/src/test/mirrorworld.ts +40 -0
- package/src/test/sentio.yaml +4 -0
- package/src/test/solana.test.ts +93 -0
- package/src/test/test-processor-server.ts +205 -0
- package/src/test/types/game_wallet.ts +1392 -0
- package/src/test/types/game_wallet_processor.ts +113 -0
- package/src/test/wormhole-token-bridge.ts +21 -0
- package/src/types/global.d.ts +17 -0
- package/src/utils/chainmap.ts +95 -0
- package/src/utils/convert.ts +6 -0
- package/src/utils/index.ts +2 -0
- package/builtin/internal/factories/index.js.map +0 -1
- package/builtin/internal/index.js.map +0 -1
- package/cli/build.js.map +0 -1
- package/processor-runner.js.map +0 -1
- package/service.js.map +0 -1
- package/target-ethers-sentio/codegen.js.map +0 -1
- package/target-ethers-sentio/index.js.map +0 -1
- package/test/erc20-template.test.js.map +0 -1
- package/test/erc20.test.js.map +0 -1
- package/test/generic-processor.test.js.map +0 -1
- package/test/solana.test.js.map +0 -1
- package/test/test-processor-server.js.map +0 -1
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/* Autogenerated file. Do not edit manually. */
|
|
2
|
+
/* tslint:disable */
|
|
3
|
+
/* eslint-disable */
|
|
4
|
+
import { Log } from "@ethersproject/providers";
|
|
5
|
+
import {
|
|
6
|
+
getWeth9Contract,
|
|
7
|
+
ApprovalEventObject,
|
|
8
|
+
TransferEventObject,
|
|
9
|
+
DepositEventObject,
|
|
10
|
+
WithdrawalEventObject,
|
|
11
|
+
} from ".";
|
|
12
|
+
const mockField = {
|
|
13
|
+
blockHash:
|
|
14
|
+
"0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
15
|
+
blockNumber: 0,
|
|
16
|
+
logIndex: 0,
|
|
17
|
+
removed: false,
|
|
18
|
+
transactionHash:
|
|
19
|
+
"0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
20
|
+
transactionIndex: 0,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export function mockApprovalLog(
|
|
24
|
+
contractAddress: string,
|
|
25
|
+
event: ApprovalEventObject
|
|
26
|
+
): Log {
|
|
27
|
+
const contract = getWeth9Contract(contractAddress);
|
|
28
|
+
const encodedLog = contract.rawContract.interface.encodeEventLog(
|
|
29
|
+
contract.rawContract.interface.getEvent("Approval"),
|
|
30
|
+
Object.values(event)
|
|
31
|
+
);
|
|
32
|
+
return {
|
|
33
|
+
...mockField,
|
|
34
|
+
address: contractAddress,
|
|
35
|
+
data: encodedLog.data,
|
|
36
|
+
topics: encodedLog.topics,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function mockTransferLog(
|
|
41
|
+
contractAddress: string,
|
|
42
|
+
event: TransferEventObject
|
|
43
|
+
): Log {
|
|
44
|
+
const contract = getWeth9Contract(contractAddress);
|
|
45
|
+
const encodedLog = contract.rawContract.interface.encodeEventLog(
|
|
46
|
+
contract.rawContract.interface.getEvent("Transfer"),
|
|
47
|
+
Object.values(event)
|
|
48
|
+
);
|
|
49
|
+
return {
|
|
50
|
+
...mockField,
|
|
51
|
+
address: contractAddress,
|
|
52
|
+
data: encodedLog.data,
|
|
53
|
+
topics: encodedLog.topics,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function mockDepositLog(
|
|
58
|
+
contractAddress: string,
|
|
59
|
+
event: DepositEventObject
|
|
60
|
+
): Log {
|
|
61
|
+
const contract = getWeth9Contract(contractAddress);
|
|
62
|
+
const encodedLog = contract.rawContract.interface.encodeEventLog(
|
|
63
|
+
contract.rawContract.interface.getEvent("Deposit"),
|
|
64
|
+
Object.values(event)
|
|
65
|
+
);
|
|
66
|
+
return {
|
|
67
|
+
...mockField,
|
|
68
|
+
address: contractAddress,
|
|
69
|
+
data: encodedLog.data,
|
|
70
|
+
topics: encodedLog.topics,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function mockWithdrawalLog(
|
|
75
|
+
contractAddress: string,
|
|
76
|
+
event: WithdrawalEventObject
|
|
77
|
+
): Log {
|
|
78
|
+
const contract = getWeth9Contract(contractAddress);
|
|
79
|
+
const encodedLog = contract.rawContract.interface.encodeEventLog(
|
|
80
|
+
contract.rawContract.interface.getEvent("Withdrawal"),
|
|
81
|
+
Object.values(event)
|
|
82
|
+
);
|
|
83
|
+
return {
|
|
84
|
+
...mockField,
|
|
85
|
+
address: contractAddress,
|
|
86
|
+
data: encodedLog.data,
|
|
87
|
+
topics: encodedLog.topics,
|
|
88
|
+
};
|
|
89
|
+
}
|
package/src/cli/build.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import chalk from 'chalk'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
import fs from 'fs'
|
|
4
|
+
import { exec } from 'child_process'
|
|
5
|
+
import { EVM, SOLANA, Target } from './config'
|
|
6
|
+
|
|
7
|
+
export async function buildProcessor(onlyGen: boolean, targets: Target[]) {
|
|
8
|
+
if (!onlyGen) {
|
|
9
|
+
await installDeps()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// targets.forEach(async (target) => await buildProcessorForTarget(onlyGen, target))
|
|
13
|
+
for (const target of targets) {
|
|
14
|
+
await buildProcessorForTarget(onlyGen, target)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!onlyGen) {
|
|
18
|
+
const WEBPACK_CONFIG = path.join(__dirname, 'webpack.config.js')
|
|
19
|
+
await execStep('yarn tsc -p .', 'Compile')
|
|
20
|
+
await execStep('yarn webpack --config=' + WEBPACK_CONFIG, 'Packaging')
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function buildProcessorForTarget(onlyGen: boolean, target: Target) {
|
|
25
|
+
if (target.chain === EVM) {
|
|
26
|
+
await codeGenEthersProcessor(target.abisDir ?? path.join('abis', 'evm'))
|
|
27
|
+
} else if (target.chain === SOLANA) {
|
|
28
|
+
codeGenSolanaProcessor(target.abisDir ?? path.join('abis', 'solana'))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (onlyGen) {
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function installDeps() {
|
|
37
|
+
await execStep('yarn install --ignore-scripts', 'Yarn Install')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function codeGenEthersProcessor(
|
|
41
|
+
abisDir: string,
|
|
42
|
+
ETHERS_TARGET = path.join(__dirname, '../target-ethers-sentio'),
|
|
43
|
+
outDir = 'src/types/internal'
|
|
44
|
+
) {
|
|
45
|
+
if (!fs.existsSync(abisDir)) {
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let haveJson = false
|
|
50
|
+
const files = fs.readdirSync(abisDir)
|
|
51
|
+
for (const file of files) {
|
|
52
|
+
if (file.toLowerCase().endsWith('.json')) {
|
|
53
|
+
haveJson = true
|
|
54
|
+
break
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (!haveJson) {
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// TODO this will fail during postinstall, need to locate real typechain path
|
|
62
|
+
await execStep(
|
|
63
|
+
'yarn typechain --target ' + ETHERS_TARGET + ` --out-dir ${outDir} ${path.join(abisDir, '*.json')}`,
|
|
64
|
+
'Type definitions gen'
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function codeGenSolanaProcessor(abisDir: string, root = '', targetPath = path.join('src', 'types')) {
|
|
69
|
+
const abisFolder = path.join(root, abisDir)
|
|
70
|
+
const abisFiles = fs.readdirSync(abisFolder)
|
|
71
|
+
const typeFolder = path.join(root, targetPath)
|
|
72
|
+
for (const file of abisFiles) {
|
|
73
|
+
if (path.extname(file) === '.json') {
|
|
74
|
+
if (!fs.existsSync(typeFolder)) {
|
|
75
|
+
fs.mkdirSync(typeFolder)
|
|
76
|
+
}
|
|
77
|
+
const idlContent = fs.readFileSync(path.join(abisFolder, file), 'utf-8')
|
|
78
|
+
const idlObj = JSON.parse(idlContent)
|
|
79
|
+
const idlName = idlObj.name
|
|
80
|
+
const idlFile = path.join(typeFolder, idlName + '.ts')
|
|
81
|
+
fs.writeFileSync(idlFile, `export const ${idlName}_idl = ${idlContent}`)
|
|
82
|
+
fs.writeFileSync(path.join(typeFolder, `${idlName}_processor.ts`), codeGenSolanaIdlProcessor(idlObj))
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function codeGenSolanaIdlProcessor(idlObj: any): string {
|
|
88
|
+
const idlName = idlObj.name
|
|
89
|
+
const idlNamePascalCase = toPascalCase(idlName)
|
|
90
|
+
const instructions: any[] = idlObj.instructions
|
|
91
|
+
return `import { BorshInstructionCoder, Instruction, Idl, BN } from '@project-serum/anchor'
|
|
92
|
+
import { SolanaBaseProcessor, SolanaContext, SolanaBindOptions } from "@sentio/sdk"
|
|
93
|
+
import { ${idlName}_idl } from "./${idlName}"
|
|
94
|
+
import bs58 from 'bs58'
|
|
95
|
+
import { PublicKey } from '@solana/web3.js'
|
|
96
|
+
|
|
97
|
+
export class ${idlNamePascalCase}Processor extends SolanaBaseProcessor {
|
|
98
|
+
static bind(options: SolanaBindOptions): ${idlNamePascalCase}Processor {
|
|
99
|
+
if (options && !options.name) {
|
|
100
|
+
options.name = '${idlNamePascalCase}'
|
|
101
|
+
}
|
|
102
|
+
return new ${idlNamePascalCase}Processor(options)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
decodeInstruction: (rawInstruction: string) => Instruction | null = (rawInstruction) => {
|
|
106
|
+
const instructionCoder = new BorshInstructionCoder(${idlName}_idl as Idl)
|
|
107
|
+
const decodedIns = instructionCoder.decode(Buffer.from(bs58.decode(rawInstruction)))
|
|
108
|
+
return decodedIns
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
${instructions.map((ins) => codeGenSolanaInstruction(idlNamePascalCase, ins)).join('')}
|
|
112
|
+
}
|
|
113
|
+
`
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function codeGenSolanaInstruction(idlName: string, ins: any): string {
|
|
117
|
+
const instructionName = ins.name
|
|
118
|
+
const argsTypeString = codeGenInstructionArgs(ins.args)
|
|
119
|
+
return `
|
|
120
|
+
on${
|
|
121
|
+
instructionName.charAt(0).toUpperCase() + instructionName.slice(1)
|
|
122
|
+
}(handler: (args: ${argsTypeString}, ctx: SolanaContext) => void): ${idlName}Processor {
|
|
123
|
+
this.onInstruction('${instructionName}', (ins: Instruction, ctx) => {
|
|
124
|
+
if (ins) {
|
|
125
|
+
handler(ins.data as ${argsTypeString}, ctx)
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
return this
|
|
129
|
+
}
|
|
130
|
+
`
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function codeGenInstructionArgs(args: { name: string; type: string }[]): string {
|
|
134
|
+
return `{ ${args.map((arg) => arg.name + ': ' + mapType(arg.type)).join(', ')} }`
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Reference: https://github.com/coral-xyz/anchor/blob/93332766f13e86efbe77c9986722731742317ede/ts/src/program/namespace/types.ts#L104
|
|
138
|
+
function mapType(tpe: string): string {
|
|
139
|
+
switch (tpe) {
|
|
140
|
+
case 'publicKey':
|
|
141
|
+
return 'PublicKey'
|
|
142
|
+
case 'bool':
|
|
143
|
+
return 'boolean'
|
|
144
|
+
case 'string':
|
|
145
|
+
return 'string'
|
|
146
|
+
case 'u8':
|
|
147
|
+
case 'i8':
|
|
148
|
+
case 'u16':
|
|
149
|
+
case 'i16':
|
|
150
|
+
case 'u32':
|
|
151
|
+
case 'i32':
|
|
152
|
+
case 'f32':
|
|
153
|
+
case 'f64':
|
|
154
|
+
return 'number'
|
|
155
|
+
case 'u64':
|
|
156
|
+
case 'i64':
|
|
157
|
+
case 'u128':
|
|
158
|
+
case 'i128':
|
|
159
|
+
return 'BN'
|
|
160
|
+
default:
|
|
161
|
+
return 'any'
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function toPascalCase(str: string) {
|
|
166
|
+
return `${str}`
|
|
167
|
+
.toLowerCase()
|
|
168
|
+
.replace(new RegExp(/[-_]+/, 'g'), ' ')
|
|
169
|
+
.replace(new RegExp(/[^\w\s]/, 'g'), '')
|
|
170
|
+
.replace(new RegExp(/\s+(.)(\w*)/, 'g'), ($1, $2, $3) => `${$2.toUpperCase() + $3}`)
|
|
171
|
+
.replace(new RegExp(/\w/), (s) => s.toUpperCase())
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async function execStep(cmd: string, stepName: string) {
|
|
175
|
+
const child = exec(cmd)
|
|
176
|
+
console.log(chalk.blue(stepName + ' begin'))
|
|
177
|
+
|
|
178
|
+
if (!child.stdout || !child.stderr) {
|
|
179
|
+
console.error(chalk.red(stepName + ' failed'))
|
|
180
|
+
process.exit(1)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
child.stdout.pipe(process.stdout)
|
|
184
|
+
child.stderr.pipe(process.stderr)
|
|
185
|
+
|
|
186
|
+
await new Promise((resolve) => {
|
|
187
|
+
child.on('close', resolve)
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
if (child.exitCode) {
|
|
191
|
+
console.error(chalk.red(stepName + ' failed'))
|
|
192
|
+
process.exit(child.exitCode)
|
|
193
|
+
}
|
|
194
|
+
console.log(chalk.blue(stepName + ' success'))
|
|
195
|
+
console.log()
|
|
196
|
+
}
|
package/src/cli/cli.ts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import commandLineArgs from 'command-line-args'
|
|
4
|
+
import commandLineUsage from 'command-line-usage'
|
|
5
|
+
import fs from 'fs'
|
|
6
|
+
import https from 'https'
|
|
7
|
+
import http from 'http'
|
|
8
|
+
import path from 'path'
|
|
9
|
+
|
|
10
|
+
import yaml from 'js-yaml'
|
|
11
|
+
import { EVM, FinalizeHost, FinalizeProjectName, SentioProjectConfig } from './config'
|
|
12
|
+
import { WriteKey } from './key'
|
|
13
|
+
import { uploadFile } from './upload'
|
|
14
|
+
import chalk from 'chalk'
|
|
15
|
+
import { buildProcessor } from './build'
|
|
16
|
+
|
|
17
|
+
const mainDefinitions = [{ name: 'command', defaultOption: true }]
|
|
18
|
+
const mainOptions = commandLineArgs(mainDefinitions, {
|
|
19
|
+
stopAtFirstUnknown: true,
|
|
20
|
+
})
|
|
21
|
+
const argv = mainOptions._unknown || []
|
|
22
|
+
|
|
23
|
+
// Process configs
|
|
24
|
+
let processorConfig: SentioProjectConfig = { host: '', project: '', source: '', build: true, targets: [] }
|
|
25
|
+
// Fist step, read from project yaml file
|
|
26
|
+
try {
|
|
27
|
+
console.log(chalk.blue('Loading Process config'))
|
|
28
|
+
// TODO correctly located sentio.yaml
|
|
29
|
+
const pwd = process.cwd()
|
|
30
|
+
const packageJson = path.join(pwd, 'package.json')
|
|
31
|
+
if (!fs.existsSync(packageJson)) {
|
|
32
|
+
console.error('package.json not found, please run this command in the root of your project')
|
|
33
|
+
process.exit(1)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const yamlPath = path.join(pwd, 'sentio.yaml')
|
|
37
|
+
if (!fs.existsSync(yamlPath)) {
|
|
38
|
+
console.error('sentio.yaml not found, please create one according to: TODO docs')
|
|
39
|
+
process.exit(1)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
processorConfig = yaml.load(fs.readFileSync('sentio.yaml', 'utf8')) as SentioProjectConfig
|
|
43
|
+
if (!processorConfig.project === undefined) {
|
|
44
|
+
console.error('Config yaml must have contain a valid project identifier')
|
|
45
|
+
process.exit(1)
|
|
46
|
+
}
|
|
47
|
+
if (processorConfig.build === undefined) {
|
|
48
|
+
processorConfig.build = true
|
|
49
|
+
}
|
|
50
|
+
if (!processorConfig.host) {
|
|
51
|
+
processorConfig.host = 'prod'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!processorConfig.source) {
|
|
55
|
+
processorConfig.source = 'src/processor.ts'
|
|
56
|
+
}
|
|
57
|
+
if (!processorConfig.targets) {
|
|
58
|
+
console.warn('targets is not defined, use EVM as the default target')
|
|
59
|
+
processorConfig.targets = []
|
|
60
|
+
}
|
|
61
|
+
if (processorConfig.targets.length === 0) {
|
|
62
|
+
// By default evm
|
|
63
|
+
processorConfig.targets.push({ chain: EVM })
|
|
64
|
+
}
|
|
65
|
+
} catch (e) {
|
|
66
|
+
console.error(e)
|
|
67
|
+
process.exit(1)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (mainOptions.command === 'upload') {
|
|
71
|
+
const optionDefinitions = [
|
|
72
|
+
{
|
|
73
|
+
name: 'help',
|
|
74
|
+
alias: 'h',
|
|
75
|
+
type: Boolean,
|
|
76
|
+
description: 'Display this usage guide.',
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: 'api-key',
|
|
80
|
+
type: String,
|
|
81
|
+
description: '(Optional) Manually provide API key rather than use saved credential',
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: 'host',
|
|
85
|
+
description: '(Optional) Override Sentio Host name',
|
|
86
|
+
type: String,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'owner',
|
|
90
|
+
description: '(Optional) Override Project owner',
|
|
91
|
+
type: String,
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
name: 'nobuild',
|
|
95
|
+
description: '(Optional) Skip build & pack file before uploading, default false',
|
|
96
|
+
type: Boolean,
|
|
97
|
+
},
|
|
98
|
+
]
|
|
99
|
+
const options = commandLineArgs(optionDefinitions, { argv })
|
|
100
|
+
if (options.help) {
|
|
101
|
+
const usage = commandLineUsage([
|
|
102
|
+
{
|
|
103
|
+
header: 'Sentio upload',
|
|
104
|
+
content: 'Upload your project files to Sentio.',
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
header: 'Options',
|
|
108
|
+
optionList: optionDefinitions,
|
|
109
|
+
},
|
|
110
|
+
])
|
|
111
|
+
console.log(usage)
|
|
112
|
+
} else {
|
|
113
|
+
if (options.host) {
|
|
114
|
+
processorConfig.host = options.host
|
|
115
|
+
}
|
|
116
|
+
if (options.nobuild) {
|
|
117
|
+
processorConfig.build = false
|
|
118
|
+
}
|
|
119
|
+
FinalizeHost(processorConfig)
|
|
120
|
+
FinalizeProjectName(processorConfig, options.owner)
|
|
121
|
+
console.log(processorConfig)
|
|
122
|
+
|
|
123
|
+
let apiOverride = undefined
|
|
124
|
+
if (options['api-key']) {
|
|
125
|
+
apiOverride = options['api-key']
|
|
126
|
+
}
|
|
127
|
+
uploadFile(processorConfig, apiOverride)
|
|
128
|
+
}
|
|
129
|
+
} else if (mainOptions.command === 'login') {
|
|
130
|
+
const optionDefinitions = [
|
|
131
|
+
{
|
|
132
|
+
name: 'help',
|
|
133
|
+
alias: 'h',
|
|
134
|
+
type: Boolean,
|
|
135
|
+
description: 'Display this usage guide.',
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
name: 'api-key',
|
|
139
|
+
type: String,
|
|
140
|
+
description: '(Required) Your API key',
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
name: 'host',
|
|
144
|
+
description: '(Optional) Override Sentio Host name',
|
|
145
|
+
type: String,
|
|
146
|
+
},
|
|
147
|
+
]
|
|
148
|
+
const options = commandLineArgs(optionDefinitions, { argv })
|
|
149
|
+
|
|
150
|
+
if (options.help || !options['api-key']) {
|
|
151
|
+
const usage = commandLineUsage([
|
|
152
|
+
{
|
|
153
|
+
header: 'Sentio login',
|
|
154
|
+
content: 'Try login to Sentio with your apikey.',
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
header: 'Options',
|
|
158
|
+
optionList: optionDefinitions,
|
|
159
|
+
},
|
|
160
|
+
])
|
|
161
|
+
console.log(usage)
|
|
162
|
+
} else {
|
|
163
|
+
if (options.host) {
|
|
164
|
+
processorConfig.host = options.host
|
|
165
|
+
}
|
|
166
|
+
FinalizeHost(processorConfig)
|
|
167
|
+
console.log(processorConfig)
|
|
168
|
+
|
|
169
|
+
const url = new URL(processorConfig.host)
|
|
170
|
+
const reqOptions = {
|
|
171
|
+
hostname: url.hostname,
|
|
172
|
+
port: url.port,
|
|
173
|
+
path: '/api/v1/processors/check_key',
|
|
174
|
+
method: 'HEAD',
|
|
175
|
+
headers: {
|
|
176
|
+
'api-key': options['api-key'],
|
|
177
|
+
},
|
|
178
|
+
}
|
|
179
|
+
const h = url.protocol == 'https:' ? https : http
|
|
180
|
+
const req = h.request(reqOptions, (res) => {
|
|
181
|
+
if (res.statusCode == 200) {
|
|
182
|
+
WriteKey(processorConfig.host, options['api-key'])
|
|
183
|
+
console.log(chalk.green('login success'))
|
|
184
|
+
} else {
|
|
185
|
+
console.error(chalk.red('login failed, code:', res.statusCode, res.statusMessage))
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
req.on('error', (error) => {
|
|
190
|
+
console.error(error)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
req.end()
|
|
194
|
+
}
|
|
195
|
+
} else if (mainOptions.command === 'build') {
|
|
196
|
+
buildProcessor(false, processorConfig.targets)
|
|
197
|
+
} else if (mainOptions.command === 'gen') {
|
|
198
|
+
buildProcessor(true, processorConfig.targets)
|
|
199
|
+
} else {
|
|
200
|
+
const usage = commandLineUsage([
|
|
201
|
+
{
|
|
202
|
+
header: 'Sentio',
|
|
203
|
+
content: 'Login & Upload your project files to Sentio.',
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
header: 'Usage',
|
|
207
|
+
content: [
|
|
208
|
+
'sentio $command --help\t\tshow detail usage of specific command',
|
|
209
|
+
'sentio login --api-key=xx\t\tsave credential to local',
|
|
210
|
+
'sentio upload\t\t\t\tbuild and upload processor to sentio',
|
|
211
|
+
'sentio gen\t\t\t\tgenerate abi',
|
|
212
|
+
'sentio build\t\t\t\tgenerate abi and build',
|
|
213
|
+
],
|
|
214
|
+
},
|
|
215
|
+
])
|
|
216
|
+
console.log(usage)
|
|
217
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export interface SentioProjectConfig {
|
|
2
|
+
project: string
|
|
3
|
+
host: string
|
|
4
|
+
source: string
|
|
5
|
+
build: boolean
|
|
6
|
+
targets: Target[]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function FinalizeHost(config: SentioProjectConfig) {
|
|
10
|
+
switch (config.host) {
|
|
11
|
+
case '':
|
|
12
|
+
case 'prod':
|
|
13
|
+
config.host = 'https://app.sentio.xyz'
|
|
14
|
+
break
|
|
15
|
+
case 'test':
|
|
16
|
+
config.host = 'https://test.sentio.xyz'
|
|
17
|
+
break
|
|
18
|
+
case 'staging':
|
|
19
|
+
config.host = 'https://staging.sentio.xyz'
|
|
20
|
+
break
|
|
21
|
+
case 'local':
|
|
22
|
+
config.host = 'http://localhost:10000'
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function FinalizeProjectName(config: SentioProjectConfig, owner: string | undefined) {
|
|
27
|
+
if (owner) {
|
|
28
|
+
let name = config.project
|
|
29
|
+
if (name.includes('/')) {
|
|
30
|
+
name = config.project.split('/')[1]
|
|
31
|
+
}
|
|
32
|
+
config.project = [owner, name].join('/')
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface Target {
|
|
37
|
+
chain: string
|
|
38
|
+
abisDir?: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Supported target chain, lower case
|
|
42
|
+
export const EVM = 'evm'
|
|
43
|
+
export const SOLANA = 'solana'
|
package/src/cli/key.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
import os from 'os'
|
|
4
|
+
|
|
5
|
+
const homeDir = os.homedir()
|
|
6
|
+
const sentioDir = path.join(homeDir, '.sentio')
|
|
7
|
+
const configFile = path.join(sentioDir, 'config.json')
|
|
8
|
+
|
|
9
|
+
interface SentioKeyConfig {
|
|
10
|
+
[key: string]: {
|
|
11
|
+
api_keys: string
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function WriteKey(host: string, api_key: string) {
|
|
16
|
+
const sentioDir = path.join(homeDir, '.sentio')
|
|
17
|
+
if (!fs.existsSync(sentioDir)) {
|
|
18
|
+
fs.mkdirSync(sentioDir, { recursive: true })
|
|
19
|
+
}
|
|
20
|
+
let config: SentioKeyConfig = {}
|
|
21
|
+
if (fs.existsSync(configFile)) {
|
|
22
|
+
const content = fs.readFileSync(configFile, 'utf8')
|
|
23
|
+
config = JSON.parse(content)
|
|
24
|
+
}
|
|
25
|
+
const hostConfig = config[host] || { api_keys: {} }
|
|
26
|
+
hostConfig.api_keys = api_key
|
|
27
|
+
config[host] = hostConfig
|
|
28
|
+
fs.writeFileSync(configFile, JSON.stringify(config, null, 2))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function ReadKey(host: string): string | undefined {
|
|
32
|
+
if (!fs.existsSync(sentioDir)) {
|
|
33
|
+
return undefined
|
|
34
|
+
}
|
|
35
|
+
const configFile = path.join(sentioDir, 'config.json')
|
|
36
|
+
if (fs.existsSync(configFile)) {
|
|
37
|
+
const content = fs.readFileSync(configFile, 'utf8')
|
|
38
|
+
const config = JSON.parse(content)
|
|
39
|
+
return config[host]?.api_keys
|
|
40
|
+
} else {
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { codeGenSolanaProcessor } from './build'
|
|
4
|
+
|
|
5
|
+
const abisDir = 'abis/solana'
|
|
6
|
+
if (process.argv.length > 2) {
|
|
7
|
+
const rootPath = process.argv[2]
|
|
8
|
+
if (process.argv.length > 3) {
|
|
9
|
+
const targetPath = process.argv[3]
|
|
10
|
+
codeGenSolanaProcessor(abisDir, rootPath, targetPath)
|
|
11
|
+
} else {
|
|
12
|
+
codeGenSolanaProcessor(abisDir, rootPath)
|
|
13
|
+
}
|
|
14
|
+
} else {
|
|
15
|
+
codeGenSolanaProcessor(abisDir)
|
|
16
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { execSync } from 'child_process'
|
|
2
|
+
import { createHash } from 'crypto'
|
|
3
|
+
import FormData from 'form-data'
|
|
4
|
+
import fs from 'fs'
|
|
5
|
+
import { SentioProjectConfig } from './config'
|
|
6
|
+
import { ReadKey } from './key'
|
|
7
|
+
import path from 'path'
|
|
8
|
+
import chalk from 'chalk'
|
|
9
|
+
import { buildProcessor } from './build'
|
|
10
|
+
import fetch from 'node-fetch'
|
|
11
|
+
|
|
12
|
+
export async function uploadFile(options: SentioProjectConfig, apiKeyOverride: string) {
|
|
13
|
+
if (options.build) {
|
|
14
|
+
await buildProcessor(false, options.targets)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
console.log(chalk.blue('Uploading'))
|
|
18
|
+
|
|
19
|
+
const PROCESSOR_FILE = path.join(process.cwd(), 'dist/lib.js')
|
|
20
|
+
|
|
21
|
+
const url = new URL(options.host)
|
|
22
|
+
const apiKey = apiKeyOverride || ReadKey(options.host)
|
|
23
|
+
|
|
24
|
+
const isProd = options.host === 'https://app.sentio.xyz'
|
|
25
|
+
if (!apiKey) {
|
|
26
|
+
const cmd = isProd ? 'sentio login' : 'sentio login --host=' + options.host
|
|
27
|
+
console.error(chalk.red('No Credential found for', options.host, '. Please run `' + cmd + '`.'))
|
|
28
|
+
process.exit(1)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!fs.existsSync(PROCESSOR_FILE)) {
|
|
32
|
+
console.error(chalk.red('File not existed ', PROCESSOR_FILE, "don't use --nobuild"))
|
|
33
|
+
process.exit(1)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const stat = fs.statSync(PROCESSOR_FILE)
|
|
37
|
+
console.log('Packed processor file size', Math.floor(stat.size / 1024) + 'K, last modified', stat.mtime)
|
|
38
|
+
const content = fs.readFileSync(PROCESSOR_FILE)
|
|
39
|
+
const hash = createHash('sha256')
|
|
40
|
+
hash.update(content)
|
|
41
|
+
const digest = hash.digest('hex')
|
|
42
|
+
|
|
43
|
+
const data = new FormData()
|
|
44
|
+
data.append('attachment', fs.createReadStream(PROCESSOR_FILE))
|
|
45
|
+
data.append('sha256', digest)
|
|
46
|
+
|
|
47
|
+
let commitSha
|
|
48
|
+
try {
|
|
49
|
+
commitSha = execSync('git rev-parse HEAD').toString().trim()
|
|
50
|
+
data.append('commitSha', commitSha)
|
|
51
|
+
} catch (e) {
|
|
52
|
+
chalk.yellow(e)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
url.pathname = '/api/v1/processors'
|
|
56
|
+
|
|
57
|
+
let packageJson: { version: string }
|
|
58
|
+
try {
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
60
|
+
packageJson = require('../package.json')
|
|
61
|
+
} catch (e) {
|
|
62
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
63
|
+
packageJson = require('../../package.json')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const res = await fetch(url, {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
headers: {
|
|
69
|
+
'api-key': apiKey,
|
|
70
|
+
project: options.project,
|
|
71
|
+
version: packageJson.version,
|
|
72
|
+
},
|
|
73
|
+
body: data,
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
if (res.ok) {
|
|
77
|
+
console.log(chalk.green('Upload success: '))
|
|
78
|
+
console.log('\t', chalk.blue('sha256:'), digest)
|
|
79
|
+
if (commitSha) {
|
|
80
|
+
console.log('\t', chalk.blue('Git commit SHA:'), commitSha)
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
console.error(chalk.red('Upload Failed'))
|
|
84
|
+
console.error(chalk.red(await res.text()))
|
|
85
|
+
}
|
|
86
|
+
}
|