Skip to content

Commit

Permalink
feat: lend vault messages
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinWoetzel committed Mar 1, 2024
1 parent fb41cf0 commit 83e21b4
Show file tree
Hide file tree
Showing 6 changed files with 809 additions and 0 deletions.
132 changes: 132 additions & 0 deletions src/contracts/definitions/lend.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import {
test,
expect,
} from 'vitest';
import {
msgGetVault,
msgGetVaults,
msgGetVaultUserPosition,
msgGetVaultUserPositions,
msgBorrow,
msgWithdraw,
} from './lend';

test('it tests the form of the vault info message', () => {
const inputVaultId = '1';

const output = {
vault: {
vault_id: inputVaultId,
},
};
expect(msgGetVault(inputVaultId)).toStrictEqual(output);
});

test('it tests the form of the multiple vaults info message', () => {
const inputStartingPageNumber = 1;

const output = {
vaults: {
starting_page: '1',
},
};
expect(msgGetVaults(inputStartingPageNumber)).toStrictEqual(output);
});

test('it tests the form of the vault user position message', () => {
const inputVaultId = '1';
const permit = {
params: {
data: 'PERMIT_DATA',
contract: 'CONTRACT',
key: 'KEY',
},
chain_id: 'CHAIN_ID',
signature: {
pub_key: {
type: 'TYPE',
value: 'VALUE',
},
signature: 'SIGNATURE',
},
};

const output = {
with_permit: {
permit,
query: {
position: {
vault_id: inputVaultId,
},
},
},
};
expect(msgGetVaultUserPosition(permit, inputVaultId)).toStrictEqual(output);
});

test('it tests the form of the vaults user position message', () => {
const inputVaultIds = ['1', '2'];
const permit = {
params: {
data: 'PERMIT_DATA',
contract: 'CONTRACT',
key: 'KEY',
},
chain_id: 'CHAIN_ID',
signature: {
pub_key: {
type: 'TYPE',
value: 'VALUE',
},
signature: 'SIGNATURE',
},
};

const output = {
with_permit: {
permit,
query: {
positions: {
vault_ids: inputVaultIds,
},
},
},
};
expect(msgGetVaultUserPositions(permit, inputVaultIds)).toStrictEqual(output);
});

test('it test the form of the borrow message', () => {
const input = {
borrowAmount: 'BORROW_AMOUNT',
maxBorrowFee: 'BORROW_FEE',
vaultId: 'VAULT_ID',
};

const output = {
vault_action: {
vault_id: input.vaultId,
msg: {
borrow: {
amount: input.borrowAmount,
max_fee: input.maxBorrowFee,
},
},
},
};
expect(msgBorrow(input)).toStrictEqual(output);
});

test('it test the form of the withdraw message', () => {
const input = {
withdrawAmount: 'WITHDRAW_AMOUNT',
vaultId: 'VAULT_ID',
};

const output = {
vault_action: {
vault_id: input.vaultId,
msg: { withdraw: input.withdrawAmount },
},
};
expect(msgWithdraw(input.withdrawAmount, input.vaultId)).toStrictEqual(output);
});
96 changes: 96 additions & 0 deletions src/contracts/definitions/lend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { AccountPermit } from '~/types/permit';

/**
* message for the getting the vault info
*/
const msgGetVault = (vaultId: string) => ({
vault: {
vault_id: vaultId,
},
});

/**
* message for the getting multiple vaults info.
* @param startingPage The data is paginated and returned
* for the starting page and all pages after. Use 1 to query all vaults.
*/
const msgGetVaults = (startingPage: number) => ({
vaults: {
starting_page: startingPage.toString(),
},
});

/**
* message for the getting a user position
*/
const msgGetVaultUserPosition = (permit: AccountPermit, vaultId: string) => ({
with_permit: {
permit,
query: {
position: {
vault_id: vaultId,
},
},
},
});

/**
* message for getting multiple user positions
*/
const msgGetVaultUserPositions = (permit: AccountPermit, vaultIds: string[]) => ({
with_permit: {
permit,
query: {
positions: {
vault_ids: vaultIds,
},
},
},
});

/**
* message to borrow silk from a lend vault
*/
const msgBorrow = ({
borrowAmount,
maxBorrowFee,
vaultId,
}: {
borrowAmount: string,
maxBorrowFee: string,
vaultId: string,
}) => ({
vault_action: {
vault_id: vaultId,
msg: {
borrow: {
amount: borrowAmount,
max_fee: maxBorrowFee,
},
},
},
});

/**
* message to withdraw collateral from a lend vault
*/
const msgWithdraw = (
withdrawAmount: string,
vaultId: string,
) => ({
vault_action: {
vault_id: vaultId,
msg: {
withdraw: withdrawAmount,
},
},
});

export {
msgGetVault,
msgGetVaults,
msgGetVaultUserPosition,
msgGetVaultUserPositions,
msgBorrow,
msgWithdraw,
};
Loading

0 comments on commit 83e21b4

Please sign in to comment.