All files / shelter shelter.ts

100% Statements 14/14
100% Branches 0/0
100% Functions 12/12
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118                        17x 17x 17x       1x                         1x                 4x                             1x                   5x       1x       1x                 1x                 7x       1x                           7x                
import { Client, Keypair } from "shelter-sdk";
import type { Client as SAC } from "sac-sdk";
import type { FakeClient } from "../fake-client/fake-client";
import { Transaction } from "../transaction/transaction";
import type { Rpc } from "../rpc/rpc";
import { Gate } from "../gate/gate";
import { Aid } from "../aid/aid";
import type { FakeSAC } from "../fake-sac/fake-sac";
import type { Pass } from "../pass/pass.interface";
 
export class Shelter {
  constructor(
    private readonly _steward: Keypair,
    private readonly _rpc: Rpc,
    private readonly _client: Client | FakeClient
  ) { }
 
  static async connected(aSteward: Keypair, aRpc: Rpc, aShelterId: string) {
    return new this(
      aSteward,
      aRpc,
      new Client({
        contractId: aShelterId,
        networkPassphrase: await aRpc.network(),
        rpcUrl: aRpc.url(),
        publicKey: aSteward.publicKey(),
      })
    );
  }
 
  async stewardId(): Promise<string> {
    return (await this._client.steward()).result;
  }
 
  async boundAid(
    recipient: Buffer,
    token: string,
    amount: bigint,
    expiration: bigint
  ): Promise<void> {
    await this._txOf(
      await this._client.bound_aid({
        recipient,
        token,
        amount,
        expiration,
      }),
      'Bound Aid Error'
    ).result();
  }
 
  async unboundAid(
    recipient: Buffer,
    token: string,
  ): Promise<void> {
    await this._txOf(
      await this._client.unbound_aid({
        recipient,
        token,
      }),
      'Unbound Aid Error'
    ).result();
  }
 
  id(): string {
    return this._client.options.contractId;
  }
 
  async aidOf(recipient: Buffer, token: string): Promise<bigint> {
    return (await this._client.aid_of({ recipient, token })).result.amount;
  }
 
  async updateReleaseKey(releaseKey: Buffer): Promise<void> {
    await this._txOf(
      await this._client.update_release_key({
        steward_key: releaseKey,
      }),
      'Update Release Key Error'
    ).result();
  }
 
  async updateSteward(newSteward: Keypair): Promise<void> {
    await this._txOf(
      await this._client.update_steward({
        new_steward: newSteward.publicKey(),
      }),
      'Update Steward Error'
    ).result();
  }
 
  gate(): Gate {
    return new Gate(this._client, this._steward, this._rpc);
  }
 
  async withdraw(sac: SAC | FakeSAC, pass: Pass): Promise<void> {
    await new Aid(
      this._steward,
      this._steward,
      sac,
      this,
      this._rpc
    ).transfer(
      this._steward.publicKey(),
      (await sac.balance({ id: this.id() })).result,
      pass
    );
  }
 
  _txOf(aRawTx: any, errorMsg: string): Transaction {
    return new Transaction(
      aRawTx,
      this._steward,
      this._rpc,
      errorMsg
    )
  }
}