-
Notifications
You must be signed in to change notification settings - Fork 36
/
provide-collateral.ts
71 lines (66 loc) · 2.48 KB
/
provide-collateral.ts
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
import { Dec, Int, MsgExecuteContract } from '@terra-money/terra.js';
import { createHookMsg } from '../../utils/cw20/create-hook-msg';
import { validateInput } from '../../utils/validate-input';
import { validateAddress } from '../../utils/validation/address';
import { validateIsGreaterThanZero } from '../../utils/validation/number';
import {
AddressProvider,
COLLATERAL_DENOMS,
MARKET_DENOMS,
} from '../../address-provider/provider';
interface Option {
address: string;
market: MARKET_DENOMS;
collateral: COLLATERAL_DENOMS;
amount: string;
}
/**
*
* @param address Client’s Terra address.
* @param market Type of stablecoin money market to deposit collateral. Currently only supports UST and KRT.
// * @param borrower (optional) — Terra address of the entity that created the loan position. If null, adds collateral to address‘s loan position.
* @param loan_id ID of address’s loan position to add collateral. For each addresses, their first loan position is given ID = 0, second loan ID = 1, third ID = 2, etc.. If this field is [(address’s current highest loan_id) + 1], a new loan position is created.
* @param symbol Symbol of collateral to deposit.
* @param amount Amount of collateral to deposit.
*/
export const fabricateProvideCollateral = ({
address,
market,
collateral,
amount,
}: Option) => (addressProvider: AddressProvider): MsgExecuteContract[] => {
validateInput([
validateAddress(address),
// borrower ? validateAddress(borrower) : validateTrue,
validateIsGreaterThanZero(amount),
]);
const bAssetTokenContract = addressProvider.bLunaToken();
const mmOverseerContract = addressProvider.overseer(market);
const custodyContract = addressProvider.custody(market, collateral);
// cw20 send + provide_collateral hook
/* eslint-disable */
return [
// provide_collateral call
new MsgExecuteContract(address, bAssetTokenContract, {
send: {
contract: custodyContract,
amount: new Int(new Dec(amount).mul(1000000)).toString(),
msg: createHookMsg({
deposit_collateral: {},
}),
},
}),
// lock_collateral call
new MsgExecuteContract(address, mmOverseerContract, {
// @see https://github.com/Anchor-Protocol/money-market-contracts/blob/master/contracts/overseer/src/msg.rs#L75
lock_collateral: {
collaterals: [
[
bAssetTokenContract,
new Int(new Dec(amount).mul(1000000)).toString(),
],
],
},
}),
];
};