add Pledge entity

add handling for pledge entity
This commit is contained in:
Barry Gitarts 2019-08-15 11:28:26 -04:00
parent a6e68fc39e
commit 08ee0999c6
5 changed files with 209 additions and 30 deletions

View File

@ -22,4 +22,15 @@ type PledgesInfo @entity {
token: String!
lifetimeReceived: BigInt!
balance: BigInt!
}
type Pledge @entity {
id: ID!
owner: String!
amount: BigInt!
token: String!
commitTime: BigInt!
nDelegates: BigInt!
intendedProject: BigInt!
pledgeState: Int!
}

View File

@ -230,3 +230,97 @@ export class PledgesInfo extends Entity {
this.set("balance", Value.fromBigInt(value));
}
}
export class Pledge extends Entity {
constructor(id: string) {
super();
this.set("id", Value.fromString(id));
}
save(): void {
let id = this.get("id");
assert(id !== null, "Cannot save Pledge entity without an ID");
assert(
id.kind == ValueKind.STRING,
"Cannot save Pledge entity with non-string ID. " +
'Considering using .toHex() to convert the "id" to a string.'
);
store.set("Pledge", id.toString(), this);
}
static load(id: string): Pledge | null {
return store.get("Pledge", id) as Pledge | null;
}
get id(): string {
let value = this.get("id");
return value.toString();
}
set id(value: string) {
this.set("id", Value.fromString(value));
}
get owner(): string {
let value = this.get("owner");
return value.toString();
}
set owner(value: string) {
this.set("owner", Value.fromString(value));
}
get amount(): BigInt {
let value = this.get("amount");
return value.toBigInt();
}
set amount(value: BigInt) {
this.set("amount", Value.fromBigInt(value));
}
get token(): string {
let value = this.get("token");
return value.toString();
}
set token(value: string) {
this.set("token", Value.fromString(value));
}
get commitTime(): BigInt {
let value = this.get("commitTime");
return value.toBigInt();
}
set commitTime(value: BigInt) {
this.set("commitTime", Value.fromBigInt(value));
}
get nDelegates(): BigInt {
let value = this.get("nDelegates");
return value.toBigInt();
}
set nDelegates(value: BigInt) {
this.set("nDelegates", Value.fromBigInt(value));
}
get intendedProject(): BigInt {
let value = this.get("intendedProject");
return value.toBigInt();
}
set intendedProject(value: BigInt) {
this.set("intendedProject", Value.fromBigInt(value));
}
get pledgeState(): i32 {
let value = this.get("pledgeState");
return value.toI32();
}
set pledgeState(value: i32) {
this.set("pledgeState", Value.fromI32(value));
}
}

View File

@ -22,4 +22,15 @@ type PledgesInfo @entity {
token: String!
lifetimeReceived: BigInt!
balance: BigInt!
}
type Pledge @entity {
id: ID!
owner: String!
amount: BigInt!
token: String!
commitTime: BigInt!
nDelegates: BigInt!
intendedProject: BigInt!
pledgeState: Int!
}

View File

@ -1,4 +1,4 @@
import { BigInt } from "@graphprotocol/graph-ts"
import { Address, BigInt, log } from "@graphprotocol/graph-ts"
import {
Contract,
Transfer,
@ -10,9 +10,10 @@ import {
ProjectAdded,
ProjectUpdated,
AddProjectCall,
DonateCall
DonateCall,
AddGiverAndDonateCall,
} from "../generated/Contract/Contract"
import { ExampleEntity, Profile, PledgesInfo } from "../generated/schema"
import { ExampleEntity, Profile, PledgesInfo, Pledge } from "../generated/schema"
export function handleAddProject(call: AddProjectCall): void {
@ -29,41 +30,102 @@ export function handleAddProject(call: AddProjectCall): void {
}
export function handleDonate(call: DonateCall): void {
let giver = call.inputs.idGiver
let receiver = call.inputs.idReceiver
let token = call.inputs.token
let amount = call.inputs.amount
// let giver = call.inputs.idGiver
// let receiver = call.inputs.idReceiver
// let token = call.inputs.token
// let amount = call.inputs.amount
let giverId = giver.toString() + token.toString()
let receiverId = receiver.toString() + token.toString()
// let giverId = giver.toString() + token.toString()
// let receiverId = receiver.toString() + token.toString()
let giverPledges = PledgesInfo.load(giverId)
let receiverPledges = PledgesInfo.load(receiverId)
// let giverPledges = PledgesInfo.load(giverId)
// let receiverPledges = PledgesInfo.load(receiverId)
if (receiverPledges == null) {
receiverPledges = new PledgesInfo(receiverId)
receiverPledges.token = token.toString()
receiverPledges.profile = giver.toHex()
}
if (giver.notEqual(new BigInt(0))) {
giverPledges.balance.minus(amount)
giverPledges.save()
}
receiverPledges.lifetimeReceived.plus(amount)
receiverPledges.balance.plus(amount)
receiverPledges.save()
// if (receiverPledges == null) {
// receiverPledges = new PledgesInfo(receiverId)
// receiverPledges.token = token.toString()
// receiverPledges.profile = receiver.toHex()
// }
// if (giver.notEqual(new BigInt(0))) {
// giverPledges.balance.minus(amount)
// giverPledges.save()
// }
// receiverPledges.lifetimeReceived.plus(amount)
// receiverPledges.balance.plus(amount)
// receiverPledges.save()
log.info(
'id receiver: {}, amount: {}, token: {}',
[
call.inputs.idReceiver.toString(),
call.inputs.amount.toString(),
call.inputs.token.toString()
]
)
}
export function handleProjectAdded(event: ProjectAdded): void {
// let profileId = event.params.idProject.toHex()
// let profile = new Profile(profileId)
// profile.url = event.params.url
// profile.save()
export function handleAddGiverAndDonate(call: AddGiverAndDonateCall): void {
// let receiver = call.inputs.idReceiver
// let token = call.inputs.token
// let amount = call.inputs.amount
// let receiverId = receiver.toString() + token.toString()
// let receiverPledges = PledgesInfo.load(receiverId)
// if (receiverPledges == null) {
// receiverPledges = new PledgesInfo(receiverId)
// receiverPledges.token = token.toString()
// receiverPledges.profile = receiver.toHex()
// }
// receiverPledges.lifetimeReceived.plus(amount)
// receiverPledges.balance.plus(amount)
// receiverPledges.save()
log.info(
'id receiver: {}, amount: {}, token: {}',
[
call.inputs.idReceiver.toString(),
call.inputs.amount.toString(),
call.inputs.token.toString()
]
)
}
export function handleTransfer(event: Transfer): void {
// Entities can be loaded from the store using a string ID; this ID
// needs to be unique across all entities of the same type
// needs to be unique across all entities of the same type
let contract = Contract.bind(event.address)
let pledge = contract.getPledge(event.params.to)
let token = pledge.value6
let owner = pledge.value1
let amount = pledge.value0
let commitTime = pledge.value4
let intendedProject = pledge.value3
let pledgeState = pledge.value7
let ndelegates = pledge.value2
log.info(
'amount: {}, owner: {}, nDelegates: {}, intendedProject: {}, commitTime: {}, oldPledge: {}, pledge token: {}, pledge state: {}',
[
pledge.value0.toString(), // amount
pledge.value1.toString(), // owner
pledge.value2.toString(), // nDelegates
pledge.value3.toString(), // intendedProject
pledge.value4.toString(), // commitTime
pledge.value5.toString(), // old pledge
pledge.value6.toHexString(), // Token
pledge.value7.toString() // pledgeState
]
)
let pledgeEntity = Pledge.load(event.params.to.toHex())
if (pledgeEntity == null) pledgeEntity = new Pledge(event.params.to.toHex())
pledgeEntity.owner = owner.toString()
pledgeEntity.token = token.toHexString()
pledgeEntity.amount = amount
pledgeEntity.commitTime = commitTime
pledgeEntity.intendedProject = intendedProject
pledgeEntity.pledgeState = pledgeState
pledgeEntity.nDelegates = ndelegates
pledgeEntity.save()
let entity = ExampleEntity.load(event.transaction.from.toHex())
// Entities only exist after they have been saved to the store;
@ -139,5 +201,6 @@ export function handleDelegateAdded(event: DelegateAdded): void {}
export function handleDelegateUpdated(event: DelegateUpdated): void {}
export function handleProjectUpdated(event: ProjectUpdated): void {}
export function handleProjectAdded(event: ProjectUpdated): void {}