def __init__(self, from_address, proposal_id, base_asset_symbol, quote_asset_symbol, init_price): dict.__init__(self, proposal_id=VarInt(proposal_id), base_asset_symbol=String(base_asset_symbol), quote_asset_symbol=String(quote_asset_symbol), init_price=VarInt(init_price)) self['from'] = Address(from_address)
def __init__(self, from_address, time_lock_id, description, amount, lock_time): dict.__init__( self, time_lock_id=VarInt(time_lock_id), description=String(description), amount=Repeated(amount), lock_time=VarInt(lock_time) ) self['from'] = Address(from_address)
def encode(self, field_id=None): buf = self.object_id() buf += Address(self['from']).encode(1) buf += String(self['symbol']).encode(2) buf += VarInt(self['amount']).encode(3) if field_id is None: return buf else: return make_prefix(field_id, 2) + VarInt(len(buf)).encode() + buf
def from_msg_obj(timelock_data): coins = [] for amount in timelock_data['amount']: coins.append(Token(VarInt(amount['amount']), amount['denom'])) return TimeLock( Address(timelock_data['from']), String(timelock_data.get('description', '')), Repeated(coins), VarInt(timelock_data['lock_time']) )
def __init__(self, sender, order_id, symbol, ordertype, side, price, quantity, timeinforce): dict.__init__(self, sender=Address(sender), symbol=String(symbol), ordertype=VarInt(ordertype), side=VarInt(side), price=VarInt(price), quantity=VarInt(quantity), timeinforce=VarInt(timeinforce)) self['id'] = String(order_id)
def decode(data, field_id): if field_id is not None: prefix = make_prefix(field_id, 2) for i in range(len(prefix)): if prefix[i] != data[i]: return VarInt(0), data data = data[len(prefix):] pub_key, data = PubKeySecp256k1.decode(data, 1) signature, data = Bytes.decode(data, 2) account_number, data = VarInt.decode(data, 3) sequence, data = VarInt.decode(data, 4) return BnbSignature(pub_key, signature, account_number, sequence), data
def encode(self, field_id=None): # buf = bytes.fromhex('ACCBA2DE') buf = self.object_id() buf += self['title'].encode(1) buf += self['description'].encode(2) buf += VarInt(proposal_type_to_int[self['proposal_type']]).encode(3) buf += self['proposer'].encode(4) buf += self['initial_deposit'].encode(5) buf += self['voting_period'].encode(6) if field_id is None: return buf else: return make_prefix(field_id, 2) + VarInt(len(buf)).encode() + buf
def __init__(self, from_address, symbol, amount): dict.__init__( self, amount=VarInt(amount), symbol=String(symbol) ) self['from'] = Address(from_address)
def encode(self, field_id=None): buf = self.object_id() buf += self['pub_key'].encode() if field_id is None: return buf else: return make_prefix(field_id, 2) + VarInt(len(buf)).encode() + buf
def decode(data, field_id=None): if field_id is not None: prefix = make_prefix(field_id, 2) for i in range(len(prefix)): if prefix[i] != data[i]: return None, data data = data[len(field_id):] assert data[0:4] == DexList.object_id() data = data[4:] from_address, data = Address.decode(data, 1) proposal_id, data = VarInt.decode(data, 2) base_asset_symbol, data = String.decode(data, 3) quote_asset_symbol, data = String.decode(data, 4) init_price, data = VarInt.decode(data, 5) return DexList(from_address, proposal_id, base_asset_symbol, quote_asset_symbol, init_price)
def __init__(self, from_address, name, symbol, total_supply, mintable): dict.__init__( self, name=String(name), symbol=String(symbol), total_supply=VarInt(total_supply), mintable=bool(mintable)) self['from'] = Address(from_address)
def encode(self, field_id=None): buf = self.object_id() buf += self['inputs'].encode(1) buf += self['outputs'].encode(2) if field_id is None: return buf else: return make_prefix(field_id, 2) + VarInt(len(buf)).encode() + buf
def encode(self, field_id=None): buf = self.object_id() buf += self['from'].encode(1) buf += self['time_lock_id'].encode(2) if field_id is None: return buf else: return make_prefix(field_id, 2) + VarInt(len(buf)).encode() + buf
def encode(self): buf = self.object_id() buf += self['msgs'].encode(1) buf += self['signatures'].encode(2) buf += self['memo'].encode(3) buf += self['source'].encode(4) if self['data'] is not None: buf += self['data'].encode(5) return VarInt(len(buf)).encode() + buf
def encode(self, field_id=None): buf = self.object_id() buf += self['proposal_id'].encode(1) buf += self['voter'].encode(2) buf += self['option'].encode(3) if field_id is None: return buf else: return make_prefix(field_id, 2) + VarInt(len(buf)).encode() + buf
def encode(self, field_id=None): buf = self.object_id() buf += self['sender'].encode(1) buf += self['symbol'].encode(2) buf += self['refid'].encode(3) if field_id is None: return buf else: return make_prefix(field_id, 2) + VarInt(len(buf)).encode() + buf
def encode(self, field_id=None): buf = self.object_id() buf += self['from'].encode(1) buf += self['description'].encode(2) buf += self['amount'].encode(3) buf += self['lock_time'].encode(4) if field_id is None: return buf else: return make_prefix(field_id, 2) + VarInt(len(buf)).encode() + buf
def from_msg_obj(timerelock_data): amounts = [] for amount in timerelock_data['amount']: amounts.append(Token(VarInt(amount['amount']), String(amount['denom']))) return TimeRelock( timerelock_data['from'], timerelock_data['time_lock_id'], timerelock_data['description'], amounts, timerelock_data['lock_time'] )
def encode(self, field_id=None): buf = self.object_id() buf += self['from'].encode(1) buf += self['proposal_id'].encode(2) buf += self['base_asset_symbol'].encode(3) buf += self['quote_asset_symbol'].encode(4) buf += self['init_price'].encode(5) if field_id is None: return buf else: return make_prefix(field_id, 2) + VarInt(len(buf)).encode() + buf
def decode(data, field_id=None): if field_id is not None: prefix = make_prefix(field_id, 2) for i in range(len(prefix)): if prefix[i] != data[i]: return VarInt(0), data data = data[len(prefix):] assert data[0:4] == PubKeySecp256k1.object_id() data = data[4:] pub_key, data = Bytes.decode(data) return PubKeySecp256k1(pub_key), data
def encode(self, field_id=None): buf = self.object_id() buf += self['from'].encode(1) buf += self['name'].encode(2) buf += self['symbol'].encode(3) buf += self['total_supply'].encode(4) buf += Bool(self['mintable']).encode(5) if field_id is None: return buf else: return make_prefix(field_id, 2) + VarInt(len(buf)).encode() + buf
def decode(data, field_id=None): if field_id is not None: prefix = make_prefix(field_id, 2) for i in range(len(prefix)): if prefix[i] != data[i]: return None, data data = data[len(field_id):] from_address, data = Address.decode(data, 1) symbol, data = String.decode(data, 2) amount, data = VarInt.decode(data, 3) return Mint(from_address, symbol, amount)
def decode(klass, data, field_id=None): if field_id is not None: prefix = make_prefix(field_id, 2) for i in range(len(prefix)): if prefix[i] != data[i]: return None, data data = data[len(field_id):] assert data[0:4] == klass.object_id() data = data[4:] from_address, data = Address.decode(data, 1) symbol, data = String.decode(data, 2) amount, data = VarInt.decode(data, 3) return klass(from_address, symbol, amount), data
def encode(self, field_id=None): buf = self.object_id() buf += self['sender'].encode(1) buf += self['id'].encode(2) buf += self['symbol'].encode(3) buf += self['ordertype'].encode(4) buf += self['side'].encode(5) buf += self['price'].encode(6) buf += self['quantity'].encode(7) buf += self['timeinforce'].encode(8) if field_id is None: return buf else: return make_prefix(field_id, 2) + VarInt(len(buf)).encode() + buf
def decode(data, field_id=None): if field_id is not None: prefix = make_prefix(field_id, 2) for i in range(len(prefix)): if prefix[i] != data[i]: return None, data data = data[len(field_id):] assert data[0:4] == Proposal.object_id() data = data[4:] title, data = String.decode(data, 1) description, data = String.decode(data, 2) proposal_type, data = VarInt.decode(data, 3) proposer, data = Address.decode(data, 4) initial_deposit, data = Repeated.decode(data, 5, Token) voting_period, data = StringVarInt.decode(data, 6) return Proposal(title, description, proposal_type, proposer, initial_deposit, voting_period)
def decode(data, field_id=None, hrp='bnb'): if field_id is not None: prefix = make_prefix(field_id, 2) for i in range(len(prefix)): if prefix[i] != data[i]: return None, data data = data[len(field_id):] assert data[0:4] == Issue.object_id() data = data[4:] address, data = Address.decode(data, 1, hrp) name, data = String.decode(data, 2) symbol, data = String.decode(data, 3) total_supply, data = VarInt.decode(data, 4) mintable, data = Bool.decode(data, 5) mintable = True if mintable else 0 return Issue(address, name, symbol, total_supply, mintable), data
def encode(self, field_id=None): return VarInt.encode(vote_option_to_varint[self], field_id)
def from_msg_obj(proposal_data): initial_deposit = Repeated([]) for deposit in proposal_data['initial_deposit']: initial_deposit.append(StringToken(deposit['amount'], deposit['denom'])), return Proposal( proposal_data['title'], proposal_data['description'], proposal_data['proposal_type'], proposal_data['proposer'], initial_deposit, proposal_data['voting_period'] ) vote_option_to_varint = { "Yes": VarInt(1), "Abstain": VarInt(2), "No": VarInt(3), "NoWithVeto": VarInt(4) } class VoteOption(String): """ A String enum for JSON, a VarInt for encoding """ def encode(self, field_id=None): return VarInt.encode(vote_option_to_varint[self], field_id) class Vote(Amino):
def test_decode_varint(): for val in [5, 257, 1000000, 0]: encoded = VarInt(val).encode() decoded, remaining = VarInt.decode(encoded) assert decoded == val assert remaining == b''
def __init__(self, from_address, time_lock_id): dict.__init__( self, time_lock_id=VarInt(time_lock_id) ) self['from'] = Address(from_address)