-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Add lido protocol #22
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
Open
alvinphilips
wants to merge
21
commits into
DappioLab:master
Choose a base branch
from
alvinphilips:feat-add-lido
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a923645
Bumped navigator to v0.2.14
alvinphilips 1825623
Added temporary ids for Lido
alvinphilips b66d18a
feat: Add Lido as a protocol (deposit)
alvinphilips 83f9246
Added Lido as a Supported protocol
alvinphilips 349c8fc
Added Lido withdraw stub in builder
alvinphilips 72cbd18
Removed unused vaultInfo from deposit()
alvinphilips 7ad028f
Ran prettier on lido.ts
alvinphilips 5a6d789
Switched to findProgramAddressSync
alvinphilips 859bd90
feat: Added withdrawal
alvinphilips c877549
Added test for Lido Adapter
alvinphilips ab682f0
Updated Lido Adapter Program ID
alvinphilips 8c52d8d
Fix account numbering in case of v1
alvinphilips 8287e0c
Removed deactivate transaction from gateway
alvinphilips 5ec577a
Removed instruction byte from adapter input struct
alvinphilips 725c7b5
Removed error thrown with Lido withdraw
alvinphilips 595d563
Added fallback in case of mistyped BN field
alvinphilips f06d835
Removed v1 code
alvinphilips 7eb7dfd
Temporary work-around to access validator index for Lido
alvinphilips 5f73732
Bumped up navigator to 0.2.20
alvinphilips 5f82a78
Added temporary work around in gateway state
alvinphilips 94e8146
Remove validatorIndex from WithdrawParams
alvinphilips File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,148 @@ | ||
| import * as anchor from "@project-serum/anchor"; | ||
| import { TOKEN_PROGRAM_ID, getAssociatedTokenAddress } from "@solana/spl-token-v2"; | ||
| import { DepositParams, GatewayParams, IProtocolVault, PAYLOAD_SIZE, WithdrawParams } from "../types"; | ||
| import { Gateway } from "@dappio-wonderland/gateway-idls"; | ||
| import { IVaultInfo, lido } from "@dappio-wonderland/navigator"; | ||
| import { struct, nu64, u8, u32 } from "buffer-layout"; | ||
| import { getActivityIndex, sigHash, createATAWithoutCheckIx, getGatewayAuthority } from "../utils"; | ||
| import { LIDO_ADAPTER_PROGRAM_ID } from "../ids"; | ||
|
|
||
| export class ProtocolLido implements IProtocolVault { | ||
| constructor( | ||
| private _connection: anchor.web3.Connection, | ||
| private _gatewayProgram: anchor.Program<Gateway>, | ||
| private _gatewayStateKey: anchor.web3.PublicKey, | ||
| private _gatewayParams: GatewayParams | ||
| ) {} | ||
| async deposit( | ||
| params: DepositParams, | ||
| vault: IVaultInfo, | ||
| userKey: anchor.web3.PublicKey | ||
| ): Promise<{ txs: anchor.web3.Transaction[]; input: Buffer }> { | ||
| // Handle payload input here | ||
| const inputLayout = struct([nu64("amount")]); | ||
| let payload = Buffer.alloc(PAYLOAD_SIZE); | ||
| inputLayout.encode( | ||
| { | ||
| amount: new anchor.BN(params.depositAmount), | ||
| }, | ||
| payload | ||
| ); | ||
|
|
||
| // Handle transaction here | ||
|
|
||
| let preInstructions = [] as anchor.web3.TransactionInstruction[]; | ||
|
|
||
| const bufferArray = [lido.LIDO_ADDRESS.toBuffer(), Buffer.from("reserve_account")]; | ||
| const [reserveAccount] = anchor.web3.PublicKey.findProgramAddressSync(bufferArray, lido.LIDO_PROGRAM_ID); | ||
| const bufferArrayMint = [lido.LIDO_ADDRESS.toBuffer(), Buffer.from("mint_authority")]; | ||
| const [mintAuthority] = anchor.web3.PublicKey.findProgramAddressSync(bufferArrayMint, lido.LIDO_PROGRAM_ID); | ||
|
|
||
| const recipientStSolAddress = await getAssociatedTokenAddress(vault.shareMint, userKey); | ||
|
|
||
| preInstructions.push(await createATAWithoutCheckIx(userKey, vault.shareMint)); | ||
|
|
||
| const remainingAccounts = [ | ||
| { pubkey: lido.LIDO_ADDRESS, isSigner: false, isWritable: true }, // 0 | ||
| { pubkey: userKey, isSigner: true, isWritable: true }, // 1 wallet.publicKey | ||
| { pubkey: recipientStSolAddress, isSigner: false, isWritable: true }, // 2 | ||
| { pubkey: vault.shareMint, isSigner: false, isWritable: true }, // 3 | ||
| { pubkey: reserveAccount, isSigner: false, isWritable: true }, // 4 | ||
| { pubkey: mintAuthority, isSigner: false, isWritable: false }, // 5 | ||
| { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // 6 | ||
| { pubkey: anchor.web3.SystemProgram.programId, isSigner: false, isWritable: false }, // 7 | ||
| ]; | ||
| const txDeposit = await this._gatewayProgram.methods | ||
| .deposit() | ||
| .accounts({ | ||
| gatewayState: this._gatewayStateKey, | ||
| adapterProgramId: LIDO_ADAPTER_PROGRAM_ID, | ||
| baseProgramId: lido.LIDO_PROGRAM_ID, | ||
| activityIndex: await getActivityIndex(userKey), | ||
| gatewayAuthority: getGatewayAuthority(), | ||
| }) | ||
| .preInstructions(preInstructions) | ||
| .remainingAccounts(remainingAccounts) | ||
| .transaction(); | ||
|
|
||
| return { txs: [txDeposit], input: payload }; | ||
| } | ||
|
|
||
| async withdraw( | ||
| params: WithdrawParams, | ||
| vault: IVaultInfo, | ||
| userKey: anchor.web3.PublicKey | ||
| ): Promise<{ txs: anchor.web3.Transaction[]; input: Buffer }> { | ||
| // Handle payload input here | ||
| const inputLayout = struct([nu64("amount"), u32("validatorIndex")]); | ||
| let payload = Buffer.alloc(PAYLOAD_SIZE); | ||
|
|
||
| const vaultInfo = vault as lido.VaultInfo; | ||
| const vaultInfoWrapper = new lido.VaultInfoWrapper(vaultInfo); | ||
|
|
||
| const heaviestValidatorIndex = vaultInfoWrapper.getHeaviestValidatorIndex(); | ||
|
|
||
| const heaviestValidator = vaultInfo.validators!.entries[heaviestValidatorIndex]; | ||
|
|
||
| inputLayout.encode( | ||
| { | ||
| amount: new anchor.BN(params.withdrawAmount), | ||
| // Set validator index. | ||
| validatorIndex: heaviestValidatorIndex, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }, | ||
| payload | ||
| ); | ||
|
|
||
| // Account to temporarily hold stSOL in | ||
| const receivingAccount = anchor.web3.Keypair.generate(); | ||
|
|
||
| const senderStSolAddress = await getAssociatedTokenAddress(vault.shareMint, userKey); | ||
|
|
||
| const bufferArrayStake = [lido.LIDO_ADDRESS.toBuffer(), Buffer.from("stake_authority")]; | ||
| const [stakeAuthority] = anchor.web3.PublicKey.findProgramAddressSync(bufferArrayStake, lido.LIDO_PROGRAM_ID); | ||
|
|
||
| const validatorStakeSeeds = [ | ||
| lido.LIDO_ADDRESS.toBuffer(), | ||
| heaviestValidator.pubkey.toBuffer(), | ||
| Buffer.from("validator_stake_account"), | ||
| Buffer.from(heaviestValidator.entry.stakeSeeds.begin.toArray("le", 8)), | ||
| ]; | ||
|
|
||
| const [validatorStakeAccount] = anchor.web3.PublicKey.findProgramAddressSync( | ||
| validatorStakeSeeds, | ||
| lido.LIDO_PROGRAM_ID | ||
| ); | ||
|
|
||
| // Set up accounts | ||
| const remainingAccounts = [ | ||
| { pubkey: lido.LIDO_ADDRESS, isSigner: false, isWritable: true }, // 1 | ||
| { pubkey: userKey, isSigner: true, isWritable: false }, // 2 | ||
| { pubkey: senderStSolAddress, isSigner: false, isWritable: true }, // 3 | ||
| { pubkey: vault.shareMint, isSigner: false, isWritable: true }, // 4 | ||
| { pubkey: heaviestValidator.pubkey, isSigner: false, isWritable: false }, // 5 | ||
| { pubkey: validatorStakeAccount, isSigner: false, isWritable: true }, // 6 | ||
| { pubkey: receivingAccount.publicKey, isSigner: true, isWritable: true }, // 7 | ||
| { pubkey: stakeAuthority, isSigner: false, isWritable: false }, // 8 | ||
| { pubkey: vaultInfo.validatorList, isSigner: false, isWritable: true }, // 9 | ||
| { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // 10 | ||
| { pubkey: anchor.web3.SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false }, // 11 | ||
| { pubkey: anchor.web3.SystemProgram.programId, isSigner: false, isWritable: false }, // 12 | ||
| { pubkey: anchor.web3.StakeProgram.programId, isSigner: false, isWritable: false }, // 13 | ||
| ]; | ||
|
|
||
| // Handle transaction here | ||
| const txWithdraw = await this._gatewayProgram.methods | ||
| .withdraw() | ||
| .accounts({ | ||
| gatewayState: this._gatewayStateKey, | ||
| adapterProgramId: LIDO_ADAPTER_PROGRAM_ID, | ||
| baseProgramId: lido.LIDO_PROGRAM_ID, | ||
| activityIndex: await getActivityIndex(userKey), | ||
| gatewayAuthority: getGatewayAuthority(), | ||
| }) | ||
| .remainingAccounts(remainingAccounts) | ||
| .transaction(); | ||
|
|
||
| return { txs: [txWithdraw], input: payload }; | ||
| } | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
So in order to support
validatorIndexinGatewayState:validatorIndexas an optional field inDepositParamsvalidatorIndexfield (metadata section) inGatewayParams(this is a work-around)this.params.validatorIndex = depositParams.validatorIndex