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 getRedeemInfoBody = {
address: "0x7f****da",
inputTokenAmount: "1.000000146425127036",
investmentCategory: "0",
investmentId: "21033",
isSingle: false,
outputTokenAddress: "0x6b175474e89094c44da98b954eedeac495271d0f",
outputTokenDecimal: "18",
slippage: ""
};

步骤 2:定义辅助函数#

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

步骤 3:获取结果#

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

someFunction();

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

{
    "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. 生成用于赎回交易的调用数据#

我们进行认购的流程如下:

5.1 赎回授权#

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

步骤 1:定义参数#

接下来,定义计算投资赎回授权额度的参数。

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

步骤 2:定义辅助函数#

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

步骤 3:获取结果#

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

someFunction();

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

{
    "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 赎回交易#

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

步骤 1:定义参数#

接下来,定义查询赎回投资品的参数。

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

步骤 2:定义辅助函数#

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

步骤 3:获取结果#

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

someFunction();

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

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

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