cosmwasm: Add Cw1SubkeyCosmWasmClient class

This commit is contained in:
willclarktech 2020-11-27 12:24:32 +00:00
parent c9d5cc50d4
commit 60c812c760
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
2 changed files with 161 additions and 0 deletions

View File

@ -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<ExecuteResult> {
const handleMsg = {
update_admins: {
admins: admins,
},
};
return this.execute(this.cw1ContractAddress, handleMsg, memo);
}
public async getAdmins(): Promise<readonly string[]> {
const { admins } = await this.queryContractSmart(this.cw1ContractAddress, { admin_list: {} });
return admins;
}
public async isAdmin(address = this.senderAddress): Promise<boolean> {
const admins = await this.getAdmins();
return admins.includes(address);
}
public async getAllAllowances(): Promise<readonly AllowanceResult[]> {
const { allowances } = await this.queryContractSmart(this.cw1ContractAddress, {
all_allowances: {},
});
return allowances;
}
public async getAllowance(address = this.senderAddress): Promise<AllowanceResult> {
return this.queryContractSmart(this.cw1ContractAddress, {
allowance: { spender: address },
});
}
public async getAllPermissions(): Promise<readonly Cw1SubkeyPermissions[]> {
const { permissions } = await this.queryContractSmart(this.cw1ContractAddress, {
all_permissions: {},
});
return permissions;
}
public async getPermissions(address = this.senderAddress): Promise<Cw1SubkeyPermissions> {
return this.queryContractSmart(this.cw1ContractAddress, {
permissions: { spender: address },
});
}
public async addAdmin(address: string, memo = ""): Promise<ExecuteResult> {
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<ExecuteResult> {
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<ExecuteResult> {
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<ExecuteResult> {
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<ExecuteResult> {
const handleMsg = {
set_permissions: {
spender: address,
permissions: permissions,
},
};
return this.execute(this.cw1ContractAddress, handleMsg, memo);
}
}

View File

@ -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<readonly string[]>;
isAdmin(address?: string): Promise<boolean>;
getAllAllowances(): Promise<readonly AllowanceResult[]>;
getAllowance(address?: string): Promise<AllowanceResult>;
getAllPermissions(): Promise<readonly Cw1SubkeyPermissions[]>;
getPermissions(address?: string): Promise<Cw1SubkeyPermissions>;
addAdmin(address: string, memo?: string): Promise<ExecuteResult>;
removeAdmin(address: string, memo?: string): Promise<ExecuteResult>;
increaseAllowance(
address: string,
amount: Coin,
expires?: Expiration,
memo?: string,
): Promise<ExecuteResult>;
decreaseAllowance(
address: string,
amount: Coin,
expires?: Expiration,
memo?: string,
): Promise<ExecuteResult>;
setPermissions(address: string, permissions: Cw1SubkeyPermissions, memo?: string): Promise<ExecuteResult>;
}