Skip to content

Commit

Permalink
Inline the preimages in the referendum (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzadp authored Jul 11, 2023
1 parent c85217c commit dc632f8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
49 changes: 19 additions & 30 deletions src/tip-opengov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import "@polkadot/types-augment";
import { until } from "@eng-automation/js";
import { ApiPromise } from "@polkadot/api";
import { ISubmittableResult } from "@polkadot/types/types";
import { blake2AsHex } from "@polkadot/util-crypto";
import { Probot } from "probot";

import { getTipUrl } from "./chain-config";
import { ContributorAccount, State, TipRequest, TipResult } from "./types";
import { formatReason, tipSizeToOpenGovTrack } from "./util";
import { byteSize, formatReason, tipSizeToOpenGovTrack } from "./util";

export async function tipOpenGov(opts: { state: State; api: ApiPromise; tipRequest: TipRequest }): Promise<TipResult> {
const {
Expand All @@ -27,40 +26,30 @@ export async function tipOpenGov(opts: { state: State; api: ApiPromise; tipReque
const proposalTx = api.tx.treasury.spend(track.value.toString(), contributorAddress);
const nonce = (await api.rpc.system.accountNextIndex(botTipAccount.address)).toNumber();
const encodedProposal = proposalTx.method.toHex();
const proposalHash = blake2AsHex(encodedProposal);
const encodedLength = Math.ceil((encodedProposal.length - 2) / 2);
const proposalByteSize = byteSize(encodedProposal);
if (proposalByteSize >= 128) {
return {
success: false,
errorMessage: `The proposal length of ${proposalByteSize} equals or exceeds 128 bytes and cannot be inlined in the referendum.`,
};
}

bot.log(
`Tip proposal for ${contributor.account.address} hash: ${proposalHash}, encoded length: ${encodedLength}, nonce: ${nonce}`,
`Tip proposal for ${contributor.account.address}, encoded proposal byte size: ${proposalByteSize}, nonce: ${nonce}`,
);

const referendumId = await api.query.referenda.referendumCount(); // The next free referendum index.
const tipResult = await new Promise<TipResult>(async (resolve, reject) => {
// create a preimage from opengov with the encodedProposal above
const preimageUnsubscribe = await api.tx.preimage
.notePreimage(encodedProposal)
.signAndSend(botTipAccount, async (result) => {
await signAndSendCallback(bot, contributor.account, "preimage", preimageUnsubscribe, result)
.then(async () => {
const readPreimage = await api.query.preimage.statusFor(proposalHash);

if (readPreimage.isEmpty) {
reject(new Error(`Preimage for ${proposalHash} was not found, check if the bot has enough funds.`));
}

const proposalUnsubscribe = await api.tx.referenda
.submit(
// TODO: There should be a way to set those types properly.
{ Origins: track.track.trackName } as never,
{ Lookup: { hash: proposalHash, len: encodedLength } },
{ after: 10 } as never,
)
.signAndSend(botTipAccount, async (refResult) => {
await signAndSendCallback(bot, contributor.account, "referendum", proposalUnsubscribe, refResult)
.then(resolve)
.catch(reject);
});
})
const proposalUnsubscribe = await api.tx.referenda
.submit(
// TODO: There should be a way to set those types properly.
{ Origins: track.track.trackName } as never,
{ Inline: encodedProposal },
{ after: 10 } as never,
)
.signAndSend(botTipAccount, async (refResult) => {
await signAndSendCallback(bot, contributor.account, "referendum", proposalUnsubscribe, refResult)
.then(resolve)
.catch(reject);
});
});
Expand Down
3 changes: 3 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,6 @@ export const formatTipSize = (tipRequest: TipRequest): string => {
* It is used to tag these usernames when there is a failure.
*/
export const teamMatrixHandles = ["@przemek", "@mak", "@yuri", "@bullrich"];

// https://stackoverflow.com/a/52254083
export const byteSize = (str: string): number => new Blob([str]).size;

0 comments on commit dc632f8

Please sign in to comment.