-
Notifications
You must be signed in to change notification settings - Fork 47
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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') | ||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a protobuf instance? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
||
} |
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]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, copy/pasta
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Busted lol