Skip to content

Commit

Permalink
- update @graphprotocol/graph-cli and @graphprotocol/graph-ts (#186)
Browse files Browse the repository at this point in the history
- add new deploy scripts
- fix type errors on subgraph mapping
  • Loading branch information
Diogomartf authored Jul 5, 2024
1 parent 3b1186d commit 8871cca
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 38 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion packages/app/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ NEXT_PUBLIC_FATHOM_SITE_ID=

RPC_GNOSIS=
RPC_MAINNET=
RPC_ARBITRUM=
RPC_ARBITRUM=

NEXT_STACKLY_SUBGRAPH_API_KEY=
9 changes: 5 additions & 4 deletions packages/sdk/src/vaults/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ export const COW_SETTLEMENT_ADDRESS_LIST: Record<ChainId, string> = {
[ChainId.ARBITRUM]: COW_SETTLEMENT_ADDRESS,
};

const STACKLY_SUBGRAPH_KEY = "5fdd9e74c326b644f8088068769d72af";
const STACKLY_SUBGRAPH_API_KEY =
process.env.STACKLY_SUBGRAPH_API_KEY ?? "e7b7ff845e506590498946cd6bf83bf6";

export const SUBGRAPH_ENDPOINT_LIST: Readonly<Record<string, string>> = {
[ChainId.ETHEREUM]: `https://gateway-arbitrum.network.thegraph.com/api/${STACKLY_SUBGRAPH_KEY}/subgraphs/id/5AMWcp9zv791teVUZT7Nm1jYeaLYmF4VYYnhh3JLZDGc`,
[ChainId.GNOSIS]: `https://gateway-arbitrum.network.thegraph.com/api/${STACKLY_SUBGRAPH_KEY}/subgraphs/id/29A9NjwmhSgF8UKRvEnRbXSyqFmYnrspyPF69mFAMVGX`,
[ChainId.ARBITRUM]: `https://api.studio.thegraph.com/query/63508/stackly-arbitrum-one/version/latest`, // todo: change to correct address
[ChainId.ETHEREUM]: `https://gateway-arbitrum.network.thegraph.com/api/${STACKLY_SUBGRAPH_API_KEY}/subgraphs/id/35bL4ohk2tnXqDnrp7NSyAKW8bbUmGDapyfe2ddCxV8H`,
[ChainId.GNOSIS]: `https://gateway-arbitrum.network.thegraph.com/api/${STACKLY_SUBGRAPH_API_KEY}/subgraphs/id/72Lysd4A2kZFqMqJtPQk3zMEEBExFfXeZbkJGTx8phRL`,
[ChainId.ARBITRUM]: `https://gateway-arbitrum.network.thegraph.com/api/${STACKLY_SUBGRAPH_API_KEY}/subgraphs/id/FNmemHB6tUh7eHmJnBFKYFf27U5GUAzXnatry4ZbrF7f`,
};

/**
Expand Down
10 changes: 5 additions & 5 deletions packages/subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"prepare:gnosis": "ts-node bin/build-subgraph.ts gnosis && bun run codegen",
"prepare:mainnet": "ts-node bin/build-subgraph.ts mainnet && bun run codegen",
"prepare:arbitrum-one": "ts-node bin/build-subgraph.ts arbitrum-one && bun run codegen",
"deploy:mainnet": "graph deploy --studio stackly-ethereum",
"deploy:gnosis": "graph deploy --studio stackly",
"deploy:arbitrum-one": "graph deploy --studio stackly-arbitrum-one",
"deploy:mainnet": "bun run prepare:mainnet && graph deploy --studio stackly-mainnet",
"deploy:gnosis": "bun run prepare:gnosis && graph deploy --studio stackly-gnosis",
"deploy:arbitrum-one": "bun run prepare:arbitrum-one && graph deploy --studio stackly-arbitrum-one",
"create-local": "graph create --node http://localhost:8020/ swaprhq/stackly",
"remove-local": "graph remove --node http://localhost:8020/ swaprhq/stackly",
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 swaprhq/stackly",
Expand All @@ -23,8 +23,8 @@
"@stackly/sdk": "workspace:^"
},
"devDependencies": {
"@graphprotocol/graph-cli": "0.51.2",
"@graphprotocol/graph-ts": "0.31.0",
"@graphprotocol/graph-cli": "0.78.0",
"@graphprotocol/graph-ts": "0.35.1",
"@types/node": "^18.11.18",
"ts-node": "^10.8.2",
"yaml": "^2.2.1"
Expand Down
99 changes: 71 additions & 28 deletions packages/subgraph/src/mappings/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,106 @@ import { OrderFactory } from "../../generated/OrderFactory/OrderFactory";
const HUNDRED_PERCENT = BigInt.fromI32(10000);

export function createOrReturnTokenEntity(contractAddress: Address): Token {
// Persist token data if it doesn't already exist
let token = Token.load(contractAddress.toHex());
let tokenId = contractAddress.toHex();
let token = Token.load(tokenId);

// Return existing token if it's already in the store
if (token !== null) {
return token;
}

// Create a new token entity
token = new Token(tokenId);
let tokenContract = ERC20Contract.bind(contractAddress);
token = new Token(contractAddress.toHex());

// Set the address field
token.address = contractAddress;

token.name = "";
// Name
let tryName = tokenContract.try_name();
if (!tryName.reverted) {
token.name = tryName.value;
}
token.name = tryName.reverted ? "Unknown Token" : tryName.value;

token.symbol = "";
// Symbol
let trySymbol = tokenContract.try_symbol();
if (!trySymbol.reverted) {
token.symbol = trySymbol.value;
}
token.symbol = trySymbol.reverted ? "UNKNOWN" : trySymbol.value;

// Decimals
let tryDecimals = tokenContract.try_decimals();
token.decimals = tryDecimals.reverted ? 18 : tryDecimals.value;

token.decimals = tokenContract.decimals();
token.save();
return token;
}

export function handleDCAOrderInitialized(event: Initialized): void {
const orderContract = DCAOrderContract.bind(event.params.order);
const order = new DCAOrder(event.params.order.toHex());
const orderAddress = event.params.order;
const orderContract = DCAOrderContract.bind(orderAddress);
const order = new DCAOrder(orderAddress.toHex());

order.createdAt = event.block.timestamp;
order.owner = orderContract.owner();
order.sellToken = createOrReturnTokenEntity(orderContract.sellToken()).id;
order.buyToken = createOrReturnTokenEntity(orderContract.buyToken()).id;
order.receiver = orderContract.receiver();

let tryOwner = orderContract.try_owner();
order.owner = tryOwner.reverted
? Address.fromString("0x0000000000000000000000000000000000000000")
: tryOwner.value;

let trySellToken = orderContract.try_sellToken();
order.sellToken = trySellToken.reverted
? ""
: createOrReturnTokenEntity(trySellToken.value).id;

let tryBuyToken = orderContract.try_buyToken();
order.buyToken = tryBuyToken.reverted
? ""
: createOrReturnTokenEntity(tryBuyToken.value).id;

let tryReceiver = orderContract.try_receiver();
order.receiver = tryReceiver.reverted
? Address.fromString("0x0000000000000000000000000000000000000000")
: tryReceiver.value;

let orderSlots: Array<BigInt> = [BigInt.fromI32(0)];
let protocolFee: BigInt = BigInt.fromI32(0);
let orderFactoryAddress: Address = (
event.transaction.to !== null
? event.transaction.to
: Address.fromString("0x0")
)!;

let orderFactoryAddress: Address;
if (event.transaction.to !== null) {
orderFactoryAddress = event.transaction.to as Address;
} else {
orderFactoryAddress = Address.fromString(
"0x0000000000000000000000000000000000000000"
);
}

const factory = OrderFactory.bind(orderFactoryAddress);
let tryProtocolFee = factory.try_protocolFee();
if (!tryProtocolFee.reverted) {
protocolFee = BigInt.fromI32(tryProtocolFee.value);
}

order.amount = orderContract.amount();
let tryOrderSlots = orderContract.try_orderSlots();
if (!tryOrderSlots.reverted) {
orderSlots = tryOrderSlots.value;
}

let tryAmount = orderContract.try_amount();
order.amount = tryAmount.reverted ? BigInt.fromI32(0) : tryAmount.value;

order.fee = protocolFee;
order.feeAmount = order.amount
.times(protocolFee)
.div(HUNDRED_PERCENT.minus(protocolFee));
order.endTime = orderContract.endTime().toI32();
order.startTime = orderContract.startTime().toI32();
order.orderSlots = orderContract.orderSlots();
order.interval = orderContract.interval();

let tryEndTime = orderContract.try_endTime();
order.endTime = tryEndTime.reverted ? 0 : tryEndTime.value.toI32();

let tryStartTime = orderContract.try_startTime();
order.startTime = tryStartTime.reverted ? 0 : tryStartTime.value.toI32();

order.orderSlots = orderSlots;

let tryInterval = orderContract.try_interval();
order.interval = tryInterval.reverted ? BigInt.fromI32(0) : tryInterval.value;

order.save();
}

Expand Down

0 comments on commit 8871cca

Please sign in to comment.