From 60c812c7600626ad4a31b36f4294be7470b45927 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Fri, 27 Nov 2020 12:24:32 +0000 Subject: [PATCH] cosmwasm: Add Cw1SubkeyCosmWasmClient class --- .../cosmwasm/src/cw1subkeycosmwasmclient.ts | 123 ++++++++++++++++++ .../types/cw1subkeycosmwasmclient.d.ts | 38 ++++++ 2 files changed, 161 insertions(+) create mode 100644 packages/cosmwasm/src/cw1subkeycosmwasmclient.ts create mode 100644 packages/cosmwasm/types/cw1subkeycosmwasmclient.d.ts diff --git a/packages/cosmwasm/src/cw1subkeycosmwasmclient.ts b/packages/cosmwasm/src/cw1subkeycosmwasmclient.ts new file mode 100644 index 00000000..a9a45297 --- /dev/null +++ b/packages/cosmwasm/src/cw1subkeycosmwasmclient.ts @@ -0,0 +1,123 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { Coin } from "@cosmjs/launchpad"; + +import { Cw1CosmWasmClient } from "./cw1cosmwasmclient"; +import { ExecuteResult } from "./signingcosmwasmclient"; +import { Expiration } from "./types"; + +export interface AllowanceResult { + readonly balance: readonly Coin[]; + readonly expires: Expiration; +} + +export interface Cw1SubkeyPermissions { + readonly delegate: boolean; + readonly redelegate: boolean; + readonly undelegate: boolean; + readonly withdraw: boolean; +} + +export class Cw1SubkeyCosmWasmClient extends Cw1CosmWasmClient { + private async setAdmins(admins: readonly string[], memo = ""): Promise { + const handleMsg = { + update_admins: { + admins: admins, + }, + }; + return this.execute(this.cw1ContractAddress, handleMsg, memo); + } + + public async getAdmins(): Promise { + const { admins } = await this.queryContractSmart(this.cw1ContractAddress, { admin_list: {} }); + return admins; + } + + public async isAdmin(address = this.senderAddress): Promise { + const admins = await this.getAdmins(); + return admins.includes(address); + } + + public async getAllAllowances(): Promise { + const { allowances } = await this.queryContractSmart(this.cw1ContractAddress, { + all_allowances: {}, + }); + return allowances; + } + + public async getAllowance(address = this.senderAddress): Promise { + return this.queryContractSmart(this.cw1ContractAddress, { + allowance: { spender: address }, + }); + } + + public async getAllPermissions(): Promise { + const { permissions } = await this.queryContractSmart(this.cw1ContractAddress, { + all_permissions: {}, + }); + return permissions; + } + + public async getPermissions(address = this.senderAddress): Promise { + return this.queryContractSmart(this.cw1ContractAddress, { + permissions: { spender: address }, + }); + } + + public async addAdmin(address: string, memo = ""): Promise { + const admins = await this.getAdmins(); + const newAdmins = admins.includes(address) ? admins : [...admins, address]; + return this.setAdmins(newAdmins, memo); + } + + public async removeAdmin(address: string, memo = ""): Promise { + const admins = await this.getAdmins(); + const newAdmins = admins.filter((admin) => admin !== address); + return this.setAdmins(newAdmins, memo); + } + + public async increaseAllowance( + address: string, + amount: Coin, + expires?: Expiration, + memo = "", + ): Promise { + const handleMsg = { + increase_allowance: { + spender: address, + amount: amount, + expires: expires, + }, + }; + return this.execute(this.cw1ContractAddress, handleMsg, memo); + } + + public async decreaseAllowance( + address: string, + amount: Coin, + expires?: Expiration, + memo = "", + ): Promise { + const handleMsg = { + decrease_allowance: { + spender: address, + amount: amount, + expires: expires, + }, + }; + return this.execute(this.cw1ContractAddress, handleMsg, memo); + } + + public async setPermissions( + address: string, + permissions: Cw1SubkeyPermissions, + memo = "", + ): Promise { + const handleMsg = { + set_permissions: { + spender: address, + permissions: permissions, + }, + }; + return this.execute(this.cw1ContractAddress, handleMsg, memo); + } +} diff --git a/packages/cosmwasm/types/cw1subkeycosmwasmclient.d.ts b/packages/cosmwasm/types/cw1subkeycosmwasmclient.d.ts new file mode 100644 index 00000000..0e7c1ef5 --- /dev/null +++ b/packages/cosmwasm/types/cw1subkeycosmwasmclient.d.ts @@ -0,0 +1,38 @@ +import { Coin } from "@cosmjs/launchpad"; +import { Cw1CosmWasmClient } from "./cw1cosmwasmclient"; +import { ExecuteResult } from "./signingcosmwasmclient"; +import { Expiration } from "./types"; +export interface AllowanceResult { + readonly balance: readonly Coin[]; + readonly expires: Expiration; +} +export interface Cw1SubkeyPermissions { + readonly delegate: boolean; + readonly redelegate: boolean; + readonly undelegate: boolean; + readonly withdraw: boolean; +} +export declare class Cw1SubkeyCosmWasmClient extends Cw1CosmWasmClient { + private setAdmins; + getAdmins(): Promise; + isAdmin(address?: string): Promise; + getAllAllowances(): Promise; + getAllowance(address?: string): Promise; + getAllPermissions(): Promise; + getPermissions(address?: string): Promise; + addAdmin(address: string, memo?: string): Promise; + removeAdmin(address: string, memo?: string): Promise; + increaseAllowance( + address: string, + amount: Coin, + expires?: Expiration, + memo?: string, + ): Promise; + decreaseAllowance( + address: string, + amount: Coin, + expires?: Expiration, + memo?: string, + ): Promise; + setPermissions(address: string, permissions: Cw1SubkeyPermissions, memo?: string): Promise; +}