Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add bindings for Karma proto #203

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion scripts/gen-proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const protoFiles = [
'address_mapper.proto',
'coin.proto',
'dpos.proto',
'dposv2.proto'
'dposv2.proto',
'karma.proto'
]

// copy the proto so end users can import it from node_modules in their own proto files
Expand Down
71 changes: 71 additions & 0 deletions src/contracts/karma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import BN from 'bn.js'
import { Client } from '../client'
import { Contract } from '../contract'
import { Address } from '../address'
import {
KarmaAddressSource, KarmaUserAmount, KarmaStateKeyUser, KarmaSources, KarmaSourceReward, KarmaState, KarmaSourceTarget, KarmaUserTarget, KarmaTotal
} from '../proto/karma_pb'
import { unmarshalBigUIntPB, marshalBigUIntPB } from '../big-uint'

export class Karma extends Contract {
static async createAsync(client: Client, callerAddr: Address): Promise<DPOS> {
const contractAddr = await client.getContractAddressAsync('dpos')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, copy/pasta

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Busted lol

if (!contractAddr) {
throw Error('Failed to resolve contract address')
}

return new Karma({ contractAddr, callerAddr, client })
}

constructor(params: { contractAddr: Address; callerAddr: Address; client: Client }) {
super(params)
}

async depositCoinAsync(amount: BN, from: Address): Promise<void> {
const karmaUserAmountReq = new KarmaUserAmount()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KarmaUserAmount gah. nvm. I'll fix it later.

karmaUserAmountReq.setAmount(marshalBigUIntPB(amount))
karmaUserAmountReq.setUser(from.MarshalPB())
return this.callAsync<void>('DepositCoin', karmaUserAmountReq)
}

async withdrawCoinAsync(amount: BN, to: Address): Promise<void> {
const karmaUserAmountReq = new KarmaUserAmount()
karmaUserAmountReq.setAmount(marshalBigUIntPB(amount))
karmaUserAmountReq.setUser(to.MarshalPB())
return this.callAsync<void>('WithdrawCoin', karmaUserAmountReq)
}

async getUserStateAsync(user: Address): Promise<KarmaState> {
let response: any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a protobuf instance?

const result = await this.staticCallAsync(
'GetUserState',
user.MarshalPB(),
response
)
return result
}

async getSourcesAsync(user: Address): Promise<KarmaSources> {
let response: any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a protobuf instance?

const result = await this.staticCallAsync(
'GetSources',
user.MarshalPB(),
response
)
return result
}

async getUserKarmaAsync(user: Address, target: KarmaSourceTarget): Promise<KarmaTotal> {
const karmaUserTargetReq = new KarmaUserTarget()
let response: any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a protobuf instance?

Copy link
Contributor Author

@gakonst gakonst Jan 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wanna add a new proto response and wrap it inside, as we do in other places, but the current karma implementation returns the data directly.

karmaUserTargetReq.setUser(user.MarshalPB())
karmaUserTargetReq.setTarget(target)
const result = await this.staticCallAsync(
'GetUserKarma',
karmaUserTargetReq,
response
)
return result
}

}
85 changes: 85 additions & 0 deletions src/proto/karma.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
syntax = "proto3";

import "proto/loom.proto";

package karma;

enum KarmaSourceTarget {
CALL = 0;
DEPLOY = 1;
}

message KarmaInitRequest {
Address Oracle = 1;
repeated KarmaSourceReward sources = 2;
repeated KarmaAddressSource users = 3;
KarmaUpkeepParams upkeep = 4;
}

message KarmaSources {
repeated KarmaSourceReward sources = 1;
}

message KarmaNewOracle {
Address new_oracle = 1;
}

message KarmaUserTarget {
Address user = 1;
KarmaSourceTarget target = 2;
}

message KarmaUserAmount {
Address user = 1;
BigUInt amount = 2;
}

message KarmaSourceReward {
string name = 1;
int64 reward = 2 [jstype = JS_STRING];
KarmaSourceTarget target = 3;
}

message KarmaSource {
string name = 1;
BigUInt count = 2;
}

message KarmaUpkeepParams {
int64 cost = 1 [jstype = JS_STRING];
int64 period = 3 [jstype = JS_STRING];
}


message KarmaAddressSource {
Address user = 1;
repeated KarmaSource sources = 2;
}

message KarmaState {
repeated KarmaSource source_states = 1;
BigUInt deploy_karma_total = 2;
BigUInt call_karma_total = 3;
int64 last_update_time = 4 [jstype = JS_STRING];
}

message KarmaStateUser {
repeated KarmaSource source_states = 1;
Address user = 2;
}

message KarmaStateKeyUser {
repeated string state_keys = 1;
Address user = 2;
}

message KarmaTotal{
BigUInt count = 1;
}

message ContractRecord{
Address owner = 1;
Address address = 2;
int64 creation_block = 3 [jstype = JS_STRING];
int64 nonce = 4 [jstype = JS_STRING];
}
Loading