react-query#QueryObserverResult TypeScript Examples
The following examples show how to use
react-query#QueryObserverResult.
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: borrow.ts From anchor-web-app with Apache License 2.0 | 4 votes |
export function borrowBorrowTx($: {
walletAddr: HumanAddr;
marketAddr: HumanAddr;
borrowAmount: UST;
gasFee: Gas;
gasAdjustment: Rate<number>;
txFee: u<UST>;
fixedGas: u<UST>;
network: NetworkInfo;
queryClient: QueryClient;
post: (tx: CreateTxOptions) => Promise<TxResult>;
txErrorReporter?: (error: unknown) => string;
borrowMarketQuery: () => Promise<
QueryObserverResult<BorrowMarket | undefined>
>;
borrowBorrowerQuery: () => Promise<
QueryObserverResult<BorrowBorrower | undefined>
>;
onTxSucceed?: () => void;
}): Observable<TxResultRendering> {
const helper = new TxHelper($);
return pipe(
_createTxOptions({
msgs: [
new MsgExecuteContract($.walletAddr, $.marketAddr, {
// @see https://github.com/Anchor-Protocol/money-market-contracts/blob/master/contracts/market/src/msg.rs#L68
borrow_stable: {
borrow_amount: formatTokenInput($.borrowAmount),
},
}),
],
// FIXME borrow's txFee is fixed_gas
fee: new Fee($.gasFee, floor($.fixedGas) + 'uusd'),
gasAdjustment: $.gasAdjustment,
}),
_postTx({ helper, ...$ }),
_pollTxInfo({ helper, ...$ }),
_fetchBorrowData({ helper, ...$ }),
({ value: { txInfo, borrowMarket, borrowBorrower } }) => {
if (!borrowMarket || !borrowBorrower) {
return helper.failedToCreateReceipt(
new Error('Failed to load borrow data'),
);
}
const rawLog = pickRawLog(txInfo, 0);
if (!rawLog) {
return helper.failedToFindRawLog();
}
const fromContract = pickEvent(rawLog, 'from_contract');
if (!fromContract) {
return helper.failedToFindEvents('from_contract');
}
try {
const borrowedAmount = pickAttributeValue<u<UST>>(fromContract, 3);
const ltv = computeLtv(
computeBorrowLimit(
borrowBorrower.overseerCollaterals,
borrowMarket.oraclePrices,
borrowMarket.bAssetLtvs,
),
computeBorrowedAmount(borrowBorrower.marketBorrowerInfo),
);
const outstandingLoan = borrowBorrower.marketBorrowerInfo.loan_amount;
return {
value: null,
phase: TxStreamPhase.SUCCEED,
receipts: [
borrowedAmount && {
name: 'Borrowed Amount',
value:
formatUSTWithPostfixUnits(demicrofy(borrowedAmount)) + ' UST',
},
ltv && {
name: 'New Borrow Usage',
value: formatRate(ltv) + ' %',
},
outstandingLoan && {
name: 'Outstanding Loan',
value:
formatUSTWithPostfixUnits(demicrofy(outstandingLoan)) + ' UST',
},
helper.txHashReceipt(),
helper.txFeeReceipt(),
],
} as TxResultRendering;
} catch (error) {
return helper.failedToParseTxResult();
}
},
)().pipe(_catchTxError({ helper, ...$ }));
}
Example #2
Source File: provideCollateral.ts From anchor-web-app with Apache License 2.0 | 4 votes |
export function borrowProvideCollateralTx($: {
collateral: WhitelistCollateral;
walletAddr: HumanAddr;
depositAmount: bAsset;
overseerAddr: HumanAddr;
gasFee: Gas;
gasAdjustment: Rate<number>;
fixedGas: u<UST>;
network: NetworkInfo;
queryClient: QueryClient;
post: (tx: CreateTxOptions) => Promise<TxResult>;
txErrorReporter?: (error: unknown) => string;
borrowMarketQuery: () => Promise<
QueryObserverResult<BorrowMarket | undefined>
>;
borrowBorrowerQuery: () => Promise<
QueryObserverResult<BorrowBorrower | undefined>
>;
onTxSucceed?: () => void;
}): Observable<TxResultRendering> {
const helper = new TxHelper({ ...$, txFee: $.fixedGas });
return pipe(
_createTxOptions({
msgs: [
// provide_collateral call
new MsgExecuteContract($.walletAddr, $.collateral.collateral_token, {
send: {
contract: $.collateral.custody_contract,
amount: formatInput(
microfy($.depositAmount, $.collateral.decimals),
$.collateral.decimals,
),
msg: createHookMsg({
deposit_collateral: {},
}),
},
}),
// lock_collateral call
new MsgExecuteContract($.walletAddr, $.overseerAddr, {
// @see https://github.com/Anchor-Protocol/money-market-contracts/blob/master/contracts/overseer/src/msg.rs#L75
lock_collateral: {
collaterals: [
[
$.collateral.collateral_token,
formatInput(
microfy($.depositAmount, $.collateral.decimals),
$.collateral.decimals,
),
],
],
},
}),
],
fee: new Fee($.gasFee, floor($.fixedGas) + 'uusd'),
gasAdjustment: $.gasAdjustment,
}),
_postTx({ helper, ...$ }),
_pollTxInfo({ helper, ...$ }),
_fetchBorrowData({ helper, ...$ }),
({ value: { txInfo, borrowMarket, borrowBorrower } }) => {
if (!borrowMarket || !borrowBorrower) {
return helper.failedToCreateReceipt(
new Error('Failed to load borrow data'),
);
}
const rawLog = pickRawLog(txInfo, 1);
if (!rawLog) {
return helper.failedToFindRawLog();
}
const fromContract = pickEvent(rawLog, 'from_contract');
if (!fromContract) {
return helper.failedToFindEvents('from_contract');
}
try {
const collateralizedAmount = pickAttributeValue<u<bLuna>>(
fromContract,
7,
);
const ltv = computeLtv(
computeBorrowLimit(
borrowBorrower.overseerCollaterals,
borrowMarket.oraclePrices,
borrowMarket.bAssetLtvs,
),
computeBorrowedAmount(borrowBorrower.marketBorrowerInfo),
);
return {
value: null,
phase: TxStreamPhase.SUCCEED,
receipts: [
collateralizedAmount && {
name: 'Collateralized Amount',
value: `${formatOutput(
demicrofy(collateralizedAmount, $.collateral.decimals),
{ decimals: $.collateral.decimals },
)} ${$.collateral.symbol}`,
},
ltv && {
name: 'New Borrow Usage',
value: formatRate(ltv) + ' %',
},
helper.txHashReceipt(),
helper.txFeeReceipt(),
],
} as TxResultRendering;
} catch (error) {
return helper.failedToParseTxResult();
}
},
)().pipe(_catchTxError({ helper, ...$ }));
}
Example #3
Source File: redeemCollateral.ts From anchor-web-app with Apache License 2.0 | 4 votes |
export function borrowRedeemCollateralTx($: {
collateral: WhitelistCollateral;
walletAddr: HumanAddr;
redeemAmount: bAsset;
overseerAddr: HumanAddr;
gasFee: Gas;
gasAdjustment: Rate<number>;
fixedGas: u<UST>;
network: NetworkInfo;
queryClient: QueryClient;
post: (tx: CreateTxOptions) => Promise<TxResult>;
txErrorReporter?: (error: unknown) => string;
borrowMarketQuery: () => Promise<
QueryObserverResult<BorrowMarket | undefined>
>;
borrowBorrowerQuery: () => Promise<
QueryObserverResult<BorrowBorrower | undefined>
>;
onTxSucceed?: () => void;
}): Observable<TxResultRendering> {
const helper = new TxHelper({ ...$, txFee: $.fixedGas });
return pipe(
_createTxOptions({
msgs: [
// unlock collateral
new MsgExecuteContract($.walletAddr, $.overseerAddr, {
// @see https://github.com/Anchor-Protocol/money-market-contracts/blob/master/contracts/overseer/src/msg.rs#L78
unlock_collateral: {
collaterals: [
[
$.collateral.collateral_token,
formatInput(
microfy($.redeemAmount, $.collateral.decimals),
$.collateral.decimals,
),
],
],
},
}),
// withdraw from custody
new MsgExecuteContract($.walletAddr, $.collateral.custody_contract, {
// @see https://github.com/Anchor-Protocol/money-market-contracts/blob/master/contracts/custody/src/msg.rs#L69
withdraw_collateral: {
amount: formatInput(
microfy($.redeemAmount, $.collateral.decimals),
$.collateral.decimals,
),
},
}),
],
fee: new Fee($.gasFee, floor($.fixedGas) + 'uusd'),
gasAdjustment: $.gasAdjustment,
}),
_postTx({ helper, ...$ }),
_pollTxInfo({ helper, ...$ }),
_fetchBorrowData({ helper, ...$ }),
({ value: { txInfo, borrowMarket, borrowBorrower } }) => {
if (!borrowMarket || !borrowBorrower) {
return helper.failedToCreateReceipt(
new Error('Failed to load borrow data'),
);
}
const rawLog = pickRawLog(txInfo, 1);
if (!rawLog) {
return helper.failedToFindRawLog();
}
const fromContract = pickEvent(rawLog, 'from_contract');
if (!fromContract) {
return helper.failedToFindEvents('from_contract');
}
try {
const redeemedAmount = pickAttributeValue<u<bLuna>>(fromContract, 16);
const ltv = computeLtv(
computeBorrowLimit(
borrowBorrower.overseerCollaterals,
borrowMarket.oraclePrices,
borrowMarket.bAssetLtvs,
),
computeBorrowedAmount(borrowBorrower.marketBorrowerInfo),
);
return {
value: null,
phase: TxStreamPhase.SUCCEED,
receipts: [
redeemedAmount && {
name: 'Redeemed Amount',
value: `${formatOutput(
demicrofy(redeemedAmount, $.collateral.decimals),
{ decimals: $.collateral.decimals },
)} ${$.collateral.symbol}`,
},
ltv && {
name: 'New Borrow Usage',
value: formatRate(ltv) + ' %',
},
helper.txHashReceipt(),
helper.txFeeReceipt(),
],
} as TxResultRendering;
} catch (error) {
return helper.failedToParseTxResult();
}
},
)().pipe(_catchTxError({ helper, ...$ }));
}
Example #4
Source File: repay.ts From anchor-web-app with Apache License 2.0 | 4 votes |
export function borrowRepayTx($: {
walletAddr: HumanAddr;
marketAddr: HumanAddr;
repayAmount: UST;
gasFee: Gas;
gasAdjustment: Rate<number>;
txFee: u<UST>;
network: NetworkInfo;
queryClient: QueryClient;
post: (tx: CreateTxOptions) => Promise<TxResult>;
txErrorReporter?: (error: unknown) => string;
borrowMarketQuery: () => Promise<
QueryObserverResult<BorrowMarket | undefined>
>;
borrowBorrowerQuery: () => Promise<
QueryObserverResult<BorrowBorrower | undefined>
>;
onTxSucceed?: () => void;
}): Observable<TxResultRendering> {
const helper = new TxHelper($);
return pipe(
_createTxOptions({
msgs: [
new MsgExecuteContract(
$.walletAddr,
$.marketAddr,
{
// @see https://github.com/Anchor-Protocol/money-market-contracts/blob/master/contracts/market/src/msg.rs#L74
repay_stable: {},
},
// sending stablecoin
new Coins([new Coin('uusd', formatTokenInput($.repayAmount))]),
),
],
// FIXME repay's txFee is not fixed_gas (user ust transfer)
fee: new Fee($.gasFee, floor($.txFee) + 'uusd'),
gasAdjustment: $.gasAdjustment,
}),
_postTx({ helper, ...$ }),
_pollTxInfo({ helper, ...$ }),
_fetchBorrowData({ helper, ...$ }),
({ value: { txInfo, borrowMarket, borrowBorrower } }) => {
if (!borrowMarket || !borrowBorrower) {
return helper.failedToCreateReceipt(
new Error('Failed to load borrow data'),
);
}
const rawLog = pickRawLog(txInfo, 0);
if (!rawLog) {
return helper.failedToFindRawLog();
}
const fromContract = pickEvent(rawLog, 'from_contract');
if (!fromContract) {
return helper.failedToFindEvents('from_contract');
}
try {
const repaidAmount = pickAttributeValue<u<UST>>(fromContract, 3);
const ltv = computeLtv(
computeBorrowLimit(
borrowBorrower.overseerCollaterals,
borrowMarket.oraclePrices,
borrowMarket.bAssetLtvs,
),
computeBorrowedAmount(borrowBorrower.marketBorrowerInfo),
);
const outstandingLoan = borrowBorrower.marketBorrowerInfo.loan_amount;
return {
value: null,
phase: TxStreamPhase.SUCCEED,
receipts: [
repaidAmount && {
name: 'Repaid Amount',
value:
formatUSTWithPostfixUnits(demicrofy(repaidAmount)) + ' UST',
},
ltv && {
name: 'New Borrow Usage',
value: formatRate(ltv) + ' %',
},
outstandingLoan && {
name: 'Outstanding Loan',
value:
formatUSTWithPostfixUnits(demicrofy(outstandingLoan)) + ' UST',
},
helper.txHashReceipt(),
helper.txFeeReceipt(),
],
} as TxResultRendering;
} catch (error) {
return helper.failedToParseTxResult();
}
},
)().pipe(_catchTxError({ helper, ...$ }));
}