DeFi API

领取奖励流程#

对于领取奖励,流程指引见概述中的领取奖励金流程部分。

1. 设置你的环境#

设置环境,详情见导入必要的 Node.js 库

2. 搜索用户持仓#

以下是一个 Node.js 请求的示例。接口详情看这里

步骤 1:定义参数#

接下来,定义获取用户持仓的请求参数。

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"
};

步骤 2:定义辅助函数#

定义一个辅助函数,用于与 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;
    });
};

步骤 3:获取结果#

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

someFunction();

发送交易后,你将收到以下响应:

{
    "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. 搜索平台信息并获取投资品详情#

以下是一个 Node.js 请求的示例。接口详情看这里

步骤 1:定义参数#

定义查询投资品详情的参数。

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

步骤 2:定义辅助函数#

定义一个辅助函数,用于与 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;
    });
};

步骤 3:获取结果#

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

someFunction();

发送交易后,你将收到以下响应:

{
    "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. 生成用于领取奖励金交易的调用数据#

以下是一个 Node.js 请求的示例。接口详情看这里

步骤 1:定义参数#

定义领取奖励金业务需要的参数。

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

步骤 2:定义辅助函数#

定义一个辅助函数,用于与 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;
});
};

步骤 3:获取结果#

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

someFunction();

发送交易后,你将收到以下响应:

{
    "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. 签名和广播#

签名时可以直接使用第三步获得的 serializedData 作为 Call data ,也可以由用户通过 originalData 自己拼装 Call data 进行签名广播。EVM 类投资品参考签名示例