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

feat(urql): refactored to urql and own codegen #1223

Merged
merged 3 commits into from
Dec 8, 2023
Merged
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
413 changes: 0 additions & 413 deletions graph/generated/schema.ts

Large diffs are not rendered by default.

40 changes: 0 additions & 40 deletions graph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,4 @@ type ClaimToken @entity {
claim: Claim!
owner: Bytes!
units: BigInt!
offers: [Offer!] @derivedFrom(field: "fractionID")
}

type Token @entity {
id: String!
name: String!
symbol: String
decimals: BigInt
}

type AcceptedToken @entity {
id: String!
token: Token!
minimumAmountPerUnit: BigInt!
accepted: Boolean!
}

enum OfferStatus {
Open
Fulfilled
Cancelled
}

type Offer @entity {
id: String!
fractionID: ClaimToken!
unitsAvailable: BigInt!
minUnitsPerTrade: BigInt!
maxUnitsPerTrade: BigInt!
status: OfferStatus!
acceptedTokens: [AcceptedToken!]!
}

type Trade @entity {
id: String!
buyer: Bytes!
offerID: Offer!
unitsSold: BigInt!
token: Token!
amountPerUnit: BigInt!
}
114 changes: 1 addition & 113 deletions graph/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { HypercertMinter } from "../generated/HypercertMinter/HypercertMinter";
import {
AcceptedToken,
Allowlist,
Claim,
ClaimToken,
Offer,
Token,
} from "../generated/schema";
import { Allowlist, Claim, ClaimToken } from "../generated/schema";
import { Address, BigInt, Bytes, log } from "@graphprotocol/graph-ts";

export const ZERO_ADDRESS = Address.fromString(
Expand Down Expand Up @@ -91,108 +84,3 @@ export function getOrCreateClaimToken(

return fraction;
}

export function getOrCreateToken(token: Address): Token {
const _tokenID = token.toHexString();
let _token = Token.load(_tokenID);

if (_token == null) {
_token = new Token(_tokenID);
log.debug("Created Token: {}", [_tokenID]);
_token.name = "Native";
_token.save();
}

log.debug("Returning Token: {}", [_tokenID]);

return _token;
}

export function getOrCreateAcceptedToken(
offerID: BigInt,
token: Address,
minimumAmountPerUnit: BigInt,
): AcceptedToken {
const _acceptedTokenID = offerID
.toHexString()
.concat("-".concat(token.toHexString()));
let acceptedToken = AcceptedToken.load(_acceptedTokenID);

if (acceptedToken == null) {
acceptedToken = new AcceptedToken(_acceptedTokenID);
log.debug("Created acceptedToken: {}", [_acceptedTokenID]);

acceptedToken.token = getOrCreateToken(token).id;
acceptedToken.minimumAmountPerUnit = minimumAmountPerUnit;
acceptedToken.accepted = true;
acceptedToken.save();
}

log.debug("Returning acceptedToken: {}", [_acceptedTokenID]);
return acceptedToken;
}

export function getOrCreateOffer(
hypercertContract: Address,
traderContract: Address,
fractionID: BigInt,
offerID: BigInt,
): Offer {
const _traderContract = HypercertTrader.bind(traderContract);

const _fractionID = getID(fractionID, hypercertContract);
const _offerID = fractionID
.toHexString()
.concat("-".concat(offerID.toString()));
let offer = Offer.load(_offerID);

if (offer == null) {
const offerOnChain = _traderContract.getOffer(offerID);
offer = new Offer(_offerID);
log.debug("Created offer: {}", [_offerID]);

offer.fractionID = _fractionID;
offer.unitsAvailable = offerOnChain.unitsAvailable;
offer.minUnitsPerTrade = offerOnChain.minUnitsPerTrade;
offer.maxUnitsPerTrade = offerOnChain.maxUnitsPerTrade;
offer.acceptedTokens = [];
offer.status = "Open";

for (let i = 0; i < offerOnChain.acceptedTokens.length; i++) {
const _acceptedToken = offerOnChain.acceptedTokens[i];
const parsedToken = getOrCreateAcceptedToken(
offerID,
_acceptedToken.token,
_acceptedToken.minimumAmountPerUnit,
);
offer.acceptedTokens.push(parsedToken.id.toString());
log.debug("Added accepted token to offer {} at place {}", [
_offerID,
_acceptedToken.length.toString(),
]);
}

log.debug("Created offerID: {}", [_offerID]);
offer.save();
}

log.debug("Returning offer: {}", [_offerID]);

return offer;
}

export function getOrCreateOfferByID(
hypercertContract: Address,
fractionID: BigInt,
offerID: BigInt,
): Offer | null {
const _fractionID = getID(fractionID, hypercertContract);
const _offerID = _fractionID.concat("-".concat(offerID.toString()));
const offer = Offer.load(_offerID);

if (offer == null) {
log.error("Offer with ID {} does not exist", [_offerID]);
}

return offer;
}
Loading
Loading