import { JSONPath } from 'jsonpath-plus' ; import { TezosConstants } from '../../../types/tezos/TezosConstants' ; import { KeyStore , Signer } from '../../../types/ExternalInterfaces' ; import * as TezosTypes from '../../../types/tezos/TezosChainTypes' ; import { TezosLanguageUtil } from '../TezosLanguageUtil' ; import { TezosMessageUtils } from '../TezosMessageUtil' ; import { TezosNodeReader } from '../TezosNodeReader' ; import { TezosNodeWriter } from '../TezosNodeWriter' ; import { TezosContractUtils } from './TezosContractUtils' ; /**
 * Wrapper for the tzBTC contract deployed on Tezos mainnet as KT1PWx2mnDueood7fEmfbBDKx1D9BAnnXitn with bigmap 31.
 */ export namespace TzbtcTokenHelper { param server param address export async function verifyDestination ( server , address ) { return TezosContractUtils . verifyDestination ( server , address , '187c967006ca95a648c770fdd76947ef' ) ; } param script export function verifyScript ( script ) { return TezosContractUtils . verifyScript ( script , 'ffcad1e376a6c8915780fe6676aceec6' ) ; } param server param mapid param account export async function getAccountBalance ( server , mapid , account ) { const value = await queryMap ( server , mapid , `(Pair "ledger" 0x${ TezosMessageUtils . writeAddress ( account ) })` ) ; return Number ( JSONPath ( { path : '$.args[0].int' , json : value } ) [ 0 ] ) ; } param server param mapid export async function getOperatorList ( server , mapid ) { const value = await queryMap ( server , mapid , '"operators"' ) ; let addresses = [ ] ; for ( const a of value ) { addresses . push ( TezosMessageUtils . readAddress ( a . bytes ) ) ; } return addresses ; } param server param mapid export async function getTokenMetadata ( server , mapid = 31 ) { return await queryMap ( server , mapid , '"tokenMetadata"' ) ; } /**
     * Returns the mapid of the associated contract and scale at which token values should be interpreted. With tzBTC, 1 means 0.00000001, that is 1 / 100000000 or 10 ^ 8.
     */ export async function getSimpleStorage ( server , address ) { const storageResult = await TezosNodeReader . getContractStorage ( server , address ) ; return { mapid : Number ( JSONPath ( { path : '$.args[0].int' , json : storageResult } ) [ 0 ] ) , scale : 8 } ; } export async function getTokenSupply ( server , mapid = 31 ) { const r = await queryMap ( server , mapid , '"totalSupply"' ) ; return Number ( JSONPath ( { path : '$.int' , json : r } ) [ 0 ] ) ; } export async function getPaused ( server , mapid = 31 ) { const r = await queryMap ( server , mapid , '"paused"' ) ; return ( JSONPath ( { path : '$.prim' , json : r } ) [ 0 ] ) . toLowerCase ( ) . startsWith ( 't' ) ; } export async function transferBalance ( server , signer , keystore , contract , fee , source , destination , amount , gas = 250000 , freight = 1000 ) { const parameters = `(Pair "${ source }" (Pair "${ destination }" ${ amount }))` ; const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( server , signer , keystore , contract , 0 , fee , freight , gas , 'transfer' , parameters , TezosTypes . TezosParameterFormat . Michelson , TezosConstants . HeadBranchOffset , true ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } export async function approveBalance ( server , signer , keystore , contract , fee , destination , amount , gas = 250000 , freight = 1000 ) { const parameters = `(Right (Right (Right (Right (Left (Right (Right (Right (Pair "${ destination }" ${ amount })))))))))` ; const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( server , signer , keystore , contract , 0 , fee , freight , gas , '' , parameters , TezosTypes . TezosParameterFormat . Michelson , TezosConstants . HeadBranchOffset , true ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } export async function mintBalance ( server , signer , keystore , contract , fee , destination , amount , gas = 250000 , freight = 1000 ) { const parameters = `(Right (Right (Right (Right (Right (Left (Left (Left (Pair "${ destination }" ${ amount })))))))))` ; const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( server , signer , keystore , contract , 0 , fee , freight , gas , '' , parameters , TezosTypes . TezosParameterFormat . Michelson ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } export async function addOperator ( server , signer , keystore , contract , fee , operator , gas = 250000 , freight = 1000 ) { const parameters = `(Right (Right (Right (Right (Right (Left (Right (Left "${ operator }" ))))))))` ; const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( server , signer , keystore , contract , 0 , fee , freight , gas , '' , parameters , TezosTypes . TezosParameterFormat . Michelson ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } async function queryMap ( server , mapid , query ) { const key = Buffer . from ( TezosMessageUtils . writePackedData ( query , '' , TezosTypes . TezosParameterFormat . Michelson ) , 'hex' ) ; const packedKey = TezosMessageUtils . writePackedData ( key , 'bytes' ) ; const encodedKey = TezosMessageUtils . encodeBigMapKey ( Buffer . from ( packedKey , 'hex' ) ) ; const mapResult = await TezosNodeReader . getValueForBigMapKey ( server , mapid , encodedKey ) ; if ( mapResult === undefined ) { throw new Error ( `Could not get data from map ${ mapid } for '${ query }'` ) ; } const bytes = JSONPath ( { path : '$.bytes' , json : mapResult } ) [ 0 ] ; return JSON . parse ( TezosLanguageUtil . hexToMicheline ( bytes . slice ( 2 ) ) . code ) ; } }