Skip to content

Commit

Permalink
update api endpoint schemas to support tdk-unity
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian committed Feb 14, 2024
1 parent 2538cb3 commit 59cdf89
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 189 deletions.
30 changes: 20 additions & 10 deletions examples/harvester/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ export const App = () => {
amount,
)} MAGIC from connected wallet to smart account`,
);
await tdk?.contract.write(contractAddresses.MAGIC, {
await tdk?.transaction.create({
address: contractAddresses.MAGIC,
abi: erc20Abi,
functionName: "transferFrom",
args: [eoaAddress, smartAccountAddress, amount],
Expand All @@ -149,7 +150,8 @@ export const App = () => {
addLog(
"Transferring Ancient Permit from connected wallet to smart account",
);
await tdk?.contract.write(contractAddresses.Consumables, {
await tdk?.transaction.create({
address: contractAddresses.Consumables,
abi: erc1155Abi,
functionName: "safeTransferFrom",
args: [eoaAddress, smartAccountAddress, permitTokenId, 1n, zeroHash],
Expand All @@ -159,7 +161,8 @@ export const App = () => {

// Queue Consumables-NftHandler approval
addLog("Approving Harvester to transfer Consumables");
await tdk?.contract.write(contractAddresses.Consumables, {
await tdk?.transaction.create({
address: contractAddresses.Consumables,
abi: erc1155Abi,
functionName: "setApprovalForAll",
args: [nftHandlerAddress, true],
Expand All @@ -168,7 +171,8 @@ export const App = () => {

// Queue Ancient Permit deposit
addLog("Staking Ancient Permit to Harvester");
await tdk?.contract.write(nftHandlerAddress, {
await tdk?.transaction.create({
address: nftHandlerAddress,
abi: nftHandlerAbi,
functionName: "stakeNft",
args: [contractAddresses.Consumables, permitTokenId, 1n],
Expand All @@ -178,7 +182,8 @@ export const App = () => {

// Queue MAGIC-Harvester approval
addLog("Approving Harvester to transfer MAGIC");
await tdk?.contract.write(contractAddresses.MAGIC, {
await tdk?.transaction.create({
address: contractAddresses.MAGIC,
abi: erc20Abi,
functionName: "approve",
args: [harvesterAddress, amount],
Expand All @@ -187,7 +192,8 @@ export const App = () => {

// // Queue Harvester deposit
addLog(`Depositing ${formatEther(amount)} MAGIC to Harvester`);
await tdk?.contract.write(harvesterAddress, {
await tdk?.transaction.create({
address: harvesterAddress,
abi: harvesterAbi,
functionName: "deposit",
args: [amount, 0n],
Expand All @@ -200,7 +206,8 @@ export const App = () => {
const handleWithdraw = async () => {
if (harvesterDeposit > 0) {
addLog("Withdrawing all MAGIC from Harvester");
await tdk?.contract.write(harvesterAddress, {
await tdk?.transaction.create({
address: harvesterAddress,
abi: harvesterAbi,
functionName: "withdrawAll",
args: [],
Expand All @@ -210,7 +217,8 @@ export const App = () => {

if (harvesterPermits > 0) {
addLog("Withdrawing all Ancient Permits from Harvester");
await tdk?.contract.write(nftHandlerAddress, {
await tdk?.transaction.create({
address: nftHandlerAddress,
abi: nftHandlerAbi,
functionName: "unstakeNft",
args: [contractAddresses.Consumables, permitTokenId, harvesterPermits],
Expand All @@ -224,7 +232,8 @@ export const App = () => {
const handleTransfer = async () => {
if (smartAccountMagic > 0) {
addLog("Transferring all MAGIC from smart account to connected wallet");
await tdk?.contract.write(contractAddresses.MAGIC, {
await tdk?.transaction.create({
address: contractAddresses.MAGIC,
abi: erc20Abi,
functionName: "transfer",
args: [eoaAddress, smartAccountMagic],
Expand All @@ -236,7 +245,8 @@ export const App = () => {
addLog(
"Transferring all Ancient Permits from smart account to connected wallet",
);
await tdk?.contract.write(contractAddresses.Consumables, {
await tdk?.transaction.create({
address: contractAddresses.Consumables,
abi: erc1155Abi,
functionName: "safeTransferFrom",
args: [
Expand Down
2 changes: 0 additions & 2 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { withCors } from "./middleware/cors";
import { withErrorHandler } from "./middleware/error";
import { withProject } from "./middleware/project";
import { withSwagger } from "./middleware/swagger";
import { contractsRoutes } from "./routes/contracts";
import { harvestersRoutes } from "./routes/harvesters";
import { projectsRoutes } from "./routes/projects";
import { transactionsRoutes } from "./routes/transactions";
Expand Down Expand Up @@ -46,7 +45,6 @@ const main = async () => {
// Routes
await Promise.all([
app.register(projectsRoutes(ctx)),
app.register(contractsRoutes(ctx)),
app.register(transactionsRoutes(ctx)),
app.register(harvestersRoutes),
]);
Expand Down
76 changes: 0 additions & 76 deletions packages/api/src/routes/contracts.ts

This file was deleted.

164 changes: 86 additions & 78 deletions packages/api/src/routes/harvesters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@ const readHarvesterParamsSchema = Type.Object({
});

const readHarvesterReplySchema = Type.Object({
harvester: Type.Object({
nftHandlerAddress: Type.String(),
permitsAddress: Type.String(),
permitsTokenId: Type.String(),
}),
user: Type.Object({
magicBalance: Type.String(),
permitsBalance: Type.Number(),
harvesterMagicAllowance: Type.String(),
harvesterPermitsApproved: Type.Boolean(),
harvesterDepositCap: Type.String(),
harvesterDepositAmount: Type.String(),
}),
id: Type.String(),
nftHandlerAddress: Type.String(),
permitsAddress: Type.String(),
permitsTokenId: Type.String(),
permitsDepositCap: Type.String(),
userMagicBalance: Type.String(),
userPermitsBalance: Type.Number(),
userMagicAllowance: Type.String(),
userApprovedPermits: Type.Boolean(),
userDepositCap: Type.String(),
userDepositAmount: Type.String(),
});

export type ReadHarvesterParams = Static<typeof readHarvesterParamsSchema>;
Expand All @@ -59,38 +57,17 @@ export const harvestersRoutes: FastifyPluginAsync = async (app) => {
chainId,
params: { id },
} = req;
const user = await getUser(req);
if (!user) {
return reply.code(401).send({ error: "Unauthorized" });
}

const contractAddresses = getContractAddresses(chainId);
const smartAccountAddress = user.address as AddressString;
const harvesterAddress = id as AddressString;

const [
{ result: magicBalance = 0n },
{ result: magicAllowance = 0n },
{ result: nftHandlerAddress = zeroAddress },
{
result: [permitsAddress, permitsTokenId] = [zeroAddress, 0n] as const,
},
{ result: depositCap = 0n },
{ result: [harvesterDepositAmount] = [0n] as const },
] = await readContracts(config, {
contracts: [
{
address: contractAddresses.MAGIC,
abi: erc20Abi,
functionName: "balanceOf",
args: [smartAccountAddress],
},
{
address: contractAddresses.MAGIC,
abi: erc20Abi,
functionName: "allowance",
args: [smartAccountAddress, harvesterAddress],
},
{
address: harvesterAddress,
abi: harvesterAbi,
Expand All @@ -101,59 +78,90 @@ export const harvestersRoutes: FastifyPluginAsync = async (app) => {
abi: harvesterAbi,
functionName: "depositCapPerWallet",
},
{
address: harvesterAddress,
abi: harvesterAbi,
functionName: "getUserDepositCap",
args: [smartAccountAddress],
},
{
address: harvesterAddress,
abi: harvesterAbi,
functionName: "getUserGlobalDeposit",
args: [smartAccountAddress],
},
],
});

if (nftHandlerAddress === zeroAddress) {
return reply.code(404).send({ error: "Not found" });
}

const [
{ result: permitsBalance = 0n },
{ result: harvesterPermitsApproved = false },
] = await readContracts(config, {
contracts: [
{
address: permitsAddress,
abi: erc1155Abi,
functionName: "balanceOf",
args: [smartAccountAddress, permitsTokenId],
},
{
address: permitsAddress,
abi: erc1155Abi,
functionName: "isApprovedForAll",
args: [smartAccountAddress, nftHandlerAddress],
},
],
});
let magicBalance = 0n;
let magicAllowance = 0n;
let permitsBalance = 0n;
let userApprovedPermits = false;
let depositCap = 0n;
let depositAmount = 0n;
const user = await getUser(req);
if (user) {
const smartAccountAddress = user.address as AddressString;
const [
{ result: magicBalanceResult },
{ result: magicAllowanceResult },
{ result: permitsBalanceResult },
{ result: userApprovedPermitsResult },
{ result: depositCapResult },
{ result: globalDepositResult },
] = await readContracts(config, {
contracts: [
{
address: contractAddresses.MAGIC,
abi: erc20Abi,
functionName: "balanceOf",
args: [smartAccountAddress],
},
{
address: contractAddresses.MAGIC,
abi: erc20Abi,
functionName: "allowance",
args: [smartAccountAddress, harvesterAddress],
},
{
address: permitsAddress,
abi: erc1155Abi,
functionName: "balanceOf",
args: [smartAccountAddress, permitsTokenId],
},
{
address: permitsAddress,
abi: erc1155Abi,
functionName: "isApprovedForAll",
args: [smartAccountAddress, nftHandlerAddress],
},
{
address: harvesterAddress,
abi: harvesterAbi,
functionName: "getUserDepositCap",
args: [smartAccountAddress],
},
{
address: harvesterAddress,
abi: harvesterAbi,
functionName: "getUserGlobalDeposit",
args: [smartAccountAddress],
},
],
});

magicBalance = magicBalanceResult ?? 0n;
magicAllowance = magicAllowanceResult ?? 0n;
permitsBalance = permitsBalanceResult ?? 0n;
userApprovedPermits = userApprovedPermitsResult ?? false;
depositCap = depositCapResult ?? 0n;
depositAmount = globalDepositResult?.[0] ?? 0n;
}

reply.send({
harvester: {
nftHandlerAddress,
permitsAddress,
permitsTokenId: permitsTokenId.toString(),
},
user: {
magicBalance: magicBalance.toString(),
permitsBalance: Number(permitsBalance),
harvesterMagicAllowance: magicAllowance.toString(),
harvesterPermitsApproved,
harvesterDepositCap: depositCap.toString(),
harvesterDepositAmount: harvesterDepositAmount.toString(),
},
id,
nftHandlerAddress,
permitsAddress,
permitsTokenId: permitsTokenId.toString(),
permitsDepositCap: "2000",
userMagicBalance: magicBalance.toString(),
userPermitsBalance: Number(permitsBalance),
userMagicAllowance: magicAllowance.toString(),
userApprovedPermits,
userDepositCap: depositCap.toString(),
userDepositAmount: depositAmount.toString(),
});
},
);
Expand Down
Loading

0 comments on commit 59cdf89

Please sign in to comment.