-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: clone ccd006 functions and update contract principal
- Loading branch information
Showing
9 changed files
with
341 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
functions/api/ccd006-citycoin-mining-v2/get-block-winner.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { fetchReadOnlyFunction } from 'micro-stacks/api'; | ||
import { uintCV } from 'micro-stacks/clarity'; | ||
import { createResponse, DEPLOYER, NETWORK } from '../../../lib/api-helpers'; | ||
|
||
// TODO: upgrade types and check if EventContext is found | ||
export async function onRequest(context: any): Promise<Response> { | ||
// check query parameters | ||
const requestUrl = new URL(context.request.url); | ||
const cityId = requestUrl.searchParams.get('cityId'); | ||
if (!cityId) return createResponse('Missing cityId parameter', 400); | ||
const height = requestUrl.searchParams.get('height'); | ||
if (!height) return createResponse('Missing height parameter', 400); | ||
|
||
// get result from contract | ||
const blockWinner = await getBlockWinner(cityId, height); | ||
|
||
// return result | ||
if (blockWinner === null) return createResponse(`Block winner not found: ${cityId} ${height}`, 404); | ||
return createResponse(blockWinner); | ||
} | ||
|
||
// returns the block winner for a given city ID and block height | ||
async function getBlockWinner(cityId: string, height: string) { | ||
try { | ||
const result = await fetchReadOnlyFunction( | ||
{ | ||
contractAddress: DEPLOYER('mainnet'), | ||
contractName: 'ccd006-citycoin-mining-v2', | ||
functionName: 'get-block-winner', | ||
functionArgs: [uintCV(Number(cityId)), uintCV(Number(height))], | ||
network: NETWORK('mainnet'), | ||
}, | ||
true | ||
); | ||
return typeof result === 'boolean' ? Boolean(result) : null; | ||
} catch (err) { | ||
return null; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
functions/api/ccd006-citycoin-mining-v2/get-coinbase-amount.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { fetchReadOnlyFunction } from 'micro-stacks/api'; | ||
import { uintCV } from 'micro-stacks/clarity'; | ||
import { createResponse, DEPLOYER, NETWORK } from '../../../lib/api-helpers'; | ||
|
||
// TODO: upgrade types and check if EventContext is found | ||
export async function onRequest(context: any): Promise<Response> { | ||
// check query parameters | ||
const requestUrl = new URL(context.request.url); | ||
const cityId = requestUrl.searchParams.get('cityId'); | ||
if (!cityId) return createResponse('Missing cityId parameter', 400); | ||
const height = requestUrl.searchParams.get('height'); | ||
if (!height) return createResponse('Missing height parameter', 400); | ||
|
||
// get result from contract | ||
const coinbaseAmount = await getCoinbaseAmount(cityId, height); | ||
|
||
// return result | ||
if (!coinbaseAmount) return createResponse(`Coinbase amount not found: ${cityId} ${height}`, 404); | ||
return createResponse(coinbaseAmount); | ||
} | ||
|
||
// returns the coinbase amount for a given city ID and block height | ||
async function getCoinbaseAmount(cityId: string, height: string) { | ||
try { | ||
const result = await fetchReadOnlyFunction( | ||
{ | ||
contractAddress: DEPLOYER('mainnet'), | ||
contractName: 'ccd006-citycoin-mining-v2', | ||
functionName: 'get-coinbase-amount', | ||
functionArgs: [uintCV(Number(cityId)), uintCV(Number(height))], | ||
network: NETWORK('mainnet'), | ||
}, | ||
true | ||
); | ||
return Number(result); | ||
} catch (err) { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { fetchReadOnlyFunction } from 'micro-stacks/api'; | ||
import { uintCV } from 'micro-stacks/clarity'; | ||
import { createResponse, DEPLOYER, NETWORK } from '../../../lib/api-helpers'; | ||
|
||
// TODO: upgrade types and check if EventContext is found | ||
export async function onRequest(context: any): Promise<Response> { | ||
// check query parameters | ||
const requestUrl = new URL(context.request.url); | ||
const cityId = requestUrl.searchParams.get('cityId'); | ||
if (!cityId) return createResponse('Missing cityId parameter', 400); | ||
const height = requestUrl.searchParams.get('height'); | ||
if (!height) return createResponse('Missing height parameter', 400); | ||
|
||
// get result from contract | ||
const highValue = await getHighValue(cityId, height); | ||
|
||
// return result | ||
if (!highValue) return createResponse(`High value not found: ${cityId} ${height}`, 404); | ||
return createResponse(highValue); | ||
} | ||
|
||
// returns the high value for a given city ID and block height | ||
async function getHighValue(cityId: string, height: string) { | ||
try { | ||
const result = await fetchReadOnlyFunction( | ||
{ | ||
contractAddress: DEPLOYER('mainnet'), | ||
contractName: 'ccd006-citycoin-mining-v2', | ||
functionName: 'get-high-value', | ||
functionArgs: [uintCV(Number(cityId)), uintCV(Number(height))], | ||
network: NETWORK('mainnet'), | ||
}, | ||
true | ||
); | ||
return Number(result); | ||
} catch (err) { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { fetchReadOnlyFunction } from 'micro-stacks/api'; | ||
import { uintCV } from 'micro-stacks/clarity'; | ||
import { createResponse, DEPLOYER, Miner, NETWORK } from '../../../lib/api-helpers'; | ||
|
||
// TODO: upgrade types and check if EventContext is found | ||
export async function onRequest(context: any): Promise<Response> { | ||
// check query parameters | ||
const requestUrl = new URL(context.request.url); | ||
const cityId = requestUrl.searchParams.get('cityId'); | ||
if (!cityId) return createResponse('Missing cityId parameter', 400); | ||
const height = requestUrl.searchParams.get('height'); | ||
if (!height) return createResponse('Missing height parameter', 400); | ||
const userId = requestUrl.searchParams.get('userId'); | ||
if (!userId) return createResponse('Missing userId parameter', 400); | ||
|
||
// get result from contract | ||
const miner = await getMiner(cityId, height, userId); | ||
|
||
// return result | ||
if (!miner) return createResponse(`Miner not found: ${cityId} ${height} ${userId}`, 404); | ||
return createResponse(miner); | ||
} | ||
|
||
// returns the miner for a given city ID, block height, and user ID | ||
async function getMiner(cityId: string, height: string, userId: string) { | ||
try { | ||
const result = await fetchReadOnlyFunction( | ||
{ | ||
contractAddress: DEPLOYER('mainnet'), | ||
contractName: 'ccd006-citycoin-mining-v2', | ||
functionName: 'get-miner', | ||
functionArgs: [uintCV(Number(cityId)), uintCV(Number(height)), uintCV(Number(userId))], | ||
network: NETWORK('mainnet'), | ||
}, | ||
true | ||
); | ||
return result as Miner; | ||
} catch (err) { | ||
return null; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
functions/api/ccd006-citycoin-mining-v2/get-mining-stats.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { fetchReadOnlyFunction } from 'micro-stacks/api'; | ||
import { uintCV } from 'micro-stacks/clarity'; | ||
import { createResponse, DEPLOYER, MiningStats, NETWORK } from '../../../lib/api-helpers'; | ||
|
||
// TODO: upgrade types and check if EventContext is found | ||
export async function onRequest(context: any): Promise<Response> { | ||
// check query parameters | ||
const requestUrl = new URL(context.request.url); | ||
const cityId = requestUrl.searchParams.get('cityId'); | ||
if (!cityId) return createResponse('Missing cityId parameter', 400); | ||
const height = requestUrl.searchParams.get('height'); | ||
if (!height) return createResponse('Missing height parameter', 400); | ||
|
||
// get result from contract | ||
const miningStats = await getMiningStats(cityId, height); | ||
|
||
// return result | ||
if (!miningStats) return createResponse(`Mining stats not found: ${cityId} ${height}`, 404); | ||
return createResponse(miningStats); | ||
} | ||
|
||
// returns the mining stats for a given city ID and block height | ||
async function getMiningStats(cityId: string, height: string) { | ||
try { | ||
const result = await fetchReadOnlyFunction( | ||
{ | ||
contractAddress: DEPLOYER('mainnet'), | ||
contractName: 'ccd006-citycoin-mining-v2', | ||
functionName: 'get-mining-stats', | ||
functionArgs: [uintCV(Number(cityId)), uintCV(Number(height))], | ||
network: NETWORK('mainnet'), | ||
}, | ||
true | ||
); | ||
return result as MiningStats; | ||
} catch (err) { | ||
return null; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
functions/api/ccd006-citycoin-mining-v2/get-reward-delay.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { fetchReadOnlyFunction } from 'micro-stacks/api'; | ||
import { createResponse, DEPLOYER, NETWORK } from '../../../lib/api-helpers'; | ||
|
||
// TODO: upgrade types and check if EventContext is found | ||
export async function onRequest(context: any): Promise<Response> { | ||
// get result from contract | ||
const rewardDelay = await getRewardDelay(); | ||
|
||
// return result | ||
if (!rewardDelay) return createResponse(`Reward delay not found`, 404); | ||
return createResponse(rewardDelay); | ||
} | ||
|
||
// returns the reward delay | ||
async function getRewardDelay() { | ||
try { | ||
const result = await fetchReadOnlyFunction( | ||
{ | ||
contractAddress: DEPLOYER('mainnet'), | ||
contractName: 'ccd006-citycoin-mining-v2', | ||
functionName: 'get-reward-delay', | ||
functionArgs: [], | ||
network: NETWORK('mainnet'), | ||
}, | ||
true | ||
); | ||
return Number(result); | ||
} catch (err) { | ||
return null; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
functions/api/ccd006-citycoin-mining-v2/has-mined-at-block.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { fetchReadOnlyFunction } from 'micro-stacks/api'; | ||
import { uintCV } from 'micro-stacks/clarity'; | ||
import { createResponse, DEPLOYER, NETWORK } from '../../../lib/api-helpers'; | ||
|
||
// TODO: upgrade types and check if EventContext is found | ||
export async function onRequest(context: any): Promise<Response> { | ||
// check query parameters | ||
const requestUrl = new URL(context.request.url); | ||
const cityId = requestUrl.searchParams.get('cityId'); | ||
if (!cityId) return createResponse('Missing cityId parameter', 400); | ||
const height = requestUrl.searchParams.get('height'); | ||
if (!height) return createResponse('Missing height parameter', 400); | ||
const userId = requestUrl.searchParams.get('userId'); | ||
if (!userId) return createResponse('Missing userId parameter', 400); | ||
|
||
// get result from contract | ||
const hasMined = await hasMinedAtBlock(cityId, height, userId); | ||
|
||
// return result | ||
if (hasMined === null) return createResponse(`Miner not found: ${cityId} ${height} ${userId}`, 404); | ||
return createResponse(hasMined); | ||
} | ||
|
||
// returns true if the given user mined at the given block height | ||
async function hasMinedAtBlock(cityId: string, height: string, userId: string) { | ||
try { | ||
const result = await fetchReadOnlyFunction( | ||
{ | ||
contractAddress: DEPLOYER('mainnet'), | ||
contractName: 'ccd006-citycoin-mining-v2', | ||
functionName: 'has-mined-at-block', | ||
functionArgs: [uintCV(Number(cityId)), uintCV(Number(height)), uintCV(Number(userId))], | ||
network: NETWORK('mainnet'), | ||
}, | ||
true | ||
); | ||
return typeof result === 'boolean' ? Boolean(result) : null; | ||
} catch (err) { | ||
return null; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
functions/api/ccd006-citycoin-mining-v2/is-block-winner.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { fetchReadOnlyFunction } from 'micro-stacks/api'; | ||
import { principalCV, uintCV } from 'micro-stacks/clarity'; | ||
import { BlockWinner, createResponse, DEPLOYER, NETWORK } from '../../../lib/api-helpers'; | ||
|
||
// TODO: upgrade types and check if EventContext is found | ||
export async function onRequest(context: any): Promise<Response> { | ||
// check query parameters | ||
const requestUrl = new URL(context.request.url); | ||
const cityId = requestUrl.searchParams.get('cityId'); | ||
if (!cityId) return createResponse('Missing cityId parameter', 400); | ||
const user = requestUrl.searchParams.get('user'); | ||
if (!user) return createResponse('Missing user parameter', 400); | ||
const claimHeight = requestUrl.searchParams.get('claimHeight'); | ||
if (!claimHeight) return createResponse('Missing claimHeight parameter', 400); | ||
|
||
// get result from contract | ||
const winner = await isBlockWinner(cityId, user, claimHeight); | ||
|
||
// return result | ||
if (!winner) return createResponse(`Block winner not found: ${cityId} ${user} ${claimHeight}`, 404); | ||
return createResponse(winner); | ||
} | ||
|
||
// returns true if the given user mined at the given block height | ||
async function isBlockWinner(cityId: string, user: string, claimHeight: string) { | ||
try { | ||
const result = await fetchReadOnlyFunction( | ||
{ | ||
contractAddress: DEPLOYER('mainnet'), | ||
contractName: 'ccd006-citycoin-mining-v2', | ||
functionName: 'is-block-winner', | ||
functionArgs: [uintCV(Number(cityId)), principalCV(user), uintCV(Number(claimHeight))], | ||
network: NETWORK('mainnet'), | ||
}, | ||
true | ||
); | ||
return result as BlockWinner; | ||
} catch (err) { | ||
return null; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
functions/api/ccd006-citycoin-mining-v2/is-mining-enabled.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { fetchReadOnlyFunction } from 'micro-stacks/api'; | ||
import { createResponse, DEPLOYER, NETWORK } from '../../../lib/api-helpers'; | ||
|
||
// TODO: upgrade types and check if EventContext is found | ||
export async function onRequest(context: any): Promise<Response> { | ||
// get result from contract | ||
const enabled = await isMiningEnabled(); | ||
|
||
// return result | ||
if (enabled === null) return createResponse(`Mining enabled status not found`, 404); | ||
return createResponse(enabled); | ||
} | ||
|
||
// returns true if mining is enabled | ||
async function isMiningEnabled() { | ||
try { | ||
const result = await fetchReadOnlyFunction( | ||
{ | ||
contractAddress: DEPLOYER('mainnet'), | ||
contractName: 'ccd006-citycoin-mining-v2', | ||
functionName: 'is-mining-enabled', | ||
functionArgs: [], | ||
network: NETWORK('mainnet'), | ||
}, | ||
true | ||
); | ||
return typeof result === 'boolean' ? Boolean(result) : null; | ||
} catch (err) { | ||
return null; | ||
} | ||
} |