-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into rotorsoft/auth-middleware-updated
- Loading branch information
Showing
23 changed files
with
858 additions
and
14 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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,62 @@ | ||
import { LaunchpadTrade } from '@hicommonwealth/schemas'; | ||
import Sequelize from 'sequelize'; | ||
import { z } from 'zod'; | ||
import type { ModelInstance } from './types'; | ||
|
||
export type LaunchpadTradeAttributes = z.infer<typeof LaunchpadTrade>; | ||
|
||
export type LaunchpadTradeInstance = ModelInstance<LaunchpadTradeAttributes>; | ||
|
||
export type LaunchpadTradeModelStatic = | ||
Sequelize.ModelStatic<LaunchpadTradeInstance>; | ||
|
||
export default (sequelize: Sequelize.Sequelize): LaunchpadTradeModelStatic => | ||
<LaunchpadTradeModelStatic>sequelize.define<LaunchpadTradeInstance>( | ||
'LaunchpadTrade', | ||
{ | ||
eth_chain_id: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
primaryKey: true, | ||
}, | ||
transaction_hash: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
primaryKey: true, | ||
}, | ||
token_address: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
trader_address: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
is_buy: { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
}, | ||
community_token_amount: { | ||
type: Sequelize.DECIMAL(78, 0), | ||
allowNull: false, | ||
}, | ||
price: { | ||
type: Sequelize.DECIMAL(78, 0), | ||
allowNull: false, | ||
}, | ||
floating_supply: { | ||
type: Sequelize.DECIMAL(78, 0), | ||
allowNull: false, | ||
}, | ||
timestamp: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
}, | ||
}, | ||
{ | ||
timestamps: false, | ||
tableName: 'LaunchpadTrades', | ||
underscored: true, | ||
indexes: [{ fields: ['token_address'] }], | ||
}, | ||
); |
45 changes: 45 additions & 0 deletions
45
libs/model/src/services/commonProtocol/launchpadHelpers.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,45 @@ | ||
import Web3 from 'web3'; | ||
|
||
export async function getLaunchpadTradeTransaction({ | ||
rpc, | ||
transactionHash, | ||
}: { | ||
rpc: string; | ||
transactionHash: string; | ||
}) { | ||
const web3 = new Web3(rpc); | ||
|
||
const txReceipt = await web3.eth.getTransactionReceipt(transactionHash); | ||
if (!txReceipt) { | ||
return; | ||
} | ||
|
||
const block = await web3.eth.getBlock(txReceipt.blockHash.toString()); | ||
|
||
const { | ||
0: traderAddress, | ||
1: tokenAddress, | ||
2: isBuy, | ||
3: communityTokenAmount, | ||
4: ethAmount, | ||
5: protocolEthAmount, | ||
6: floatingSupply, | ||
} = web3.eth.abi.decodeParameters( | ||
['address', 'address', 'bool', 'uint256', 'uint256', 'uint256', 'uint256'], | ||
txReceipt.logs[1].data!.toString(), | ||
); | ||
|
||
return { | ||
txReceipt, | ||
block, | ||
parsedArgs: { | ||
traderAddress: traderAddress as string, | ||
tokenAddress: tokenAddress as string, | ||
isBuy: isBuy as boolean, | ||
communityTokenAmount: communityTokenAmount as bigint, | ||
ethAmount: ethAmount as bigint, | ||
protocolEthAmount: protocolEthAmount as bigint, | ||
floatingSupply: floatingSupply as bigint, | ||
}, | ||
}; | ||
} |
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,74 @@ | ||
import { Command, InvalidState } from '@hicommonwealth/core'; | ||
import * as schemas from '@hicommonwealth/schemas'; | ||
import { commonProtocol } from '@hicommonwealth/shared'; | ||
import { models } from '../database'; | ||
import { mustExist } from '../middleware/guards'; | ||
import { getLaunchpadTradeTransaction } from '../services/commonProtocol/launchpadHelpers'; | ||
|
||
const launchpadEthChainIds = Object.values( | ||
commonProtocol.factoryContracts, | ||
).reduce<number[]>((acc, contract) => { | ||
if (contract.launchpad) { | ||
acc.push(contract.chainId); | ||
} | ||
return acc; | ||
}, []); | ||
|
||
export function CreateLaunchpadTrade(): Command< | ||
typeof schemas.CreateLaunchpadTrade | ||
> { | ||
return { | ||
...schemas.CreateLaunchpadTrade, | ||
auth: [], | ||
body: async ({ payload }) => { | ||
const { eth_chain_id, transaction_hash } = payload; | ||
if (!launchpadEthChainIds.includes(eth_chain_id)) { | ||
throw Error( | ||
`EVM Chain ${eth_chain_id} does not have deployed launchpad`, | ||
); | ||
} | ||
|
||
const existingTrade = await models.LaunchpadTrade.findOne({ | ||
where: { | ||
eth_chain_id, | ||
transaction_hash, | ||
}, | ||
}); | ||
if (existingTrade) { | ||
return existingTrade?.get({ plain: true }); | ||
} | ||
|
||
const chainNode = await models.ChainNode.scope('withPrivateData').findOne( | ||
{ | ||
where: { | ||
eth_chain_id, | ||
}, | ||
}, | ||
); | ||
mustExist('Chain Node', chainNode); | ||
|
||
const result = await getLaunchpadTradeTransaction({ | ||
rpc: chainNode.private_url! || chainNode.url!, | ||
transactionHash: transaction_hash, | ||
}); | ||
if (!result) { | ||
throw new InvalidState('Transaction not found'); | ||
} | ||
|
||
const trade = await models.LaunchpadTrade.create({ | ||
eth_chain_id, | ||
transaction_hash, | ||
token_address: result.parsedArgs.tokenAddress, | ||
trader_address: result.parsedArgs.traderAddress, | ||
is_buy: result.parsedArgs.isBuy, | ||
community_token_amount: result.parsedArgs.communityTokenAmount, | ||
price: | ||
result.parsedArgs.communityTokenAmount / result.parsedArgs.ethAmount, | ||
floating_supply: result.parsedArgs.floatingSupply, | ||
timestamp: Number(result.block.timestamp), | ||
}); | ||
|
||
return trade.get({ plain: true }); | ||
}, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './CreateLaunchpadTrade.command'; | ||
export * from './CreateToken.command'; | ||
export * from './GetTokens.query'; |
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
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
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
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
Oops, something went wrong.