Skip to content

在 TideBit DeFi 自定義 EIP 712 contract 辦法

Emily Liang edited this page Mar 17, 2023 · 5 revisions

基本元素

domain

interface of domain

interface IDomain {
  chainId: string;
  name: string;
  verifyingContract: string;
  version: string;
}
  • chainId: EIP-155中的鏈id aka Rinkeby testnet or Ethereum Main Net。用戶代理應當拒絕簽名如果和目前的活躍鏈不匹配的話。
  • name: 用戶可讀的簽名域名的名稱。例如Dapp的名稱或者協議。
  • verifyingContract:驗證簽名的合約地址。用戶代理可以做合約特定的網絡釣魚預防。
  • version: 簽名域名的目前主版本。不同版本的簽名不兼容。

primaryType

type of primaryType

 primaryType: string;
  • primaryType: Refers to the keys of the types object below.

types

interface of types

interface ITypes {
  [key: string]: IType[];
}

interface of type

interface IType {
  name: string;
  type: string;
  optional?: boolean;
}
  • Definition of typed structured data
  • type member類型可以是 atomic type、dynamic type 或者 reference type。
  • atomic type 有:bytes1到bytes32,uint8到uint256,int8到int256,bool和address
  • dynamic type 有bytes和string。
  • 定義 primary type 及 primary type 裡面用到的 reference type

message

IJSON

message: IJSON;

type IJSON = string | number | boolean | null | IJSON[] | {[key: string]: IJSON};
  • 根據 primary type 定義的 object
  • 如果有 optional 的參數:
    • 將 type: string 的 default 值設為 '' (空字串)
    • 將 type: uint256 的 default 值設為 0
    • 將 type: bool 的 default 值設為 false

範例

inferface of EIP712Data

interface IEIP712Data {
  domain: IDomain;
  message: IJSON;
  primaryType: string;
  types: ITypes;
}

以 create CFD order contract 為例

const CFDOrderCreate: IEIP712Data = {
  domain: {
    chainId: '0x1',
    name: 'TideBit-DeFi Create CFD Order',
    verifyingContract: '0xCAFECA5CCD019431B17B132e45e6638Ee2397be8',
    version: 'v1.0.0',
  },
  primaryType: 'CreateCFDOrderData',
  types: {
    CreateCFDOrderData: [
      {name: 'ticker', type: 'string'},
      {name: 'quotation', type: 'Quotation'},
      {name: 'typeOfPosition', type: 'string'},
      {name: 'price', type: 'uint256'},
      {name: 'amount', type: 'string'},
      {name: 'targetAsset', type: 'string'},
      {name: 'uniAsset', type: 'string'},
      {name: 'margin', type: 'Margin'},
      {name: 'leverage', type: 'uint256'},
      {name: 'liquidationPrice', type: 'uint256'},
      {name: 'liquidationTime', type: 'uint256'},
      {name: 'fee', type: 'uint256'},
      {name: 'createTimestamp', type: 'uint256'},
      {name: 'takeProfit', type: 'uint256'},
      {name: 'stopLoss', type: 'uint256'},
      {name: 'guaranteedStop', type: 'bool'},
      {name: 'guaranteedStopFee', type: 'uint256'},
      {name: 'remark', type: 'string'},
    ],
    Margin: [
      {name: 'asset', type: 'string'},
      {name: 'amount', type: 'uint256'},
    ],
    Quotation: [
      {name: 'ticker', type: 'string'},
      {name: 'targetAsset', type: 'string'},
      {name: 'uniAsset', type: 'string'},
      {name: 'price', type: 'uint256'},
      {name: 'deadline', type: 'uint256'},
      {name: 'signature', type: 'string'},
    ],
  },
  message: {
    ticker: 'ETH',
    typeOfPosition: 'BUY',
    price: 21023,
    amount: 2,
    targetAsset: 'ETH',
    uniAsset: 'USDT',
    leverage: 5,
    margin: {
      asset: 'BTC',
      amount: 112,
    },
    liquidationPrice: 19083,
    liquidationTime: 1679030710,
    createTimestamp: 1678944310,
    fee: 0,
    quotation: {
      ticker: 'ETH',
      targetAsset: 'ETH',
      uniAsset: 'USDT',
      price: 21023,
      deadline: 1678944325,
      signature: '0x',
    },
    takeProfit: 0,
    stopLoss: 0,
    guaranteedStop: 0,
    guaranteedStopFee: 0,
    remark: '',
  },
};

Ref