Skip to content

Commit

Permalink
Merge pull request #16 from terra-money/feature/latest_updates
Browse files Browse the repository at this point in the history
Feature/latest updates
  • Loading branch information
simke9445 authored Feb 21, 2024
2 parents 5554482 + e8aebe4 commit a92fbe2
Show file tree
Hide file tree
Showing 33 changed files with 1,617 additions and 734 deletions.
45 changes: 36 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# warp-sdk

WarpSdk provides a Typescript API for interacting with warp protocol, the automation protocol for the Cosmos ecosystem. The SDK provides a simple way to interact with the warp protocol's contracts, allowing developers to perform operations such as creating and managing jobs, templates, and accounts.
The Warp SDK provides a Typescript API for interacting with Warp Protocol, the decentralized automation tool for the Cosmos ecosystem. Warp allows developers to create novel features or experiences for their users through cost-efficient, on-chain automation—no smart contract changes necessary.

The SDK provides a simple way to interact with Warp Protocol's contracts to automatically execute transactions in the future based on any available on-chain data. The trigger for execution is referred to as a condition, and the corresponding job encompasses the executable message. Warp jobs are submitted to a job queue, where participants called keepers monitor the conditions and—once met—execute the pre-signed job message.

Read on below, check out the [docs](https://docs.warp.money/) for more information, or [get in touch](https://docs.google.com/forms/d/e/1FAIpQLSeYGdWL9tIHC3BP2riYXtT_cyZVDMGKrrSH0JCPCdV3PtZGyg/viewform) with the team to start building with Warp.

## Installation

Expand Down Expand Up @@ -30,7 +34,7 @@ const lcd = new LCDClient({
'pisco-1': piscoLcdClientConfig,
});

const wallet = new Wallet(lcd, new MnemonicKey({ mnemonic: '<your mnemonic here>' }));
const wallet = new Wallet(lcd, new MnemonicKey({ mnemonic: '...' }));

const sdk = new WarpSdk(wallet, piscoLcdClientConfig);
const sender = wallet.key.accAddress(piscoLcdClientConfig.prefix);
Expand All @@ -46,20 +50,43 @@ const nextExecution = variable

const condition = cond.uint(uint.env('time'), 'gt', uint.ref(nextExecution));

const executions = [
{
condition,
msgs: [msg.execute('terra10788fkzah89xrdm27zkj5yvhj9x3494lxawzm5qq3vvxcqz2yzaqyd3enk', { harvest: {} })],
},
];

const recurring = true;
const durationDays = '30';
const vars = [nextExecution];

const estimateJobRewardMsg = job
.estimate()
.recurring(recurring)
.durationDays(durationDays)
.vars(vars)
.executions(executions)
.compose();

const reward = await sdk.estimateJobReward(sender, estimateJobRewardMsg);

const operationalAmount = await sdk.estimateJobFee(sender, estimateJobRewardMsg, reward.amount.toString());

const createJobMsg = job
.create()
.name('eris-harvest')
.description('This job harvests rewards for eris protoocl vaults each day.')
.labels([])
.recurring(true)
.requeueOnEvict(true)
.reward('50000')
.cond(condition)
.var(nextExecution)
.msg(msg.execute('terra10788fkzah89xrdm27zkj5yvhj9x3494lxawzm5qq3vvxcqz2yzaqyd3enk', { harvest: {} }))
.recurring(recurring)
.reward(reward.amount.toString())
.operationalAmount(operationalAmount.amount.toString())
.vars(vars)
.durationDays(durationDays)
.executions(executions)
.compose();

sdk.createJob(sender, createJobMsg).then((response) => {
sdk.createJob(sender, createJobMsg, [operationalAmount]).then((response) => {
console.log(response);
});

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"url": "https://github.com/terra-money/warp-sdk/issues"
},
"homepage": "https://github.com/terra-money/warp-sdk#readme",
"version": "0.1.68",
"version": "0.2.1",
"scripts": {
"clean": "rm -rf dist",
"build": "yarn clean && yarn build:node && yarn build:browser",
Expand All @@ -42,9 +42,10 @@
"generate-types": {
"contracts": [
"warp-controller",
"warp-account",
"warp-resolver",
"warp-templates"
"warp-templates",
"warp-account",
"warp-account-tracker"
],
"output": "src/types/contracts"
},
Expand Down
6 changes: 2 additions & 4 deletions src/composers/account.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { warp_account, warp_resolver } from '../types';

type GenericMsg = Extract<warp_account.ExecuteMsg, { generic: {} }>;

export class AccountComposer {
generic(msgs: warp_resolver.CosmosMsgFor_Empty[]): GenericMsg {
msgs(msgs: warp_resolver.WarpMsg[]): Extract<warp_account.ExecuteMsg, { warp_msgs: {} }> {
return {
generic: {
warp_msgs: {
msgs,
},
};
Expand Down
22 changes: 11 additions & 11 deletions src/composers/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export class ConditionComposer {
}

public string(
left: warp_resolver.ValueFor_String,
left: warp_resolver.StringValueFor_String,
op: warp_resolver.StringOp,
right: warp_resolver.ValueFor_String
right: warp_resolver.StringValueFor_String
): warp_resolver.Condition {
const expr: warp_resolver.GenExprFor_ValueFor_StringAnd_StringOp = { left, op, right };
const expr: warp_resolver.GenExprFor_StringValueFor_StringAnd_StringOp = { left, op, right };
return this.expr({ string: expr });
}

Expand Down Expand Up @@ -161,37 +161,37 @@ export class DecimalValueComposer {
}

export class StringValueComposer {
public simple(value: string): warp_resolver.ValueFor_String {
public simple(value: string): warp_resolver.StringValueFor_String {
return { simple: value };
}

public ref(ref: warp_resolver.Variable): warp_resolver.ValueFor_String {
public ref(ref: warp_resolver.Variable): warp_resolver.StringValueFor_String {
return { ref: `$warp.variable.${variableName(ref)}` };
}
}

export class UpdateFnComposer {
public uint(value: warp_resolver.NumValueFor_Uint256And_NumExprOpAnd_IntFnOp): warp_resolver.UpdateFnValue {
public uint(value: warp_resolver.NumValueFor_Uint256And_NumExprOpAnd_IntFnOp): warp_resolver.FnValue {
return { uint: value };
}

public int(value: warp_resolver.NumValueForInt128And_NumExprOpAnd_IntFnOp): warp_resolver.UpdateFnValue {
public int(value: warp_resolver.NumValueForInt128And_NumExprOpAnd_IntFnOp): warp_resolver.FnValue {
return { int: value };
}

public decimal(value: warp_resolver.NumValueFor_Decimal256And_NumExprOpAnd_DecimalFnOp): warp_resolver.UpdateFnValue {
public decimal(value: warp_resolver.NumValueFor_Decimal256And_NumExprOpAnd_DecimalFnOp): warp_resolver.FnValue {
return { decimal: value };
}

public timestamp(value: warp_resolver.NumValueForInt128And_NumExprOpAnd_IntFnOp): warp_resolver.UpdateFnValue {
public timestamp(value: warp_resolver.NumValueForInt128And_NumExprOpAnd_IntFnOp): warp_resolver.FnValue {
return { timestamp: value };
}

public block_height(value: warp_resolver.NumValueForInt128And_NumExprOpAnd_IntFnOp): warp_resolver.UpdateFnValue {
public block_height(value: warp_resolver.NumValueForInt128And_NumExprOpAnd_IntFnOp): warp_resolver.FnValue {
return { block_height: value };
}

public bool(value: warp_resolver.Variable): warp_resolver.UpdateFnValue {
public bool(value: warp_resolver.Variable): warp_resolver.FnValue {
return { bool: `$warp.variable.${variableName(value)}` };
}
}
2 changes: 1 addition & 1 deletion src/composers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const composers = {
string,
cond,
fn,
msg,
msg: {},
template,
job,
variable,
Expand Down
111 changes: 86 additions & 25 deletions src/composers/job.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EstimateJobMsg } from 'sdk';
import { Execution } from '../types';
import { warp_controller, warp_resolver } from '../types/contracts';

export class JobSequenceMsgComposer {
Expand Down Expand Up @@ -39,14 +41,15 @@ export class JobSequenceMsgComposer {
export class CreateJobMsgComposer {
private _name: string | undefined;
private _recurring: boolean | undefined;
private _requeue_on_evict: boolean | undefined;
private _reward: warp_controller.Uint128 | undefined;
private _description: string;
private _labels: string[];
private _assetsToWithdraw: warp_controller.AssetInfo[] | undefined;
private _msgs: warp_resolver.CosmosMsgFor_Empty[] = [];
private _vars: warp_resolver.Variable[] = [];
private _condition: warp_resolver.Condition | undefined;
private _executions: Execution[] = [];
private _durationDays: string;
private _fundingAccount: string;
private _operationalAmount: warp_controller.Uint128 | undefined;

static new(): CreateJobMsgComposer {
return new CreateJobMsgComposer();
Expand All @@ -62,11 +65,6 @@ export class CreateJobMsgComposer {
return this;
}

requeueOnEvict(requeue_on_evict: boolean): CreateJobMsgComposer {
this._requeue_on_evict = requeue_on_evict;
return this;
}

reward(reward: warp_controller.Uint128): CreateJobMsgComposer {
this._reward = reward;
return this;
Expand All @@ -82,61 +80,124 @@ export class CreateJobMsgComposer {
return this;
}

assetsToWithdraw(assetsToWithdraw: warp_controller.AssetInfo[]): CreateJobMsgComposer {
this._assetsToWithdraw = assetsToWithdraw;
durationDays(durationDays: string): CreateJobMsgComposer {
this._durationDays = durationDays;
return this;
}

fundingAccount(fundingAccount?: string): CreateJobMsgComposer {
this._fundingAccount = fundingAccount;
return this;
}

msg(msg: warp_resolver.CosmosMsgFor_Empty): CreateJobMsgComposer {
this._msgs.push(msg);
operationalAmount(operationalAmount: warp_controller.Uint128): CreateJobMsgComposer {
this._operationalAmount = operationalAmount;
return this;
}

cond(condition: warp_resolver.Condition): CreateJobMsgComposer {
this._condition = condition;
assetsToWithdraw(assetsToWithdraw: warp_controller.AssetInfo[]): CreateJobMsgComposer {
this._assetsToWithdraw = assetsToWithdraw;
return this;
}

var(variable: warp_resolver.Variable): CreateJobMsgComposer {
this._vars.push(variable);
executions(executions: Execution[]): CreateJobMsgComposer {
this._executions = executions;
return this;
}

vars(vars: warp_resolver.Variable[]): CreateJobMsgComposer {
this._vars = vars;
return this;
}

compose(): warp_controller.CreateJobMsg {
if (
this._name === undefined ||
this._recurring === undefined ||
this._requeue_on_evict === undefined ||
this._reward === undefined ||
this._description === undefined ||
this._labels === undefined
this._labels === undefined ||
this._operationalAmount == undefined
) {
throw new Error('All required fields must be provided');
}

if (this._condition === undefined) {
throw new Error('Condition must be provided');
}

const createJobMsg: warp_controller.CreateJobMsg = {
name: this._name,
recurring: this._recurring,
requeue_on_evict: this._requeue_on_evict,
reward: this._reward,
description: this._description,
labels: this._labels,
condition: JSON.stringify(this._condition),
msgs: JSON.stringify(this._msgs),
executions: this._executions.map((e) => ({
condition: JSON.stringify(e.condition),
msgs: JSON.stringify(e.msgs),
})),
duration_days: this._durationDays,
vars: JSON.stringify(this._vars),
assets_to_withdraw: this._assetsToWithdraw,
operational_amount: this._operationalAmount,
funding_account: this._fundingAccount,
};

return createJobMsg;
}
}

export class EstimateJobMsgComposer {
private _recurring: boolean | undefined;
private _vars: warp_resolver.Variable[] = [];
private _executions: Execution[] = [];
private _durationDays: string;

static new(): EstimateJobMsgComposer {
return new EstimateJobMsgComposer();
}

recurring(recurring: boolean): EstimateJobMsgComposer {
this._recurring = recurring;
return this;
}

durationDays(durationDays: string): EstimateJobMsgComposer {
this._durationDays = durationDays;
return this;
}

executions(executions: Execution[]): EstimateJobMsgComposer {
this._executions = executions;
return this;
}

vars(vars: warp_resolver.Variable[]): EstimateJobMsgComposer {
this._vars = vars;
return this;
}

compose(): EstimateJobMsg {
if (this._recurring === undefined || this._durationDays === undefined) {
throw new Error('All required fields must be provided');
}

const estimateJobMsg: EstimateJobMsg = {
recurring: this._recurring,
executions: this._executions.map((e) => ({
condition: JSON.stringify(e.condition),
msgs: JSON.stringify(e.msgs),
})),
duration_days: this._durationDays,
vars: JSON.stringify(this._vars),
};

return estimateJobMsg;
}
}

export class JobComposer {
create(): CreateJobMsgComposer {
return new CreateJobMsgComposer();
}

estimate(): EstimateJobMsgComposer {
return new EstimateJobMsgComposer();
}
}
Loading

0 comments on commit a92fbe2

Please sign in to comment.