記號分類架構的鷹架式 TypeScript 專案

Blockchain App Builder 會從您的權杖規格檔案進行輸入,並產生功能完整的鷹架式鏈碼專案。

專案會自動產生記號週期類別和函數,包括 CRUD 和非 CRUD 方法。系統會自動支援引數驗證、封送處理 (Marshal) / 解除封送處理 (unmarshal) 以及透明保存功能。

如需鷹架專案的相關資訊,以及與記號無關的方法,請參閱 Scaffolded TypeScript Chaincode Project

Model

每個記號化模型類別都會擴充 Token 類別,進而擴充 OchainModel 類別。Token 類別是從 ../lib/token 匯入。在 OchainModel 類別中擷取「通透保存功能 (Transparent Persistence Capability)」或簡化的 ORM。

import * as yup from 'yup';
import { Id, Mandatory, Validate, ReadOnly } from '../lib/decorators';
import { Token } from '../lib/token';
 
@Id('token_id')
export class Digicur extends Token<Digicur> {
 
    public readonly assetType = 'otoken';
 
    @Mandatory()
    @Validate(yup.string().required().matches(/^[A-Za-z0-9][A-Za-z0-9_-]*$/).max(16))
    public token_id: string;
 
    @ReadOnly('digicur')
    public token_name: string;
 
    @Validate(yup.string().trim().max(256))
    public token_desc: string;
 
    @ReadOnly('fungible')
    public token_type: string;
 
    @ReadOnly(["divisible","mintable","transferable","burnable","holdable","roles"])
    public behaviors: string[];
 
    @ReadOnly({minter_role_name: "minter", burner_role_name: "burner", notary_role_name: "notary"})
    public roles: object;
 
    @ReadOnly({max_mint_quantity: 20000})
    public mintable: object;
 
    @ReadOnly({decimal: 1})
    public divisible: object;
 
    @Validate(yup.number())
    public token_to_currency_ratio: number;
 
    @Validate(yup.string())
    public currency_representation: string;
 
}

控制器

主控制器類別延伸了 OchainController 類別。只有一個主控制器。

export class DigiCurrCCController extends OchainController{

您可以建立不限數目的類別、函數或檔案,但只能呼叫在主控制器類別中定義的方法。其他方法則為隱藏。

您可以使用記號 SDK 方法,為商業應用程式撰寫自訂方法。

自動產生的記號方法

Blockchain App Builder 會自動產生支援權杖和權杖生命週期的方法。您可以使用這些方法來初始化記號、管理角色和帳戶,以及完成其他記號生命週期工作,而無需進行任何其他編碼。控制器方法必須要有 @Validator(...params) 修飾器才能呼叫。

存取控制管理的方法

addTokenAdmin
此方法會將使用者新增為鏈碼的 Token Admin。鏈碼的 Token Admin 只能呼叫此方法。
@Validator(yup.string(), yup.string())
public async addTokenAdmin(org_id: string, user_id: string) {
    await this.Ctx.Auth.checkAuthorization('ADMIN.addAdmin', 'TOKEN');
    return await this.Ctx.Admin.addAdmin(org_id, user_id);
}
參數:
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,包含新增為鏈碼 Token Admin 之使用者詳細資訊的訊息。
傳回值範例:
{"msg":"Successfully added Admin (Org_Id: Org1MSP, User_Id: User1)"}
removeTokenAdmin
此方法會將使用者移除為鏈碼的 Token Admin。鏈碼的 Token Admin 只能呼叫此方法。
@Validator(yup.string(), yup.string())
public async removeTokenAdmin(org_id: string, user_id: string) {
    await this.Ctx.Auth.checkAuthorization('ADMIN.removeAdmin', 'TOKEN');
    return await this.Ctx.Admin.removeAdmin(org_id, user_id);
}
參數:
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,包含以鏈碼 Token Admin 移除之使用者詳細資訊的訊息。
傳回值範例:
{"msg": "Successfully removed Admin (Org_Id: Org1MSP, User_Id: User1)"}
isTokenAdmin
如果函數的呼叫程式是 Token Admin,此方法會傳回布林值 true,否則會傳回 falseToken AdminOrg Admin 可在區塊鏈網路中的任何其他使用者呼叫此功能。其他使用者只能在自己的帳戶呼叫此方法。
@Validator(yup.string(), yup.string())
  public async isTokenAdmin(org_id: string, user_id: string) {
    await this.Ctx.Auth.checkAuthorization("ADMIN.isUserTokenAdmin", "TOKEN");
    return await this.Ctx.Auth.isUserTokenAdmin(org_id, user_id);
  }
參數:
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 如果呼叫程式是 Token Admin,此方法會傳回 true,否則會傳回 false
getAllTokenAdmins
此方法會傳回屬於鏈碼 Token Admin 的所有使用者清單。此方法只能由鏈碼的 Token Admin 或任何 Org Admin 呼叫。
@Validator()
public async getAllTokenAdmins() {
    await this.Ctx.Auth.checkAuthorization('ADMIN.getAllAdmins', 'TOKEN');
    return await this.Ctx.Admin.getAllAdmins();
}
參數:
傳回值:
  • 成功時,JSON 格式的 admins 陣列包含 orgIduserId 物件。
傳回值範例:
{"admins":[{"org_id":"Org1MSP","user_id":"admin"}]}
addOrgAdmin
此方法會將使用者新增為組織的 Org Admin。此方法只能由鏈碼的 Token Admin 或指定組織的 Org Admin 呼叫。
@Validator(yup.string(), yup.string())
  public async addOrgAdmin(org_id: string, user_id: string) {
    await this.Ctx.Auth.checkAuthorization("ADMIN.addOrgAdmin", "TOKEN", { org_id });
    return await this.Ctx.Admin.addOrgAdmin(org_id, user_id);
  }
參數:
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,包含新增為組織 Org Admin 之使用者詳細資訊的訊息。
傳回值範例:
{
    "msg": "Successfully added Org Admin (Org_Id: Org1MSP, User_Id: orgAdmin)"
}
removeOrgAdmin
此方法會將使用者移除為組織的 Org Admin。此方法只能由鏈碼的 Token Admin 或由指定組織的 Org Admin 呼叫。
@Validator(yup.string(), yup.string())
  public async removeOrgAdmin(org_id: string, user_id: string) {
    await this.Ctx.Auth.checkAuthorization("ADMIN.removeOrgAdmin", "TOKEN", { org_id });
    return await this.Ctx.Admin.removeOrgAdmin(org_id, user_id);
  }
參數:
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,包含作為組織 Org Admin 移除之使用者詳細資訊的訊息。
傳回值範例:
{
  "msg": "Successfully removed Org Admin (Org_Id Org1MSP User_Id orgAdmin)"
}
getOrgAdmins
此方法會傳回組織 Org Admin 的所有使用者清單。此方法只能由鏈碼的 Token Admin 或任何組織的 Org Admin 呼叫。
  @Validator()
  public async getOrgAdmins() {
    await this.Ctx.Auth.checkAuthorization("ADMIN.getOrgAdmins", "TOKEN");
    return await this.Ctx.Admin.getAllOrgAdmins();
  }
參數:
傳回值:
  • 成功時,JSON 格式的陣列包含 orgIduserId 物件。
傳回值範例:
{
    "admins": [
        {
            "org_id": "Org1MSP",
            "user_id": "orgadmin"
        },
        {
            "org_id": "Org1MSP",
            "user_id": "orgadmin1"
        },
        {
            "org_id": "Org1MSP",
            "user_id": "orgadmin2"
        }
    ]
}

記號組態管理的方法

init
部署或升級鏈碼時會呼叫此方法。每個 Token Admin 都是由必要 adminList 參數中的 user_idorg_id 資訊所識別。user_id 是執行處理擁有者或登入執行處理之使用者的使用者名稱或電子郵件 ID。org_id 是目前網路組織中使用者的成員服務提供者 (MSP) ID。
任何 Token Admin 使用者都可以透過呼叫 addAdminremoveAdmin 方法來新增和移除其他 Token Admin 使用者。
public async init(adminList: TokenAdminAsset[]) {
    await this.Ctx.Admin.initAdmin(adminList);
    return;
}
參數:
  • adminList array - 指定記號管理員清單的 {user_id, org_id} 資訊陣列。adminList 陣列是必要參數。
參數範例:Mac OSX 與 Linux CLI:
'[{"user_id":"userid", "org_id":"OrgMSPId"}]'
參數範例:Microsoft Windows CLI:
"[{\"user_id\":\"userid\", \"org_id\":\"OrgMSPId\"}]"
參數範例:Oracle Blockchain Platform 主控台:
["[{\"user_id\":\"userid\", \"org_id\":\"OrgMSPId\"}]"]
initialize<Token Name>Token
此方法會建立記號並初始化記號特性。資產及其特性會儲存在狀態資料庫中。此方法只能由鏈碼的 Token Admin 呼叫。
@Validator(Digicur)
    public async initializeDigicurToken(token_asset: Digicur) {
        await this.Ctx.Auth.checkAuthorization('TOKEN.save', 'TOKEN');
        return await this.Ctx.Token.save(token_asset)
    }
參數:
  • asset: <Token Class> - 將記號資產當作此方法的參數傳送。記號資產的特性會在模型檔案中說明。
傳回值:
  • 成功時,所建立權杖資產的 JSON 表示法。
傳回值範例:
{
    "assetType": "otoken",
    "token_id": "digiCurr101",
    "token_name": "digicur",
    "token_type": "fungible",
    "behaviors": [
        "divisible",
        "mintable",
        "transferable",
        "burnable",
        "roles"
    ],
    "roles": {
        "minter_role_name": "minter"
    },
    "mintable": {
        "max_mint_quantity": 1000
    },
    "divisible": {
        "decimal": 2
    },
    "currency_name": "DOLLAR",
    "token_to_currency_ratio": 1
}
update<Token Name>Token
此方法會更新記號特性。建立權杖資產之後,只能更新 token_desc 特性和自訂特性。鏈碼的 Token Admin 只能呼叫此方法。
@Validator(Digicur)
public async updateDigicurToken(token_asset: Digicur) {
    await this.Ctx.Auth.checkAuthorization('TOKEN.update', 'TOKEN');
    return await this.Ctx.Token.update(token_asset);
}
參數:
  • asset: <Token Class> - 將記號資產當作此方法的參數傳送。記號資產的特性會在模型檔案中說明。
傳回值:
  • 成功時,憑證資產的更新 JSON 表示法。
傳回值範例:
{
    "assetType": "otoken",
    "token_id": "digiCurr101",
    "token_name": "digicur",
    "token_desc": "Digital Currency equiv of dollar",
    "token_type": "fungible",
    "behaviors": [
        "divisible",
        "mintable",
        "transferable",
        "burnable",
        "roles"
    ],
    "roles": {
        "minter_role_name": "minter"
    },
    "mintable": {
        "max_mint_quantity": 1000
    },
    "divisible": {
        "decimal": 2
    },
    "currency_name": "DOLLAR",
    "token_to_currency_ratio": 1
}
getTokenDecimals
此方法會傳回為小數記號設定的小數位數。如果未指定記號的 divisible 行為,則預設值為 0。此方法只能由鏈碼的 Token AdminOrg Admin 呼叫。
@Validator(yup.string())
public async getTokenDecimals(token_id: string) {
    const token_asset = await this.getTokenObject(token_id);
    await this.Ctx.Auth.checkAuthorization('TOKEN.getDecimals', 'TOKEN');
    return {
        msg: `Token Id: ${token_id} has ${this.Ctx.Token.getDecimals(token_asset)} decimal places.`
    };
}
參數:
  • token_id: string - 記號的 ID。
傳回值:
  • 成功時,會顯示記號小數位數的 JSON 字串。
傳回值範例:
{"msg":"Token Id: digiCurr101 has 1 decimal places."}
getTokenById
如果權杖物件存在於狀態資料庫中,這個方法就會傳回該物件。此方法只能由鏈碼的 Token AdminOrg Admin 呼叫。
@Validator(yup.string())
public async getTokenById(token_id: string) {
    await this.Ctx.Auth.checkAuthorization('TOKEN.get', 'TOKEN');
    const token = await this.getTokenObject(token_id);
    return token;
}
參數:
  • token_id: string - 記號的 ID。
傳回值:
  • 成功時,代表權杖資產的 JSON 物件。
傳回值範例:
{
    "assetType": "otoken",
    "token_id": "digiCurr101",
    "token_name": "digicur",
    "token_desc": "Digital Currency equiv of dollar",
    "token_type": "fungible",
    "behaviors": [
        "divisible",
        "mintable",
        "transferable",
        "burnable",
        "roles"
    ],
    "roles": {
        "minter_role_name": "minter"
        "burner_role_name": "burner",
        "notary_role_name": "notary"
    },
    "mintable": {
        "max_mint_quantity": 2000
    },
    "divisible": {
        "decimal": 1
    },
    "currency_name": "DOLLAR",
    "token_to_currency_ratio": 1
}
getTokenHistory
此方法會傳回指定記號 ID 的記號歷史記錄。任何使用者都可以呼叫此方法。
  @Validator(yup.string())
  public async getTokenHistory(tokenId: string) {
    await this.Ctx.Auth.checkAuthorization("TOKEN.getTokenHistory", "TOKEN");
    return await this.Ctx.Token.history(tokenId);
  }
參數:
  • tokenId: string - 記號的 ID。
傳回值:
  • 成功時,代表權杖歷史記錄的 JSON 物件。
傳回值範例:

[
    {
        "trxId": "0d75f09446a60088afb948c6aca046e261fddcd43df416076201cdc5565f1a35",
        "timeStamp": "2023-09-01T16:48:41.000Z",
        "value": {
            "assetType": "otoken",
            "token_id": "token",
            "token_name": "fiatmoneytok",
            "token_desc": "updatedDesc",
            "token_standard": "ttf+",
            "token_type": "fungible",
            "token_unit": "fractional",
            "behaviors": [
                "divisible",
                "mintable",
                "transferable",
                "burnable",
                "roles"
            ],
            "roles": {
                "minter_role_name": "minter"
            },
            "mintable": {
                "max_mint_quantity": 1000
            },
            "divisible": {
                "decimal": 2
            }
        }
    },
    {
        "trxId": "3666344878b043b65d5b821cc79c042ba52aec467618800df5cf14eac69f72fa",
        "timeStamp": "2023-08-31T20:24:55.000Z",
        "value": {
            "assetType": "otoken",
            "token_id": "token",
            "token_name": "fiatmoneytok",
            "token_standard": "ttf+",
            "token_type": "fungible",
            "token_unit": "fractional",
            "behaviors": [
                "divisible",
                "mintable",
                "transferable",
                "burnable",
                "roles"
            ],
            "roles": {
                "minter_role_name": "minter"
            },
            "mintable": {
                "max_mint_quantity": 1000
            },
            "divisible": {
                "decimal": 2
            }
        }
    }
]
getAllTokens
此方法會傳回所有儲存在狀態資料庫中的記號。此方法只能由鏈碼的 Token AdminOrg Admin 呼叫。此方法使用 Berkeley 資料庫 SQL 豐富查詢,且只能在連線至遠端 Oracle Blockchain Platform 網路時呼叫。
@Validator()
public async getAllTokens() {
    await this.Ctx.Auth.checkAuthorization('TOKEN.getAllTokens', 'TOKEN');
    return await this.Ctx.Token.getAllTokens();
}
參數:
傳回值:
  • 成功時,代表所有權杖資產的 JSON 物件。
getTokensByName
此方法會傳回具有指定名稱的所有記號物件。此方法只能由鏈碼的 Token AdminOrg Admin 呼叫。此方法使用 Berkeley 資料庫 SQL 豐富查詢,且只能在連線至遠端 Oracle Blockchain Platform 網路時呼叫。
@Validator(yup.string())
public async getTokensByName(token_name: string) {
    await this.Ctx.Auth.checkAuthorization('TOKEN.getTokensByName', 'TOKEN');
    return await this.Ctx.Token.getTokensByName(token_name);
}
參數:
  • token_name: string - 要擷取的記號名稱。此名稱對應至規格檔案中的 token_name 特性。值是記號的類別名稱。
傳回值:
  • 成功時,符合名稱之所有權杖資產的 JSON 物件。

帳戶管理方法

createAccount
此方法會為指定的使用者和記號建立一個帳戶。必須為任何時候會有權杖的任何使用者建立帳戶。帳戶會追蹤餘額、保留餘額及交易歷史記錄。帳戶 ID 是一組文數字字元,前面加上 oaccount~<token asset name>~,後面接著執行處理擁有者的使用者名稱或電子郵件 ID (user_id) 雜湊,或登入執行處理的使用者,即目前網路組織中使用者的成員服務提供者 ID (org_id)。此方法只能由鏈碼的 Token Admin 或由指定組織的 Org Admin 呼叫。
  @Validator(yup.string(), yup.string(), yup.string())
  public async createAccount(org_id: string, user_id: string, token_type: string) {
    await this.Ctx.Auth.checkAuthorization("ACCOUNT.createAccount", "TOKEN", { org_id });
    return await this.Ctx.Account.createAccount(org_id, user_id, token_type);
  }
參數:
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
  • token_type: string - 記號的類型,必須是 fungible
傳回值:
  • 成功時,所建立帳戶的 JSON 物件。bapAccountVersion 參數定義於帳戶物件中供內部使用。
傳回值範例:
{
  "assetType": "oaccount",
  "account_id": "oaccount~abc74791148b761352b98df58035601b6f5480448ac2b4a3a7eb54bdbebf48eb",
  "bapAccountVersion": 0,
  "user_id": "admin",
  "org_id": "Org1MSP",
  "token_type": "fungible",
  "token_id": "",
  "token_name": "",
  "balance": 0,
  "onhold_balance": 0
}
associateTokenToAccount
此方法會將有趣的記號與帳戶建立關聯。此方法只能由鏈碼的 Token Admin 或相關組織的 Org Admin 呼叫。
  @Validator(yup.string(), yup.string())
  public async associateTokenToAccount(account_id: string, token_id: string) {
    await this.Ctx.Auth.checkAuthorization("ACCOUNT.associateToken", "TOKEN", { account_id });
    return await this.Ctx.Account.associateToken(account_id, token_id);
  }
參數:
  • account_id: string - 帳戶的 ID。
  • token_id: string - 記號的 ID。
傳回值:
  • 成功時,更新帳戶的 JSON 物件。bapAccountVersion 參數定義於帳戶物件中供內部使用。
傳回值範例:
{
    "assetType": "oaccount",
    "account_id": "oaccount~abc74791148b761352b98df58035601b6f5480448ac2b4a3a7eb54bdbebf48eb",
    "bapAccountVersion": 0,
    "user_id": "admin",
    "org_id": "Org1MSP",
    "token_type": "fungible",
    "token_id": "fungible",
    "token_name": "fiatmoneytok",
    "balance": 0,
    "onhold_balance": 0
}
getAccount
此方法會傳回指定之使用者和記號的帳戶詳細資訊。此方法只能由鏈碼的 Token Admin、指定組織的 Org Admin 或帳戶的 AccountOwner 呼叫。
@Validator(yup.string(), yup.string(), yup.string())
public async getAccount(token_id: string, org_id: string, user_id: string) {
  const account_id = await this.Ctx.Account.generateAccountId(token_id, org_id, user_id);
  await this.Ctx.Auth.checkAuthorization("ACCOUNT.getAccount", "TOKEN", { account_id });
  return await this.Ctx.Account.getAccountWithStatus(account_id);
}
參數:
  • token_id: string - 記號的 ID。
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,包含下列特性的 JSON 帳戶物件:
  • account_id - 使用者帳戶的 ID。
  • user_id - 使用者的使用者名稱或電子郵件 ID。
  • org_id - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • token_id - 記號的 ID。
  • token_name - 記號的名稱。
  • balance - 帳戶的目前餘額。
  • onhold_balance - 帳戶的目前保留餘額。
  • bapAccountVersion - 內部使用的帳戶物件參數。
  • status - 使用者帳戶的目前狀態。
傳回值範例:
{
  "bapAccountVersion": 0,
  "assetType": "oaccount",
  "status": "active",
  "account_id": "oaccount~2de8db6b91964f8c9009136831126d3cfa94e1d00c4285c1ea3e6d1f36479ed4",
  "user_id": "idcqa",
  "org_id": "appdev",
  "token_type": "fungible",
  "token_id": "t1",
  "token_name": "obptok",
  "balance": 0,
  "onhold_balance": 0
}
getAccountHistory
此方法會傳回指定之使用者和記號的帳戶歷史記錄詳細資訊。此方法只能由鏈碼的 Token Admin 或帳戶的 AccountOwner 呼叫。
  @Validator(yup.string(), yup.string(), yup.string())
  public async getAccountHistory(token_id: string, org_id: string, user_id: string) {
    const account_id = await this.Ctx.Account.generateAccountId(token_id, org_id, user_id);
    await this.Ctx.Auth.checkAuthorization("ACCOUNT.history", "TOKEN", { account_id });
    return await this.Ctx.Account.history(account_id);
  }
參數:
  • token_id: string - 記號的 ID。
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,包含下列特性的 JSON 帳戶物件陣列:
  • trxId - 分類帳所傳回之交易的交易 ID。
  • timeStamp - 異動的時間。
  • value - 帳戶物件的 JSON 字串。
傳回值範例:
[
    {
      "trxId":"2gsdh17fff222467e5667be042e33ce18e804b3e065cca15de306f837e416d7c3e",
      "timeStamp":1629718288,
      "value":{
         "assetType":"oaccount",
         "account_id":"oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
         "user_id":"user1",
         "org_id":"Org1MSP",
         "token_id":"digiCurr101",
         "token_name":"digicur",
         "balance":100,
         "onhold_balance":0,
         "bapAccountVersion": 1
   },
   {
      "trxId":"9fd07fff222467e5667be042e33ce18e804b3e065cca15de306f837e416d7c3e",
      "timeStamp":1629718288,
      "value":{
         "assetType":"oaccount",
         "account_id":"oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
         "user_id":"user1",
         "org_id":"Org1MSP",
         "token_id":"digiCurr101",
         "token_name":"digicur",
         "balance":0,
         "onhold_balance":0,
         "bapAccountVersion": 0
      }
   }
]
getAccountOnHoldBalance
此方法會傳回指定帳戶與變數替代字的目前保留餘額。此方法只能由鏈碼的 Token Admin、指定組織的 Org Admin 或帳戶的 AccountOwner 呼叫。
  @Validator(yup.string(), yup.string(), yup.string())
  public async getAccountOnHoldBalance(token_id: string, org_id: string, user_id: string) {
    const account_id = await this.Ctx.Account.generateAccountId(token_id, org_id, user_id);
    await this.Ctx.Auth.checkAuthorization("ACCOUNT.getAccountOnHoldBalance", "TOKEN", { account_id });
    return await this.Ctx.Account.getAccountOnHoldBalance(account_id);
  }
參數:
  • token_id: string - 記號的 ID。
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,代表目前保留餘額的 JSON。
傳回值範例:
{"msg":"Total Holding Balance is: 0","holding_balance":0}
getAllAccounts
此方法會傳回所有帳戶的清單。鏈碼的 Token Admin 只能呼叫此方法。此方法使用 Berkeley 資料庫 SQL 豐富查詢,且只能在連線至遠端 Oracle Blockchain Platform 網路時呼叫。
@Validator()
public async getAllAccounts() {
    await this.Ctx.Auth.checkAuthorization('ACCOUNT.getAllAccounts', 'TOKEN');
    return await this.Ctx.Account.getAllAccounts();
}
參數:
傳回值:
  • 成功時,所有帳戶的 JSON 陣列。
getUserByAccountId
此方法會傳回指定帳戶的使用者詳細資訊 (org_iduser_id)。鏈碼的任何使用者都可以呼叫此方法。
@Validator(yup.string())
public async getUserByAccountId(account_id: string) {
    return await this.Ctx.Account.getUserByAccountId(account_id);
}
參數:
  • account_id: string - 帳戶的 ID。
傳回值:
  • 成功時,使用者詳細資料 (org_idtoken_iduser_id) 的 JSON 物件。
傳回值範例:
{
    "token_id": "digiCurr101",
    "user_id": "user1",
    "org_id": "Org1MSP"
}
getAccountBalance
此方法會傳回指定帳戶和權杖的目前餘額。此方法只能由鏈碼的 Token Admin、指定組織的 Org Admin 或帳戶的 AccountOwner 呼叫。
  @Validator(yup.string(), yup.string(), yup.string())
  public async getAccountBalance(token_id: string, org_id: string, user_id: string) {
    const account_id = await this.Ctx.Account.generateAccountId(token_id, org_id, user_id);
    await this.Ctx.Auth.checkAuthorization("ACCOUNT.getAccountBalance", "TOKEN", { account_id });
    return await this.Ctx.Account.getAccountBalance(account_id);
  }
參數:
  • token_id: string - 記號的 ID。
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,代表目前帳戶餘額的 JSON。
傳回值範例:
{"msg":"Current Balance is: 0","user_balance":0}
getAllOrgAccounts
此方法會傳回屬於指定組織的所有權杖帳戶清單。此方法只能由鏈碼的 Token Admin 或由指定組織的 Org Admin 呼叫。
  @Validator(yup.string())
  public async getAllOrgAccounts(org_id: string) {
    await this.Ctx.Auth.checkAuthorization("ACCOUNT.getAllOrgAccounts", "TOKEN", { org_id });
    return await this.Ctx.Account.getAllOrgAccounts(org_id);
  }
參數:
  • org_id: string - 組織的成員服務提供者 (MSP) ID。
傳回值:
  • 成功時,指定組織的所有帳戶清單。
傳回值範例:
[
    {
        "key": "oaccount~2de8db6b91964f8c9009136831126d3cfa94e1d00c4285c1ea3e6d1f36479ed4",
        "valueJson": {
            "bapAccountVersion": 0,
            "assetType": "oaccount",
            "account_id": "oaccount~2de8db6b91964f8c9009136831126d3cfa94e1d00c4285c1ea3e6d1f36479ed4",
            "user_id": "idcqa",
            "org_id": "appdev",
            "token_type": "fungible",
            "token_id": "token",
            "token_name": "fiatmoneytok",
            "balance": 0,
            "onhold_balance": 0
        }
    },
    {
        "key": "oaccount~620fcf5deb5fd5a65c0b5b10fda129de0f629ccd232c5891c130e24a574df50a",
        "valueJson": {
            "bapAccountVersion": 0,
            "assetType": "oaccount",
            "account_id": "oaccount~620fcf5deb5fd5a65c0b5b10fda129de0f629ccd232c5891c130e24a574df50a",
            "user_id": "example_minter",
            "org_id": "appdev",
            "token_type": "fungible",
            "token_id": "token",
            "token_name": "fiatmoneytok",
            "balance": 0,
            "onhold_balance": 0
        }
    }
]

角色管理方法

addRole
這個方法會將角色新增至指定的使用者和記號。此方法只能由鏈碼的 Token Admin 或由同時具備指定角色之指定組織的 Org Admin 呼叫。
  @Validator(yup.string(), yup.string(), yup.string(), yup.string())
  public async addRole(token_id: string, role: string, org_id: string, user_id: string) {
    const token_asset = await this.getTokenObject(token_id);
    const account_id = await this.Ctx.Account.generateAccountId(token_id, org_id, user_id);
    await this.Ctx.Auth.checkAuthorization("TOKEN.addRoleMember", "TOKEN", { token_id, org_id, role });
    return await this.Ctx.Token.addRoleMember(role, account_id, token_asset);
  }
參數:
  • token_id: string - 記號的 ID。
  • role: string - 要新增至指定使用者的角色名稱。mintableburnable 行為對應至規格檔案的 minter_role_nameburner_role_name 特性。同樣地,notary 角色會對應至規格檔案的 notary_role_name 特性。
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,會出現含有帳戶詳細資訊的訊息。
傳回值範例:
{"msg":"Successfully added role 'minter' to Account Id: oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f (Org-Id: Org1MSP, User-Id: user1)"}
removeRole
此方法會從指定的使用者和記號移除角色。此方法只能由鏈碼的 Token Admin 或由同時具備指定角色之指定組織的 Org Admin 呼叫。
  @Validator(yup.string(), yup.string(), yup.string(), yup.string())
  public async removeRole(token_id: string, role: string, org_id: string, user_id: string) {
    const token_asset = await this.getTokenObject(token_id);
    const account_id = await this.Ctx.Account.generateAccountId(token_id, org_id, user_id);
    await this.Ctx.Auth.checkAuthorization("TOKEN.removeRoleMember", "TOKEN", { token_id, org_id, role });
    return await this.Ctx.Token.removeRoleMember(role, account_id, token_asset);
  }
參數:
  • token_id: string - 記號的 ID。
  • role: string - 要從指定使用者移除的角色名稱。mintableburnable 行為對應至規格檔案的 minter_role_nameburner_role_name 特性。同樣地,notary 角色會對應至規格檔案的 notary_role_name 特性。
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,會出現含有帳戶詳細資訊的訊息。
傳回值範例:
{"msg":"Successfully removed role 'minter' from Account Id: oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f (Org-Id: Org1MSP, User-Id: user1)"}
getAccountsByRole
此方法會傳回指定角色和權杖的所有帳戶 ID 清單。鏈碼的 Token Admin 只能呼叫此方法。
@Validator(yup.string(), yup.string())
public async getAccountsByRole(token_id: string, role: string) {
   await this.Ctx.Auth.checkAuthorization('ROLE.getAccountsByRole', 'TOKEN');
   return await this.Ctx.Role.getAccountsByRole(token_id, role);
}
參數:
  • token_id: string - 記號的 ID。
  • role: string - 要搜尋的角色名稱。
傳回值:
  • 成功時,帳戶 ID 的 JSON 陣列。
傳回值範例:
{"accounts":["oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f"]}
getAccountsByUser
此方法會傳回指定之組織 ID 與使用者 ID 的所有帳戶 ID 清單。此方法只能由鏈碼的 Token Admin、指定組織的 Org Admin 或參數中指定的 Account Owner 呼叫。
  @Validator(yup.string(), yup.string())
  public async getAccountsByUser(org_id: string, user_id: string) {
    await this.Ctx.Auth.checkAuthorization("ACCOUNT.getAccountsByUser", "TOKEN", { org_id, user_id });
    return await this.Ctx.Account.getAccountsByUser(org_id, user_id);
  }
參數:
  • org_id string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,帳戶 ID 的 JSON 陣列。
傳回值範例:
{"accounts":["oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f"]}
getUsersByRole
此方法會針對指定的角色和記號,傳回所有使用者的清單。鏈碼的 Token Admin 只能呼叫此方法。
@Validator(yup.string(), yup.string())
public async getUsersByRole(token_id: string, role: string) {
    await this.Ctx.Auth.checkAuthorization('ROLE.getUsersByRole', 'TOKEN');
    return await this.Ctx.Role.getUsersByRole(token_id, role);
}
參數:
  • token_id: string - 記號的 ID。
  • role: string - 要搜尋的角色名稱。
傳回值:
  • 成功時,使用者物件的 JSON 陣列 (org_idtoken_iduser_id)。
傳回值範例:
{"users":[{"token_id":"digiCurr101","user_id":"user1","org_id":"Org1MSP"}]}
isInRole
此方法會傳回布林值,指示使用者和記號是否具有指定的角色。此方法只能由鏈碼的 Token Admin、帳戶的 AccountOwner 或指定組織的 Org Admin 呼叫。
  @Validator(yup.string(), yup.string(), yup.string(), yup.string())
  public async isInRole(token_id: string, org_id: string, user_id: string, role: string) {
    const token_asset = await this.getTokenObject(token_id);
    const account_id = await this.Ctx.Account.generateAccountId(token_id, org_id, user_id);
    await this.Ctx.Auth.checkAuthorization("TOKEN.isInRole", "TOKEN", { account_id });
    return { result: await this.Ctx.Token.isInRole(role, account_id, token_asset) };
  }
參數:
  • token_id: string - 記號的 ID。
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
  • role: string - 要搜尋的角色名稱。
傳回值:
  • 成功時,布林結果的 JSON 字串。
傳回值範例:
{"result":"false"}
getOrgAccountsByRole
此方法會傳回在指定組織中具有指定角色之所有帳戶的相關資訊。此方法只能由鏈碼的 Token Admin 或由指定組織的 Org Admin 呼叫。
   @Validator(yup.string(), yup.string(), yup.string())
  public async getOrgAccountsByRole(token_id: string, role: string, org_id: string) {
    await this.Ctx.Auth.checkAuthorization("ROLE.getOrgAccountsByRole", "TOKEN", { org_id });
    return await this.Ctx.Role.getOrgAccountsByRole(token_id, role, org_id);
  }
參數:
  • token_id: string - 記號的 ID。
  • role: string - 要檢查之角色的名稱。
  • org_id: string - 組織的成員服務提供者 (MSP) ID。
傳回值:
  • 成功時,指定組織中具有指定角色的所有帳戶清單。
傳回值範例:
{
    "accounts": [
        "oaccount~abc74791148b761352b98df58035601b6f5480448ac2b4a3a7eb54bdbebf48eb",
        "oaccount~9c650574af9025a6106c8d12a801b079eda9ae2e3399fc2fbd5bd683d738a850"
    ]
}
getOrgUsersByRole
此方法會傳回在指定組織中具有指定角色之所有使用者的相關資訊。此方法只能由鏈碼的 Token Admin 或由指定組織的 Org Admin 呼叫。
  @Validator(yup.string(), yup.string(), yup.string())
  public async getOrgUsersByRole(token_id: string, role: string, org_id: string) {
    await this.Ctx.Auth.checkAuthorization("ROLE.getOrgUsersByRole", "TOKEN", { org_id });
    return await this.Ctx.Role.getOrgUsersByRole(token_id, role, org_id);
  }
參數:
  • token_id: string - 記號的 ID。
  • role: string - 要檢查之角色的名稱。
  • org_id: string - 組織的成員服務提供者 (MSP) ID。
傳回值:
  • 成功時,指定組織中具有指定角色的所有使用者清單。
傳回值範例:
{
    "users": [
        {
            "token_id": "token",
            "user_id": "admin",
            "org_id": "Org1MSP"
        },
        {
            "token_id": "token",
            "user_id": "orgAdmin",
            "org_id": "Org1MSP"
        }
    ]
}

交易歷史記錄管理的方法

getAccountTransactionHistory
此方法會傳回指定使用者和權杖的帳戶交易歷史記錄明細陣列。此方法只能由鏈碼的 Token Admin、指定組織的 Org Admin 或帳戶的 AccountOwner 呼叫。
 @Validator(yup.string(), yup.string(), yup.string())
  public async getAccountTransactionHistory(token_id: string, org_id: string, user_id: string) {
    const account_id = await this.Ctx.Account.generateAccountId(token_id, org_id, user_id);
    await this.Ctx.Auth.checkAuthorization("ACCOUNT.getAccountTransactionHistory", "TOKEN", { account_id });
    return await this.Ctx.Account.getAccountTransactionHistory(account_id, org_id, user_id.toLowerCase());
  }
參數:
  • token_id: string - 記號的 ID。
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,包含下列特性的 JSON 帳戶交易物件陣列:
  • transaction_id - 交易的 ID。
  • transacted_account - 進行交易的帳戶。
  • transaction_type - 交易類型。
  • transacted_amount - 交易的金額。
  • timestamp - 異動的時間。
  • balance - 交易時的帳戶餘額。
  • onhold_balance - 交易時的保留餘額。
  • token_id - 記號的 ID。
  • holding_id - holdTokens 方法所傳回的唯一 ID。
傳回值範例:
[
    {
        "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775",
        "transacted_amount": 20,
        "timestamp": "2021-08-17T06:04:24.000Z",
        "balance": 930,
        "onhold_balance": 0,
        "token_id": "digiCurr101",
        "transaction_type": "BULKTRANSFER",
        "sub_transactions": [
            {
                "transacted_account": "oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
                "transaction_type": "DEBIT",
                "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775~c4ca4238a0b923820dcc509a6f75849b",
                "transacted_amount": 10
            },
            {
                "transacted_account": "oaccount~digicur~38848e87296d67c8a90918f78cf55f9c9baab2cdc8c928535471aaa1210c706e",
                "transaction_type": "DEBIT",
                "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775~c81e728d9d4c2f636f067f89cc14862c",
                "transacted_amount": 10
            }
        ]
    },
    {
        "transaction_id": "otransaction~757864d5369bd0539d044caeb3bb4898db310fd7aa740f45a9938771903d43da",
        "transacted_amount": 50,
        "timestamp": "2021-08-17T06:02:44.000Z",
        "balance": 950,
        "onhold_balance": 0,
        "token_id": "digiCurr101",
        "transacted_account": "oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
        "transaction_type": "DEBIT"
    }
]
getAccountTransactionHistoryWithFilters
此方法會傳回指定使用者和權杖的帳戶交易歷史記錄明細陣列。此方法只能由鏈碼的 Token Admin、指定組織的 Org Admin 或帳戶的 AccountOwner 呼叫。連線至遠端 Oracle Blockchain Platform 網路時才能呼叫此方法。
  @Validator(yup.string(), yup.string(), yup.string(), yup.object().nullable())
  public async getAccountTransactionHistoryWithFilters(token_id: string, org_id: string, user_id: string, filters?: Filters) {
    const account_id = await this.Ctx.Account.generateAccountId(token_id, org_id, user_id);
    await this.Ctx.Auth.checkAuthorization("ACCOUNT.getAccountTransactionHistoryWithFilters", "TOKEN", { account_id });
    return await this.Ctx.Account.getAccountTransactionHistoryWithFilters(account_id, org_id, user_id.toLowerCase(), filters);
  }
參數:
  • token_id: string - 記號的 ID。
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
  • filters: string - 選擇性參數。如果空白,則會傳回所有記錄。PageSize 特性決定要傳回的記錄數目。如果 PageSize 為 0,則預設頁面大小為 20。Bookmark 特性決定要傳回之記錄的開始索引。如需詳細資訊,請參閱 Hyperledger Fabric 文件。必須以 RFC-3339 格式指定 StartTimeEndTime 特性。
範例:

ochain invoke GetAccountTransactionHistoryWithFilters 'token1' 'appbuilder12' 'user_minter' '{"PageSize":10,"Bookmark":"1","StartTime":"2022-01-25T17:41:42Z","EndTime":"2022-01-25T17:59:10Z"}'

[
  {
    "transaction_id": "otransaction~672897b5a4fa78b421c000e4d6d4f71f3d46529bfbb5b4be10bf5471dc35ce89",
    "transacted_amount": 5,
    "timestamp": "2022-04-20T15:46:04.000Z",
    "token_id": "tokenId",
    "transacted_account": "oaccount~16c38d804413ebabf416360d374f76c973d4e71c74adfde73cc40c7c274883b8",
    "transaction_type": "DEBIT",
    "balance": 90,
    "onhold_balance": 0
  },
  {
    "transaction_id": "otransaction~467bb67a33aaffca4487f33dcd46c9844efdb5421a2e7b6aa2d53152eb2c6d85",
    "transacted_amount": 5,
    "timestamp": "2022-04-20T15:45:47.000Z",
    "token_id": "tokenId",
    "transacted_account": "oaccount~fbf95683b21bbc91a22205819ac1e2e9c90355d536821ed3fe22b7d23915c248",
    "transaction_type": "DEBIT",
    "balance": 95,
    "onhold_balance": 0
  },
  {
    "transaction_id": "otransaction~c6d56ce54a9bbe24597d1d10448e39316dc6f16328bf3c5b0c8ef10e1dfeb397",
    "transacted_amount": 100,
    "timestamp": "2022-04-20T15:44:26.000Z",
    "token_id": "tokenId",
    "transacted_account": "oaccount~deb5fb0906c40506f6c2d00c573b774e01a53dd91499e651d92ac4778b6add6a",
    "transaction_type": "MINT",
    "balance": 100,
    "onhold_balance": 0
  }
]
getSubTransactionById
此方法會傳回指定使用者和權杖的帳戶交易歷史記錄明細陣列。此方法只能由鏈碼的 Token Admin 或帳戶的 AccountOwner 呼叫。
  @Validator(yup.string())
  public async getSubTransactionsById(transaction_id: string) {
    await this.Ctx.Auth.checkAuthorization("ACCOUNT.getSubTransactionsById", "TOKEN", { transaction_id });
    return await this.Ctx.Account.getSubTransactionsById(transaction_id);
  }
參數:
  • transaction_id: string - 大量傳輸交易的 ID。
傳回值:
  • 指定之大量移轉交易 ID 的 JSON 格式帳戶子交易物件陣列。
範例:

ochain invoke GetAccountSubTransactionHistory 'otransaction~21972b4d206bd52ea77924efb259c67217edb23b4386580d1bee696f6f864b9b'

[
    {
        "transacted_account": "oaccount~16c38d804413ebabf416360d374f76c973d4e71c74adfde73cc40c7c274883b8",
        "transaction_type": "DEBIT",
        "transaction_id": "otransaction~6e0f8fe4a6430322170b9c619b04b6c9f1c8d257923f611b866bdf69d7fe6cb8~c81e728d9d4c2f636f067f89cc14862c",
        "transacted_amount": 5,
        "timestamp": "2022-04-20T15:52:21.000Z",
        "token_id": "token1",
        "balance": 80,
        "onhold_balance": 0
    },
    {
        "transacted_account": "oaccount~fbf95683b21bbc91a22205819ac1e2e9c90355d536821ed3fe22b7d23915c248",
        "transaction_type": "DEBIT",
        "transaction_id": "otransaction~6e0f8fe4a6430322170b9c619b04b6c9f1c8d257923f611b866bdf69d7fe6cb8~c4ca4238a0b923820dcc509a6f75849b",
        "transacted_amount": 5,
        "timestamp": "2022-04-20T15:52:21.000Z",
        "token_id": "token1",
        "balance": 85,
        "onhold_balance": 0
    }
]
getSubTransactionsByIdWithFilters
此方式會傳回指定交易的帳戶子交易歷史記錄明細陣列。
  @Validator(yup.string(), yup.object().nullable())
  public async getSubTransactionsByIdWithFilters(transaction_id: string, filters?: SubTransactionFilters) {
    await this.Ctx.Auth.checkAuthorization("ACCOUNT.getSubTransactionsByIdWithFilters", "TOKEN", { transaction_id });
    return await this.Ctx.Account.getSubTransactionsByIdWithFilters(transaction_id, filters);
  } 
參數:
  • transaction_id: string - 交易的 ID。
  • filters: string - 選擇性參數。如果空白,則會傳回所有記錄。PageSize 特性決定要傳回的記錄數目。如果 PageSize 為 0,則預設頁面大小為 20。Bookmark 特性決定要傳回之記錄的開始索引。如需詳細資訊,請參閱 Hyperledger Fabric 文件。必須以 RFC-3339 格式指定 StartTimeEndTime 特性。
傳回值:
  • 指定之大量移轉交易 ID 的 JSON 格式帳戶子交易物件陣列。
範例:

ochain invoke GetAccountSubTransactionHistoryWithFilters 'otransaction~21972b4d206bd52ea77924efb259c67217edb23b4386580d1bee696f6f864b9b' '{"PageSize":10,"Bookmark":"1"}'

[
  {
    "transaction_id": "otransaction~6e0f8fe4a6430322170b9c619b04b6c9f1c8d257923f611b866bdf69d7fe6cb8~c81e728d9d4c2f636f067f89cc14862c",
    "transacted_amount": 5,
    "timestamp": "2022-04-20T15:52:21.000Z",
    "token_id": "tokenId",
    "transacted_account": "oaccount~16c38d804413ebabf416360d374f76c973d4e71c74adfde73cc40c7c274883b8",
    "transaction_type": "DEBIT",
    "balance": 80,
    "onhold_balance": 0
  },
  {
    "transaction_id": "otransaction~6e0f8fe4a6430322170b9c619b04b6c9f1c8d257923f611b866bdf69d7fe6cb8~c4ca4238a0b923820dcc509a6f75849b",
    "transacted_amount": 5,
    "timestamp": "2022-04-20T15:52:21.000Z",
    "token_id": "tokenId",
    "transacted_account": "oaccount~fbf95683b21bbc91a22205819ac1e2e9c90355d536821ed3fe22b7d23915c248",
    "transaction_type": "DEBIT",
    "balance": 85,
    "onhold_balance": 0
  }
]
getTransactionById
此方法會傳回 Transaction 資產的歷史記錄。
@Validator(yup.string())
    public async getTransactionById(transaction_id: string) {
        return await this.Ctx.Transaction.getTransactionById(transaction_id);
    }
參數:
  • transaction_id string - 交易資產的 ID。
傳回值:
  • 成功時,交易歷史記錄的 JSON 陣列。
傳回值範例:
{
    "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775",
    "history": [
        {
            "trxId": "68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775",
            "timeStamp": 1629180264,
            "value": {
                "assetType": "otransaction",
                "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775",
                "token_id": "digiCurr101",
                "from_account_id": "oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df",
                "to_account_id": "",
                "transaction_type": "BULKTRANSFER",
                "amount": 20,
                "timestamp": "2021-08-17T06:04:24.000Z",
                "number_of_sub_transactions": 2,
                "holding_id": ""
            }
        }
    ],
    "sub_transactions": [
        {
            "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775~c4ca4238a0b923820dcc509a6f75849b",
            "history": [
                {
                    "trxId": "68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775",
                    "timeStamp": 1629180264,
                    "value": {
                        "assetType": "otransaction",
                        "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775~c4ca4238a0b923820dcc509a6f75849b",
                        "token_id": "digiCurr101",
                        "from_account_id": "oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df",
                        "to_account_id": "oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
                        "transaction_type": "TRANSFER",
                        "amount": 10,
                        "timestamp": "2021-08-17T06:04:24.000Z",
                        "number_of_sub_transactions": 0,
                        "holding_id": ""
                    }
                }
            ]
        },
        {
            "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775~c81e728d9d4c2f636f067f89cc14862c",
            "history": [
                {
                    "trxId": "68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775",
                    "timeStamp": 1629180264,
                    "value": {
                        "assetType": "otransaction",
                        "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775~c81e728d9d4c2f636f067f89cc14862c",
                        "token_id": "digiCurr101",
                        "from_account_id": "oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df",
                        "to_account_id": "oaccount~digicur~38848e87296d67c8a90918f78cf55f9c9baab2cdc8c928535471aaa1210c706e",
                        "transaction_type": "TRANSFER",
                        "amount": 10,
                        "timestamp": "2021-08-17T06:04:24.000Z",
                        "number_of_sub_transactions": 0,
                        "holding_id": ""
                    }
                }
            ]
        }
    ]
}
deleteHistoricalTransactions
此方法會從狀態資料庫中刪除較舊的交易。
@Validator(yup.date())
    public async deleteHistoricalTransactions(time_to_expiration: Date) {
        await this.Ctx.Auth.checkAuthorization('TRANSACTION.deleteTransactions', 'TOKEN');
        return await this.Ctx.Transaction.deleteTransactions(time_to_expiration);
    }
參數:
  • time_to_expiration Date - 表示何時刪除交易的時戳。將會刪除早於指定時間的交易資產。
傳回值範例:
"payload": {
    "msg": "Successfuly deleted transaction older than date: Thu Aug 19 2021 11:19:36 GMT+0000 (Coordinated Universal Time).",
    "transactions": [
        "otransaction~ec3366dd48b4ce2838f820f2f138648e6e55a07226713e59b411ff31b0d21058"
    ]
}

記號行為管理方法 - 可調整行為

issueTokens
這個方法會提示記號,然後由方法的呼叫程式所擁有。呼叫者必須具有帳戶與較小者角色。可提示的記號數目受規格檔案中 mintable 行為的 max_mint_quantity 特性限制。如果未指定 max_mint_quantity 特性,則可提示不限數目的記號。數量必須在規格檔案中 divisible 行為的 decimal 參數所指定的小數值內。只有具備較小角色的帳戶 AccountOwner 才能呼叫此方法。
@Validator(yup.string(), yup.number().positive())
public async issueTokens(token_id: string, quantity: number) {
    const token_asset = await this.getTokenObject(token_id);
    return await this.Ctx.Token.mint(quantity, token_asset);
}
參數:
  • token_id: string - 記號的 ID。
  • quantity - 要進行 mint 的記號數目。
傳回值:
  • 成功時,會出現含有帳戶詳細資訊的訊息。
傳回值範例:
{
    "msg": "Successfully minted 1000 tokens to Account Id: \
oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df (Org-Id: Org1MSP, User-Id: user1)  ",
}
getTotalMintedTokens
此方法會傳回指定記號的 Minted 記號總數。此方法只能由鏈碼的 Token Admin 或任何 Org Admin 呼叫。
@Validator(yup.string())
 public async getTotalMintedTokens(token_id: string) {
     const token_asset = await this.getTokenObject(token_id);
     await this.Ctx.Auth.checkAuthorization('TOKEN.getTotalMintedTokens', 'TOKEN');
     const totalMintedTokens = await this.Ctx.Token.getTotalMintedTokens(token_asset);
     return {
         msg: `Total minted token for Token Id: ${token_id} is ${totalMintedTokens} tokens.`,
         quantity: totalMintedTokens
     };
 }
參數:
  • token_id: string - 記號的 ID。
傳回值:
  • 成功時,表示記號總數的 JSON 字串。
傳回值範例:
{"msg":"Total minted token for Token Id: digiCurr101 is 100 tokens.","quantity":100}
getNetTokens
此方法會傳回系統中特定記號可用的記號總數。淨記號總計是記號燒錄後剩餘的記號量。在方程式形式中:淨記號 = 提示的記號總計 - 消耗的記號總計。如果沒有燒錄記號,則淨記號的數目等於提示的記號總數。此方法只能由鏈碼的 Token Admin 或任何 Org Admin 呼叫。
@Validator(yup.string())
public async getNetTokens(token_id: string) {
	const token_asset = await this.getTokenObject(token_id);
	await this.Ctx.Auth.checkAuthorization('TOKEN.getNetTokens', 'TOKEN');
	const netTokens = await this.Ctx.Token.getNetTokens(token_asset);
	return {
		msg: `Net supply of token for Token Id: ${token_id} is ${netTokens} tokens.`,
		quantity: netTokens
	};
}
參數:
  • token_id: string - 記號的 ID。
傳回值:
  • 成功時,表示記號淨數的 JSON 字串。
傳回值範例:
{"msg":"Net supply of token for Token Id: digiCurr101 is 0 tokens.","quantity":0}

記號行為管理方法 - 可傳輸行為

transferTokens
此方法會將記號從呼叫程式傳輸至指定的帳戶。方法的呼叫程式必須要有一個帳戶。數量必須在規格檔案中 divisible 行為的 decimal 參數所指定的小數值內。此方法只能由帳戶的 AccountOwner 呼叫。
@Validator(yup.string(), yup.string(), yup.string(), yup.number().positive())
public async transferTokens(token_id: string, to_org_id: string, to_user_id: string, quantity: number) {
   const token_asset = await this.getTokenObject(token_id);
   const to_account_id = await this.Ctx.Account.generateAccountId(token_id, to_org_id, to_user_id);
   return await this.Ctx.Token.transfer(to_account_id, quantity, token_asset);
}
參數:
  • token_id: string - 記號的 ID。
  • to_org_id: string - 目前組織中接收者 (受款人) 的成員服務提供者 (MSP) ID。
  • to_user_id: string - 接收者的使用者名稱或電子郵件 ID。
  • quantity: number - 要傳輸的記號數目。
傳回值:
  • 成功時,包含付款人與受款人帳戶明細的訊息。
傳回值範例:
{
    "msg": "Successfully transferred 400 tokens from account id: oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df (Org-Id: Org1MSP, User-Id: user1) to account id: oaccount~digicur~682bb71de419602af74e3f226345ef308445ca51010737900c112435f676152df (Org-Id: Org1MSP, User-Id: user2) ",
}
bulkTransferTokens
此方法是用來執行從呼叫程式帳戶到 flow 物件中指定帳戶的大量記號傳輸。此方法之規格 file.The 呼叫程式中 divisible 行為的 decimal 參數所指定的數量必須在已建立帳戶的小數值內。此方法只能由帳戶的 AccountOwner 呼叫。
@Validator(yup.string(), yup.array().of(yup.object()))
public async bulkTransferTokens(token_id: string, flow: object[]) {
     const token_asset = await this.getTokenObject(token_id);
     return await this.Ctx.Token.bulkTransfer(flow, token_asset);
}
參數:
  • token_id: string - 記號的 ID。
  • flow : object[] - 指定接收者和數量的 JSON 物件陣列。
    [{
    	"to_org_id": "Org1MSP",
    	"to_user_id": "user1",
    	"quantity": 10
    }, {
    	"to_org_id": "Org1MSP",
    	"to_user_id": "user2",
    	"quantity": 10
    }]
    • to_orgId: string - 目前組織中接收者的成員服務提供者 (MSP) ID。
    • userId: string - 接收者的使用者名稱或電子郵件 ID。
    • quantity: number - 要傳輸的記號數目。
傳回值:
  • 表示成功的訊息。
傳回值範例:
{
    "msg": "Successfully transferred 20 tokens from Account Id           'oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df' (Org-Id: Org1MSP, User-Id: admin).",
    "from_account_id": "oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df",
    "sub_transactions": [
        {
            "to_account_id": "oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
            "amount": 10
        },
        {
            "to_account_id": "oaccount~digicur~38848e87296d67c8a90918f78cf55f9c9baab2cdc8c928535471aaa1210c706e",
            "amount": 10
        }
    ]
}

記號行為管理方法 - 可保留行為

holdTokens
此方法會代表記號擁有者的 to_account_id 帳戶建立保留。已指定公證人帳戶,其負責完成或解除保留。建立保留時,付款人的指定變數替代字餘額會設為保留。保留餘額必須等到完成或解除後才能移轉。這個方法的呼叫程式必須已經建立帳戶。此方法只能由帳戶的 AccountOwner 呼叫。
@Validator(yup.string(), yup.string(), yup.string(), yup.string(), yup.string(), yup.string(), yup.number().positive(), yup.date())
public async holdTokens(
    token_id: string,
    operation_id: string,
    to_org_id: string,
    to_user_id: string,
    notary_org_id: string,
    notary_user_id: string,
    quantity: number,
    time_to_expiration: Date
) {
    const token_asset = await this.getTokenObject(token_id);
    const to_account_id = await this.Ctx.Account.generateAccountId(token_id, to_org_id, to_user_id);
    const notary_account_id = await this.Ctx.Account.generateAccountId(token_id, notary_org_id, notary_user_id);
    return await this.Ctx.Token.hold(operation_id, to_account_id, notary_account_id, quantity, time_to_expiration, token_asset);
}
參數:
  • token_id: string - 記號的 ID。
  • operation_id: string - 識別保留作業的唯一 ID。通常這個 ID 會由用戶端應用程式傳送。
  • to_org_id: string - 目前組織中接收者的成員服務提供者 (MSP) ID。
  • to_user_id: string - 接收者的使用者名稱或電子郵件 ID。
  • notary_org_id: string - 目前組織中公證人的會員服務提供者 (MSP) ID。
  • notary_user_id: string - 公證人的使用者名稱或電子郵件 ID。
  • quantity: number - 要保留的記號數目。
  • time_to_expiration - 保留到期的時間。為永久保留指定 0 。否則,請採用 RFC-3339 格式。例如,2021-06-02T12:46:06Z
傳回值:
  • 成功時,包含來電者帳戶與保留明細的訊息。
傳回值範例:
{
  "msg":"AccountId oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df (Org-Id: Org1MSP , User-Id: admin) is   successfully holding 10 tokens"
}
executeHoldTokens
這個方法會完成記號的保留。權杖擁有者之前持有的權杖數量會轉移給接收者。如果 quantity 值小於實際保留值,則記號的原始擁有者可再使用剩餘金額。只有具有指定作業 ID 之 notary 角色的 AccountOwner ID 才能呼叫此方法。保留只能由公證人完成。
@Validator(yup.string(), yup.string(), yup.number().positive())
public async executeHoldTokens(token_id: string, operation_id: string, quantity: number) {
    const token_asset = await this.getTokenObject(token_id);
    return await this.Ctx.Token.executeHold(operation_id, quantity, token_asset);
}
參數:
  • token_id: string - 記號的 ID。
  • operation_id: string - 識別保留作業的唯一 ID。通常這個 ID 會由用戶端應用程式傳送。
  • quantity: number - 要傳輸的保留記號數目。
傳回值:
  • 成功時,包含來電者帳戶 ID 與交易數量的訊息。
傳回值範例:
{
 "msg":"Account Id: oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df (Org-Id: Org1MSP, User-Id: admin) is successfully executed '10' tokens from Operation Id 'opr_121'."
}
releaseHoldTokens
此方法會解除記號的保留。移轉未完成,所有保留的變數替代字會再次可供原始擁有者使用。AccountOwner ID 可在指定的時間限制內以 notary 角色呼叫此方法,或在指定的時間限制之後由付款人、受款人或公證人呼叫。
@Validator(yup.string(), yup.string())
public async releaseHoldTokens(token_id: string, operation_id: string) {
    const token_asset = await this.getTokenObject(token_id);
    return await this.Ctx.Token.releaseHold(operation_id, token_asset);
}
參數:
  • token_id: string - 記號的 ID。
  • operation_id: string - 識別保留作業的唯一 ID。通常這個 ID 會由用戶端應用程式傳送。
傳回值:
  • 成功時,表示已解除保留的訊息。
傳回值範例:
{
 "msg":"Successfully released '10' tokens from Operation Id 'opr_121' to Account Id: oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df (Org-Id: Org1MSP, User-Id: user1)."
}
getOnHoldIds
這個方法會傳回指定帳戶之所有持有 ID 的清單。此方法可由鏈碼的 Token Admin、指定組織的 Org Admin 或帳戶的 AccountOwner 呼叫。
  @Validator(yup.string(), yup.string(), yup.string())
  public async getOnHoldIds(token_id: string, org_id: string, user_id: string) {
    const account_id = await this.Ctx.Account.generateAccountId(token_id, org_id, user_id);
    await this.Ctx.Auth.checkAuthorization("ACCOUNT.getOnHoldIds", "TOKEN", { account_id });
    return await this.Ctx.Account.getOnHoldIds(account_id);
  }
參數:
  • token_id: string - 記號的 ID。
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,JSON 保留 ID 清單。
傳回值範例:
{"msg":"Holding Ids are: ohold~digicur~digiCurr101~opr_121","holding_ids":["ohold~digicur~digiCurr101~opr_121"]}
getOnHoldDetailsWithOperationId
此方法會傳回指定作業 ID 和記號的保留交易詳細資訊。任何人都可以呼叫這個方法。
@Validator(yup.string(), yup.string())
public async getOnHoldDetailsWithOperationId(token_id: string, operation_id: string) {
    return await this.Ctx.Hold.getOnHoldDetailsWithOperationId(token_id, operation_id);
}
參數:
  • token_id: string - 記號的 ID。
  • operation_id: string - 識別保留作業的唯一 ID。通常這個 ID 會由用戶端應用程式傳送。
傳回值:
  • 成功時,包含下列特性的 JSON 保留物件:
  • holding_id - 交易的持有 ID。
  • operation_id: string - 識別保留作業的唯一 ID。通常這個 ID 會由用戶端應用程式傳送。
  • from_account_id - 保留權杖目前擁有者的帳戶 ID。
  • to_account_id - 接收者的帳戶 ID。
  • notary_account_id - 公證人的帳戶 ID。
  • token_id: string - 已儲存記號的 ID。
  • quantity - 保留 ID 的記號數量。
  • time_to_expiration - 保留到期的持續時間。
傳回值範例:
{
    "assetType": "ohold",
    "holding_id": "ohold~digicur~digiCurr101~opr_121",
    "operation_id": "opr_121",
    "token_name": "digicur",
    "from_account_id": "oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df",
    "to_account_id": "oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
    "notary_account_id": "oaccount~digicur~38848e87296d67c8a90918f78cf55f9c9baab2cdc8c928535471aaa1210c706e",
    "token_id": "digiCurr101",
    "quantity": 10,
    "time_to_expiration": "2022-08-01T18:30:00.000Z"
}
getOnHoldBalanceWithOperationId
此方法會傳回指定作業 ID 與變數替代字的保留餘額。任何人都可以呼叫這個方法。
@Validator(yup.string(), yup.string())
public async getOnHoldBalanceWithOperationId(token_id: string, operation_id: string) {
    return await this.Ctx.Hold.getOnHoldBalanceWithOperationId(token_id, operation_id);
}
參數:
  • token_id: string - 記號的 ID。
  • operation_id: string - 識別保留作業的唯一 ID。通常這個 ID 會由用戶端應用程式傳送。
傳回值:
  • 成功時,表示保留餘額的 JSON 字串。
傳回值範例:
{
	"msg": "Current Holding Balance of Operation 'opr_121' for token 'digiCurr101' is: 10",
	"holding_balance": 10
}

記號行為管理方法 - 可燒錄行為

burnTokens
此方法會停用或燒錄來自交易呼叫程式帳戶的記號。這個方法的呼叫程式必須具有一個帳戶以及 burner 角色。數量必須在規格檔案中 divisible 行為的 decimal 參數所指定的小數值內。帳戶的 AccountOwner 可以使用工作人員角色呼叫此方法。
@Validator(yup.string(), yup.number().positive())
public async burnTokens(token_id: string, quantity: number) {
    const token_asset = await this.getTokenObject(token_id);
    return await this.Ctx.Token.burn(quantity, token_asset);
}
參數:
  • token_id: string - 記號的 ID。
  • quantity - 要燒錄的記號數目。
傳回值:
  • 成功時,會出現一則成功訊息,內含已燒錄記號的數量與帳戶 ID。
傳回值範例:
{
    "msg": "Successfully burned 10 tokens from account id: oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df (Org-Id: Org1MSP, User-Id: admin)"
}

自訂方法

您可以使用記號 SDK 方法,為商業應用程式撰寫自訂方法。

為了避免重複花費,請勿結合在狀態資料庫中相同索引鍵 - 值組上運作的多個非同步函數。請改用 bulkTransferTokens 方法,在單一方法中進行多重傳輸。

下列範例顯示如何在自訂方法中使用記號 SDK 方法。呼叫 buyTicket 方法時,它會從來電者帳戶將 20 個權杖傳輸至賣方帳戶,並傳回轉帳的交易訊息。

@Validator(yup.string(), yup.string(), yup.string(), yup.string(), yup.string())
public async buyTicket(token_id: string, seller_org_id: string, seller_user_id: string) {
	const token = await this.getTokenObject(token_id);

	/**
	* The following method this.Ctx.Account.generateAccountId(token_id, seller_org_id, seller_user_id) generates account id of the seller.
	*/
	const seller_account_id = await this.Ctx.Account.generateAccountId(token_id, seller_org_id, seller_user_id);

	/**
	* The following method this.Ctx.Token.transfer(seller_account_id, 20, token) transfers the quantity 20 from caller's
	* account & to seller's account.
	*/
	const transaction = await this.Ctx.Token.transfer(seller_account_id, 20, token);

	return transaction;
}

如果您在自訂方法中使用多個記號 SDK 方法,請勿使用會影響狀態資料庫中相同索引鍵 - 值組的方法。下列範例顯示進行多重調動的正確方法:

@Validator(yup.string(), yup.string(), yup.string(), yup.string(), yup.string())
public async sendTokens(token_id: string, user1_org_id: string, user1_user_id: string, user2_org_id: string, user2_user_id: string) {
    const token = await this.getTokenObject(token_id);
    const user1_account_id = await Account.generateAccountId(token_id, user1_org_id, user1_user_id);
    const user2_account_id = await Account.generateAccountId(token_id, user2_org_id, user2_user_id);
    await token.transfer(user1_account_id, 20);
    await token.transfer(user2_account_id, 30);
}

請改用 bulkTransferTokens 方法,從呼叫端的帳戶轉移到多個帳戶,如以下程式碼片段所示。

bulkTransferTokens(token_id: string, flow: object[])

附註:

如果您在自訂方法中使用多個可能影響狀態資料庫中相同索引鍵 - 值組的記號 SDK 方法,請啟用記號鏈碼的 MVCC 最佳化。如需詳細資訊,請參閱 MVCC 最佳化

權杖 SDK 方法

存取控制管理的方法

權杖 SDK 提供存取控制函數。部分方法只能由記號的 Token AdminOrg AdminAccountOwner 呼叫。您可以使用此功能來確保作業只由預期的使用者執行。任何未經授權的存取都會導致錯誤。若要使用存取控制功能,請從 ../lib/auth 模組匯入 Authorization 類別。
import { Authorization } from '../lib/auth';
addAdmin
此方法會將使用者新增為記號鏈碼的 Token Admin
Ctx.Admin.addAdmin(org_id: string, user_id: string)
參數:
  • user_id - 使用者的使用者名稱或電子郵件 ID。
  • org_id - 目前網路組織中使用者的成員服務提供者 (MSP) ID。
傳回值:
  • 成功時,含有 JSON 物件的承諾訊息會列出新增為權杖鏈碼之 Token Admin 的使用者詳細資訊。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
    "msg": "Successfully added Admin (Org_Id: Org1MSP, User_Id: user1)"
}
removeAdmin
此方法會將使用者移除為記號鏈碼的 Token Admin
Ctx.Admin.removeAdmin(org_id: string, user_id: string)
參數:
  • user_id - 使用者的使用者名稱或電子郵件 ID。
  • org_id - 目前網路組織中使用者的成員服務提供者 (MSP) ID。
傳回值:
  • 成功時,含有 JSON 物件的承諾訊息會列出不再是記號鏈碼之 Token Admin 的使用者詳細資訊。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
    "msg": "Successfully removed Admin (Org_Id: Org1MSP, User_Id: user1)"
}
getAllAdmins
此方法會傳回 Token Admin 記號鏈碼的所有使用者清單。
Ctx.Admin.getAllAdmins()
參數:
傳回值:
  • 成功時,使用 JSON 物件的承諾會列出記號鏈碼 Token Admin 的所有使用者詳細資訊。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
    "admins": [
        {
            "orgId": "Org1MSP",
            "userId": "admin"
        }
    ]
}
checkAuthorization
您可以使用此方法將存取控制檢查新增至作業。某些記號方法只能由記號的 Token AdminAccountOwner 執行,或由具有多個帳戶的使用者的 MultipleAccountOwner 執行。../lib/constant.ts 檔案中描述了存取控制對應。您可以編輯 ../lib/constant.ts 檔案來修改存取控制。若要使用您自己的存取控制或停用存取控制,請從自動產生的控制器方法與自訂方法移除存取控制程式碼。
export const TOKENACCESS = {
  ADMIN: {
    isUserTokenAdmin: ["Admin", "OrgAdmin"],
    addTokenAdmin: ["Admin"],
    removeTokenAdmin: ["Admin"],
    getAllAdmins: ["Admin", "OrgAdmin"],
    addOrgAdmin: ["Admin", "OrgAdminForOrgId"],
    removeOrgAdmin: ["Admin", "OrgAdminForOrgId"],
    getOrgAdmins: ["Admin", "OrgAdmin"],
  },
  TOKEN: {
    save: ["Admin"],
    getAllTokens: ["Admin", "OrgAdmin"],
    get: ["Admin", "OrgAdmin"],
    update: ["Admin"],
    getDecimals: ["Admin", "OrgAdmin"],
    getTokensByName: ["Admin", "OrgAdmin"],
    addRoleMember: ["Admin", "OrgAdminRoleCheck"],
    removeRoleMember: ["Admin", "OrgAdminRoleCheck"],
    isInRole: ["Admin", "OrgAdminForAccountId", "AccountOwner"],
    getTotalMintedTokens: ["Admin", "OrgAdmin"],
    getNetTokens: ["Admin", "OrgAdmin"],
    getTokenHistory: ["Admin", "OrgAdmin"],
  },
  ROLE: {
    getAccountsByRole: ["Admin"],
    getOrgAccountsByRole: ["Admin", "OrgAdminForOrgId"],
    getUsersByRole: ["Admin"],
    getOrgUsersByRole: ["Admin", "OrgAdminForOrgId"],
  },
  TRANSACTION: {
    deleteTransactions: ["Admin"],
  },ACCOUNT: {
    createAccount: ["Admin", "OrgAdminForOrgId"],
    associateToken: ["Admin", "OrgAdminForAccountId"],
    getAllAccounts: ["Admin"],
    getAllOrgAccounts: ["Admin", "OrgAdminForOrgId"],
    getAccountsByUser: ["Admin", "OrgAdminForOrgId", "MultipleAccountOwner"],
    getAccount: ["Admin", "OrgAdminForAccountId", "AccountOwner"],
    history: ["Admin", "AccountOwner"],
    getAccountTransactionHistory: ["Admin", "OrgAdminForAccountId", "AccountOwner"],
    getAccountTransactionHistoryWithFilters: ["Admin", "OrgAdminForAccountId", "AccountOwner"],
    getSubTransactionsById: ["Admin", "TransactionInvoker"],
    getSubTransactionsByIdWithFilters: ["Admin", "TransactionInvoker"],
    getAccountBalance: ["Admin", "OrgAdminForAccountId", "AccountOwner"],
    getAccountOnHoldBalance: ["Admin", "OrgAdminForAccountId", "AccountOwner"],
    getOnHoldIds: ["Admin", "OrgAdminForAccountId", "AccountOwner"],
    getConversionHistory: ["Admin", "OrgAdminForAccountId", "AccountOwner"],
  },
  ACCOUNT_STATUS: {
    get: ["Admin", "OrgAdminForAccountId", "AccountOwner"],
    history: ["Admin", "OrgAdminForAccountId", "AccountOwner"],
    activateAccount: ["Admin", "OrgAdminForOrgId"],
    suspendAccount: ["Admin", "OrgAdminForOrgId"],
    deleteAccount: ["Admin", "OrgAdminForOrgId"],
  },
  TOKEN_CONVERSION: {
    initializeExchangePoolUser: ["Admin"],
    addConversionRate: ["Admin"],
    updateConversionRate: ["Admin"],
    getConversionRate: ["Admin", "OrgAdmin", "AnyAccountOwner"],
    getConversionRateHistory: ["Admin", "OrgAdmin", "AnyAccountOwner"],
    tokenConversion: ["Admin", "AnyAccountOwner"],
    getExchangePoolUser: ["Admin"],
  },
}
await this.Ctx.Auth.checkAuthorization(<parameters>);
參數:
  • classFuncName: string - 類別與方法之間的對應值,如 ../lib/constant.ts 檔案中所述。
  • ...args - 一個變數引數,其中 args[0] 會採用 'TOKEN' 常數,而 args[1] 會採用 account_id 來新增 AccountOwner 的存取控制檢查。若要新增 MultipleAccountOwner 的存取控制檢查,args[1] 會採用 org_id,而 args[2] 則採用 user_id
傳回值:
  • 成功時,即是承諾。發生錯誤時,拒絕並顯示錯誤訊息。
isUserTokenAdmin
如果函數的呼叫程式是 Token Admin,此方法會傳回布林值 true。否則,方法會傳回 false
Ctx.Auth.isUserTokenAdmin()
參數:
  • user_id - 使用者的使用者名稱或電子郵件 ID。
  • org_id - 目前網路組織中使用者的成員服務提供者 (MSP) ID。
傳回值:
  • 如果發生錯誤,則為布林回應與錯誤訊息。
addOrgAdmin
此方法會將使用者新增為組織的 Org Admin
Ctx.Admin.addOrgAdmin(org_id, user_id)
參數:
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,包含新增為組織 Org Admin 之使用者詳細資訊的訊息。
傳回值範例:
{
    "msg": "Successfully added Org Admin (Org_Id: Org1MSP, User_Id: orgAdmin)"
}
removeOrgAdmin
此方法會將使用者移除為組織的 Org Admin
Ctx.Admin.removeOrgAdmin(org_id, user_id)
參數:
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,包含作為組織 Org Admin 移除之使用者詳細資訊的訊息。
傳回值範例:
{
  "msg": "Successfully removed Org Admin (Org_Id Org1MSP User_Id orgAdmin)"
}
getOrgAdmins
此方法會傳回組織 Org Admin 的所有使用者清單。
Ctx.Admin.getAllOrgAdmins()
參數:
傳回值:
  • 成功時,JSON 格式的陣列包含 orgIduserId 物件。
傳回值範例:
{
    "admins": [
        {
            "org_id": "Org1MSP",
            "user_id": "orgadmin"
        },
        {
            "org_id": "Org1MSP",
            "user_id": "orgadmin1"
        },
        {
            "org_id": "Org1MSP",
            "user_id": "orgadmin2"
        }
    ]
}

記號組態管理的方法

getTokenDecimals
此方法會傳回小數記號可用的小數位數。如果未指定 divisible 行為,則預設值為 0。
Ctx.Token.getTokenDecimals(token_id: string)
參數:
  • token_id: string - 記號的 ID。
傳回值:
  • 成功時,記號在數字資料類型中的小數位數。發生錯誤時,會傳回錯誤訊息。
傳回值範例:
1
getAllTokens
此方法會傳回儲存於狀態資料庫中的所有記號資產。此方法使用 Berkeley 資料庫 SQL 豐富查詢,且只能在連線至遠端 Oracle Blockchain Platform 網路時呼叫。
Ctx.Token.getAllTokens()
參數:
傳回值:
  • 成功時,它會傳回包含所有權杖資產的承諾。發生錯誤時,會傳回錯誤訊息。
傳回值範例:
{
    "returnCode": "Success",
    "error": "",
    "result": {
        "txid": "98e0a0a115803d25b843d630e6b23c435a192a03eb0a301fc9375f05da49a8b2",
        "payload": [
            {
                "key": "token1",
                "valueJson": {
                    "assetType": "otoken",
                    "token_id": "token1",
                    "token_name": "vtok",
                    "token_type": "fungible",
                    "behaviours": [
                        "divisible",
                        "mintable",
                        "transferable",
                        "burnable",
                        "holdable",
                        "roles"
                    ],
                    "roles": {
                        "burner_role_name": "burner",
                        "notary_role_name": "notary"
                    },
                    "mintable": {
                        "max_mint_quantity": 0
                    },
                    "divisible": {
                        "decimal": 1
                    }
                }
            }
        ],
        "encode": "JSON"
    }
}
getTokensByName
此方法會傳回具有指定名稱的所有權杖資產。此方法使用 Berkeley 資料庫 SQL 豐富查詢,且只能在連線至遠端 Oracle Blockchain Platform 網路時呼叫。
Ctx.Token.getTokensByName(token_name: string)
參數:
  • token_name: string - 與模型的 Token_name 特性對應的記號名稱。值是記號的類別名稱。
傳回值:
  • 它會以 JSON 格式傳回指定之名稱的所有權杖資產陣列。
傳回值範例:
{
    "returnCode": "Success",
    "error": "",
    "result": {
        "txid": "98e0a0a115803d25b843d630e6b23c435a192a03eb0a301fc9375f05da49a8b2",
        "payload": [
            {
                "key": "token1",
                "valueJson": {
                    "assetType": "otoken",
                    "token_id": "token1",
                    "token_name": "vtok",
                    "token_type": "fungible",
                    "behaviours": [
                        "divisible",
                        "mintable",
                        "transferable",
                        "burnable",
                        "holdable",
                        "roles"
                    ],
                    "roles": {
                        "burner_role_name": "burner",
                        "notary_role_name": "notary"
                    },
                    "mintable": {
                        "max_mint_quantity": 0
                    },
                    "divisible": {
                        "decimal": 1
                    }
                }
            }
        ],
        "encode": "JSON"
    }
}
get
如果權杖物件存在於狀態資料庫中,這個方法就會傳回該物件。
Ctx.Token.get(token_id: string)
參數:
  • token_id: string - 要傳回的記號 ID。
傳回值:
  • 成功時,使用權杖的 JSON 表示法做出承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
    "assetType": "otoken",
    "token_id": "token1",
    "token_name": "account",
    "token_desc": "Token 1",
    "token_type": "fungible",
    "behaviors": [
        "divisible",
        "mintable",
        "transferable",
        "burnable",
        "holdable",
        "roles"
    ],
    "roles": {
        "minter_role_name": "minter",
        "burner_role_name": "burner",
        "notary_role_name": "notary"
    },
    "mintable": {
        "max_mint_quantity": 20000
    },
    "divisible": {
        "decimal": 1
    },
    "token_to_currency_ratio": 2,
    "currency_representation": "EURO"
}
isTokenType
此方法指示是否有指定 ID 的權杖資產存在。
Ctx.Token.isTokenType(token_id: string)
參數:
  • token_id: string - 要檢查的記號 ID。
傳回值:
  • 成功時,如果具有指定 ID 的權杖資產存在,則承諾為 true 。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
true
save
此方法會建立記號,並將其特性儲存在狀態資料庫中。
Ctx.Token.save(token: <Instance of Token Class>,extraMetadata?:any)
參數:
  • token: <Instance of Token Class> - 要操作的記號資產。
傳回值:
  • 成功時,含有權杖詳細資訊的承諾訊息。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
   "assetType":"otoken",
   "token_id":"digiCurr101",
   "token_name":"digicur",
   "token_type":"fungible",
   "behaviors":[
      "divisible",
      "mintable",
      "transferable",
      "burnable",
      "roles"
   ],
   "roles":{
      "minter_role_name":"minter"
   },
   "mintable":{
      "max_mint_quantity":1000
   },
   "divisible":{
      "decimal":2
   },
   "currency_name":"DOLLAR",
   "token_to_currency_ratio":1
}
update
此方法會更新記號特性。建立權杖資產之後,您只會更新 token_desc 值及其自訂特性。
Ctx.Token.update(token: <Instance of Token Class>)
參數:
  • token: <Instance of Token Class> - 要操作的記號資產。
傳回值:
  • 成功時,含有權杖詳細資訊的承諾訊息。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
   "assetType":"otoken",
   "token_id":"digiCurr101",
   "token_name":"digicur",
   "token_desc":"Digital Currency equiv of dollar",
   "token_type":"fungible",
   "behaviors":[
      "divisible",
      "mintable",
      "transferable",
      "burnable",
      "roles"
   ],
   "roles":{
      "minter_role_name":"minter"
   },
   "mintable":{
      "max_mint_quantity":1000
   },
   "divisible":{
      "decimal":2
   },
   "currency_name":"DOLLAR",
   "token_to_currency_ratio":1
}
getByRange
此方法會在內部呼叫結構 getStateByRange 方法。即使從分類帳傳回任何具有指定 ID 的資產,此方法會將資產轉換成呼叫程式資產類型。
<Token ClassCtx.Token.getByRange(start_token_id: string, end_token_id: string, token_class_reference?: <Instance of Token Class> )
參數:
  • startId: string - 範圍的開始索引鍵。此索引鍵包含在範圍內。
  • endId: string - 範圍的結束索引鍵。此索引鍵已從範圍中排除。
  • token: <Instance of Token Class> - 要操作的記號資產。
傳回值:
  • 成功時,陣列為 <Token Class> 的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
範例:
@validator(yup.string(), yup.string())
public async getDigiCurrGetByRange(start_token_id: string, end_token_id: string) {
   return await this.Ctx.Token.getByRange(start_token_id, end_token_id, DigiCurr);
}
傳回值範例:
[
    {
        "assetType": "otoken",
        "token_id": "token1",
        "token_name": "digicur",
        "token_desc": "Token 1",
        "token_type": "fungible",
        "behaviors": [
            "divisible",
            "mintable",
            "transferable",
            "burnable",
            "holdable",
            "roles"
        ],
        "roles": {
            "minter_role_name": "minter",
            "burner_role_name": "burner",
            "notary_role_name": "notary"
        },
        "mintable": {
            "max_mint_quantity": 20000
        },
        "divisible": {
            "decimal": 0
        },
        "token_to_currency_ratio": 1.5,
        "currency_representation": "USD"
    },
    {
        "assetType": "otoken",
        "token_id": "token2",
        "token_name": "digicur",
        "token_desc": "Token2",
        "token_type": "fungible",
        "behaviors": [
            "divisible",
            "mintable",
            "transferable",
            "burnable",
            "holdable",
            "roles"
        ],
        "roles": {
            "minter_role_name": "minter",
            "burner_role_name": "burner",
            "notary_role_name": "notary"
        },
        "mintable": {
            "max_mint_quantity": 20000
        },
        "divisible": {
            "decimal": 0
        },
        "token_to_currency_ratio": 1,
        "currency_representation": "EURO"
    }
]
history
此方法會傳回指定記號的歷史記錄。
Ctx.Token.history(tokenId)
參數:
  • tokenId: string - 記號的 ID。
傳回值:
  • 成功時,具有指定變數替代字之帳戶歷史記錄明細陣列的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
[
    {
        "trxId": "0d75f09446a60088afb948c6aca046e261fddcd43df416076201cdc5565f1a35",
        "timeStamp": "2023-09-01T16:48:41.000Z",
        "value": {
            "assetType": "otoken",
            "token_id": "token",
            "token_name": "fiatmoneytok",
            "token_desc": "updatedDesc",
            "token_standard": "ttf+",
            "token_type": "fungible",
            "token_unit": "fractional",
            "behaviors": [
                "divisible",
                "mintable",
                "transferable",
                "burnable",
                "roles"
            ],
            "roles": {
                "minter_role_name": "minter"
            },
            "mintable": {
                "max_mint_quantity": 1000
            },
            "divisible": {
                "decimal": 2
            }
        }
    },
    {
        "trxId": "3666344878b043b65d5b821cc79c042ba52aec467618800df5cf14eac69f72fa",
        "timeStamp": "2023-08-31T20:24:55.000Z",
        "value": {
            "assetType": "otoken",
            "token_id": "token",
            "token_name": "fiatmoneytok",
            "token_standard": "ttf+",
            "token_type": "fungible",
            "token_unit": "fractional",
            "behaviors": [
                "divisible",
                "mintable",
                "transferable",
                "burnable",
                "roles"
            ],
            "roles": {
                "minter_role_name": "minter"
            },
            "mintable": {
                "max_mint_quantity": 1000
            },
            "divisible": {
                "decimal": 2
            }
        }
    }
]

帳戶管理方法

getCallerAccountId
這個方法會傳回呼叫程式的帳戶 ID。
Ctx.Account.getCallerAccountId(token_id: string)
參數:
  • tokenId: string - 記號的 ID。
傳回值:
  • 成功時,具有來電者帳戶 ID 的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f
generateAccountId
此方法會傳回帳戶 ID,這是一組文數字字元,前面加上 oaccount~<token asset name>~,後面接著執行處理擁有者的使用者名稱或電子郵件 ID (user_id) 雜湊、登入執行處理的使用者、目前網路組織中使用者的成員服務提供者 ID (org_id) 以及唯一權杖 ID (token_id)。
Ctx.Account.generateAccountId(token_id: string, org_id: string, user_id: string)
參數:
  • tokenId: string - 記號的 ID。
  • orgId: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • userId: string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,所產生帳戶 ID 的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f
createAccount
此方法會為指定的使用者和記號建立一個帳戶。每個有權杖的使用者都必須有帳戶。帳戶會追蹤使用者的餘額、保留餘額及交易歷史記錄。帳戶 ID 是一組文數字字元,前面加上 oaccount~<token asset name>~,後面接著執行處理擁有者的使用者名稱或電子郵件 ID (user_id) 雜湊,或登入執行處理的使用者,即目前網路組織中使用者的成員服務提供者 ID (org_id)。此方法只能由鏈碼的 Token Admin 或由指定組織的 Org Admin 呼叫。
this.Ctx.Account.createAccount(org_id: string, user_id: string, token_type: string)
參數:
  • org_id: string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id: string - 使用者的使用者名稱或電子郵件 ID。
  • token_type: string - 記號的類型,必須是 fungible
傳回值:
  • 成功時,JSON 格式的新帳戶物件。
傳回值範例:
{
  "assetType": "oaccount",
  "bapAccountVersion": 0,
  "account_id": "oaccount~abc74791148b761352b98df58035601b6f5480448ac2b4a3a7eb54bdbebf48eb",
  "user_id": "admin",
  "org_id": "Org1MSP",
  "token_type": "fungible",
  "token_id": "",
  "token_name": "",
  "balance": 0,
  "onhold_balance": 0
}
associateTokenToAccount
此方法會將有趣的記號與帳戶建立關聯。此方法只能由鏈碼的 Token Admin 或相關組織的 Org Admin 呼叫。
async associateTokenToAccount(account_id: string, token_id: string)
參數:
  • account_id: string - 帳戶的 ID。
  • token_id: string - 記號的 ID。
傳回值:
  • 成功時,更新帳戶的 JSON 物件。
傳回值範例:
{
    "assetType": "oaccount",
    "bapAccountVersion": 0,
    "account_id": "oaccount~abc74791148b761352b98df58035601b6f5480448ac2b4a3a7eb54bdbebf48eb",
    "user_id": "admin",
    "org_id": "Org1MSP",
    "token_type": "fungible",
    "token_id": "fungible",
    "token_name": "fiatmoneytok",
    "balance": 0,
    "onhold_balance": 0
}
getAccountWithStatus
此方法會傳回指定帳戶的帳戶詳細資訊,包括帳戶狀態。
Ctx.Account.getAccountWithStatus(account_id: string)
參數:
  • account_id: string - 帳戶的 ID。
傳回值:
  • 成功時,帳戶明細的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
  "bapAccountVersion": 0,
  "assetType": "oaccount",
  "status": "active",
  "account_id": "oaccount~2de8db6b91964f8c9009136831126d3cfa94e1d00c4285c1ea3e6d1f36479ed4",
  "user_id": "idcqa",
  "org_id": "appdev",
  "token_type": "fungible",
  "token_id": "t1",
  "token_name": "obptok",
  "balance": 0,
  "onhold_balance": 0
}
getAccount
此方法會傳回指定帳戶的帳戶詳細資料。
Ctx.Account.getAccount(account_id: string)
參數:
  • account_id: string - 帳戶的 ID。
傳回值:
  • 成功時,帳戶明細的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
   "assetType":"oaccount",
   "bapAccountVersion": 0,
   "account_id":"oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
   "user_id":"user1",
   "org_id":"Org1MSP",
   "token_id":"digiCurr101",
   "token_name":"digicur",
   "balance":0,
   "onhold_balance":0
}
history
此方法會傳回指定帳戶的帳戶歷史記錄詳細資料陣列。
Ctx.Account.history(account_id: string)
參數:
  • account_id: string - 帳戶的 ID。
傳回值:
  • 成功時,包含帳戶歷史記錄明細陣列的承諾。發生錯誤時,拒絕並顯示錯誤訊息。傳回值與 "getAccountHistory" 方法相同。
傳回值範例:
[
    {
      "trxId":"2gsdh17fff222467e5667be042e33ce18e804b3e065cca15de306f837e416d7c3e",
      "timeStamp":1629718288,
      "value":{
         "assetType":"oaccount",
         "account_id":"oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
         "user_id":"user1",
         "org_id":"Org1MSP",
         "token_id":"digiCurr101",
         "token_name":"digicur",
         "balance":100,
         "onhold_balance":0,
         "bapAccountVersion": 1
   },
   {
      "trxId":"9fd07fff222467e5667be042e33ce18e804b3e065cca15de306f837e416d7c3e",
      "timeStamp":1629718288,
      "value":{
         "assetType":"oaccount",
         "account_id":"oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
         "user_id":"user1",
         "org_id":"Org1MSP",
         "token_id":"digiCurr101",
         "token_name":"digicur",
         "balance":0,
         "onhold_balance":0,
         "bapAccountVersion": 0
      }
   }
]
getAccountOnHoldBalance
此方式會傳回指定科目的保留餘額。
Ctx.Account.getAccountOnHoldBalance(account_id: string)
參數:
  • account_id: string - 帳戶的 ID。
傳回值:
  • 成功時,具有 JSON 物件的承諾會顯示指定帳戶的保留餘額。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
   "holding_balance":0,
   "msg":"Total Holding Balance of Account Id oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f (org_id: Org1MSP, user_id: user1) is 0"
}
getAllAccounts
此方法會傳回所有帳戶的清單。此方法使用 Berkeley 資料庫 SQL 豐富查詢,且只能在連線至遠端 Oracle Blockchain Platform 網路時呼叫。
Ctx.Account.getAllAccounts()
參數:
傳回值:
  • 成功時,使用列出所有帳戶的 JSON 物件的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
[
           {
               "key": "oaccount~digicur~2e2ef3375ae347cbd7b4d3d7be5cece803f9c36a184aaf2b8d332c5d2dcead52",
               "valueJson": {
                   "assetType": "oaccount",
                   "account_id": "oaccount~digicur~2e2ef3375ae347cbd7b4d3d7be5cece803f9c36a184aaf2b8d332c5d2dcead52",
                   "user_id": "admin",
                   "org_id": "Org1MSP",
                   "token_id": "digiCurr101",
                   "token_name": "digicur",
                   "bapAccountVersion": 0,
                   "balance": 0,
                   "onhold_balance": 0
               }
           },
           {
               "key": "oaccount~digicur~30080c7e5ba94035af57fbbccbbb495e92515e4b2b3dbcd476eb1c0343e4da65",
               "valueJson": {
                   "assetType": "oaccount",
                   "account_id": "oaccount~digicur~30080c7e5ba94035af57fbbccbbb495e92515e4b2b3dbcd476eb1c0343e4da65",
                   "bapAccountVersion": 0,
                   "user_id": "user1",
                   "org_id": "Org1MSP",
                   "token_id": "digiCurr101",
                   "token_name": "digicur",
                   "balance": 0,
                   "onhold_balance": 0
               }
           },
           {
               "key": "oaccount~digicur~cbde438258cb01a82f71a9a9f8029243c40c6d836a505432120529c2b3c2ff0c",
               "valueJson": {
                   "assetType": "oaccount",
                   "account_id": "oaccount~digicur~cbde438258cb01a82f71a9a9f8029243c40c6d836a505432120529c2b3c2ff0c",
                   "bapAccountVersion": 0,
                   "user_id": "user2",
                   "org_id": "Org1MSP",
                   "token_id": "digiCurr101",
                   "token_name": "digicur",
                   "balance": 0,
                   "onhold_balance": 0
               }
           },
           {
               "key": "oaccount~digicur~ecbc3aefcc562d3049c988717940195b30297e95012b7824bbd33a57ca50a626",
               "valueJson": {
                   "assetType": "oaccount",
                   "account_id": "oaccount~digicur~ecbc3aefcc562d3049c988717940195b30297e95012b7824bbd33a57ca50a626",
                   "bapAccountVersion": 0,
                   "user_id": "user3",
                   "org_id": "Org1MSP",
                   "token_id": "digiCurr101",
                   "token_name": "digicur",
                   "balance": 500,
                   "onhold_balance": 0
               }
           }
       ]
getUserByAccountId
此方法會傳回指定帳戶的使用者詳細資訊。
Ctx.Account.getUserByAccountId(account_id: string)
參數:
  • account_id: string - 帳戶的 ID。
傳回值:
  • 成功時,一個包含三個特性的 JSON 物件承諾:
    • user_id - 使用者的使用者名稱或電子郵件 ID。
    • org_id - 目前網路組織中使用者的成員服務提供者 (MSP) ID。
    • token_id - 記號的 ID。
  • 發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
   "token_id": "digiCurr101",
   "user_id": "user1",
   "org_id": "Org1MSP"
}
getAccountBalance
此方法會傳回指定帳戶的帳戶餘額。
Ctx.Account.getAccountBalance(account_id: string)
參數:
  • account_id: string - 帳戶的 ID。
傳回值:
  • 成功時,含有包含兩個特性之 JSON 物件的承諾訊息:
    • msg - 顯示目前餘額的訊息。
    • user_balance - 目前餘額的數值。
  • 發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
    "msg": "Current Balance is: 200",
    "user_balance": 200
}
getAllOrgAccounts
此方法會傳回屬於指定組織的所有權杖帳戶清單。
Ctx.Account.getAllOrgAccounts(org_id: string) 
參數:
  • org_id: string - 組織的成員服務提供者 (MSP) ID。
傳回值:
  • 成功時,指定組織的所有帳戶清單。
傳回值範例:
[
    {
        "key": "oaccount~2de8db6b91964f8c9009136831126d3cfa94e1d00c4285c1ea3e6d1f36479ed4",
        "valueJson": {
            "bapAccountVersion": 0,
            "assetType": "oaccount",
            "account_id": "oaccount~2de8db6b91964f8c9009136831126d3cfa94e1d00c4285c1ea3e6d1f36479ed4",
            "user_id": "idcqa",
            "org_id": "appdev",
            "token_type": "fungible",
            "token_id": "token",
            "token_name": "fiatmoneytok",
            "balance": 0,
            "onhold_balance": 0
        }
    },
    {
        "key": "oaccount~620fcf5deb5fd5a65c0b5b10fda129de0f629ccd232c5891c130e24a574df50a",
        "valueJson": {
            "bapAccountVersion": 0,
            "assetType": "oaccount",
            "account_id": "oaccount~620fcf5deb5fd5a65c0b5b10fda129de0f629ccd232c5891c130e24a574df50a",
            "user_id": "example_minter",
            "org_id": "appdev",
            "token_type": "fungible",
            "token_id": "token",
            "token_name": "fiatmoneytok",
            "balance": 0,
            "onhold_balance": 0
        }
    }
]

角色管理方法

addRoleMember
這個方法會將角色新增至指定的使用者和記號。
Ctx.Token.addRoleMember(role: string, account_id: string, token: <Instance of Token Class>)
參數:
  • role: string - 要新增至指定使用者的角色名稱。mintableburnable 行為對應至規格檔案的 minter_role_nameburner_role_name 特性。同樣地,notary 角色會對應至規格檔案的 notary_role_name 特性。
  • account_id: number - 要新增角色的帳戶 ID。
  • token: <Instance of Token Class> - 要操作的記號資產。
傳回值:
  • 成功時,會收到成功訊息的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
    "msg":"Successfully added role minter to oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f (org_id :          Org1MSP, user_id : user1)"
}
removeRoleMember
此方法會從指定的使用者和記號移除角色。
Ctx.Token.removeRoleMember(role: string, account_id: string, token: <Instance of Token Class>)
參數:
  • role: string - 要從指定使用者移除的角色名稱。mintableburnable 行為對應至規格檔案的 minter_role_nameburner_role_name 特性。同樣地,notary 角色會對應至規格檔案的 notary_role_name 特性。
  • account_id: number - 移除角色的來源帳戶 ID。
  • token: <Instance of Token Class> - 要操作的記號資產。
傳回值:
  • 成功時,會收到成功訊息的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
  "msg":"successfully removed member_id oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f (org_id : Org1MSP, user_id : user1) from role minter"
}
getAccountsByRole
此方法會傳回所指定角色和記號的所有帳戶清單。
Ctx.Role.getAccountsByRole(token_id: string, role: string)
參數:
  • token_id: string - 記號的 ID。
  • role: string - 要搜尋的角色名稱。
傳回值:
  • 成功時,使用 JSON 物件的承諾會列出指定角色和權杖的所有帳戶。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
    "accounts": [
        "oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df",
        "oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f"
    ]
}
getAccountsByUser
此方法會傳回指定使用者的所有帳戶 ID 清單。
async getAccountsByUser(org_id: string, user_id: string)
參數:
  • org_id string - 目前組織中使用者的成員服務提供者 (MSP) ID。
  • user_id string - 使用者的使用者名稱或電子郵件 ID。
傳回值:
  • 成功時,帳戶 ID 的 JSON 陣列。
傳回值範例:
{"accounts":["oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f"]}
getUsersByRole
此方法會針對指定的角色和記號,傳回所有使用者的清單。
Ctx.Role.getUsersByRole(token_id: string, role: string)
參數:
  • token_id: string - 記號的 ID。
  • role: string - 要搜尋的角色名稱。
傳回值:
  • 成功時,使用 JSON 物件的承諾會列出指定角色和權杖的所有使用者。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
   "users":[
      {
         "token_id":"digiCurr101",
         "user_id":"user1",
         "org_id":"Org1MSP"
      }
   ]
}
isInRole
此方法指示使用者和記號是否具有指定的角色。
Ctx.Token.isInRole(role: string, account_id: string, token: <Instance of Token Class>)
參數:
  • role: string - 要檢查之角色的名稱。
  • account_id: number - 要檢查的帳戶 ID。
  • token: <Instance of Token Class> - 要操作的記號資產。
傳回值:
  • 成功時,如果使用者具有角色,則為 true 的承諾;如果使用者沒有角色,則為 false 。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{"result":"true"}
roleCheck
此方法會檢查提供的帳戶 ID 是否為任何角色的成員。
Ctx.Token.roleCheck(account_id: string, token: <Instance of Token Class>)
參數:
  • account_id: string - 要檢查的帳戶 ID。
  • token: <Instance of Token Class> - 要操作的記號資產。
傳回值:
  • 如果帳戶 ID 是任何角色的一部分,則會傳回 true。否則,它會傳回 false
傳回值範例:
{ result: true }
getOrgUsersByRole
此方法會傳回在指定組織中具有指定角色之所有使用者的相關資訊。
Ctx.Role.getOrgUsersByRole(token_id: string, role: string, org_id: string)
參數:
  • token_id: string - 記號的 ID。
  • role: string - 要檢查之角色的名稱。
  • org_id: string - 組織的成員服務提供者 (MSP) ID。
傳回值:
  • 成功時,指定組織中具有指定角色的所有使用者清單。
傳回值範例:
{
    "users": [
        {
            "token_id": "token",
            "user_id": "admin",
            "org_id": "Org1MSP"
        },
        {
            "token_id": "token",
            "user_id": "orgAdmin",
            "org_id": "Org1MSP"
        }
    ]
}
getOrgAccountsByRole
此方法會傳回在指定組織中具有指定角色之所有帳戶的相關資訊。
Ctx.Role.getOrgAccountsByRole(token_id: string, role: string, org_id: string)
參數:
  • token_id: string - 記號的 ID。
  • role: string - 要檢查之角色的名稱。
  • org_id: string - 組織的成員服務提供者 (MSP) ID。
傳回值:
  • 成功時,指定組織中具有指定角色的所有帳戶清單。
傳回值範例:
{
    "accounts": [
        "oaccount~abc74791148b761352b98df58035601b6f5480448ac2b4a3a7eb54bdbebf48eb",
        "oaccount~9c650574af9025a6106c8d12a801b079eda9ae2e3399fc2fbd5bd683d738a850"
    ]
}

交易歷史記錄管理的方法

getTransactionById
此方法會傳回 Transaction 資產的歷史記錄。
async getTransactionById(transaction_id: string)
參數:
  • transaction_id: string - 交易資產的 ID。
傳回值:
  • 成功時,交易資產歷史記錄。
傳回值範例:
{
    "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775",
    "history": [
        {
            "trxId": "68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775",
            "timeStamp": 1629180264,
            "value": {
                "assetType": "otransaction",
                "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775",
                "token_id": "digiCurr101",
                "from_account_id": "oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df",
                "to_account_id": "",
                "transaction_type": "BULKTRANSFER",
                "amount": 20,
                "timestamp": "2021-08-17T06:04:24.000Z",
                "number_of_sub_transactions": 2,
                "holding_id": ""
            }
        }
    ],
    "sub_transactions": [
        {
            "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775~c4ca4238a0b923820dcc509a6f75849b",
            "history": [
                {
                    "trxId": "68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775",
                    "timeStamp": 1629180264,
                    "value": {
                        "assetType": "otransaction",
                        "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775~c4ca4238a0b923820dcc509a6f75849b",
                        "token_id": "digiCurr101",
                        "from_account_id": "oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df",
                        "to_account_id": "oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
                        "transaction_type": "TRANSFER",
                        "amount": 10,
                        "timestamp": "2021-08-17T06:04:24.000Z",
                        "number_of_sub_transactions": 0,
                        "holding_id": ""
                    }
                }
            ]
        },
        {
            "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775~c81e728d9d4c2f636f067f89cc14862c",
            "history": [
                {
                    "trxId": "68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775",
                    "timeStamp": 1629180264,
                    "value": {
                        "assetType": "otransaction",
                        "transaction_id": "otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775~c81e728d9d4c2f636f067f89cc14862c",
                        "token_id": "digiCurr101",
                        "from_account_id": "oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df",
                        "to_account_id": "oaccount~digicur~38848e87296d67c8a90918f78cf55f9c9baab2cdc8c928535471aaa1210c706e",
                        "transaction_type": "TRANSFER",
                        "amount": 10,
                        "timestamp": "2021-08-17T06:04:24.000Z",
                        "number_of_sub_transactions": 0,
                        "holding_id": ""
                    }
                }
            ]
        }
    ]
}
deleteHistoricalTransactions
此方式會傳回指定科目的交易歷史記錄明細陣列。
async deleteHistoricalTransactions(time_to_expiration: Date)
參數:
  • time_to_expiration: Date - 表示何時刪除交易的時戳。將會刪除早於指定時間的交易資產。
傳回值:
  • 傳回值與 "getAccountTransactionHistory" 方法相同。
  • 成功時,帳戶交易物件陣列的承諾:
    • transaction_id - 交易的 ID。
    • transacted_account - 進行交易的帳戶。
    • transaction_type - 交易類型。
    • transacted_amount - 交易的金額。
    • timestamp - 異動的時間。
    • balance - 交易時的帳戶餘額。
    • onhold_balance - 交易時的保留餘額。
    • sub_transactions - 僅限大量移轉,屬於大量移轉一部分的交易清單。
    • holding_id - holdTokens 方法所傳回的唯一 ID。
    • token_id - 記號的 ID。
  • 發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
"payload": {
            "msg": "Successfuly deleted transaction older than date: Thu Aug 19 2021 11:19:36 GMT+0000 (Coordinated Universal Time).",
            "transactions": [
                "otransaction~ec3366dd48b4ce2838f820f2f138648e6e55a07226713e59b411ff31b0d21058"
            ]
        }
getAccountTransactionHistory
此方式會傳回指定科目的交易歷史記錄明細陣列。
Ctx.Account.getAccountTransactionHistory(account_id: string)
參數:
  • account_id: string - 帳戶的 ID。
傳回值:
  • 傳回值與 "getAccountTransactionHistory" 方法相同。
  • 成功時,帳戶交易物件陣列的承諾:
    • transaction_id - 交易的 ID。
    • transacted_account - 進行交易的帳戶。
    • transaction_type - 交易類型。
    • transacted_amount - 交易的金額。
    • timestamp - 異動的時間。
    • balance - 交易時的帳戶餘額。
    • onhold_balance - 交易時的保留餘額。
    • sub_transactions - 僅限大量移轉,屬於大量移轉一部分的交易清單。
    • holding_id - holdTokens 方法所傳回的唯一 ID。
    • token_id - 記號的 ID。
  • 發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
[
   {
      "transaction_id":"otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775",
      "transacted_amount":20,
      "timestamp":"2021-08-17T06:04:24.000Z",
      "balance":60,
      "onhold_balance":0,
      "token_id":"digiCurr101",
      "transaction_type":"BULKTRANSFER",
      "sub_transactions":[
         {
            "transacted_account":"oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df",
            "transaction_type":"CREDIT",
            "transaction_id":"otransaction~68f46c90d0d8d6b93d827e6b9e0152b4845e6e42a61965e63a9bbf1d8e0fc775~c4ca4238a0b923820dcc509a6f75849b",
            "transacted_amount":10
         }
      ]
   },
   {
      "transaction_id":"otransaction~757864d5369bd0539d044caeb3bb4898db310fd7aa740f45a9938771903d43da",
      "transacted_amount":50,
      "timestamp":"2021-08-17T06:02:44.000Z",
      "balance":50,
      "onhold_balance":0,
      "token_id":"digiCurr101",
      "transacted_account":"oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df",
      "transaction_type":"CREDIT"
   }
]
getAccountTransactionHistoryWithFilters
此方式會傳回指定科目的交易歷史記錄明細陣列。連線至遠端 Oracle Blockchain Platform 網路時才能呼叫此方法。
await this.Ctx.Account.getAccountTransactionHistoryWithFilters(account_id: string, filters?: Filters)
參數:
  • account_id: string - 帳戶的 ID。
  • filters: string - 選擇性參數。如果空白,則會傳回所有記錄。PageSize 特性決定要傳回的記錄數目。如果 PageSize 為 0,則預設頁面大小為 20。Bookmark 特性決定要傳回之記錄的開始索引。如需詳細資訊,請參閱 Hyperledger Fabric 文件。必須以 RFC-3339 格式指定 StartTimeEndTime 特性。
範例:

ochain invoke getAccountTransactionHistoryWithFilters 'token1' 'appbuilder12' 'user_minter' '{"PageSize":10,"Bookmark":"1","StartTime":"2022-01-25T17:41:42Z","EndTime":"2022-01-25T17:59:10Z"}'

[
    {
        "transaction_id": "otransaction~672897b5a4fa78b421c000e4d6d4f71f3d46529bfbb5b4be10bf5471dc35ce89",
        "transacted_amount": 5,
        "timestamp": "2022-04-20T15:46:04.000Z",
        "token_id": "token1",
        "transacted_account": "oaccount~16c38d804413ebabf416360d374f76c973d4e71c74adfde73cc40c7c274883b8",
        "transaction_type": "DEBIT",
        "balance": 90,
        "onhold_balance": 0
    },
    {
        "transaction_id": "otransaction~467bb67a33aaffca4487f33dcd46c9844efdb5421a2e7b6aa2d53152eb2c6d85",
        "transacted_amount": 5,
        "timestamp": "2022-04-20T15:45:47.000Z",
        "token_id": "token1",
        "transacted_account": "oaccount~fbf95683b21bbc91a22205819ac1e2e9c90355d536821ed3fe22b7d23915c248",
        "transaction_type": "DEBIT",
        "balance": 95,
        "onhold_balance": 0
    },
    {
        "transaction_id": "otransaction~c6d56ce54a9bbe24597d1d10448e39316dc6f16328bf3c5b0c8ef10e1dfeb397",
        "transacted_amount": 100,
        "timestamp": "2022-04-20T15:44:26.000Z",
        "token_id": "token1",
        "transacted_account": "oaccount~deb5fb0906c40506f6c2d00c573b774e01a53dd91499e651d92ac4778b6add6a",
        "transaction_type": "MINT",
        "balance": 100,
        "onhold_balance": 0
    }
]
getSubTransactionHistory
此方法會傳回指定交易之交易歷史記錄明細的陣列。
await this.Ctx.Account.getSubTransactionHistory(transaction_id)
參數:
  • transaction_id: string - 大量傳輸交易的 ID。
範例:

ochain invoke GetAccountSubTransactionHistory 'otransaction~21972b4d206bd52ea77924efb259c67217edb23b4386580d1bee696f6f864b9b'

[
    {
        "transacted_account": "oaccount~16c38d804413ebabf416360d374f76c973d4e71c74adfde73cc40c7c274883b8",
        "transaction_type": "DEBIT",
        "transaction_id": "otransaction~6e0f8fe4a6430322170b9c619b04b6c9f1c8d257923f611b866bdf69d7fe6cb8~c81e728d9d4c2f636f067f89cc14862c",
        "transacted_amount": 5,
        "timestamp": "2022-04-20T15:52:21.000Z",
        "token_id": "token1",
        "balance": 80,
        "onhold_balance": 0
    },
    {
        "transacted_account": "oaccount~fbf95683b21bbc91a22205819ac1e2e9c90355d536821ed3fe22b7d23915c248",
        "transaction_type": "DEBIT",
        "transaction_id": "otransaction~6e0f8fe4a6430322170b9c619b04b6c9f1c8d257923f611b866bdf69d7fe6cb8~c4ca4238a0b923820dcc509a6f75849b",
        "transacted_amount": 5,
        "timestamp": "2022-04-20T15:52:21.000Z",
        "token_id": "token1",
        "balance": 85,
        "onhold_balance": 0
    }
]
getSubTransactionHistoryWithFilters
此方法會傳回指定交易之子交易歷史記錄明細的陣列。
await this.Ctx.Account.getSubTransactionHistoryWithFilters(transaction_id: string, filters?: SubTransactionFilters)
參數:
  • transaction_id: string - 大量傳輸交易的 ID。
  • filters: string - 選擇性參數。如果空白,則會傳回所有記錄。PageSize 特性決定要傳回的記錄數目。如果 PageSize 為 0,則預設頁面大小為 20。Bookmark 特性決定要傳回之記錄的開始索引。如需詳細資訊,請參閱 Hyperledger Fabric 文件。必須以 RFC-3339 格式指定 StartTimeEndTime 特性。
範例:

ochain invoke GetAccountSubTransactionHistoryWithFilters 'otransaction~21972b4d206bd52ea77924efb259c67217edb23b4386580d1bee696f6f864b9b' '{"PageSize":10,"Bookmark":"1"}'

[
    {
        "transaction_id": "otransaction~6e0f8fe4a6430322170b9c619b04b6c9f1c8d257923f611b866bdf69d7fe6cb8~c81e728d9d4c2f636f067f89cc14862c",
        "transacted_amount": 5,
        "timestamp": "2022-04-20T15:52:21.000Z",
        "token_id": "token1",
        "transacted_account": "oaccount~16c38d804413ebabf416360d374f76c973d4e71c74adfde73cc40c7c274883b8",
        "transaction_type": "DEBIT",
        "balance": 80,
        "onhold_balance": 0
    },
    {
        "transaction_id": "otransaction~6e0f8fe4a6430322170b9c619b04b6c9f1c8d257923f611b866bdf69d7fe6cb8~c4ca4238a0b923820dcc509a6f75849b",
        "transacted_amount": 5,
        "timestamp": "2022-04-20T15:52:21.000Z",
        "token_id": "token1",
        "transacted_account": "oaccount~fbf95683b21bbc91a22205819ac1e2e9c90355d536821ed3fe22b7d23915c248",
        "transaction_type": "DEBIT",
        "balance": 85,
        "onhold_balance": 0
    }
]

權杖行為管理

記號生命週期管理方法是以「記號分類標準架構」的標準為基礎。若要使用記號生命週期方法,請從 ../lib/token 模組匯入 Token 類別。
import { Token } from '../lib/token';

記號行為管理方法 - 可調整行為

mint
這個方法會提示某個數量的記號,然後由方法的呼叫程式所擁有。呼叫者必須具有帳戶與較小者角色。數量必須在規格檔案中 divisible 行為的 decimal 參數所指定的小數值內。
Ctx.Token.mint(quantity: number, token: <Instance of Token Class>)
參數:
  • quantity: number - 要提示的記號總數。
  • token: <Instance of Token Class> - 要加密的記號資產。
傳回值:
  • 成功時,會收到成功訊息和 toAccount 詳細資訊的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
  "msg":"Successfully minted 1000 tokens to Account Id: oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df (Org-Id: Org1MSP, User-Id: admin)"
}
getTotalMintedTokens
此方法會傳回提示的記號總數。
Ctx.Token.getTotalMintedTokens(token: <Instance of Token Class>)
參數:
  • token: <Instance of Token Class> - 要操作的記號資產。
傳回值:
  • 成功時,數字資料類型中提示的記號數量。發生錯誤時,會傳回錯誤訊息。
傳回值範例:
4000
getNetTokens
此方法會傳回系統中可用記號的淨數量。網路記號是記號被燒錄後剩餘的記號量。在方程式形式中:淨記號 = 提示的記號總計 - 消耗的記號總計。如果沒有燒錄記號,則淨記號的數目等於提示的記號總數。
Ctx.Token.getNetTokens(token: <Instance of Token Class>)
參數:
  • token: <Instance of Token Class> - 要操作的記號資產。
傳回值:
  • 成功時,數字資料類型的淨變數替代字數量。發生錯誤時,會傳回錯誤訊息。
傳回值範例:
2000
getMaxMintQuantity
此方式會傳回變數替代字的最大可修改數量。如果未指定 max_mint_quantity 行為,則預設值為 0,允許任意數目的記號進行調解。
Ctx.Token.getMaxMintQuantity(token: <Instance of Token Class>)
參數:
  • token: <Instance of Token Class> - 要操作的記號資產。
傳回值:
  • 成功時,數字資料類型中變數替代字的最大可修改數量。發生錯誤時,會傳回錯誤訊息。
傳回值範例:
20000

記號行為管理方法 - 可傳輸行為

transfer
此方法會將記號從交易呼叫程式傳輸至 to_account_id 帳戶。此方法的呼叫程式必須要有一個帳戶,而且數量必須在規格檔案中 divisible 行為的 decimal 參數所指定的小數值內。
Ctx.Token.transfer(to_account_id: string, quantity: number, token: <Instance of Token Class>)
參數:
  • to_account_id: string - 接收記號的帳戶 ID。
  • quantity: number - 要傳輸的記號總數。
傳回值:
  • 成功時,會收到成功訊息的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
 "msg":"Successfully transferred 50 tokens from account id: oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df (Org-Id: Org1MSP, User-Id: admin) to account id: oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f (Org-Id: Org1MSP, User-Id: user1)"
}
bulkTransfer
此方法是用來執行從呼叫程式帳戶到 flow 物件中指定帳戶的大量記號傳輸。這個方法的呼叫程式必須已經建立帳戶。
Ctx.Token.bulkTransfer(flow: object[], token: <Instance of Token Class>)
參數:
  • flow: object[] - 指定接收者詳細資訊和數量的 JSON 物件陣列。移轉數量必須在規格檔案中 divisible 行為的 decimal 參數所指定的小數值內。舉例而言:
    [{
    	"to_org_id": "Org1MSP",
    	"to_user_id": "user1",
    	"quantity": 10
    }, {
    	"to_org_id": "Org1MSP",
    	"to_user_id": "user2",
    	"quantity": 10
    }]
  • token: <Instance of Token Class> - 要操作的記號資產。
傳回值:
  • 成功時,會收到成功訊息和帳戶資訊的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
    "from_account_id": "oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
    "msg": "Successfully transferred 2 tokens from Account Id oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f (Org-Id: Org1MSP, User-Id: user1)",
    "sub_transactions": [
        {
            "amount": 1,
            "to_account_id": "oaccount~digicur~38848e87296d67c8a90918f78cf55f9c9baab2cdc8c928535471aaa1210c706e"
        },
        {
            "amount": 1,
            "to_account_id": "oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df"
        }
    ]
}

記號行為管理方法 - 可保留行為

hold
此方法會代表記號擁有者的 to_account_id 帳戶建立保留。已指定公證人帳戶,其負責完成或解除保留。建立保留時,付款人的指定變數替代字餘額會設為保留。保留餘額必須等到完成或解除後才能移轉。這個方法的呼叫程式必須已經建立帳戶。
Ctx.Token.hold(operation_id: string, to_account_id: string, notary_account_id: string, quantity: number, time_to_expiration: Date, token: <Instance of Token Class>)
參數:
  • operation_id: string - 識別保留作業的唯一 ID。通常這個 ID 會由用戶端應用程式傳送。
  • to_account_id: string - 接收記號的帳戶 ID。
  • notary__account_id: string - 公證帳戶的 ID。
  • quantity: number - 要保留的記號總數。
  • time_to_expiration: Date - 保留到期的持續時間。為永久保留指定 0 。否則,請採用 RFC-3339 格式。例如 2021-06-02T12
  • token: <Instance of Token Class> - 要保留的記號資產。
傳回值:
  • 成功時,會收到成功訊息的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
 "msg": "account id: oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df (org_id : Org1MSP, user_id : user1) is successfully holding 10 tokens",
}
executeHold
此方式會完成記號的保留,將先前保留的指定記號數量移轉給接收者。如果 quantity 值小於實際保留值,則記號的原始擁有者可再使用剩餘金額。只有具有指定作業 ID 之 notary 角色的 AccountOwner ID 才能呼叫此方法。保留只能由公證人完成。
Ctx.Token.executeHold(operation_id: string, quantity: number, token: <Instance of Token Class>)
參數:
  • operation_id: string - 識別保留作業的唯一 ID。通常這個 ID 會由用戶端應用程式傳送。
  • quantity: number - 完成保留的記號總數。
  • token: <Instance of Token Class> - 要完成保留的記號資產。
傳回值:
  • 成功時,會收到成功訊息的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
 "msg": "user with accountId: oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df (org_id : Org1MSP, user_id : user1) has successfully executed 5  tokens(digiCurr101) from the hold with Operation Id opr_121",
}
releaseHold
此方法會解除記號的保留。移轉未完成,所有保留的變數替代字會再次可供原始擁有者使用。AccountOwner ID 可在指定的時間限制內以 notary 角色呼叫此方法,或在指定的時間限制之後由付款人、受款人或公證人呼叫。
Ctx.Token.releaseHold(operation_id: string, token: <Instance of Token Class>)
參數:
  • operation_id: string - 識別保留作業的唯一 ID。通常這個 ID 會由用戶端應用程式傳送。
  • token: <Instance of Token Class> - 要解除保留的記號資產。
傳回值:
  • 成功時,會收到成功訊息的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
  "msg": "Successfully released 5 tokens from Operation Id opr_121 to Account Id: oaccount~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df (org_id : Org1MSP, user_id : user1)",
}
getOnHoldIds
這個方法會傳回指定帳戶之所有持有 ID 的清單。
Ctx.Account.getOnHoldIds(account_id: string)
參數:
  • account_id: string - 帳戶的 ID。
傳回值:
  • 成功時,具有 JSON 物件的承諾會列出指定帳戶的所有持有 ID。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
   "msg":"Holding Ids are: ohold~digicur~digiCurr101~opr_121",
   "holding_ids":[
      "ohold~digicur~digiCurr101~opr_121"
   ]
}
getOnHoldDetailsWithOperationId
此方法會傳回指定作業 ID 和記號的保留交易詳細資訊。
Ctx.Hold.getOnHoldDetailsWithOperationId(token_id: string, operation_id: string)
參數:
  • token_id: string - 記號的 ID。
  • operation_id: string - 識別保留作業的唯一 ID。通常這個 ID 會由用戶端應用程式傳送。
傳回值:
  • 成功時,包含下列特性的保留物件:
    • holding_id - 交易的持有 ID。
    • operation_id: string - 識別保留作業的唯一 ID。通常這個 ID 會由用戶端應用程式傳送。
    • from_account_id - 保留權杖目前擁有者的帳戶 ID。
    • to_account_id - 接收者的帳戶 ID。
    • notary_account_id - 公證人的帳戶 ID。
    • token_id: string - 已儲存記號的 ID。
    • quantity - 保留 ID 的記號數量。
    • time_to_expiration - 保留到期的持續時間。
  • 發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
    "assetType": "ohold",
    "holding_id": "ohold~digicur~digiCurr101~opr_121",
    "operation_id": "opr_121",
    "token_name": "digicur",
    "from_account_id": "oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df",
    "to_account_id": "oaccount~digicur~b4f45440aa2a7942db64443d047027e9d714d62cba5c3d546d64f368642f622f",
    "notary_account_id": "oaccount~digicur~38848e87296d67c8a90918f78cf55f9c9baab2cdc8c928535471aaa1210c706e",
    "token_id": "digiCurr101",
    "quantity": 10,
    "time_to_expiration": "2022-08-01T18:30:00.000Z"
}
getOnHoldBalanceWithOperationId
此方法會傳回指定作業 ID 與變數替代字的保留餘額。任何人都可以呼叫這個方法。
Ctx.Hold.getOnHoldBalanceWithOperationId(token_id: string, operation_id: string)
參數:
  • token_id: string - 記號的 ID。
  • operation_id: string - 識別保留作業的唯一 ID。通常這個 ID 會由用戶端應用程式傳送。
傳回值:
  • 成功時,具有指定作業 ID 與變數替代字之保留餘額的承諾物件。發生錯誤時,拒絕並顯示錯誤訊息
傳回值範例:
{
    "msg": "Current Holding Balance of Operation 'op1' for token 'token1' is: 10",
    "holding_balance": 10
}

記號行為管理方法 - 可燒錄行為

burn
此方法會停用或燒錄來自交易呼叫程式帳戶的記號。這個方法的呼叫程式必須具有一個帳戶以及 burner 角色。數量必須在規格檔案中 divisible 行為的 decimal 參數所指定的小數值內。
Ctx.Token.burn(quantity: number, token: <Instance of Token Class>)
參數:
  • quantity: number - 要燒錄的記號總數。
  • token: <Instance of Token Class> - 要燒錄的記號資產。
傳回值:
  • 成功時,會收到成功訊息的承諾。發生錯誤時,拒絕並顯示錯誤訊息。
傳回值範例:
{
 "msg":"Successfully burned 10 tokens from account id: oaccount~digicur~682bb71de419602af74e3f226345ae308445ca51010737900c1d85f0376152df (Org-Id: Org1MSP, User-Id: admin)"
}