Exemplo n.º 1
0
def createRingSettlementBlock(state, data):
    block = RingSettlementBlock()
    block.onchainDataAvailability = data["onchainDataAvailability"]
    block.exchangeID = state.exchangeID
    block.merkleRootBefore = str(state.getRoot())
    block.timestamp = int(data["timestamp"])
    block.protocolTakerFeeBips = int(data["protocolTakerFeeBips"])
    block.protocolMakerFeeBips = int(data["protocolMakerFeeBips"])
    block.operatorAccountID = int(data["operatorAccountID"])

    context = Context(block.operatorAccountID, block.timestamp,
                      block.protocolTakerFeeBips, block.protocolMakerFeeBips)

    # Protocol fee payment / Operator payment
    accountBefore_P = copyAccountInfo(state.getAccount(0))

    for ringInfo in data["rings"]:
        ring = ringFromJSON(ringInfo, state)
        ringSettlement = state.settleRing(context, ring)
        block.ringSettlements.append(ringSettlement)

    # Protocol fee payment
    rootBefore = state._accountsTree._root
    proof = state._accountsTree.createProof(0)
    state.updateAccountTree(0)
    accountAfter = copyAccountInfo(state.getAccount(0))
    rootAfter = state._accountsTree._root
    block.accountUpdate_P = AccountUpdateData(0, proof, rootBefore, rootAfter,
                                              accountBefore_P, accountAfter)

    # Operator payments
    account = state.getAccount(context.operatorAccountID)
    rootBefore = state._accountsTree._root
    accountBefore = copyAccountInfo(state.getAccount(
        context.operatorAccountID))
    proof = state._accountsTree.createProof(context.operatorAccountID)
    for ringSettlement in block.ringSettlements:
        ringSettlement.balanceUpdateA_O = account.updateBalance(
            ringSettlement.ring.orderA.tokenB, ringSettlement.balanceDeltaA_O)
        ringSettlement.balanceUpdateB_O = account.updateBalance(
            ringSettlement.ring.orderB.tokenB, ringSettlement.balanceDeltaB_O)
    account.nonce += 1
    state.updateAccountTree(context.operatorAccountID)
    accountAfter = copyAccountInfo(state.getAccount(context.operatorAccountID))
    rootAfter = state._accountsTree._root
    block.accountUpdate_O = AccountUpdateData(context.operatorAccountID, proof,
                                              rootBefore, rootAfter,
                                              accountBefore, accountAfter)

    block.merkleRootAfter = str(state.getRoot())
    return block
Exemplo n.º 2
0
def createOffchainWithdrawalBlock(state, data):
    block = OffchainWithdrawalBlock()
    block.onchainDataAvailability = data["onchainDataAvailability"]
    block.exchangeID = state.exchangeID
    block.merkleRootBefore = str(state.getRoot())
    block.operatorAccountID = int(data["operatorAccountID"])

    for withdrawalInfo in data["withdrawals"]:
        accountID = int(withdrawalInfo["accountID"])
        tokenID = int(withdrawalInfo["tokenID"])
        amount = int(withdrawalInfo["amount"])
        feeTokenID = int(withdrawalInfo["feeTokenID"])
        fee = int(withdrawalInfo["fee"])

        withdrawal = state.offchainWithdraw(block.exchangeID, accountID, tokenID, amount,
                                            block.operatorAccountID, feeTokenID, fee)
        withdrawal.signature = withdrawalInfo["signature"]
        block.withdrawals.append(withdrawal)

    # Operator payments
    account = state.getAccount(block.operatorAccountID)
    rootBefore = state._accountsTree._root
    accountBefore = copyAccountInfo(state.getAccount(block.operatorAccountID))
    proof = state._accountsTree.createProof(block.operatorAccountID)
    for withdrawal in block.withdrawals:
        withdrawal.balanceUpdateF_O = account.updateBalance(withdrawal.feeTokenID, int(withdrawal.feeValue))
    state.updateAccountTree(block.operatorAccountID)
    accountAfter = copyAccountInfo(state.getAccount(block.operatorAccountID))
    rootAfter = state._accountsTree._root
    block.accountUpdate_O = AccountUpdateData(block.operatorAccountID, proof, rootBefore, rootAfter, accountBefore, accountAfter)

    block.merkleRootAfter = str(state.getRoot())
    return block
Exemplo n.º 3
0
def createRingSettlementBlock(state, data):
    block = RingSettlementBlock()
    block.onchainDataAvailability = data["onchainDataAvailability"]
    block.realmID = state.realmID
    block.merkleRootBefore = str(state.getRoot())
    block.timestamp = int(data["timestamp"])
    block.operatorAccountID = int(data["operatorAccountID"])

    context = Context(block.operatorAccountID, block.timestamp)

    # Operator payment
    rootBefore = state._accountsTree._root
    accountBefore = copyAccountInfo(state.getAccount(block.operatorAccountID))

    for ringInfo in data["rings"]:
        ring = ringFromJSON(ringInfo, state)
        ringSettlement = state.settleRing(context, ring)
        block.ringSettlements.append(ringSettlement)

    # Operator payment
    proof = state._accountsTree.createProof(block.operatorAccountID)
    state.updateAccountTree(block.operatorAccountID)
    accountAfter = copyAccountInfo(state.getAccount(block.operatorAccountID))
    rootAfter = state._accountsTree._root
    block.accountUpdate_O = AccountUpdateData(block.operatorAccountID, proof,
                                              rootBefore, rootAfter,
                                              accountBefore, accountAfter)

    block.merkleRootAfter = str(state.getRoot())
    return block
Exemplo n.º 4
0
def createOrderCancellationBlock(state, data):
    block = OrderCancellationBlock()
    block.onchainDataAvailability = data["onchainDataAvailability"]
    block.exchangeID = state.exchangeID
    block.merkleRootBefore = str(state.getRoot())
    block.operatorAccountID = int(data["operatorAccountID"])

    for cancelInfo in data["cancels"]:
        accountID = int(cancelInfo["accountID"])
        orderTokenID = int(cancelInfo["orderTokenID"])
        orderID = int(cancelInfo["orderID"])
        feeTokenID = int(cancelInfo["feeTokenID"])
        fee = int(cancelInfo["fee"])
        label = int(cancelInfo["label"])

        cancel = state.cancelOrder(block.exchangeID, accountID, orderTokenID, orderID,
                                   block.operatorAccountID, feeTokenID, fee, label)

        cancel.signature = cancelInfo["signature"]
        block.cancels.append(cancel)

    # Operator payments
    account = state.getAccount(block.operatorAccountID)
    rootBefore = state._accountsTree._root
    accountBefore = copyAccountInfo(state.getAccount(block.operatorAccountID))
    proof = state._accountsTree.createProof(block.operatorAccountID)
    for cancel in block.cancels:
        cancel.balanceUpdateF_O = account.updateBalance(cancel.feeTokenID, int(cancel.feeValue))
    state.updateAccountTree(block.operatorAccountID)
    accountAfter = copyAccountInfo(state.getAccount(block.operatorAccountID))
    rootAfter = state._accountsTree._root
    block.accountUpdate_O = AccountUpdateData(block.operatorAccountID, proof, rootBefore, rootAfter, accountBefore, accountAfter)

    block.merkleRootAfter = str(state.getRoot())
    return block
Exemplo n.º 5
0
def createInternalTransferBlock(state, data):
    block = InternalTransferBlock()
    block.onchainDataAvailability = data["onchainDataAvailability"]
    block.exchangeID = state.exchangeID
    block.merkleRootBefore = str(state.getRoot())
    block.operatorAccountID = int(data["operatorAccountID"])

    numConditionalTransfers = 0
    for transInfo in data["transfers"]:
        accountFromID = int(transInfo["accountFromID"])
        accountToID = int(transInfo["accountToID"])
        transTokenID = int(transInfo["transTokenID"])
        amount = int(transInfo["amount"])
        feeTokenID = int(transInfo["feeTokenID"])
        fee = int(transInfo["fee"])
        type = int(transInfo["type"])

        transfer = state.internalTransfer(block.exchangeID, block.operatorAccountID,
                                        accountFromID, accountToID, transTokenID,
                                        amount, feeTokenID, fee, type)

        if type > 0:
            numConditionalTransfers += 1

        transfer.signature = transInfo["signature"]
        transfer.numConditionalTransfersAfter = numConditionalTransfers
        block.transfers.append(transfer)

    # Operator payments
    account = state.getAccount(block.operatorAccountID)
    rootBefore = state._accountsTree._root
    accountBefore = copyAccountInfo(state.getAccount(block.operatorAccountID))
    proof = state._accountsTree.createProof(block.operatorAccountID)
    for transfer in block.transfers:
        transfer.balanceUpdateF_O = account.updateBalance(transfer.feeTokenID, int(transfer.feeValue))
    state.updateAccountTree(block.operatorAccountID)
    accountAfter = copyAccountInfo(state.getAccount(block.operatorAccountID))
    rootAfter = state._accountsTree._root
    block.accountUpdate_O = AccountUpdateData(block.operatorAccountID, proof, rootBefore, rootAfter, accountBefore, accountAfter)

    block.merkleRootAfter = str(state.getRoot())
    return block
Exemplo n.º 6
0
def createOffchainWithdrawalBlock(state, data):
    block = OffchainWithdrawalBlock()
    block.onchainDataAvailability = data["onchainDataAvailability"]
    block.realmID = state.realmID
    block.merkleRootBefore = str(state.getRoot())
    block.operatorAccountID = int(data["operatorAccountID"])

    # Operator payment
    rootBefore = state._accountsTree._root
    accountBefore = copyAccountInfo(state.getAccount(block.operatorAccountID))

    for withdrawalInfo in data["withdrawals"]:
        accountID = int(withdrawalInfo["accountID"])
        tokenID = int(withdrawalInfo["tokenID"])
        amount = int(withdrawalInfo["amount"])
        walletAccountID = int(withdrawalInfo["walletAccountID"])
        feeTokenID = int(withdrawalInfo["feeTokenID"])
        fee = int(withdrawalInfo["fee"])
        walletSplitPercentage = int(withdrawalInfo["walletSplitPercentage"])

        withdrawal = state.offchainWithdraw(block.realmID, accountID, tokenID,
                                            amount, block.operatorAccountID,
                                            walletAccountID, feeTokenID, fee,
                                            walletSplitPercentage)
        block.withdrawals.append(withdrawal)

    # Operator payment
    proof = state._accountsTree.createProof(block.operatorAccountID)
    state.updateAccountTree(block.operatorAccountID)
    accountAfter = copyAccountInfo(state.getAccount(block.operatorAccountID))
    rootAfter = state._accountsTree._root
    block.accountUpdate_O = AccountUpdateData(block.operatorAccountID, proof,
                                              rootBefore, rootAfter,
                                              accountBefore, accountAfter)

    block.merkleRootAfter = str(state.getRoot())
    return block
Exemplo n.º 7
0
def createOrderCancellationBlock(state, data):
    block = OrderCancellationBlock()
    block.onchainDataAvailability = data["onchainDataAvailability"]
    block.realmID = state.realmID
    block.merkleRootBefore = str(state.getRoot())
    block.operatorAccountID = int(data["operatorAccountID"])

    # Operator payment
    rootBefore = state._accountsTree._root
    accountBefore = copyAccountInfo(state.getAccount(block.operatorAccountID))

    for cancelInfo in data["cancels"]:
        accountID = int(cancelInfo["accountID"])
        orderTokenID = int(cancelInfo["orderTokenID"])
        orderID = int(cancelInfo["orderID"])
        walletAccountID = int(cancelInfo["walletAccountID"])
        feeTokenID = int(cancelInfo["feeTokenID"])
        fee = int(cancelInfo["fee"])
        walletSplitPercentage = int(cancelInfo["walletSplitPercentage"])

        block.cancels.append(
            state.cancelOrder(block.realmID, accountID, orderTokenID, orderID,
                              walletAccountID, block.operatorAccountID,
                              feeTokenID, fee, walletSplitPercentage))

    # Operator payment
    proof = state._accountsTree.createProof(block.operatorAccountID)
    state.updateAccountTree(block.operatorAccountID)
    accountAfter = copyAccountInfo(state.getAccount(block.operatorAccountID))
    rootAfter = state._accountsTree._root
    block.accountUpdate_O = AccountUpdateData(block.operatorAccountID, proof,
                                              rootBefore, rootAfter,
                                              accountBefore, accountAfter)

    block.merkleRootAfter = str(state.getRoot())
    return block
Exemplo n.º 8
0
def createBlock(state, data):
    block = Block()
    block.exchange = str(data["exchange"])
    block.merkleRootBefore = str(state.getRoot())
    block.timestamp = int(data["timestamp"])
    block.protocolTakerFeeBips = int(data["protocolTakerFeeBips"])
    block.protocolMakerFeeBips = int(data["protocolMakerFeeBips"])
    block.operatorAccountID = int(data["operatorAccountID"])

    context = Context(block.operatorAccountID, block.timestamp,
                      block.protocolTakerFeeBips, block.protocolMakerFeeBips)

    # Protocol fee payment / index
    accountBefore_P = copyAccountInfo(state.getAccount(0))
    accountBefore_I = copyAccountInfo(state.getAccount(1))

    for transactionInfo in data["transactions"]:
        txType = transactionInfo["txType"]
        if txType == "Noop":
            transaction = GeneralObject()
        if txType == "SpotTrade":
            transaction = ringFromJSON(transactionInfo, state)
        if txType == "Transfer":
            transaction = transferFromJSON(transactionInfo)
        if txType == "Withdraw":
            transaction = withdrawFromJSON(transactionInfo)
        if txType == "Deposit":
            transaction = depositFromJSON(transactionInfo)
        if txType == "AccountUpdate":
            transaction = accountUpdateFromJSON(transactionInfo)

        transaction.txType = txType
        tx = state.executeTransaction(context, transaction)
        txWitness = GeneralObject()
        txWitness.witness = tx.witness
        if txType == "Noop":
            txWitness.noop = tx.input
        if txType == "SpotTrade":
            txWitness.spotTrade = tx.input
        if txType == "Transfer":
            txWitness.transfer = tx.input
        if txType == "Withdraw":
            txWitness.withdraw = tx.input
        if txType == "Deposit":
            txWitness.deposit = tx.input
        if txType == "AccountUpdate":
            txWitness.accountUpdate = tx.input
        txWitness.witness.numConditionalTransactionsAfter = context.numConditionalTransactions
        block.transactions.append(txWitness)

    # Protocol fees
    rootBefore = state._accountsTree._root
    proof = state._accountsTree.createProof(0)
    state.updateAccountTree(0)
    accountAfter = copyAccountInfo(state.getAccount(0))
    rootAfter = state._accountsTree._root
    block.accountUpdate_P = AccountUpdateData(0, proof, rootBefore, rootAfter,
                                              accountBefore_P, accountAfter)

    # Index
    rootBefore = state._accountsTree._root
    proof = state._accountsTree.createProof(1)
    state.updateAccountTree(1)
    accountAfter = copyAccountInfo(state.getAccount(1))
    rootAfter = state._accountsTree._root
    block.accountUpdate_I = AccountUpdateData(1, proof, rootBefore, rootAfter,
                                              accountBefore_I, accountAfter)

    # Operator
    account = state.getAccount(context.operatorAccountID)
    rootBefore = state._accountsTree._root
    accountBefore = copyAccountInfo(state.getAccount(
        context.operatorAccountID))
    proof = state._accountsTree.createProof(context.operatorAccountID)
    account.nonce += 1
    state.updateAccountTree(context.operatorAccountID)
    accountAfter = copyAccountInfo(state.getAccount(context.operatorAccountID))
    rootAfter = state._accountsTree._root
    block.accountUpdate_O = AccountUpdateData(context.operatorAccountID, proof,
                                              rootBefore, rootAfter,
                                              accountBefore, accountAfter)

    block.merkleRootAfter = str(state.getRoot())
    return block