The Typescript SDK for interacting with Scallop lending protocol on SUI network
Description
This SDK is used to interact with sui-lending-protocal and is written based on another sui-integrated tool, sui-kit. It consists of three main functional modules, namely Scallop Client, Scallop Address, and Scallop txBuilder. Here's a brief introduction to each of them:
-
Client: Helps users encapsulate basic operations for interacting with the contract. Once the instance is created, it can be called directly for use.
-
Address: Used to manage the addresses of the lending contract. It's prepackaged into the client and provides the addresses of mainly production environment for customers to query addresses, usually used in conjunction with the builder.
-
Builder: Used for more detailed organization of the lending protocol's transaction blocks. You can build your own transaction combinations according to your needs by this model.
How to use
Pre-requisites
-
Installation:
pnpm install @scallop-io/sui-scallop-sdk
-
Create an instance:
const sdk = new Scallop({
networkType: 'testnet',
...
})
const client = sdk.createScallopClient(...);
const address = sdk.createAddress(...);
const txBuilder = await sdk.createTxBuilder(...);
import {
ScallopClient,
ScallopAddress,
} from '@scallop-io/sui-scallop-sdk'
const client = new ScallopClient(...);
const address = new ScallopAddress(...);
Use Client
For the original codes, please refer to test/index.spec.ts
file.
You need to setup the .env
file before testing. (Reference .env.example
)
-
Setup the network
const NETWORK: NetworkType = 'testnet';
-
Run the test
pnpm run test:unit test/index.spec.ts
-
Get Market Query Data
it('Should get market query data', async () => {
const marketData = await client.queryMarket();
console.info('marketData:', marketData);
expect(!!marketData).toBe(true);
});
-
Open Obligation Account
it('Should open a obligation account', async () => {
const openObligationResult = await client.openObligation();
console.info('openObligationResult:', openObligationResult);
expect(openObligationResult.effects.status.status).toEqual('success');
});
-
Get Obligations and Query Data
it('Should get obligations and its query data', async () => {
const obligations = await client.getObligations();
console.info('obligations', obligations);
for (const { id } of obligations) {
const obligationData = await client.queryObligation(id);
console.info('id:', id);
console.info('obligationData:');
console.dir(obligationData, { depth: null, colors: true });
expect(!!obligationData).toBe(true);
}
});
-
Get Test Coin
it('Should get test coin', async () => {
const mintTestCoinResult = await client.mintTestCoin('btc', 10 ** 11);
console.info('mintTestCoinResult:', mintTestCoinResult);
expect(mintTestCoinResult.effects.status.status).toEqual('success');
});
-
Deposit Collateral
it('Should depoist collateral successfully', async () => {
const obligations = await client.getObligations();
const depositCollateralResult = await client.depositCollateral(
'btc',
10 ** 11,
true,
obligations[0]?.id
);
console.info('depositCollateralResult:', depositCollateralResult);
expect(depositCollateralResult.effects.status.status).toEqual('success');
});
-
Withdraw Collateral
it('Should withdraw collateral successfully', async () => {
const obligations = await client.getObligations();
if (obligations.length === 0) throw Error('Obligation is required.');
const withdrawCollateralResult = await client.withdrawCollateral(
'eth',
10 ** 10,
true,
obligations[0].id,
obligations[0].keyId
);
console.info('withdrawCollateralResult:', withdrawCollateralResult);
expect(withdrawCollateralResult.effects.status.status).toEqual('success');
});
-
Deposit Asset
it('Should depoist asset successfully', async () => {
const depositResult = await client.deposit('usdc', 10 ** 10, true);
console.info('depositResult:', depositResult);
expect(depositResult.effects.status.status).toEqual('success');
});
-
Withdraw Asset
it('Should withdraw asset successfully', async () => {
const withdrawResult = await client.withdraw('usdc', 5 * 10 ** 8, true);
console.info('withdrawResult:', withdrawResult);
expect(withdrawResult.effects.status.status).toEqual('success');
});
-
Borrow Asset
it('Should borrow asset successfully', async () => {
const obligations = await client.getObligations();
if (obligations.length === 0) throw Error('Obligation is required.');
const borrowResult = await client.borrow(
'usdc',
10 ** 9,
true,
obligations[0].id,
obligations[0].keyId
);
console.info('borrowResult:', borrowResult);
expect(borrowResult.effects.status.status).toEqual('success');
});
-
Repay Asset
it('Should repay asset successfully', async () => {
const obligations = await client.getObligations();
if (obligations.length === 0) throw Error('Obligation is required.');
const repayResult = await client.repay(
'usdc',
10 ** 8,
true,
obligations[0].id
);
console.info('repayResult:', repayResult);
expect(repayResult.effects.status.status).toEqual('success');
});
-
Flash Loan
it('Should flash loan successfully', async () => {
const flashLoanResult = await client.flashLoan(
'usdc',
10 ** 9,
(txBlock, coin) => {
return coin;
},
true
);
console.info('flashLoanResult:', flashLoanResult);
expect(flashLoanResult.effects.status.status).toEqual('success');
});
Use Scallop Transaction Builder
Use Scallop Transaction Builder
Use address manager
General Users will basically only use the get
, getAddresses
or getAllAddresses
methods to read addresses. Here are some simple examples:
const address = new ScallopAddress({
id: TEST_ADDRESSES_ID,
network: NETWORK,
});
await addressBuilder.read();
const address = addressBuilder.get('core.coins.usdc.id');
const addresses = addressBuilder.getAddresses();
const allAddresses = addressBuilder.getAllAddresses();
Scallop currently maintains this address 6462a088a7ace142bb6d7e9b
for use in the production environment.
Of course, you can also directly use the sui-scallop-api project to directly request an addresses.
The rest of the features are for Scallop administrators to use, and require a set of API authentication key to use the create, update, and delete address functions.
const address = new ScallopAddress({
id: TEST_ADDRESSES_ID,
auth: process.env.API_KEY,
network: NETWORK,
});
const addresses = await addressBuilder.create(...);
const allAddresses = await addressBuilder.update(id, ...);
const allAddresses = await addressBuilder.delete(id, ...);
License
APACHE-2.0