-
Notifications
You must be signed in to change notification settings - Fork 68
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: Amped Finance #43
base: main
Are you sure you want to change the base?
Conversation
projects/amped/functions/example.ts
Outdated
* @returns Transaction result | ||
*/ | ||
export async function example({ chainName, account, amount }: Props, { notify, getProvider }: FunctionOptions): Promise<FunctionReturn> { | ||
// Validate chain |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, but we need remove example from prod
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, deleted
}, | ||
], | ||
required: ['chainName', 'account', 'tokenIn'], | ||
parameters: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#36 (comment) you can't omit optional args
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, yes I think these were treated as optional as we default them to a null value if not provided
I have included each of these arguments as required now
}) as bigint; | ||
|
||
await notify(`Native price: ${nativePrice.toString()}`); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pls change this. notify(Native price: ${nativePrice.toString()}
); for notify(Native price from Amped Finance: ${nativePrice.toString()}
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
|
||
// Price is in 1e30, balance in 1e18, result should be in USD | ||
const nativeBalanceUsd = (Number(nativeBalanceBigInt) * Number(nativePrice)) / 1e48; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you have problem here. JavaScript Number (IEEE 754) cannot accurately represent values beyond 2^53 - 1 (~9 quadrillion). Consider use bigint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated accordingly
const nativeBalanceUsd = (Number(nativeBalanceBigInt) * Number(nativePrice)) / 1e48; | ||
|
||
await notify(`Native balance USD: ${nativeBalanceUsd}`); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same. change notify
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
abi: ERC20, | ||
functionName: 'balanceOf', | ||
args: [account], | ||
}) as bigint; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use multicall or await Promise.all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, updated to use Promise.all
}) as bigint; | ||
|
||
// Price is in 1e30, balance in token decimals, result should be in USD | ||
const balanceUsd = (Number(balance) * Number(price)) / (Math.pow(10, token.decimals) * 1e30); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved
*/ | ||
export async function addLiquidity( | ||
{ chainName, account, tokenSymbol, amount, percentOfBalance = 25, minUsdg = '0', minGlp = '0' }: Props, | ||
{ getProvider, notify, sendTransactions }: FunctionOptions, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not really good way by default without any warning ask 25% of balance from user. Suggest change percentOfBalance from optional to required and ask user input proper amount.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I think this was implemented this way for testing purposes.
We have now asked that the user provide either a % of the token balance or an exact amount
// Warn if price impact is high | ||
if (priceImpact > 1) { | ||
await notify(`Warning: High price impact (${priceImpact.toFixed(2)}%). Consider reducing the amount.`); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx for adding necessary checks. Rest, almost all don't do this.
const provider = getProvider(chainId); | ||
|
||
// Check token approval if not native token | ||
if (tokenIn !== CONTRACT_ADDRESSES[NETWORKS.SONIC].NATIVE_TOKEN) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you treat wrapped as native, so actually you NEED approve in that case too. Isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You make a good point, I was implementing NATIVE as wrapped native. I have separated the two tokens now.
adding S as liquidity as native token is fine
adding wS will check for approval before adding to the pool
await notify('Checking token approval...'); | ||
|
||
// Check current allowance for RewardRouterV2 | ||
const allowance = await provider.readContract({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just use checkApprove function. This function check allowance and add to transaction array tx for approve and return transaction array for your function for further fill.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, have done this
await notify(`Approval transaction submitted. Waiting for confirmation...`); | ||
|
||
// Wait for approval to be confirmed before proceeding | ||
await provider.waitForTransactionReceipt({ hash: approvalTx.data[0].hash }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you definitely don't need do this waitForTransactionReceipt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
// Prepare transaction data | ||
const txData: TransactionParams = { | ||
target: CONTRACT_ADDRESSES[NETWORKS.SONIC].REWARD_ROUTER, | ||
value: tokenIn === CONTRACT_ADDRESSES[NETWORKS.SONIC].NATIVE_TOKEN ? parsedAmount : 0n, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you need work with native you can use const NATIVE_ADDRESS: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE". Are you sure that you don't have here problem for sending msg.value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the native address to handle separately now
|
||
// Send transaction | ||
await notify('Executing transaction...'); | ||
const txResult = await sendTransactions({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
anon-integration-guide/projects/wagmi/functions/stake.ts
Lines 75 to 98 in 8864932
const result: TransactionReturn = await sendTransactions({ chainId, account, transactions }); | |
const stakeData = result.data[result.data.length - 1]; | |
if (result.isMultisig) { | |
return toResult(stakeData.message); | |
} | |
// Get transaction receipt and parse Transfer event | |
if (!stakeData.hash) return toResult(`Staked ${formatEther(amountInWei)} WAGMI to sWAGMI on ${chainName}, but failed to receive tx hash. ${stakeData.message}`); | |
const receipt = await provider.getTransactionReceipt({ hash: stakeData.hash }); | |
const transferEvents = parseEventLogs({ | |
logs: receipt.logs, | |
abi: sWagmiAbi, | |
eventName: 'Transfer', | |
}); | |
const stakeEvent = transferEvents.find((log) => log.args.from === zeroAddress); | |
if (!stakeEvent?.args?.value) { | |
return toResult(`Staked ${formatEther(amountInWei)} WAGMI to sWAGMI on ${chainName}, but couldn't verify received sWAGMI amount. ${stakeData.message}`); | |
} | |
const stakedAmount = formatEther(stakeEvent.args.value); | |
return toResult(`Staked ${formatEther(amountInWei)} WAGMI and received ${stakedAmount} sWAGMI on ${chainName}. ${stakeData.message}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have implemented this
|
||
try { | ||
const publicClient = getProvider(chainId); | ||
const amountInWei = parseUnits(amount, 18); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider check amountInWei > 0n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
Amped Finance Integration
Description
Implementation of Amped Finance perpetual swaps and AMM exchange integration:
Core Features
✅ Liquidity Operations
✅ Leveraged Trading
✅ Information Services
✅ Risk Management
Technical Improvements
Enhanced error handling with contextual messages
Robust input validation for financial operations
Type-safe implementations for core operations
Gas-optimized transaction building
Comprehensive response formatting
Supported Networks
Sonic (Mainnet)
Test Results
All core test suites passing (6/6):
Implementation Details
Modular architecture with separated concerns:
Features
Next Steps
References
Amped Finance
Protocol Type: Decentralized Perpetual Exchange
Twitter: @AmpedFinance
Documentation: https://amped.gitbook.io/amped/