module worldsibu import * as yup from 'yup' ; import { ChaincodeTx } from '@worldsibu/convector-platform-fabric' ; import { Controller , ConvectorController , Invokable , Param } from '@worldsibu/convector-core' ; import { Token , CompanyToken , Element } from './token.model' ; @ Controller ( 'token' ) export class TokenController extends ConvectorController < ChaincodeTx > { @ Invokable ( ) public async init ( @ Param ( Token ) token ) { const totalSupply = Object . keys ( token . balances ) . reduce ( ( total , fingerprint ) => total + token . balances [ fingerprint ] , 0 ) ; if ( totalSupply !== token . totalSupply ) { throw new Error ( 'The total supply does not match with the initial balances' ) ; } await token . save ( ) ; } @ Invokable ( ) public async transfer ( @ Param ( yup . string ( ) ) tokenId , @ Param ( yup . string ( ) ) to , @ Param ( yup . number ( ) . moreThan ( 0 ) ) amount ) { const token = await Token . getOne ( tokenId ) ; if ( ! token . id ) { throw new Error ( `No token found with id ${ tokenId }` ) ; } console . log ( 'Using token id' , token . id ) ; if ( token . balances [ this . sender ] < amount ) { throw new Error ( 'The sender does not have enough founds' ) ; } token . balances [ to ] += amount ; token . balances [ this . sender ] -= amount ; await token . save ( ) ; } @ Invokable ( ) public async get ( @ Param ( yup . string ( ) ) tokenId ) { const token = await Token . getOne ( tokenId ) ; if ( ! token . id ) { throw new Error ( `No token found with id ${ tokenId }` ) ; } return token . toJSON ( ) ; } @ Invokable ( ) public async whoAmI ( ) { return this . sender ; } @ Invokable ( ) public async failMe ( ) { throw new Error ( 'Expected to fail' ) ; } @ Invokable ( ) public async createCompanyToken ( @ Param ( CompanyToken ) token ) { const totalSupply = Object . keys ( token . balances ) . reduce ( ( total , fingerprint ) => total + token . balances [ fingerprint ] , 0 ) ; if ( totalSupply !== token . totalSupply ) { throw new Error ( 'The total supply does not match with the initial balances' ) ; } await token . save ( ) ; } @ Invokable ( ) public async getIdentityInfo ( ) { const mspId = this . tx . identity . getMSPID ( ) ; const cert = this . tx . identity . getX509Certificate ( ) ; const subject = cert . subject . commonName ; const issuer = cert . issuer . commonName ; return `Cert Fingerprint ${ this . sender } for ${ subject } (${ mspId }) issued by ${ issuer }` ; } @ Invokable ( ) public async saveRecursive ( @ Param ( Element ) el ) { await el . save ( ) ; } }