DeFi API
Reward claiming process

Reward claiming process#

For the reward claiming process, please refer to the reward claiming 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.

2. Search user positions#

Here is an example of a Node.js request.

Step 1: Define parameters#

Next, define the request parameters to retrieve 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 request, 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 retrieve investment product details#

Here is an example of a Node.js request.

Step 1: Define parameters#

Define the parameters to query 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 request, 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. Generating invocation data for claiming bonus rewards#

Here is an example of a Node.js request for generating invocation data. For detailed information about the API, refer to the documentation.

Step 1: Define parameters#

Define the parameters required for claiming bonus rewards.

const expectOutputList = [
{
"chainId": "56",
"coinAmount": "10",
"tokenAddress": "0x526a913a7a4518aa2abc3dcd3c46a9c73f40f94a"
}
];
const getTransactionBonusBody = {
address: "0x7f****da",
expectOutputList: expectOutputList,
investmentId: "21670",
extra: "{\"callDataExtJson\":\"\",\"redeemCalldataExtInfo\":[],\"rewardAddress\":\"\"}"
};

Step 2: Define helper function#

Define a helper function for interacting with the DeFi API.

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

Step 3: Get the result#

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

someFunction();

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

{
    "code": 0,
    "msg": "",
    "data":
    {
        "dataList":
        [
            {
                "callDataType": "CLAIM",
                "from": "0x7f****da",
                "to": "0xed****bd",
                "value": "0x3fdc04eea0dcf6",
                "serializedData": "0x9e4bdf3a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000",
                "originalData": "{\"callDataType\":\"CLAIM\",\"methodDefine\":\"getReward(address, bytes)\",\"methodId\":\"0x9e4bdf3a\",\"methodType\":\"METHOD_ID\",\"params\":{\"params\":[\"0x0000000000000000000000000000000000000000\",\"org.web3j.abi.datatypes.DynamicBytes@59dc08a\"]},\"useAdapter\":false}",
                "signatureData": "b19f75a8c211c752ff5c82aa130eccee229d16dd05d56874c7a9fd876c5d6d61500dc326ff0aa65b747bca2a2b5cd111e93b53deda73491d8eca97acf8cceb521c"
            }
        ]
    }
}

5. Signing and broadcasting#

When signing, you can directly use the serializedData obtained in step 3 as callData. Alternatively, users can manually assemble callData using originalData for signing and broadcasting. For an example of signing EVM-based investment products, you can refer to the signature example.