Search in sources :

Example 6 with TokensInAccount

use of com.radixdlt.application.tokens.state.TokensInAccount in project radixdlt by radixdlt.

the class TxBuilder method putFeeReserve.

public <T extends ResourceInBucket, X extends Exception> void putFeeReserve(REAddr feePayer, UInt256 amount, Function<UInt256, X> exceptionSupplier) throws X {
    var buf = ByteBuffer.allocate(2 + 1 + ECPublicKey.COMPRESSED_BYTES);
    buf.put(SubstateTypeId.TOKENS.id());
    buf.put((byte) 0);
    buf.put(feePayer.getBytes());
    var index = SubstateIndex.create(buf.array(), TokensInAccount.class);
    // Take
    var remainder = downFungible(index, p -> p.resourceAddr().isNativeToken() && p.holdingAddress().equals(feePayer), amount, exceptionSupplier);
    lowLevelBuilder.syscall(Syscall.FEE_RESERVE_PUT, amount);
    if (!remainder.isZero()) {
        up(new TokensInAccount(feePayer, REAddr.ofNativeToken(), remainder));
    }
    this.feeReservePut = amount;
}
Also used : TokensInAccount(com.radixdlt.application.tokens.state.TokensInAccount)

Example 7 with TokensInAccount

use of com.radixdlt.application.tokens.state.TokensInAccount in project radixdlt by radixdlt.

the class TokensConstraintScryptV3 method registerParticles.

private void registerParticles(Loader os) {
    os.substate(new SubstateDefinition<>(TokenResource.class, SubstateTypeId.TOKEN_RESOURCE.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var addr = REFieldSerialization.deserializeResourceAddr(buf);
        var granularity = REFieldSerialization.deserializeNonZeroUInt256(buf);
        if (!granularity.equals(UInt256.ONE)) {
            throw new DeserializeException("Granularity must be one.");
        }
        var isMutable = REFieldSerialization.deserializeBoolean(buf);
        var minter = REFieldSerialization.deserializeOptionalKey(buf);
        return new TokenResource(addr, granularity, isMutable, minter.orElse(null));
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeREAddr(buf, s.addr());
        REFieldSerialization.serializeUInt256(buf, UInt256.ONE);
        REFieldSerialization.serializeBoolean(buf, s.isMutable());
        REFieldSerialization.serializeOptionalKey(buf, s.optionalOwner());
    }));
    os.substate(new SubstateDefinition<>(TokenResourceMetadata.class, SubstateTypeId.TOKEN_RESOURCE_METADATA.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var addr = REFieldSerialization.deserializeResourceAddr(buf);
        var symbol = REFieldSerialization.deserializeString(buf);
        var name = REFieldSerialization.deserializeString(buf);
        var description = REFieldSerialization.deserializeString(buf);
        var url = REFieldSerialization.deserializeUrl(buf);
        var iconUrl = REFieldSerialization.deserializeUrl(buf);
        return new TokenResourceMetadata(addr, symbol, name, description, iconUrl, url);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeREAddr(buf, s.addr());
        REFieldSerialization.serializeString(buf, s.symbol());
        REFieldSerialization.serializeString(buf, s.name());
        REFieldSerialization.serializeString(buf, s.description());
        REFieldSerialization.serializeString(buf, s.url());
        REFieldSerialization.serializeString(buf, s.iconUrl());
    }));
    os.substate(new SubstateDefinition<>(TokensInAccount.class, SubstateTypeId.TOKENS.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var holdingAddr = REFieldSerialization.deserializeAccountREAddr(buf);
        var addr = REFieldSerialization.deserializeResourceAddr(buf);
        var amount = REFieldSerialization.deserializeNonZeroUInt256(buf);
        return new TokensInAccount(holdingAddr, addr, amount);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeREAddr(buf, s.holdingAddress());
        REFieldSerialization.serializeREAddr(buf, s.resourceAddr());
        buf.put(s.amount().toByteArray());
    }));
}
Also used : SubstateTypeId(com.radixdlt.atom.SubstateTypeId) TokenResourceMetadata(com.radixdlt.application.tokens.state.TokenResourceMetadata) Loader(com.radixdlt.atomos.Loader) REFieldSerialization(com.radixdlt.atom.REFieldSerialization) TokenResource(com.radixdlt.application.tokens.state.TokenResource) Set(java.util.Set) TokensInAccount(com.radixdlt.application.tokens.state.TokensInAccount) SubstateDefinition(com.radixdlt.atomos.SubstateDefinition) StandardCharsets(java.nio.charset.StandardCharsets) com.radixdlt.constraintmachine(com.radixdlt.constraintmachine) ResourceCreatedEvent(com.radixdlt.constraintmachine.REEvent.ResourceCreatedEvent) ReservedSymbolException(com.radixdlt.constraintmachine.exceptions.ReservedSymbolException) SystemConstraintScrypt(com.radixdlt.application.system.scrypt.SystemConstraintScrypt) ConstraintScrypt(com.radixdlt.atomos.ConstraintScrypt) ProcedureException(com.radixdlt.constraintmachine.exceptions.ProcedureException) Pattern(java.util.regex.Pattern) DeserializeException(com.radixdlt.serialization.DeserializeException) UInt256(com.radixdlt.utils.UInt256) TokenResource(com.radixdlt.application.tokens.state.TokenResource) TokenResourceMetadata(com.radixdlt.application.tokens.state.TokenResourceMetadata) TokensInAccount(com.radixdlt.application.tokens.state.TokensInAccount) DeserializeException(com.radixdlt.serialization.DeserializeException)

Aggregations

TokensInAccount (com.radixdlt.application.tokens.state.TokensInAccount)7 SystemConstraintScrypt (com.radixdlt.application.system.scrypt.SystemConstraintScrypt)1 TokenResource (com.radixdlt.application.tokens.state.TokenResource)1 TokenResourceMetadata (com.radixdlt.application.tokens.state.TokenResourceMetadata)1 REFieldSerialization (com.radixdlt.atom.REFieldSerialization)1 SubstateTypeId (com.radixdlt.atom.SubstateTypeId)1 ConstraintScrypt (com.radixdlt.atomos.ConstraintScrypt)1 Loader (com.radixdlt.atomos.Loader)1 SubstateDefinition (com.radixdlt.atomos.SubstateDefinition)1 com.radixdlt.constraintmachine (com.radixdlt.constraintmachine)1 ResourceCreatedEvent (com.radixdlt.constraintmachine.REEvent.ResourceCreatedEvent)1 ProcedureException (com.radixdlt.constraintmachine.exceptions.ProcedureException)1 ReservedSymbolException (com.radixdlt.constraintmachine.exceptions.ReservedSymbolException)1 DeserializeException (com.radixdlt.serialization.DeserializeException)1 UInt256 (com.radixdlt.utils.UInt256)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1