Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix ci
Browse files Browse the repository at this point in the history
guibescos committed Sep 18, 2024
1 parent 3d78b2a commit 5aa9f98
Showing 3 changed files with 103 additions and 79 deletions.
65 changes: 39 additions & 26 deletions frontend/pages/approve.tsx
Original file line number Diff line number Diff line change
@@ -11,27 +11,31 @@ import { capitalizeFirstLetter } from 'utils/capitalizeFirstLetter'
import { useStakeConnection } from 'hooks/useStakeConnection'
import { useSplitRequest } from 'hooks/useSplitRequest'

export async function getStakeAccountsPubkeys(user: PublicKey, stakeConnection : StakeConnection){
const program = stakeConnection.program;
const res = await stakeConnection.program.provider.connection.getProgramAccounts(
program.programId,
{
encoding: "base64",
filters: [
{
memcmp: program.coder.accounts.memcmp("positionData"),
},
{
memcmp: {
offset: 8,
bytes: user.toBase58(),
export async function getStakeAccountsPubkeys(
user: PublicKey,
stakeConnection: StakeConnection
) {
const program = stakeConnection.program
const res =
await stakeConnection.program.provider.connection.getProgramAccounts(
program.programId,
{
encoding: 'base64',
filters: [
{
memcmp: program.coder.accounts.memcmp('positionData'),
},
},
],
}
);
{
memcmp: {
offset: 8,
bytes: user.toBase58(),
},
},
],
}
)

return res.map((account) => new PublicKey(account.pubkey));
return res.map((account) => new PublicKey(account.pubkey))
}
const ApproveSplit: NextPage = () => {
const anchorWallet = useAnchorWallet()
@@ -61,23 +65,32 @@ const ApproveSplit: NextPage = () => {

const handleSelectStakeAccount = (event: any) => {
const loadStakeAccount = async () => {
if (stakeAccounts && stakeConnection){
const stakeAccount = stakeAccounts.find(s => s.toString() === event.target.value)
if (stakeAccounts && stakeConnection) {
const stakeAccount = stakeAccounts.find(
(s) => s.toString() === event.target.value
)
if (stakeAccount) {
setSelectStakeAccount(await stakeConnection.loadStakeAccount(stakeAccount))
setSelectStakeAccount(
await stakeConnection.loadStakeAccount(stakeAccount)
)
}
}
}
}
loadStakeAccount()
}

useEffect(() => {
const loadStakeAccounts = async () => {
if (stakeConnection && anchorWallet && owner) {
const stakeAccounts = await getStakeAccountsPubkeys(new PublicKey(owner), stakeConnection)
const stakeAccounts = await getStakeAccountsPubkeys(
new PublicKey(owner),
stakeConnection
)
setStakeAccounts(stakeAccounts)
if (stakeAccounts.length > 0){
setSelectStakeAccount(await stakeConnection.loadStakeAccount(stakeAccounts[0]))
if (stakeAccounts.length > 0) {
setSelectStakeAccount(
await stakeConnection.loadStakeAccount(stakeAccounts[0])
)
} else {
setSelectStakeAccount(undefined)
}
92 changes: 47 additions & 45 deletions frontend/pages/create_locked_account.tsx
Original file line number Diff line number Diff line change
@@ -10,20 +10,23 @@ import { capitalizeFirstLetter } from 'utils/capitalizeFirstLetter'
import { useStakeConnection } from 'hooks/useStakeConnection'

function formatLockSummary(lock: {
periodicVesting:
{
initialBalance: BN,
periodDuration: BN,
startDate: BN,
periodicVesting: {
initialBalance: BN
periodDuration: BN
startDate: BN
numPeriods: BN
}
}): string[] {
const lockSummary = getLockSummary(lock, null);
const lockSummary = getLockSummary(lock, null)
if (lockSummary.schedule) {
const rows = lockSummary.schedule.map((x) => ` - ${x.amount.toString()} on ${new Date(Number(x.date) * 1000).toDateString()}`)
const rows = lockSummary.schedule.map(
(x) =>
` - ${x.amount.toString()} on ${new Date(
Number(x.date) * 1000
).toDateString()}`
)
return rows
}
else {
} else {
return ['Invalid schedule']
}
}
@@ -35,14 +38,13 @@ const CreateLockedAccount: NextPage = () => {
const [firstUnlock, setFirstUnlock] = useState<Date>()
const [numPeriods, setNumPeriods] = useState<BN>()
const [lock, setLock] = useState<{
periodicVesting:
{
initialBalance: BN,
periodDuration: BN,
startDate: BN,
periodicVesting: {
initialBalance: BN
periodDuration: BN
startDate: BN
numPeriods: BN
}
}>();
}>()
const [hasTested, setHasTested] = useState<boolean>()

useEffect(() => {
@@ -56,17 +58,24 @@ const CreateLockedAccount: NextPage = () => {
}, [owner])

useEffect(() => {
if (amount && startDate && firstUnlock && numPeriods && firstUnlock.getTime() > startDate.getTime()) {
if (
amount &&
startDate &&
firstUnlock &&
numPeriods &&
firstUnlock.getTime() > startDate.getTime()
) {
setLock({
periodicVesting: {
initialBalance: amount.toBN(),
periodDuration: new BN(firstUnlock.getTime() / 1000).sub(new BN(startDate.getTime() / 1000)),
periodDuration: new BN(firstUnlock.getTime() / 1000).sub(
new BN(startDate.getTime() / 1000)
),
startDate: new BN(startDate.getTime() / 1000),
numPeriods
}
numPeriods,
},
})
}
else {
} else {
setLock(undefined)
}
}, [amount, startDate, firstUnlock, numPeriods])
@@ -87,28 +96,26 @@ const CreateLockedAccount: NextPage = () => {
}

const handleSetStartDate = (event: any) => {
setStartDate(new Date(event.target.value))
setStartDate(new Date(event.target.value))
}

const handleSetFirstUnlock = (event: any) => {
setFirstUnlock(new Date(event.target.value))
setFirstUnlock(new Date(event.target.value))
}

const handleSetNumPeriods = (event: any) => {
try {
const numPeriods = new BN(event.target.value)
if (numPeriods.gte(new BN(1))){
if (numPeriods.gte(new BN(1))) {
setNumPeriods(numPeriods)
} else{
} else {
setNumPeriods(undefined)
}

} catch (e) {
setNumPeriods(undefined)
}
}


const { data: stakeConnection } = useStakeConnection()
const [stakeAccounts, setStakeAccounts] = useState<StakeAccount[]>()

@@ -127,12 +134,7 @@ const CreateLockedAccount: NextPage = () => {
const createLockedAccount = async () => {
if (stakeConnection && owner && amount)
try {
await stakeConnection.setupVestingAccount(
amount,
owner,
lock,
false
)
await stakeConnection.setupVestingAccount(amount, owner, lock, false)
toast.success('Successfully created locked account')
} catch (err) {
toast.error(capitalizeFirstLetter(err.message))
@@ -142,9 +144,7 @@ const CreateLockedAccount: NextPage = () => {
return (
<Layout>
<SEO title={'Create Locked Account'} />
<p className=" p-2 ">
Create a locked account with any unlock schedule
</p>
<p className=" p-2 ">Create a locked account with any unlock schedule</p>
<p className=" text-sm ">Owner</p>
<input
type="text"
@@ -163,22 +163,26 @@ const CreateLockedAccount: NextPage = () => {
<input
type="date"
style={{ color: 'black' }}
onChange={handleSetStartDate} />
onChange={handleSetStartDate}
/>
<p className=" text-sm ">First unlock date</p>
<input
type="date"
style={{ color: 'black' }}
onChange={handleSetFirstUnlock} />
onChange={handleSetFirstUnlock}
/>
<p className=" text-sm ">Number of unlocks</p>
<input
type="number"
style={{ color: 'black' }}
value={numPeriods ? numPeriods.toString() : ''}
onChange={handleSetNumPeriods} />
onChange={handleSetNumPeriods}
/>
<p className=" text-sm ">
Schedule: {lock ? formatLockSummary(lock).map(
(x) => <p key="0">{x}</p>
) : 'Invalid schedule'}
Schedule:{' '}
{lock
? formatLockSummary(lock).map((x) => <p key="0">{x}</p>)
: 'Invalid schedule'}
</p>
{stakeAccounts && stakeAccounts.length > 0 && (
<a
@@ -191,9 +195,7 @@ const CreateLockedAccount: NextPage = () => {
</a>
)}
{owner && !hasTested && (
<p style={{ color: 'red' }}>
{`Warning, this owner hasn't tested`}
</p>
<p style={{ color: 'red' }}>{`Warning, this owner hasn't tested`}</p>
)}
{stakeConnection && owner && amount ? (
<p>
25 changes: 17 additions & 8 deletions frontend/pages/request.tsx
Original file line number Diff line number Diff line change
@@ -49,10 +49,15 @@ const RequestSplit: NextPage = () => {
useEffect(() => {
const loadStakeAccounts = async () => {
if (stakeConnection) {
const stakeAccounts = await getStakeAccountsPubkeys(stakeConnection.userPublicKey(), stakeConnection)
const stakeAccounts = await getStakeAccountsPubkeys(
stakeConnection.userPublicKey(),
stakeConnection
)
setStakeAccounts(stakeAccounts)
if (stakeAccounts.length > 0){
setSelectStakeAccount(await stakeConnection.loadStakeAccount(stakeAccounts[0]))
if (stakeAccounts.length > 0) {
setSelectStakeAccount(
await stakeConnection.loadStakeAccount(stakeAccounts[0])
)
} else {
setSelectStakeAccount(undefined)
}
@@ -65,13 +70,17 @@ const RequestSplit: NextPage = () => {

const handleSelectStakeAccount = (event: any) => {
const loadStakeAccount = async () => {
if (stakeAccounts && stakeConnection){
const stakeAccount = stakeAccounts.find(s => s.toString() === event.target.value)
if (stakeAccount) {
setSelectStakeAccount(await stakeConnection.loadStakeAccount(stakeAccount))
if (stakeAccounts && stakeConnection) {
const stakeAccount = stakeAccounts.find(
(s) => s.toString() === event.target.value
)
if (stakeAccount) {
setSelectStakeAccount(
await stakeConnection.loadStakeAccount(stakeAccount)
)
}
}
}
}
loadStakeAccount()
}

0 comments on commit 5aa9f98

Please sign in to comment.