Main usages
1) Install:
yarn add @nested-finance/lego-contracts
# or
npm i @nested-finance/lego-contracts -S
2) Get an instance
To get an instance that can sign transactions, you have two options:
a) Specify a network, and a wallet:
import { connect } from '@nested-finance/lego-contracts';
const nested = await connect({
chain: Chain.poly,
signer: Wallet.fromMnemonic(process.env.MNEMONIC),
});
b) Specify a signer, connected to a provider:
const nested = await connect({
signer:
});
3) Try to create a portfolio
const USDC = '0x2791bca1f2de4661ed88a30c99a7a9449aa84174';
const SUSHI = '0x0b3f868e0be5597d5db7feb59e1cadbb0fdda50a';
const MATIC = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
const op = nested.createPortfolio(USDC, {
name: ...,
});
if (!(await op.isApproved())) {
await op.approve();
}
const fiveUsdc = '0x4c4b40';
await op.addToken(SUSHI, 0.03).setInputAmount(fiveUsdc);
await op.addToken(MATIC, 0.03).setInputAmount(fiveUsdc);
const {id, publicUrl} = await op.execute();
console.log(`Porfolio ${id} created, you can visualize it at ${publicUrl}`);
⚠️ important: All calls that require passing an amount are supporting:
- A BigNumber
- Hexadcimal string (which will be converted to BigNumber)
- A number, which will automatically converted to the right amount: The token decimals will be fetched, and the amount converted.
Meaning that those are equivalent:
await op.addToken(SUSHI, 0.03).setInputAmount('0x4c4b40');
await op.addToken(SUSHI, 0.03).setInputAmount(BigNumber.from('0x4c4b40'));
await op.addToken(SUSHI, 0.03).setInputAmount(5);
4) Add tokens from your wallet in an existing porfolio
const op = nested.addTokensToPortfolio(id, MATIC);
if (!(await op.isApproved())) {
await op.approve();
}
const oneMatic = '0x0de0b6b3a7640000';
await op.addToken(USDC, 0.03).setInputAmount(oneMatic);
await op.addToken(MATIC, 0.03).setInputAmount(oneMatic);
await op.execute();
5) Swap a token to other(s) token(s) within your porfolio
const op = nested.swapSingleToMulti(id, MATIC);
await op.swapTo(USDC, 0.03).setInputAmount(oneMatic);
await op.swapTo(SUSHI, 0.03).setInputAmount(oneMatic);
await op.execute();
6) Swap multiple tokens to a single token within your porfolio
const op = nested.swapMultiToSingle(id, SUSHI);
await op.swapFrom(USDC, 0.03).setInputAmount(fiveUsdc);
await op.swapFrom(SUSHI, 0.03).setInputAmount(oneSushi);
await op.execute();
7) Remove some (but not all) tokens from your porfolio, to your wallet:
const op = nested.sellTokensToWallet(id, SUSHI);
await op.sellToken(USDC, 0.03).setInputAmount(fiveUsdc);
await op.sellToken(USDC, 0.03).setInputAmount(oneMatic);
await op.execute();
8) Liquidate & destroy your whole porfolio, and get your assets back
const op = nested.liquidateToWalletAndDestroy(id, USDC, 0.3);
const quotes = await op.refreshAssets();
await op.execute();
Advanced usages:
Operation tweaking:
Each operation will return a TokenOrder
instance, that allows you to inspect and tweak the operation being configured.
For instance, lets say that you are adding tokens to a porfolio:
const token = op.addToken(USDC, 0.03);
Then several properties will help you to know what you are going to get, like token.price
and token.guaranteedPrice
.
If you have a UI to modify the added amount, you will be able to tweak it like that:
await token.setInputAmount('0x456');
or you can specify the output token amount you'd like:
await token.setOutputAmount('0x456');
There are several other uselful methods/properties on each operation. Consult the types for more details.
Prepare call data / Non signed operations:
You can get an instance that is not bound to a signer via the connect()
function. The simplest way being:
const nested = await connect({ chain: Chain.poly });
(check the types for more advanced options)
When you have such an instance, all .execute()
methods will throw an error. Instead, you can get the call data that are expected to be sent to our contracts. For instance:
const callData = await op.buildCallData();
It will contain 3 properties:
to
is the contract to calldata
is the serialized call data to pass to this conractvalue
is the value of your transaction (always 0, unless you are trying to add tokens paid for using the native token)
You can use these call data simply like that, when you have a signed provider:
const tx = await signer.sendTransaction(callData);
await tx.wait();