{
  "type": "script",
  "title": "Stake Delegation",
  "description": "Registers the derived stake key and delegates it to a stake pool in a single transaction, then returns the delegated address so it can be funded. The target stake pool is exposed as a flat argument, so an auto-generated dapp can delegate to any pool without touching the code, and it is checked with assert() before the wallet builds anything",
  "exportAs": "SingleDelegate",
  "require": {
    "not": {
      "walletTypeIn": [
        "burner",
        "gift"
      ]
    }
  },
  "return": {
    "mode": "last"
  },
  "args": {
    "stake-pool-key-hash": "bd24b3372791f401cc029455c44ea77f3c8750ce3b804a354af0ff16",
    "stake-pool-ticker": "PEACE"
  },
  "run": {
    "argumentGuards": {
      "type": "macro",
      "run": {
        "stake-pool-key-hash-must-be-a-string": "{ assert( isType('string',get('args.stake-pool-key-hash')) ,'stake-pool-key-hash must be a string' , get('args') ) }",
        "stake-pool-key-hash-must-not-be-empty": "{ assert( not(isEmptyString(get('args.stake-pool-key-hash'))) ,'stake-pool-key-hash must not be empty' , get('args') ) }",
        "stake-pool-key-hash-must-be-56-chars": "{ assert( and(eq(get('args.stake-pool-key-hash'),truncate(get('args.stake-pool-key-hash'),56,0,'')),neq(get('args.stake-pool-key-hash'),truncate(get('args.stake-pool-key-hash'),55,0,''))) ,'stake-pool-key-hash must be exactly 28 bytes (56 hex chars)' , get('args') ) }",
        "stake-pool-key-hash-must-not-be-bech32": "{ assert( neq(truncate(get('args.stake-pool-key-hash'),4,0,''),'pool') ,'stake-pool-key-hash must be the raw hex pool key hash, not a bech32 pool id starting with \"pool1\"' , get('args') ) }"
      }
    },
    "dependencies": {
      "type": "script",
      "run": {
        "currentAddress": {
          "type": "getCurrentAddress"
        },
        "addressInfo": {
          "type": "macro",
          "run": "{getAddressInfo(get('cache.dependencies.currentAddress'))}"
        }
      }
    },
    "registrationCert": {
      "type": "certificate",
      "cert": {
        "kind": "StakeRegistration",
        "pubKeyHashHex": "{get('cache.dependencies.addressInfo.stakingKeyHash')}"
      }
    },
    "delegationCert": {
      "type": "certificate",
      "cert": {
        "kind": "StakeDelegation",
        "pubKeyHashHex": "{get('cache.dependencies.addressInfo.stakingKeyHash')}",
        "poolKeyHashHex": "{get('args.stake-pool-key-hash')}"
      }
    },
    "txs": {
      "type": "script",
      "args": "{get('args')}",
      "run": {
        "buildDelegate": {
          "type": "buildTx",
          "title": "{join(' ',getArray('Stake Delegation to',get('args.stake-pool-ticker')))}",
          "tx": {
            "certificates": [
              {
                "certHex": "{get('cache.registrationCert')}"
              },
              {
                "certHex": "{get('cache.delegationCert')}"
              }
            ],
            "options": {
              "autoProvision": {
                "workspaceNativeScript": true
              },
              "autoOptionalSigners": {
                "nativeScript": true
              },
              "autoClean": {
                "certReRegistrations": true
              }
            }
          }
        },
        "signDelegate": {
          "type": "signTxs",
          "detailedPermissions": false,
          "txs": [
            "{get('cache.txs.buildDelegate.txHex')}"
          ]
        },
        "submitDelegate": {
          "type": "submitTxs",
          "txs": "{get('cache.txs.signDelegate')}"
        }
      }
    },
    "finally": {
      "type": "macro",
      "run": {
        "address": "{get('cache.dependencies.currentAddress')}",
        "rewardAddress": "{get('cache.dependencies.addressInfo.rewardAddress')}",
        "stakePoolKeyHash": "{get('args.stake-pool-key-hash')}",
        "txHash": "{get('cache.txs.buildDelegate.txHash')}"
      }
    }
  }
}