use of com.radixdlt.application.tokens.state.PreparedUnstakeOwnership in project radixdlt by radixdlt.
the class StakingConstraintScryptV4 method main.
@Override
public void main(Loader os) {
os.substate(new SubstateDefinition<>(PreparedStake.class, SubstateTypeId.PREPARED_STAKE.id(), buf -> {
REFieldSerialization.deserializeReservedByte(buf);
var delegate = REFieldSerialization.deserializeKey(buf);
var owner = REFieldSerialization.deserializeAccountREAddr(buf);
var amount = REFieldSerialization.deserializeNonZeroUInt256(buf);
return new PreparedStake(amount, owner, delegate);
}, (s, buf) -> {
REFieldSerialization.serializeReservedByte(buf);
REFieldSerialization.serializeKey(buf, s.delegateKey());
REFieldSerialization.serializeREAddr(buf, s.owner());
buf.put(s.amount().toByteArray());
}));
os.substate(new SubstateDefinition<>(PreparedUnstakeOwnership.class, SubstateTypeId.PREPARED_UNSTAKE.id(), buf -> {
REFieldSerialization.deserializeReservedByte(buf);
var delegate = REFieldSerialization.deserializeKey(buf);
var owner = REFieldSerialization.deserializeAccountREAddr(buf);
var amount = REFieldSerialization.deserializeNonZeroUInt256(buf);
return new PreparedUnstakeOwnership(delegate, owner, amount);
}, (s, buf) -> {
REFieldSerialization.serializeReservedByte(buf);
REFieldSerialization.serializeKey(buf, s.delegateKey());
REFieldSerialization.serializeREAddr(buf, s.owner());
buf.put(s.amount().toByteArray());
}));
defineStaking(os);
}
use of com.radixdlt.application.tokens.state.PreparedUnstakeOwnership in project radixdlt by radixdlt.
the class UnstakeOwnershipConstructor method construct.
@Override
public void construct(UnstakeOwnership action, TxBuilder txBuilder) throws TxBuilderException {
var buf = ByteBuffer.allocate(2 + ECPublicKey.COMPRESSED_BYTES + (1 + ECPublicKey.COMPRESSED_BYTES));
buf.put(SubstateTypeId.STAKE_OWNERSHIP.id());
buf.put((byte) 0);
buf.put(action.fromDelegate().getCompressedBytes());
buf.put(action.accountAddr().getBytes());
if (buf.hasRemaining()) {
// Sanity
throw new IllegalStateException();
}
var index = SubstateIndex.create(buf.array(), StakeOwnership.class);
var change = txBuilder.downFungible(index, p -> p.owner().equals(action.accountAddr()) && p.delegateKey().equals(action.fromDelegate()), action.amount(), available -> {
var from = StakeOwnershipBucket.from(action.fromDelegate(), action.accountAddr());
return new NotEnoughResourcesException(from, action.amount(), available);
});
if (!change.isZero()) {
txBuilder.up(new StakeOwnership(action.fromDelegate(), action.accountAddr(), change));
}
txBuilder.up(new PreparedUnstakeOwnership(action.fromDelegate(), action.accountAddr(), action.amount()));
txBuilder.end();
}
use of com.radixdlt.application.tokens.state.PreparedUnstakeOwnership in project radixdlt by radixdlt.
the class UnstakeTokensConstructorV2 method construct.
@Override
public void construct(UnstakeTokens action, TxBuilder txBuilder) throws TxBuilderException {
var validatorStake = txBuilder.find(ValidatorStakeData.class, action.fromDelegate());
var ownershipAmt = action.amount().multiply(validatorStake.totalOwnership()).divide(validatorStake.amount());
// TODO: construct this in substate definition
var buf = ByteBuffer.allocate(2 + ECPublicKey.COMPRESSED_BYTES + (1 + ECPublicKey.COMPRESSED_BYTES));
buf.put(SubstateTypeId.STAKE_OWNERSHIP.id());
buf.put((byte) 0);
buf.put(action.fromDelegate().getCompressedBytes());
buf.put(action.accountAddr().getBytes());
if (buf.hasRemaining()) {
// Sanity
throw new IllegalStateException();
}
var index = SubstateIndex.create(buf.array(), StakeOwnership.class);
var change = txBuilder.downFungible(index, p -> p.owner().equals(action.accountAddr()) && p.delegateKey().equals(action.fromDelegate()), ownershipAmt, available -> {
var from = StakeOwnershipBucket.from(action.fromDelegate(), action.accountAddr());
return new NotEnoughResourcesException(from, action.amount(), available);
});
if (!change.isZero()) {
txBuilder.up(new StakeOwnership(action.fromDelegate(), action.accountAddr(), change));
}
txBuilder.up(new PreparedUnstakeOwnership(action.fromDelegate(), action.accountAddr(), ownershipAmt));
txBuilder.end();
}
Aggregations