Operations

Compose operations

Example diagra
JavaScript & Node
1
//Prepare Operation 1
2
const operation1 = new defire.Operations.Operation1({
3
amountA: "70000000000000000000", //70
4
amountB: "115000000000000000000" //115
5
});
6
7
//Prepare Operation 2
8
const operation2 = new defire.Operations.Operation2({
9
amountC:: "80000000000000000000" //80
10
});
11
12
//Prepare Operation 3
13
const operation3 = new defire.Operations.Operation3({
14
amountB: "20000000000000000000" //20
15
});
16
17
//100% of Asset D from Operation 1 goes to Operation 3
18
operation1.redirectOutput({
19
asset: "0xF3320aDc1A0bfb5236c5C3a630E6Bc3cB308964C", //address of D
20
isPercentage: true,
21
amount: "1000000000000000000", //100%
22
to: operation3.address
23
}),
24
25
//190 of Asset E from Operation 2 goes to Operation 3
26
operation2.redirectOutput({
27
asset: "0x8c7C9cb53B241f5AE4DE5b13D4B894Ce9e92fD91", //address of E
28
amount: "190000000000000000000", //190
29
to: operation3.address
30
})
31
32
await defire.actions.execute([
33
operation1,
34
operation2,
35
operation3
36
]);
Copied!

Custom operations

Understand the risks to create custom operations
It is possible to create custom operations to execute DeFi instruments or any other that are not in the official list of Defire operations.

Smart Contract

To create a custom operation, the first step is to create and deploy its smart contract. The smart contract must implement this interface:
1
pragma solidity ^0.5.0;
2
3
/**
4
* @title IOperation
5
* @dev Interface for Defire operation contracts.
6
*/
7
interface IOperation {
8
/**
9
* Execute the operation.
10
* @param _inAmounts amounts of assets in.
11
* @param _params params of the operation.
12
*/
13
function operate(uint256[] calldata _inAmounts, bytes calldata _params)
14
external
15
payable
16
returns (uint256[] memory);
17
18
/**
19
* Returns the assets that the operation receives.
20
* @param _params params of the operation.
21
*/
22
function getInAssets(bytes calldata _params)
23
external
24
view
25
returns (address[] memory);
26
27
/**
28
* Returns the assets that the operation returns.
29
* @param _params params of the operation.
30
*/
31
function getOutAssets(bytes calldata _params)
32
external
33
view
34
returns (address[] memory);
35
}
Copied!
For more examples on operations, check the official ones.

Create instance

Assets in: any Assets out: any
A lender supplies DAI to the protocol and receives CDAI tokens in exchange (IOU tokens). Based on demand and supply of DAI in the market, functions of the compound protocol will calculate the interest accrued for all those issued CDAI tokens.
JavaScript & Node
1
const customOperation = new defire.Operations.Custom({
2
operationAddress: "0x4425B295B8C1Ebb250aFd556b9F2A6A3F9e6bd79",
3
amounts: ["5000000000000000000", "1500000000000000000"],
4
params: [{
5
type: "bool",
6
value: true
7
}]
8
});
Copied!
Constructor object fields
operationAddress required string
Smart contract address of the custom operation
amounts required array of string
Amounts to deposit in the right order for each asset that the operation receives.
params required array of object :
    type required string (Solidity type of operation param)
    value requiredany (operation param value)
Extra params for of the operation to execute.
Returns
Creates a new operation object

Execute from account action

There is an action method that executes the custom operation from Defire main account.
JavaScript & Node
1
//Execute single from an account
2
const result = await defire.actions.executeUnsafe(customOperation);
3
4
//Execute multiple from an account. Custom and official ones.
5
const result = await defire.actions.executeUnsafe([
6
customOperation,
7
customOperation2,
8
officialOperation3
9
]);
Copied!
Method parameters
operation required operation object | operations array of operation object
DeFi operation/s to be executed.
Returns
This method returns a Promise which resolves with true if the operation/s execution succeeded or throws an error if it failed.

Execute from portfolio action

For security reasons, only portfolios that whitelisted the custom operations on creation can execute them.
JavaScript & Node
1
//Whitelist the custom operation
2
const portfolio = await defire.actions.createPortfolio(
3
owners: ["0xF335fD8CEE75f7759b2fb05a9B3B8A9c85bb1dA3"],
4
managers: ["0xeA51f872dcaA7f4C3895092B95004a64AE680004"]
5
whitelistedOps: [
6
customOperation,
7
customOperation2,
8
officialOperation3
9
]
10
);
11
12
//Execute single from a portfolio
13
const result = await portfolio.actions.execute(customOperation);
14
15
//Execute multiple from a portfolio. Custom and official ones.
16
const result = await portfolio.actions.execute([
17
customOperation1,
18
customOperation2,
19
officialOperation3
20
]);
Copied!
Method parameters
operation required operation object | operations array of operation object
DeFi operation/s to be executed.
Returns
This method returns a Promise which resolves with true if the operation/s execution succeeded or throws an error if it failed.

Execute from fund action

For security reasons, only fund that whitelisted the custom operations on creation can execute them.
JavaScript & Node
1
//Whitelist the custom operation
2
const fund = await defire.actions.createFund(
3
managers: ["0xeA51f872dcaA7f4C3895092B95004a64AE680004"],
4
whitelistedOps: [
5
customOperation,
6
customOperation2,
7
officialOperation3
8
]
9
);
10
11
//Execute single from a fund
12
const result = await fund.actions.execute(customOperation);
13
14
//Execute multiple from a fund. Custom and official ones.
15
const result = await fund.actions.execute([
16
customOperation1,
17
customOperation2,
18
officialOperation3
19
]);
Copied!
Method parameters
operation required operation object | operations array of operation object
DeFi operation/s to be executed.
Returns
This method returns a Promise which resolves with true if the operation/s execution succeeded or throws an error if it failed.
Last modified 1yr ago