@polkadot/types/interfaces#OpenTip TypeScript Examples
The following examples show how to use
@polkadot/types/interfaces#OpenTip.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Tip.tsx From crust-apps with Apache License 2.0 | 6 votes |
function extractTipState (tip: OpenTip | OpenTipTo225, allAccounts: string[]): TipState {
const closesAt = tip.closes.unwrapOr(null);
let finder: AccountId | null = null;
let deposit: Balance | null = null;
if (isCurrentTip(tip)) {
finder = tip.finder;
deposit = tip.deposit;
} else if (tip.finder.isSome) {
const finderInfo = tip.finder.unwrap();
finder = finderInfo[0];
deposit = finderInfo[1];
}
const values = tip.tips.map(([, value]) => value).sort((a, b) => a.cmp(b));
const midIndex = Math.floor(values.length / 2);
const median = values.length
? values[midIndex]
: BN_ZERO;
return {
closesAt,
deposit,
finder,
isFinder: !!finder && allAccounts.includes(finder.toString()),
isTipped: !!values.length,
isTipper: tip.tips.some(([address]) => allAccounts.includes(address.toString())),
median
};
}
Example #2
Source File: Tips.tsx From crust-apps with Apache License 2.0 | 6 votes |
function extractTips (tipsWithHashes?: [[string[]], Option<OpenTip>[]], inHashes?: string[] | null): Tip[] | undefined {
if (!tipsWithHashes || !inHashes) {
return undefined;
}
const [[hashes], optTips] = tipsWithHashes;
return optTips
.map((opt, index): [string, OpenTip | null] => [hashes[index], opt.unwrapOr(null)])
.filter((val): val is [string, OpenTip] => inHashes.includes(val[0]) && !!val[1])
.sort((a, b) =>
a[1].closes.isNone
? b[1].closes.isNone
? 0
: -1
: b[1].closes.isSome
? b[1].closes.unwrap().cmp(a[1].closes.unwrap())
: 1
);
}
Example #3
Source File: gov.ts From sdk with Apache License 2.0 | 6 votes |
/**
* Query tips of treasury.
*/
async function getTreasuryTips(api: ApiPromise) {
const tipKeys = await (api.query.tips || api.query.treasury).tips.keys();
const tipHashes = tipKeys.map((key) => key.args[0].toHex());
const optTips = (await (api.query.tips || api.query.treasury).tips.multi(tipHashes)) as Option<OpenTip>[];
const tips = optTips
.map((opt, index) => [tipHashes[index], opt.unwrapOr(null)])
.filter((val) => !!val[1])
.sort((a: any[], b: any[]) => a[1].closes.unwrapOr(BN_ZERO).cmp(b[1].closes.unwrapOr(BN_ZERO)));
return Promise.all(
tips.map(async (tip: any[]) => {
const detail = tip[1].toJSON();
const reason = (await (api.query.tips || api.query.treasury).reasons(detail.reason)) as Option<Bytes>;
const tips = detail.tips.map((e: any) => ({
address: e[0],
value: e[1],
}));
return {
hash: tip[0],
...detail,
reason: reason.isSome ? hexToString(reason.unwrap().toHex()) : null,
tips,
};
})
);
}
Example #4
Source File: Tip.tsx From crust-apps with Apache License 2.0 | 5 votes |
function isCurrentTip (tip: OpenTip | OpenTipTo225): tip is OpenTip {
return !!(tip as OpenTip)?.findersFee;
}
Example #5
Source File: Tips.tsx From crust-apps with Apache License 2.0 | 5 votes |
function Tips ({ className = '', defaultId, hashes, isMember, members, onSelectTip }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const [onlyUntipped, setOnlyUntipped] = useState(false);
const bestNumber = useBestNumber();
const tipsWithHashes = useCall<[[string[]], Option<OpenTip>[]]>(hashes && (api.query.tips || api.query.treasury).tips.multi, [hashes], TIP_OPTS);
const tips = useMemo(
() => extractTips(tipsWithHashes, hashes),
[hashes, tipsWithHashes]
);
const headerRef = useRef([
[t('tips'), 'start'],
[t('finder'), 'address media--1400'],
[t('reason'), 'start'],
[],
[],
[undefined, 'badge media--1700'],
[],
[undefined, 'media--1700']
]);
return (
<Table
className={className}
empty={tips && t<string>('No open tips')}
filter={isMember && (
<div className='tipsFilter'>
<Toggle
label={t<string>('show only untipped/closing')}
onChange={setOnlyUntipped}
value={onlyUntipped}
/>
</div>
)}
header={headerRef.current}
>
{tips && tips.map(([hash, tip]): React.ReactNode => (
<Tip
bestNumber={bestNumber}
defaultId={defaultId}
hash={hash}
isMember={isMember}
key={hash}
members={members}
onSelect={onSelectTip}
onlyUntipped={onlyUntipped}
tip={tip}
/>
))}
</Table>
);
}