Skip to content

Commit

Permalink
Merge pull request #54 from mfactory-lab/converter
Browse files Browse the repository at this point in the history
fix: update sdk, fix lock unlock proof request
  • Loading branch information
harniy authored Feb 8, 2024
2 parents 77cd9bb + 62f15c4 commit a4d5233
Show file tree
Hide file tree
Showing 7 changed files with 1,348 additions and 1,320 deletions.
31 changes: 25 additions & 6 deletions src/hooks/converter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { LAMPORTS_PER_SOL } from '@solana/web3.js'
import type { TokenData } from '@/config'
import type { ConvertToken } from '@/stores/convertet'
import { convertTokenSymbol } from '@/utils/converter'

export function useConverter() {
const converterStore = useConverterStore()
const userStore = useUserStore()
const connectionStore = useConnectionStore()

const { notify } = useQuasar()

Expand All @@ -29,7 +31,7 @@ export function useConverter() {

const pairAccount = computed(() => converterStore.state.selectedPair?.account)
const pairRatio = computed(() => pairAccount.value?.ratio?.num?.toNumber() ?? 1)
const pairLockedAmount = computed(() => pairAccount.value?.lockedAmount?.toNumber() ?? 0)
const pairLockedAmount = computed(() => pairAccount.value?.lockedAmount?.toNumber() / LAMPORTS_PER_SOL ?? 0)
const pairLockFee = computed(() => pairAccount.value?.lockFee)

const tokenASymbol = computed(() => {
Expand All @@ -47,32 +49,49 @@ export function useConverter() {
})

async function lockUnlockToken(lock = true) {
if (!pairAccount.value) {
const proofRequest = userStore.state.certificates?.find(c => c.data?.policy.toBase58() === String(userStore.requiredPolicy))?.pubkey

if (!pairAccount.value || !proofRequest) {
return
}
try {
converterStore.state.converting = true
const tokenA = pairAccount.value.tokenA
const tokenB = pairAccount.value.tokenB
const proofRequest = pairAccount.value.policy
const amount = Number(converterStore.state.from.amount)
const amount = Number(converterStore.state.from.amount * LAMPORTS_PER_SOL)

let res

if (lock) {
await converterStore.converterClient.lockTokens({
res = await converterStore.converterClient.lockTokens({
tokenA,
tokenB,
amount,
proofRequest,
})
} else {
await converterStore.converterClient.unlockTokens({
res = await converterStore.converterClient.unlockTokens({
tokenA,
tokenB,
amount,
proofRequest,
})
}

console.log(res)

notify({
message: 'Transaction confirmed',
type: 'positive',
actions: [{
label: 'Explore',
color: 'white',
target: '_blank',
href: `https://explorer.solana.com/tx/${res.signature}?cluster=${connectionStore.cluster}`,
onClick: () => false,
}],
})

await converterStore.getAllTokens()
} catch (err) {
console.error('lockedToken error: ', err)
Expand Down
32 changes: 16 additions & 16 deletions src/sdk/idl/smart_converter.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.5",
"version": "0.0.6",
"name": "smart_converter",
"instructions": [
{
Expand Down Expand Up @@ -242,25 +242,25 @@
"isOptional": true
},
{
"name": "user",
"name": "pair",
"isMut": true,
"isSigner": false
},
{
"name": "userAuthority",
"isMut": true,
"isSigner": true
},
{
"name": "pair",
"isMut": true,
"name": "pairAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "pairAuthority",
"name": "user",
"isMut": false,
"isSigner": false
},
{
"name": "userAuthority",
"isMut": true,
"isSigner": true
},
{
"name": "whitelistedUserInfo",
"isMut": false,
Expand Down Expand Up @@ -340,7 +340,7 @@
},
{
"name": "user",
"isMut": true,
"isMut": false,
"isSigner": false
},
{
Expand Down Expand Up @@ -890,10 +890,10 @@
{
"name": "user",
"type": "publicKey",
"index": true
"index": false
},
{
"name": "userWallet",
"name": "userAuthority",
"type": "publicKey",
"index": false
},
Expand All @@ -920,10 +920,10 @@
{
"name": "user",
"type": "publicKey",
"index": true
"index": false
},
{
"name": "userWallet",
"name": "userAuthority",
"type": "publicKey",
"index": false
},
Expand Down Expand Up @@ -988,4 +988,4 @@
"binaryVersion": "0.29.0",
"libVersion": "0.29.0"
}
}
}
4 changes: 2 additions & 2 deletions src/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@smart-converter/sdk",
"version": "0.0.5",
"private": true,
"type": "module",
"version": "0.0.6",
"private": true,
"main": "src/index.ts",
"scripts": {
"generate": "DEBUG='(solita|rustbin):(info|error)' solita"
Expand Down
33 changes: 21 additions & 12 deletions src/sdk/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Buffer } from 'node:buffer'
import type { Address } from '@coral-xyz/anchor'
import { AnchorProvider, BN, Program, web3 } from '@coral-xyz/anchor'
import type { ConfirmOptions, Connection } from '@solana/web3.js'
import { Keypair, PublicKey, SystemProgram, Transaction } from '@solana/web3.js'
import { AnchorProvider, BN, Program, web3 } from '@coral-xyz/anchor'
import {
MINT_SIZE,
TOKEN_PROGRAM_ID,
Expand All @@ -13,6 +13,8 @@ import {
getAccount,
getAssociatedTokenAddressSync,
getMinimumBalanceForRentExemptMint,
createSetAuthorityInstruction,
AuthorityType
} from '@solana/spl-token'
import type { Admin, Manager, Pair, Ratio, User, WhitelistedUserInfo } from './generated'
import {
Expand Down Expand Up @@ -194,6 +196,13 @@ export class SmartConverterClient {
} else {
pair = this.pda.pair(tokenA, tokenB)[0]
pairAuthority = this.pda.pairAuthority(pair)[0]

tx.add(createSetAuthorityInstruction(
tokenB,
this.provider.publicKey,
AuthorityType.MintTokens,
pairAuthority
))
}

await this.handleMissingTokenAccount(tx, tokenA, pairAuthority)
Expand Down Expand Up @@ -674,13 +683,13 @@ class SmartConverterPDA {
}
}

export interface Wallet {
export type Wallet = {
signTransaction(tx: Transaction): Promise<Transaction>
signAllTransactions(txs: Transaction[]): Promise<Transaction[]>
publicKey: PublicKey
}

interface AddPairProps {
type AddPairProps = {
/// Security token mint address
tokenA: PublicKey
/// Utility token mint address
Expand All @@ -692,17 +701,17 @@ interface AddPairProps {
policy?: PublicKey
}

interface WhitelistProps {
type WhitelistProps = {
userWallet: PublicKey
tokenA: PublicKey
tokenB: PublicKey
}

interface UpdateUserProps {
type UpdateUserProps = {
userWallet: PublicKey
}

interface LockTokensProps {
type LockTokensProps = {
/// Mint A
tokenA: PublicKey
/// Mint B
Expand All @@ -718,35 +727,35 @@ interface LockTokensProps {
proofRequest?: PublicKey
}

interface ManagerProps {
type ManagerProps = {
managerWallet: PublicKey
}

interface RemovePairProps {
type RemovePairProps = {
tokenA: PublicKey
tokenB: PublicKey
}

interface SetAdminProps {
type SetAdminProps = {
authority: PublicKey
}

interface WithdrawFeeProps {
type WithdrawFeeProps = {
tokenA: PublicKey
tokenB: PublicKey
destination: PublicKey
amount: number | BN
}

interface UnlockTokensProps {
type UnlockTokensProps = {
tokenA: PublicKey
tokenB: PublicKey
amount: number | BN
feePayer?: PublicKey
proofRequest?: PublicKey
}

interface UpdatePairProps {
type UpdatePairProps = {
tokenA: PublicKey
tokenB: PublicKey
newAuthority?: PublicKey
Expand Down
30 changes: 15 additions & 15 deletions src/sdk/src/generated/instructions/lockTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ export const lockTokensStruct = new beet.BeetArgsStruct<
['instructionDiscriminator', beet.uniformFixedSizeArray(beet.u8, 8)],
['amount', beet.u64],
],
'LockTokensInstructionArgs'
'LockTokensInstructionArgs',
)
/**
* Accounts required by the _lockTokens_ instruction
*
* @property [] proofRequest (optional)
* @property [_writable_] user
* @property [_writable_, **signer**] userAuthority
* @property [_writable_] pair
* @property [] pairAuthority
* @property [] user
* @property [_writable_, **signer**] userAuthority
* @property [] whitelistedUserInfo
* @property [] admin
* @property [] manager
Expand All @@ -57,10 +57,10 @@ export const lockTokensStruct = new beet.BeetArgsStruct<
*/
export type LockTokensInstructionAccounts = {
proofRequest?: web3.PublicKey
user: web3.PublicKey
userAuthority: web3.PublicKey
pair: web3.PublicKey
pairAuthority: web3.PublicKey
user: web3.PublicKey
userAuthority: web3.PublicKey
whitelistedUserInfo: web3.PublicKey
admin: web3.PublicKey
manager: web3.PublicKey
Expand Down Expand Up @@ -96,7 +96,7 @@ export const lockTokensInstructionDiscriminator = [
export function createLockTokensInstruction(
accounts: LockTokensInstructionAccounts,
args: LockTokensInstructionArgs,
programId = new web3.PublicKey('JDe51ZjpQ3tZzL6QTVPHt5VT5NzaDuJnrTmJJUFrC3vm')
programId = new web3.PublicKey('JDe51ZjpQ3tZzL6QTVPHt5VT5NzaDuJnrTmJJUFrC3vm'),
) {
const [data] = lockTokensStruct.serialize({
instructionDiscriminator: lockTokensInstructionDiscriminator,
Expand All @@ -109,25 +109,25 @@ export function createLockTokensInstruction(
isSigner: false,
},
{
pubkey: accounts.user,
pubkey: accounts.pair,
isWritable: true,
isSigner: false,
},
{
pubkey: accounts.userAuthority,
isWritable: true,
isSigner: true,
},
{
pubkey: accounts.pair,
isWritable: true,
pubkey: accounts.pairAuthority,
isWritable: false,
isSigner: false,
},
{
pubkey: accounts.pairAuthority,
pubkey: accounts.user,
isWritable: false,
isSigner: false,
},
{
pubkey: accounts.userAuthority,
isWritable: true,
isSigner: true,
},
{
pubkey: accounts.whitelistedUserInfo,
isWritable: false,
Expand Down
8 changes: 4 additions & 4 deletions src/sdk/src/generated/instructions/unlockTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ export const unlockTokensStruct = new beet.BeetArgsStruct<
['instructionDiscriminator', beet.uniformFixedSizeArray(beet.u8, 8)],
['amount', beet.u64],
],
'UnlockTokensInstructionArgs'
'UnlockTokensInstructionArgs',
)
/**
* Accounts required by the _unlockTokens_ instruction
*
* @property [] proofRequest (optional)
* @property [_writable_] user
* @property [] user
* @property [_writable_, **signer**] userAuthority
* @property [_writable_] pair
* @property [] pairAuthority
Expand Down Expand Up @@ -96,7 +96,7 @@ export const unlockTokensInstructionDiscriminator = [
export function createUnlockTokensInstruction(
accounts: UnlockTokensInstructionAccounts,
args: UnlockTokensInstructionArgs,
programId = new web3.PublicKey('JDe51ZjpQ3tZzL6QTVPHt5VT5NzaDuJnrTmJJUFrC3vm')
programId = new web3.PublicKey('JDe51ZjpQ3tZzL6QTVPHt5VT5NzaDuJnrTmJJUFrC3vm'),
) {
const [data] = unlockTokensStruct.serialize({
instructionDiscriminator: unlockTokensInstructionDiscriminator,
Expand All @@ -110,7 +110,7 @@ export function createUnlockTokensInstruction(
},
{
pubkey: accounts.user,
isWritable: true,
isWritable: false,
isSigner: false,
},
{
Expand Down
Loading

0 comments on commit a4d5233

Please sign in to comment.