DeFi API
Redemption process

Redemption process#

For the redemption process, refer to the redemption process section in the overview.

1. Set up your environment#

Set up your environment. For details, please refer to the [Importing Necessary Node.js Libraries](./nodejs-environment#1.-import-the-necessary-node.js-libraries.

2. Search for user positions#

Here is an example of a Node.js request. Refer to the API reference for detailed information.

Step 1: Define parameters#

Next, define the parameters for querying user positions.

const domain = 'https://www.okx.com';
const exploreBasePath = '/api/v5/defi/explore';
const transactionBasePath = '/api/v5/defi/transaction';
const userBasePath = '/api/v5/defi/user';
const calculatorBasePath = '/api/v5/defi/calculator';
const exploreBaseUrl = domain + exploreBasePath; // Explore API base url
const transactionBaseUrl = domain + transactionBasePath; // Transaction API base url
const userBaseUrl = domain + userBasePath; // User API base url
const calculatorBaseUrl = domain + calculatorBasePath; // Calculator API base url

const getUserAssetBody = {
"address":"0x7f****da",
"investmentId":"15299",
"chainId":"137"
};

Step 2: Define helper function#

Define a helper function to interact with the DeFi API.

const getUserAssetData = async () => {
  return fetch(calculatorBaseUrl + '/asset-detail', {
    method: 'POST',
    headers: headersParams,
    body: JSON.stringify(getUserAssetBody),
  })
    .then((res) => res.json())
    .then((res) => {
      return res;
    });
};

Step 3: Get the result#

async function someFunction() {
    const { data: userAssetData } = await getUserAssetData();
    console.log(userAssetData);
}

someFunction();

After sending the transaction, you will receive the following response:

{
    "code": 0,
    "msg": "",
    "data":
    [
        {
            "investmentName": "MATIC earn MATIC",
            "investmentId": "15299",
            "investType": "5",
            "investName": "Stake",
            "assetsTokenList":
            [
                {
                    "tokenSymbol": "MATIC",
                    "tokenAddress": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
                    "network": "MATIC",
                    "tokenPrecision": "18",
                    "coinAmount": "0E-18",
                    "currencyAmount": "0"
                }
            ],
            "rewardDefiTokenInfo":
            [
                {
                    "baseDefiTokenInfos":
                    [
                        {
                            "tokenSymbol": "MATIC",
                            "tokenAddress": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
                            "network": "MATIC",
                            "tokenPrecision": "18",
                            "coinAmount": "10.907755587159277921",
                            "currencyAmount": "8.3804286176144732267043",
                            "buttonType": "3"
                        }
                    ],
                    "buttonType": "0",
                    "claimMode": "1",
                    "rewardType": "1"
                }
            ],
            "totalValue": "8.3804286176144732267043"
        }
    ]
}

3. Search platform information and get investment product details#

Here is an example of a Node.js request. For detailed information about the API, please refer to here.

Step 1: Define parameters#

Next, define the parameters for querying investment product details.

const getProductDetailParam = {
  investmentId: '21010',
  investmentCategory: ''
};

Step 2: Define helper function#

Define a helper function to interact with the DeFi API.

const getProductDetailRequestUrl = (api, queryParams) => {
  const path = api + '?' + new URLSearchParams(queryParams).toString();
  return {
    apiRequestUrl: exploreBaseUrl + path,
    path: exploreBasePath + path,
  };
};
const getProductDetailData = async () => {
  const { apiRequestUrl, path } = getProductDetailRequestUrl(
    '/product/detail',
    getProductDetailParam
  );
  return fetch(apiRequestUrl, {
    method: 'GET',
    headers: headersParams,
  })
    .then((res) => res.json())
    .then((res) => {
      return res;
    });
};

Step 3: Get the result#

async function someFunction() {
    const { data: productDetailData } = await getProductDetailData();
    console.log(productDetailData);
}

someFunction();

After sending the transaction, you will receive the following response:

{
    "code": 0,
    "msg": "",
    "data":
    {
        "investmentId": "21010",
        "investmentName": "USDC-ETH",
        "chainId": "324",
        "rate": "0.06340",
        "investType": "2",
        "platformName": "SpaceFi",
        "platformId": "491",
        "analysisPlatformId": "294",
        "poolVersion": "1",
        "rateType": "1",
        "tvl": "4464444.45647",
        "underlyingToken":
        [
            {
                "tokenSymbol": "USDC",
                "tokenAddress": "0x3355df6d4c9c3035724fd0e3914de96a5a83aaf4",
                "isBaseToken": false
            },
            {
                "tokenSymbol": "ERA_ETH",
                "tokenAddress": "0x0000000000000000000000000000000000000000",
                "isBaseToken": true
            }
        ],
        "isInvestable": true,
        "earnedToken":
        [
            {
                "tokenSymbol": "USDC",
                "tokenAddress": "0x3355df6d4c9c3035724fd0e3914de96a5a83aaf4",
                "isBaseToken": false
            },
            {
                "tokenSymbol": "ERA_ETH",
                "tokenAddress": "0x0000000000000000000000000000000000000000",
                "isBaseToken": true
            }
        ],
        "lpToken":
        [
            {
                "tokenSymbol": "SLP",
                "tokenAddress": "0xd0ce094412898760c2a5e37abec39b0e785b45ae",
                "isBaseToken": false
            }
        ],
        "subscriptionMethod": "2",
        "redeemMethod": "2"
    }
}

4. Redemption estimate calculation#

You can obtain an estimated redemption information based on the input redemption amount. Here is an example of a Node.js request. For detailed information about the API, please refer to here.

Step 1: Define parameters#

Next, define the parameters for calculating the estimated redemption information.

const getRedeemInfoBody = {
address: "0x7f****da",
inputTokenAmount: "1.000000146425127036",
investmentCategory: "0",
investmentId: "21033",
isSingle: false,
outputTokenAddress: "0x6b175474e89094c44da98b954eedeac495271d0f",
outputTokenDecimal: "18",
slippage: ""
};

Step 2: Define helper function#

Define a helper function to interact with the DeFi API.

const getRedeemInfoData = async () => {
  return fetch(calculatorBaseUrl + '/redeem-info', {
    method: 'POST',
    headers: headersParams,
    body: JSON.stringify(getRedeemInfoBody),
  })
    .then((res) => res.json())
    .then((res) => {
      return res;
    });
};

Step 3: Get the result#

async function someFunction() {
    const { data: redeemInfoData } = await getRedeemInfoData();
    console.log(redeemInfoData);
}

someFunction();

After sending the transaction, you will receive the following response:

{
    "code": 0,
    "msg": "",
    "data":
    {
        "investWithTokenList":
        [
            {
                "tokenSymbol": "CORE",
                "tokenName": "Core",
                "tokenAddress": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
                "network": "CORE",
                "chainId": "1116",
                "tokenPrecision": "18",
                "isBaseToken": true,
                "coinAmount": "1",
                "currencyAmount": "0.4918"
            }
        ],
        "gainsTokenList":
        [
            {
                "tokenSymbol": "CORE",
                "tokenName": "Core",
                "tokenAddress": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
                "network": "CORE",
                "tokenPrecision": "18",
                "coinAmount": "0.00069321843585011",
                "dataType": "0"
            }
        ],
        "approveStatusList":
        [
            {
                "tokenSymbol": "CORE",
                "tokenAddress": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
                "network": "CORE",
                "chainId": "1116",
                "tokenPrecision": "18",
                "isNeedApprove": false,
                "approveAddress": "0x0000000000000000000000000000000000001007",
                "orderType": "3"
            }
        ],
        "isSwapInvest": false,
        "estimateGasFee": "99343",
        "isAllowSubscribe": true,
        "validatorName": "OKXEarn"
    }
}

5. Generate invocation data for redemption transaction#

The subscription process for redemption involves the following steps:

5.1 Redemption authorization#

Here is an example of a Node.js request. For detailed information about the API, please refer to here.

Step 1: Define parameters#

Next, define the parameters for calculating the investment redemption authorization amount.

const userInputList = [
{
"chainId": "56",
"coinAmount": "10",
"tokenAddress": "0x526a913a7a4518aa2abc3dcd3c46a9c73f40f94a"
}
];
const getTransactionAuthorizationBody = {
address: '0x7f****da',
type: '4',
investmentId: '6925',
userInputList: userInputList
};

Step 2: Define helper function#

Define a helper function to interact with the DeFi API.

const getTransactionAuthorizationData = async () => {
return fetch(transactionBaseUrl + '/authorization', {
method: 'POST',
headers: headersParams,
body: JSON.stringify(getTransactionAuthorizationBody),
})
.then((res) => res.json())
.then((res) => {
return res;
});
};

Step 3: Get the result#

async function someFunction() {
    const { data: transactionAuthorizationData } = await getTransactionAuthorizationData();
    console.log(transactionAuthorizationData);
}

someFunction();

After sending the transaction, you will receive the following response:

{
    "code": 0,
    "msg": "",
    "data":
    {
        "dataList":
        [
            {
                "from": "0x7f****da",
                "to": "0x52****4a",
                "value": "0x0",
                "serializedData": "0x095ea7b30000000000000000000000002c34a2fb1d0b4f55de51e1d0bdefaddce6b7cdd60000000000000000000000000000000000000000000000008ac7230489e80000",
                "originalData": "{\"methodDefine\":\"approve(address,uint256)\",\"methodId\":\"0x095ea7b3\",\"methodType\":\"METHOD_ID\",\"params\":{\"params\":[\"0x2c34a2fb1d0b4f55de51e1d0bdefaddce6b7cdd6\",\"10000000000000000000\"]},\"useAdapter\":false}",
                "signatureData": "1bca0efbd1c809de94cdd8c924329c7ac79a4d346742de61925e8494f2c84c446bd3cf42b56d690c85ff513beadf46cd18f4e3e74845a92c48451584615f749d1c"
            }
        ]
    }
}

5.2 Redemption transaction#

Here is an example of a Node.js request. For detailed information about the API, please refer to here.

Step 1: Define parameters#

Next, define the parameters for querying the investment product for redemption.

const userInputList = [
   {
   "coinAmount": "0.6"
   }
   ];
const getTransactionRedemptionBody = {
  address: '0x7f****da',
  investmentId: '6925',
  userInputList: userInputList,
  extra: "{\"redeemAll\":1}"
};

Step 2: Define helper function#

Define a helper function to interact with the DeFi API.

const getTransactionRedemptionData = async () => {
  return fetch(transactionBaseUrl+'/redemption', {
    method: 'POST',
    headers: headersParams,
    body: JSON.stringify(getTransactionRedemptionBody),
  })
    .then((res) => res.json())
    .then((res) => {
      return res;
    });
};

Step 3: Get the result#

async function someFunction() {
    const { data: transactionRedemptionData } = await getTransactionRedemptionData();
    console.log(transactionRedemptionData);
}

someFunction();

After sending the transaction, you will receive the following response:

{
    "code": 0,
    "msg": "",
    "data":
    {
        "dataList":
        [
            {
                "callDataType": "WITHDRAW",
                "from": "0x7f****da",
                "to": "0x83****ea",
                "value": "0x0",
                "serializedData": "0xba08765200000000000000000000000000000000000000000000000000000000000000000000000000000000000000007f429edeff8afc7bb3a2cf7db832fc86f6fa99da0000000000000000000000007f429edeff8afc7bb3a2cf7db832fc86f6fa99da",
                "originalData": "{\"callDataType\":\"WITHDRAW\",\"methodDefine\":\"redeem(uint256,address,address)\",\"methodId\":\"0xba087652\",\"methodType\":\"METHOD_ID\",\"params\":{\"params\":[\"0\",\"0x7f429edeff8afc7bb3a2cf7db832fc86f6fa99da\",\"0x7f429edeff8afc7bb3a2cf7db832fc86f6fa99da\"]},\"useAdapter\":false}",
                "signatureData": "e6beeb6f9df6ecb7eb7791e57226f9eca1d7c3efa869172bb5021b9c827ad21b46e3dc4566608af727555e0a47aa90cf261331f4e1691b0201aa0b8a00ea685b1b"
            }
        ]
    }
}

6. Signing and broadcasting#

During the signing process, you can directly use the serializedData obtained in step 3 as callData, or you can assemble your own callData using the originalData and sign and broadcast it. For EVM-based investment products, you can refer to the signature example.