-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtip.ts
More file actions
78 lines (64 loc) · 2.49 KB
/
tip.ts
File metadata and controls
78 lines (64 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { createClient, PolkadotClient, TypedApi } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws-provider";
import { ChainDescriptor, getDescriptor, getWsUrl } from "./chain-config";
import { tipOpenGov, tipOpenGovReferendumExtrinsic } from "./tip-opengov";
import { State, TipNetwork, TipRequest, TipResult } from "./types";
export type API<T extends TipNetwork> = TypedApi<ChainDescriptor<T>>;
async function createApi(
network: TipNetwork,
state: State,
): Promise<{
client: PolkadotClient;
}> {
const { bot } = state;
const provider = getWsProvider(getWsUrl(network));
const client = createClient(provider);
// Check that it works
await client.getFinalizedBlock();
// Set up the types
const api = client.getTypedApi(getDescriptor(network));
try {
const version = await api.apis.Core.version();
bot.log(`You are connected to chain ${version.spec_name}#${version.spec_version}`);
} catch (e) {
console.error("Error getting core version", e);
}
return { client };
}
/**
* Tips the user using the Bot account.
* The bot will send the referendum creation transaction itself and pay for the fees.
*/
export async function tipUser(state: State, tipRequest: TipRequest): Promise<TipResult> {
const { client } = await createApi(tipRequest.contributor.account.network, state);
try {
return await tipOpenGov({ state, client, tipRequest });
} finally {
client.destroy();
}
}
/**
* Prepare a referendum extrinsic, but do not actually send it to the chain.
* Create a transaction creation link for the user.
*/
export async function tipUserLink(
state: State,
tipRequest: TipRequest,
): Promise<{ success: false; errorMessage: string } | { success: true; extrinsicCreationLink: string }> {
const { network } = tipRequest.contributor.account;
const { client } = await createApi(network, state);
try {
const preparedExtrinsic = await tipOpenGovReferendumExtrinsic({ client, tipRequest });
if (!preparedExtrinsic.success) {
return preparedExtrinsic;
}
const transactionHex = (await preparedExtrinsic.referendumExtrinsic.getEncodedData()).asHex();
const polkadotAppsUrl = `https://polkadot.js.org/apps/?rpc=${getWsUrl(network)}#/`;
const extrinsicCreationLink = `${polkadotAppsUrl}extrinsics/decode/${transactionHex}`;
return { success: true, extrinsicCreationLink };
} catch (e) {
return { success: false, errorMessage: e instanceof Error ? e.stack ?? e.message : String(e) };
} finally {
client.destroy();
}
}