This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from ar-io/pe-5428-delegated-staking-v2
feat(PE-5428): delegated staking v2
- Loading branch information
Showing
34 changed files
with
3,396 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const decreaseDelegateStakeSchema = { | ||
$id: '#/definitions/decreaseDelegateStake', | ||
type: 'object', | ||
properties: { | ||
function: { | ||
type: 'string', | ||
const: 'delegateStake', | ||
}, | ||
target: { | ||
type: 'string', | ||
pattern: '^[a-zA-Z0-9-_]{43}$', | ||
}, | ||
qty: { | ||
type: 'integer', | ||
minimum: 1, | ||
}, | ||
}, | ||
required: ['target', 'qty'], | ||
additionalProperties: false, | ||
}; | ||
|
||
module.exports = { | ||
decreaseDelegateStakeSchema, | ||
}; |
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,24 @@ | ||
const delegateStakeSchema = { | ||
$id: '#/definitions/delegateStake', | ||
type: 'object', | ||
properties: { | ||
function: { | ||
type: 'string', | ||
const: 'delegateStake', | ||
}, | ||
target: { | ||
type: 'string', | ||
pattern: '^[a-zA-Z0-9-_]{43}$', | ||
}, | ||
qty: { | ||
type: 'integer', | ||
minimum: 1, | ||
}, | ||
}, | ||
required: ['target', 'qty'], | ||
additionalProperties: false, | ||
}; | ||
|
||
module.exports = { | ||
delegateStakeSchema, | ||
}; |
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,170 @@ | ||
import { | ||
DELEGATED_STAKE_UNLOCK_LENGTH, | ||
INVALID_INPUT_MESSAGE, | ||
} from '../../constants'; | ||
import { | ||
getBaselineState, | ||
stubbedArweaveTxId, | ||
stubbedGatewayData, | ||
} from '../../tests/stubs'; | ||
import { IOState } from '../../types'; | ||
import { decreaseDelegateStake } from './decreaseDelegateStake'; | ||
|
||
describe('decreaseDelegateStake', () => { | ||
describe('invalid inputs', () => { | ||
it.each([[0, '', stubbedArweaveTxId.concat(stubbedArweaveTxId), true]])( | ||
'should throw an error on invalid target', | ||
async (badLabel: unknown) => { | ||
const initialState: IOState = { | ||
...getBaselineState(), | ||
gateways: { | ||
[stubbedArweaveTxId]: { | ||
...stubbedGatewayData, | ||
}, | ||
}, | ||
}; | ||
const error = await decreaseDelegateStake(initialState, { | ||
caller: stubbedArweaveTxId, | ||
input: { | ||
label: badLabel, | ||
}, | ||
}).catch((e: any) => e); | ||
expect(error).toBeInstanceOf(Error); | ||
expect(error.message).toEqual( | ||
expect.stringContaining(INVALID_INPUT_MESSAGE), | ||
); | ||
}, | ||
); | ||
|
||
it.each([['bad-port', '0', 0, -1, true, Number.MAX_SAFE_INTEGER]])( | ||
'should throw an error on invalid qty', | ||
async (badQty: unknown) => { | ||
const initialState = getBaselineState(); | ||
const error = await decreaseDelegateStake(initialState, { | ||
caller: 'test', | ||
input: { | ||
qty: badQty, | ||
settings: { | ||
port: badQty, | ||
}, | ||
}, | ||
}).catch((e) => e); | ||
expect(error).toBeInstanceOf(Error); | ||
expect(error.message).toEqual( | ||
expect.stringContaining(INVALID_INPUT_MESSAGE), | ||
); | ||
}, | ||
); | ||
}); | ||
|
||
describe('valid inputs', () => { | ||
it('should decrease the delegate stake', async () => { | ||
const initialState: IOState = { | ||
...getBaselineState(), | ||
gateways: { | ||
[stubbedArweaveTxId]: { | ||
...stubbedGatewayData, | ||
delegates: { | ||
['existing-delegate']: { | ||
delegatedStake: 1000, | ||
start: 0, | ||
vaults: {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
const { state } = await decreaseDelegateStake(initialState, { | ||
caller: 'existing-delegate', | ||
input: { | ||
target: stubbedArweaveTxId, | ||
qty: 500, | ||
}, | ||
}); | ||
const expectedDecreasedDelegateData = { | ||
delegatedStake: 500, | ||
start: 0, | ||
vaults: { | ||
[SmartWeave.transaction.id]: { | ||
balance: 500, | ||
start: SmartWeave.block.height, | ||
end: SmartWeave.block.height + DELEGATED_STAKE_UNLOCK_LENGTH, | ||
}, | ||
}, | ||
}; | ||
expect( | ||
state.gateways[stubbedArweaveTxId].delegates['existing-delegate'], | ||
).toEqual(expectedDecreasedDelegateData); | ||
}); | ||
}); | ||
|
||
it('should error if the decrease would lower the delegate stake than the required minimum stake', async () => { | ||
const initialState: IOState = { | ||
...getBaselineState(), | ||
gateways: { | ||
[stubbedArweaveTxId]: { | ||
...stubbedGatewayData, | ||
delegates: { | ||
['existing-delegate']: { | ||
delegatedStake: 1000, | ||
start: 0, | ||
vaults: {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
const error = await decreaseDelegateStake(initialState, { | ||
caller: 'existing-delegate', | ||
input: { | ||
target: stubbedArweaveTxId, | ||
qty: 901, | ||
}, | ||
}).catch((e) => e); | ||
expect(error).toBeInstanceOf(Error); | ||
expect(error.message).toEqual( | ||
expect.stringContaining( | ||
'Remaining delegated stake must be greater than the minimum delegated stake amount.', | ||
), | ||
); | ||
}); | ||
|
||
it('should move the delegate stake to a vault if the decrease amount equals the current amount delegated', async () => { | ||
const initialState: IOState = { | ||
...getBaselineState(), | ||
gateways: { | ||
[stubbedArweaveTxId]: { | ||
...stubbedGatewayData, | ||
delegates: { | ||
['existing-delegate']: { | ||
delegatedStake: 1000, | ||
start: 0, | ||
vaults: {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
const { state } = await decreaseDelegateStake(initialState, { | ||
caller: 'existing-delegate', | ||
input: { | ||
target: stubbedArweaveTxId, | ||
qty: 1000, | ||
}, | ||
}); | ||
const expectedDecreasedDelegateData = { | ||
delegatedStake: 0, | ||
start: 0, | ||
vaults: { | ||
[SmartWeave.transaction.id]: { | ||
balance: 1000, | ||
start: SmartWeave.block.height, | ||
end: SmartWeave.block.height + DELEGATED_STAKE_UNLOCK_LENGTH, | ||
}, | ||
}, | ||
}; | ||
expect( | ||
state.gateways[stubbedArweaveTxId].delegates['existing-delegate'], | ||
).toEqual(expectedDecreasedDelegateData); | ||
}); | ||
}); |
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,49 @@ | ||
import { safeDecreaseDelegateStake } from '../../delegates'; | ||
import { | ||
BlockHeight, | ||
ContractWriteResult, | ||
IOState, | ||
IOToken, | ||
PstAction, | ||
} from '../../types'; | ||
import { getInvalidAjvMessage } from '../../utilities'; | ||
import { validateDecreaseDelegateStake } from '../../validations'; | ||
|
||
export class DecreaseDelegateStake { | ||
target: string; | ||
qty: IOToken; | ||
|
||
constructor(input: any) { | ||
if (!validateDecreaseDelegateStake(input)) { | ||
throw new ContractError( | ||
getInvalidAjvMessage( | ||
validateDecreaseDelegateStake, | ||
input, | ||
'decreaseDelegateStake', | ||
), | ||
); | ||
} | ||
const { target, qty } = input; | ||
this.target = target; | ||
this.qty = new IOToken(qty); | ||
} | ||
} | ||
|
||
export const decreaseDelegateStake = async ( | ||
state: IOState, | ||
{ caller, input }: PstAction, | ||
): Promise<ContractWriteResult> => { | ||
const { gateways } = state; | ||
const { target, qty } = new DecreaseDelegateStake(input); | ||
|
||
safeDecreaseDelegateStake({ | ||
gateways, | ||
fromAddress: caller, | ||
gatewayAddress: target, | ||
qty, | ||
id: SmartWeave.transaction.id, | ||
startHeight: new BlockHeight(SmartWeave.block.height), | ||
}); | ||
|
||
return { state }; | ||
}; |
Oops, something went wrong.