import { JSONPath } from 'jsonpath-plus' ; import { KeyStore , Signer } from '../../../types/ExternalInterfaces' ; import * as TezosTypes from '../../../types/tezos/TezosChainTypes' ; import { TezosMessageUtils } from '../TezosMessageUtil' ; import { TezosNodeReader } from '../TezosNodeReader' ; import { TezosNodeWriter } from '../TezosNodeWriter' ; import { TezosContractUtils } from './TezosContractUtils' ; import { TezosConseilClient } from '../../../reporting/tezos/TezosConseilClient' ; import { ConseilServerInfo } from '../../../types/conseil/QueryTypes' ; import { TezosConstants } from '../../../types/tezos/TezosConstants' ; import { ContractMapDetailsItem } from '../../../types/conseil/ConseilTezosTypes' ; import { TezosParameterFormat } from '../../../types/tezos/TezosChainTypes' ; /** The expected checksum for the Wrapped Tezos contracts. */ const CONTRACT_CHECKSUMS = { token : 'd48b45bd77d2300026fe617c5ba7670e' , oven : '5e3c30607da21a0fc30f7be61afb15c7' , core : '7b9b5b7e7f0283ff6388eb783e23c452' } ; /** The expected checksum for the Wrapped Tezos scripts. */ const SCRIPT_CHECKSUMS = { token : '' , oven : '' , core : '' } ; /**
 * Property bag containing the results of opening an oven.
 */ export type OpenOvenResult = { operationHash : string ; ovenAddress : string ; } ; export interface WrappedTezosStorage { balanceMap : number ; approvalsMap : number ; supply : number ; administrator : string ; paused : boolean ; pauseGuardian : string ; outcomeMap : number ; swapMap : number ; } export interface WrappedTezosBalanceRecord { } export interface WrappedTezosApprovalRecord { } export interface WrappedTezosOutcomeRecord { } export interface WrappedTezosSwapRecord { } /**
 * Types for the Oven Map .
 *
 * Key: The oven's address.
 * Value: The oven owner's address.
 */ export type OvenMapSchema = { key : string ; value : string ; } ; see https *
 * The token represented by these contracts trades with symbol 'WXTZ' and is specified with 10^-6 precision. Put
 * simply, 1 XTZ = 1 WXTZ, and 0.000_001 XTZ = 1 Mutez = 0.000_001 WXTZ.
 *
 * Canonical Data:
 * - Delphinet:
 *  - Token Contract: KT1JYf7xjCJAqFDfNpuump9woSMaapy1WcMY
 *  - Core Contract: KT1S98ELFTo6mdMBqhAVbGgKAVgLbdPP3AX8
 *  - Token Balances Map ID: 14566
 *  - Oven List Map ID: 14569
 * TODO(keefertaylor): Add additional data for mainnet here.
 *
 *  author Keefer Taylor, Staker Services Ltd <keefer@stakerdao.com>
  export namespace WrappedTezosHelper { param nodeUrl param tokenContractAddress param ovenContractAddress param coreContractAddress returns export async function verifyDestination ( nodeUrl , tokenContractAddress , ovenContractAddress , coreContractAddress ) { const tokenMatched = await TezosContractUtils . verifyDestination ( nodeUrl , tokenContractAddress , CONTRACT_CHECKSUMS . token ) ; const ovenMatched = await TezosContractUtils . verifyDestination ( nodeUrl , ovenContractAddress , CONTRACT_CHECKSUMS . oven ) ; const coreMatched = await TezosContractUtils . verifyDestination ( nodeUrl , coreContractAddress , CONTRACT_CHECKSUMS . core ) ; return tokenMatched && ovenMatched && coreMatched ; } param tokenScript param ovenScript param coreScript returns export function verifyScript ( tokenScript , ovenScript , coreScript ) { const tokenMatched = TezosContractUtils . verifyScript ( tokenScript , SCRIPT_CHECKSUMS . token ) ; const ovenMatched = TezosContractUtils . verifyScript ( ovenScript , SCRIPT_CHECKSUMS . oven ) ; const coreMatched = TezosContractUtils . verifyScript ( coreScript , SCRIPT_CHECKSUMS . core ) ; return tokenMatched && ovenMatched && coreMatched ; } param server param address export async function getSimpleStorage ( server , address ) { const storageResult = await TezosNodeReader . getContractStorage ( server , address ) ; return { balanceMap : Number ( JSONPath ( { path : '$.args[1].args[1].int' , json : storageResult } ) [ 0 ] ) , approvalsMap : Number ( JSONPath ( { path : '$.args[1].args[0].args[1].int' , json : storageResult } ) [ 0 ] ) , supply : Number ( JSONPath ( { path : '$.args[3].int' , json : storageResult } ) [ 0 ] ) , administrator : JSONPath ( { path : '$.args[1].args[0].args[0].string' , json : storageResult } ) [ 0 ] , paused : ( JSONPath ( { path : '$.args[2].prim' , json : storageResult } ) [ 0 ] ) . toString ( ) . toLowerCase ( ) . startsWith ( 't' ) , pauseGuardian : JSONPath ( { path : '$.args[1].args[2].string' , json : storageResult } ) [ 0 ] , outcomeMap : Number ( JSONPath ( { path : '$.args[0].args[0].args[1].int' , json : storageResult } ) [ 0 ] ) , swapMap : Number ( JSONPath ( { path : '$.args[0].args[1].int' , json : storageResult } ) [ 0 ] ) } ; } param nodeUrl param mapId param account returns export async function getAccountBalance ( server , mapid , account ) { const packedKey = TezosMessageUtils . encodeBigMapKey ( Buffer . from ( TezosMessageUtils . writePackedData ( account , 'address' ) , 'hex' ) ) ; const mapResult = await TezosNodeReader . getValueForBigMapKey ( server , mapid , packedKey ) ; if ( mapResult === undefined ) { return 0 ; } const numberString = JSONPath ( { path : '$.int' , json : mapResult } ) ; return Number ( numberString ) ; } param nodeUrl param signer param keystore param tokenContractAddress param fee param sourceAddress param destinationAddress param amount param gasLimit param storageLimit returns export async function transferBalance ( nodeUrl , signer , keystore , tokenContractAddress , fee , sourceAddress , destinationAddress , amount , gasLimit = 51300 , storageLimit = 70 ) { const parameters = `Pair "${ sourceAddress }" (Pair "${ destinationAddress }" ${ amount })` ; const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( nodeUrl , signer , keystore , tokenContractAddress , 0 , fee , storageLimit , gasLimit , 'transfer' , parameters , TezosTypes . TezosParameterFormat . Michelson , TezosConstants . HeadBranchOffset , true ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } param nodeUrl param signer param keystore param ovenAddress param fee param amountMutez param gasLimit param storageLimit returns export async function depositToOven ( nodeUrl , signer , keystore , ovenAddress , fee , amountMutez , gasLimit = 150000 , storageLimit = 10 ) { const parameters = 'Unit' ; const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( nodeUrl , signer , keystore , ovenAddress , amountMutez , fee , storageLimit , gasLimit , '' , parameters , TezosTypes . TezosParameterFormat . Michelson , TezosConstants . HeadBranchOffset , true ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } param nodeUrl param signer param keystore param ovenAddress param fee param amountMutez param gasLimit param storageLimit returns export async function withdrawFromOven ( nodeUrl , signer , keystore , ovenAddress , fee , amountMutez , gasLimit = 121000 , storageLimit = 0 ) { const parameters = `${ amountMutez }` ; const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( nodeUrl , signer , keystore , ovenAddress , 0 , fee , storageLimit , gasLimit , 'withdraw' , parameters , TezosTypes . TezosParameterFormat . Michelson , TezosConstants . HeadBranchOffset , true ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } param serverInfo param coreContractAddress param ovenOwner param ovenListBigMapId export async function listOvens ( serverInfo , coreContractAddress , ovenOwner , ovenListBigMapId ) { const mapData = await TezosConseilClient . getBigMapData ( serverInfo , coreContractAddress ) ; if ( mapData === undefined ) { throw new Error ( "Could not fetch map data!" ) ; } const { maps } = mapData ; let ovenListMap = undefined ; for ( let i = 0 ; i < maps . length ; i ++ ) { if ( maps [ i ] . definition . index === ovenListBigMapId ) { ovenListMap = maps [ i ] ; break ; } } if ( ovenListMap === undefined ) { throw new Error ( "Could not find specified map ID!" ) ; } const { content } = ovenListMap ; const normalizedOvenList = content . map ( ( oven ) => { return { key : TezosMessageUtils . readAddress ( oven . key . replace ( /\"/g , '' ) . replace ( /\n/ , '' ) . replace ( "0x" , "" ) ) , value : TezosMessageUtils . readAddress ( oven . value . replace ( /\"/g , '' ) . replace ( /\n/ , '' ) . replace ( "0x" , "" ) ) } ; } ) ; const ownedOvens = normalizedOvenList . filter ( ( oven ) : boolean => { return ovenOwner === oven . value ; } ) ; return ownedOvens . map ( ( oven ) => { return oven . key ; } ) ; } param nodeUrl param signer param keystore param fee param coreAddress param baker param gasLimit param storageLimit returns export async function deployOven ( nodeUrl , signer , keystore , fee , coreAddress , baker = undefined , gasLimit = 115000 , storageLimit = 1100 ) { const entryPoint = 'runEntrypointLambda' ; const lambdaName = 'createOven' ; const bakerParam = baker !== undefined ? `Some "${ baker }"` : 'None' ; const bytes = TezosMessageUtils . writePackedData ( `Pair ${ bakerParam } "${ keystore . publicKeyHash }"` , 'pair (option key_hash) address' , TezosParameterFormat . Michelson ) ; const parameters = `Pair "${ lambdaName }" 0x${ bytes }` ; const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( nodeUrl , signer , keystore , coreAddress , 0 , fee , storageLimit , gasLimit , entryPoint , parameters , TezosTypes . TezosParameterFormat . Michelson , TezosConstants . HeadBranchOffset , true ) ; const operationHash = TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; const ovenAddress = TezosMessageUtils . calculateContractAddress ( operationHash , 0 ) ; return { operationHash , ovenAddress } ; } param nodeUrl param signer param keystore param fee param ovenAddress param bakerAddress param gasLimit param storageLimit returns export async function setOvenBaker ( nodeUrl , signer , keystore , fee , ovenAddress , bakerAddress , gasLimit = 23500 , storageLimit = 0 ) { const parameters = `Some "${ bakerAddress }"` ; const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( nodeUrl , signer , keystore , ovenAddress , 0 , fee , storageLimit , gasLimit , 'setDelegate' , parameters , TezosTypes . TezosParameterFormat . Michelson , TezosConstants . HeadBranchOffset , true ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } param nodeUrl param signer param keystore param fee param ovenAddress param gasLimit param storageLimit returns export async function clearOvenBaker ( nodeUrl , signer , keystore , fee , ovenAddress , gasLimit = 23500 , storageLimit = 0 ) { const parameters = `None` ; const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( nodeUrl , signer , keystore , ovenAddress , 0 , fee , storageLimit , gasLimit , 'setDelegate' , parameters , TezosTypes . TezosParameterFormat . Michelson , TezosConstants . HeadBranchOffset , true ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } }