import { JSONPath } from 'jsonpath-plus' ; import { KeyStore , Signer } from '../../../types/ExternalInterfaces' ; import * as TezosTypes from '../../../types/tezos/TezosChainTypes' ; import { TezosConstants } from '../../../types/tezos/TezosConstants' ; import { TezosNodeReader } from '../TezosNodeReader' ; import { TezosNodeWriter } from '../TezosNodeWriter' ; import { TezosContractUtils } from './TezosContractUtils' ; export namespace BabylonDelegationHelper { param server param address export async function verifyDestination ( server , address ) { return TezosContractUtils . verifyDestination ( server , address , 'd99cb8b4c7e40166f59c0f3c30724225' ) ; } param script export function verifyScript ( script ) { return TezosContractUtils . verifyScript ( script , 'a585489ffaee60d07077059539d5bfc8' ) ; } export async function getSimpleStorage ( server , address ) { const storageResult = await TezosNodeReader . getContractStorage ( server , address ) ; return { administrator : JSONPath ( { path : '$.string' , json : storageResult } ) [ 0 ] } ; } param server param keyStore param delegator param delegate param fee param derivationPath export function setDelegate ( server , signer , keyStore , contract , delegate , fee ) { if ( contract . startsWith ( 'KT1' ) ) { const parameters = `[{ "prim": "DROP" }, { "prim": "NIL", "args": [{ "prim": "operation" }] }, { "prim": "PUSH", "args": [{ "prim": "key_hash" }, { "string": "${ delegate }" } ] }, { "prim": "SOME" }, { "prim": "SET_DELEGATE" }, { "prim": "CONS" } ]` ; return TezosNodeWriter . sendContractInvocationOperation ( server , signer , keyStore , contract , 0 , fee , 0 , TezosConstants . P005ManagerContractWithdrawalGasLimit , 'do' , parameters , TezosTypes . TezosParameterFormat . Micheline ) ; } else { return TezosNodeWriter . sendDelegationOperation ( server , signer , keyStore , delegate , fee ) ; } } param server param keyStore param contract param fee param derivationPath export function unSetDelegate ( server , signer , keyStore , contract , fee ) { if ( contract . startsWith ( 'KT1' ) ) { const parameters = `[{ "prim": "DROP" }, { "prim": "NIL", "args": [{ "prim": "operation" }] }, { "prim": "NONE", "args": [{ "prim": "key_hash" }] }, { "prim": "SET_DELEGATE" }, { "prim": "CONS" } ]` ; return TezosNodeWriter . sendContractInvocationOperation ( server , signer , keyStore , contract , 0 , fee , 0 , TezosConstants . P005ManagerContractWithdrawalGasLimit , 'do' , parameters , TezosTypes . TezosParameterFormat . Micheline ) ; } else { return TezosNodeWriter . sendUndelegationOperation ( server , signer , keyStore , fee ) ; } } param server param keyStore param contract param fee param amount param derivationPath export function withdrawDelegatedFunds ( server , signer , keyStore , contract , fee , amount ) { return sendDelegatedFunds ( server , signer , keyStore , contract , fee , amount , keyStore . publicKeyHash ) ; } param server param keyStore param contract param fee param amount param derivationPath param destination export function sendDelegatedFunds ( server , signer , keyStore , contract , fee , amount , destination ) { let parameters = `[ { "prim": "DROP" },
            { "prim": "NIL", "args": [ { "prim": "operation" } ] },
            { "prim": "PUSH", "args": [ { "prim": "key_hash" }, { "string": "${ destination }" } ] },
            { "prim": "IMPLICIT_ACCOUNT" },
            { "prim": "PUSH", "args": [ { "prim": "mutez" }, { "int": "${ amount }" } ] },
            { "prim": "UNIT" },
            { "prim": "TRANSFER_TOKENS" },
            { "prim": "CONS" } ]` ; return TezosNodeWriter . sendContractInvocationOperation ( server , signer , keyStore , contract , 0 , fee , TezosConstants . P005ManagerContractWithdrawalStorageLimit , TezosConstants . P005ManagerContractWithdrawalGasLimit , 'do' , parameters , TezosTypes . TezosParameterFormat . Micheline ) ; } param server param keyStore param contract param fee param amount param derivationPath export function depositDelegatedFunds ( server , signer , keyStore , contract , fee , amount ) { return TezosNodeWriter . sendContractInvocationOperation ( server , signer , keyStore , contract , amount , fee , 0 , TezosConstants . P005ManagerContractDepositGasLimit , undefined , undefined ) ; } export function deployManagerContract ( server , signer , keyStore , delegate , fee , amount , optimizeFee = false ) { const code = `[ { "prim": "parameter",
        "args":
          [ { "prim": "or",
              "args":
                [ { "prim": "lambda",
                    "args":
                      [ { "prim": "unit" }, { "prim": "list", "args": [ { "prim": "operation" } ] } ], "annots": [ "%do" ] },
                  { "prim": "unit", "annots": [ "%default" ] } ] } ] },
      { "prim": "storage", "args": [ { "prim": "key_hash" } ] },
      { "prim": "code",
        "args":
          [ [ [ [ { "prim": "DUP" }, { "prim": "CAR" },
                  { "prim": "DIP", "args": [ [ { "prim": "CDR" } ] ] } ] ],
              { "prim": "IF_LEFT",
                "args":
                  [ [ { "prim": "PUSH", "args": [ { "prim": "mutez" }, { "int": "0" } ] },
                      { "prim": "AMOUNT" },
                      [ [ { "prim": "COMPARE" }, { "prim": "EQ" } ],
                        { "prim": "IF", "args": [ [], [ [ { "prim": "UNIT" }, { "prim": "FAILWITH" } ] ] ] } ],
                      [ { "prim": "DIP", "args": [ [ { "prim": "DUP" } ] ] },
                        { "prim": "SWAP" } ],
                      { "prim": "IMPLICIT_ACCOUNT" },
                      { "prim": "ADDRESS" },
                      { "prim": "SENDER" },
                      [ [ { "prim": "COMPARE" }, { "prim": "EQ" } ],
                        { "prim": "IF", "args": [ [], [ [ { "prim": "UNIT" },{ "prim": "FAILWITH" } ] ] ] } ],
                      { "prim": "UNIT" }, { "prim": "EXEC" },
                      { "prim": "PAIR" } ],
                    [ { "prim": "DROP" },
                      { "prim": "NIL", "args": [ { "prim": "operation" } ] },
                      { "prim": "PAIR" } ] ] } ] ] } ]` ; const storage = `{ "string": "${ keyStore . publicKeyHash }" }` ; return TezosNodeWriter . sendContractOriginationOperation ( server , signer , keyStore , amount , delegate , fee , 600 , 20000 , code , storage , TezosTypes . TezosParameterFormat . Micheline , TezosConstants . HeadBranchOffset , optimizeFee ) ; } }