Ejemplo n.º 1
0
def burn(tokenId, amount):
    Require(_whenNotPaused())
    RequireWitness(Admin)
    # make sure the tokenId has been created in property contract <=> name of tokenId is NOT None
    propertyReversedHash = getPropertyReversedHash()
    balance = DynamicAppCall(propertyReversedHash, "balanceOf",
                             [SelfContractAddress])
    Require(balance > 0)
    Require(amount > 0 and amount <= balance)
    # burn tokens
    burnRes = DynamicAppCall(propertyReversedHash, "burnToken",
                             [SelfContractAddress, tokenId, amount])
    Require(burnRes)
    Notify(["burn", tokenId, amount])
    return True
Ejemplo n.º 2
0
def lock(fromAddress, fromAsset, toChainId, toAddress, amount, fee, id):
    """
    :param fromAddress: from Account
    :param fromAsset: asset address, not hash, should be reversed hash
    :param toChainId: !=3
    :param toAddress: bytearray
    :param amount: > fee
    :param fee: >= 0
    :param id: like uin in eth
    :return:
    """
    assert (CheckWitness(fromAddress))
    assert (not ifPause())
    assert (toChainId != 0 and toChainId != OntChainIdOnPoly)
    assert (len(toAddress) > 0)
    assert (amount > fee)
    assert (fee > 0)

    lockProxy = getLockProxy()
    toAssethash = DynamicAppCall(lockProxy, 'getAssetHash', [fromAsset, toChainId])
    assert (len(toAssethash) > 0)
    
    # transfer fee to fee collector
    feeCollector = getFeeCollector()
    assert (len(feeCollector) == 20)

    if (fromAsset != ONT_ADDRESS and fromAsset != ONG_ADDRESS):
        # approve and transfer fee
        res = DynamicAppCall(fromAsset, "approve", [fromAddress, lockProxy, amount - fee])
        assert (res == True)
        res = DynamicAppCall(fromAsset, 'transfer', [fromAddress, feeCollector, fee])
        assert (res == True)
    else:
        # native token dont need to approve,just transfer fee
        param = state(fromAddress, feeCollector, fee)
        res = Invoke(0, fromAsset, 'transfer', [param])
        if res and res == b'\x01':
            flag = True
        else:
            flag = False
        assert (flag == True)
    
    # call lock-proxy contract lock
    res = DynamicAppCall(lockProxy, 'lock', [fromAsset, fromAddress, toChainId, toAddress, amount - fee])
    assert (res == True)
    
    PolyWrapperLock(fromAsset, fromAddress, toChainId, toAddress, amount - fee, fee, id)
    return True
Ejemplo n.º 3
0
def _transferLinkFromContact(link, toAcct, amount):
    params = [ContractAddress, toAcct, amount]
    res = DynamicAppCall(link, 'transfer', params)
    if res and res == b'\x01':
        return True
    else:
        return False
Ejemplo n.º 4
0
def callBackFunction(callbackAddress, callbackFunctionId, requestId, data):
    params = [requestId, data]
    res = DynamicAppCall(callbackAddress, callbackFunctionId, params)
    if res and res == b'\x01':
        return True
    else:
        return False
Ejemplo n.º 5
0
def _transferOPE4FromContract(_oep4ReversedAddress, _to, _amount):
    param = [SelfContractAddress, _to, _amount]
    res = DynamicAppCall(_oep4ReversedAddress, "transfer", param)
    if res and res == b'\x01':
        return True
    else:
        return False
Ejemplo n.º 6
0
def setGP(gpId, gpLimit, price, gpContent):
    """
    :param gpId: token as the identity of gift package
    :param price: how many ong does this gpId will be sold
    :param gpLimit: how many gift packages (GP) will be available.
    :param gpContent: [[tokenId1, amount1], [tokenId2, amount2], ..., [tokenIdN, amountN]]
    :return:
    """
    RequireWitness(Admin)
    gpKey = _concatkey(GP_PREFIX, gpId)
    Require(not Get(GetContext(), gpKey))
    gpMap = {"price": price}
    Require(gpLimit > 0)
    content = []
    # ta means [tokenId_n, amount_n]
    for ta in gpContent:
        tokenId = ta[0]
        amount = ta[1]
        # make sure the tokenId is legal
        Require(tokenId >= 1001 and tokenId <= 999999)
        # make sure the tokenId has been created in property contract <=> name of tokenId is NOT None
        res = DynamicAppCall(getPropertyReversedHash(), "name", [tokenId])
        Require(res)
        Require(amount > 0)
        content.append([tokenId, amount])
    contentInfo = Serialize(content)
    gpMap["content"] = contentInfo
    # put the gp info into the storage
    Put(GetContext(), _concatkey(GP_PREFIX, gpId), Serialize(gpMap))
    # update the left gift package number in storage
    Put(GetContext(), _concatkey(GP_LEFT_PREFIX, gpId), gpLimit)
    Notify(["setGP", gpId, gpLimit, price, gpContent])
    return True
Ejemplo n.º 7
0
def _transferOEP4FromContact(oep4ReverseAddr, toAcct, amount):
    params = [ContractAddress, toAcct, amount]
    res = DynamicAppCall(oep4ReverseAddr, 'transfer', params)
    if res and res == b'\x01':
        return True
    else:
        return False
Ejemplo n.º 8
0
def createExchange(token_hash):
    # Ensure token is a contract with nonzero contract hash
    assert (token_hash != ZERO_ADDRESS and len(token_hash) == 20)

    # Ensure templateCode existgetExchange
    templateScript = Get(GetContext(), EXCHANGE_TEMPLATE_KEY)
    assert (len(templateScript) > 0)
    tokenCount = Get(GetContext(), TOKEN_COUNT_KEY)

    # # append unused byte code to avm code to produce different contract
    newTokenCount = tokenCount + 1
    templateScript = concat(templateScript, newTokenCount)

    # Deploy replica contract
    assert (Create(templateScript, True, "uniswap_exchange", "1.0", "uniswap_factory", "email", "uniswap_exchange contract created by uniswap_factory contract"))

    # Invoke the newly deployed contract to set up the token exchange pair
    exchangeHash = AddressFromVmCode(templateScript)
    exchangeAddr = bytearray_reverse(exchangeHash)
    tokenAddr = bytearray_reverse(token_hash)
    assert (DynamicAppCall(exchangeAddr, "setup", [tokenAddr, GetExecutingScriptHash()]))

    # Store the map between token and exchange contract hash
    Put(GetContext(), concat(TOKEN_TO_EXCHANGE_PREFIX, token_hash), exchangeAddr)
    Put(GetContext(), concat(EXCHANGE_TO_TOKEN_PREFIX, exchangeHash), tokenAddr)

    # Add the token count
    Put(GetContext(), TOKEN_COUNT_KEY, newTokenCount)

    # Map token with token id
    Put(GetContext(), concat(ID_TO_TOKEN_PREFIX, newTokenCount), tokenAddr)

    # Fire the event
    NewExchangeEvent(tokenAddr, exchangeAddr)
    return True
Ejemplo n.º 9
0
def callBackFunction(callbackAddress, callbackFunctionId, from_acct, amount,
                     data):
    params = [from_acct, amount, data]
    res = DynamicAppCall(callbackAddress, callbackFunctionId, params)
    if res and res == b'\x01':
        return True
    else:
        return False
def DynamicSendOng(revesedContractAddress, ongAmount):
    params = [selfContractAddr_, ongAmount]
    Notify(["params: ", params])
    res = DynamicAppCall(revesedContractAddress, "receiveONG", params)
    Notify(
        ["111_DynamicSendOng", revesedContractAddress, "receiveONG", params])
    Notify(["222_DynamicSendOng", res])
    return res
Ejemplo n.º 11
0
def _doMintTransfer(account, content, gpAmount):
    argsForMultiMintToken = []
    for ta in content:
        tokenId = ta[0]
        amount = ta[1] * gpAmount
        # mintToken(mintAcct, toAcct, tokenId, amount)
        argsForMultiMintToken.append(
            [SelfContractAddress, account, tokenId, amount])
    assert (DynamicAppCall(getPropertyReversedHash(), "multiMintToken",
                           argsForMultiMintToken))
    return True
Ejemplo n.º 12
0
def lock(fromAddress, fromAsset, toChainId, toAddress, amount, fee, id):
    """
    :param fromAddress: from Account
    :param fromAsset: asset address, not hash, should be reversed hash
    :param toChainId: !=3
    :param toAddress: bytearray
    :param amount: > fee
    :param fee: >= 0
    :param id: like uin in eth
    :return:
    """
    assert (CheckWitness(fromAddress))
    assert (not ifPause())
    assert (toChainId != 0 and toChainId != OntChainIdOnPoly)
    assert (len(toAddress) > 0)
    assert (amount > fee)
    assert (fee > 0)

    lockProxy = getLockProxy()
    toAssethash = DynamicAppCall(lockProxy, 'getAssetHash',
                                 [fromAsset, toChainId])
    assert (len(toAssethash) > 0)

    # call lock contract lock
    res = DynamicAppCall(fromAsset, "approve",
                         [fromAddress, lockProxy, amount - fee])
    assert (res == True)
    res = DynamicAppCall(
        lockProxy, 'lock',
        [fromAsset, fromAddress, toChainId, toAddress, amount - fee])
    assert (res == True)

    # transfer fee to fee collector
    feeCollector = getFeeCollector()
    assert (len(feeCollector) == 20)
    res = DynamicAppCall(fromAsset, 'transfer',
                         [fromAddress, feeCollector, fee])
    assert (res == True)
    PolyWrapperLock(fromAsset, fromAddress, toChainId, toAddress, amount - fee,
                    fee, id)
    return True
Ejemplo n.º 13
0
def _doTransfer(account, content, gpAmount):
    argsForTransferMulti = []
    for ta in content:
        tokenId = ta[0]
        amount = ta[1] * gpAmount
        # transfer(fromAcct, toAcct, tokenId, amount)
        argsForTransferMulti.append(
            [SelfContractAddress, account, tokenId, amount])
    assert (DynamicAppCall(getPropertyReversedHash(), "transferMulti",
                           argsForTransferMulti))

    return True
Ejemplo n.º 14
0
def _transferLink(link, fromAcct, toAcct, amount):
    """
    transfer _transferOEP4
    :param fromacct:
    :param toacct:
    :param amount:
    :return:
    """
    RequireWitness(fromAcct)
    params = [fromAcct, toAcct, amount]
    res = DynamicAppCall(link, 'transfer', params)
    if res and res == b'\x01':
        return True
    else:
        return False
Ejemplo n.º 15
0
def _transferOEP4(oep4ReverseAddr, fromAcct, toAcct, amount):
    """
    transfer _transferOEP4
    :param fromacct:
    :param toacct:
    :param amount:
    :return:
    """
    assert (CheckWitness(fromAcct))
    params = [fromAcct, toAcct, amount]
    res = DynamicAppCall(oep4ReverseAddr, 'transfer', params)
    if res and res == b'\x01':
        return True
    else:
        return False
Ejemplo n.º 16
0
def Main(operation, args):
    print("appcall in")
    if elt_in(['add', 'sub', 'mul'], operation):
        print("StaticAppCall")
        return CalculatorContract(operation, args)
    elif operation[0:1] == 'd':
        address = bytearray_reverse(
            hexstring2bytes('1a6f62cc0ff3d9ae32b0b924aeda2056a9fdfccb'))
        print("DynamicAppCall")
        operation = operation[1:]
        print(operation)
        return DynamicAppCall(address, operation, args)
    elif operation == 'name':
        print("getname")
        return NAME
Ejemplo n.º 17
0
def Main(operation, args):
    global z0
    z0 = 999
    b0[a0[4]][0] = 30
    b0[a0[0]][a0[4]][1] = 20

    x = 'local'
    assert (b[1] == 7)
    assert (b[2] == 7)
    assert (b[3] == 7)
    assert (b[4] == 7)
    b[1] = 90
    global a
    a[0][0] = '00'
    assert (x == 'local')

    checkglobal()
    assert (b[1] == 'checkglobal')
    assert (a == 9)
    print(a)
    print(b[1])
    assert (b0 == 8888)
    assert (y0[2] == 88)

    if elt_in(['add', 'sub', 'mul'], operation):
        print("StaticAppCall")
        return CalculatorContract(operation, args)
    elif operation[0:1] == 'd':
        address = bytearray_reverse(
            hexstring2bytes('91125f47bd4823324de4897b64e0c2b3b503dfed'))
        print("DynamicAppCall")
        operation = operation[1:]
        print(operation)
        return DynamicAppCall(address, operation, args)
    elif operation == 'name':
        print("getname")
        return NAME
def DynamicSendToken(revesedContractAddress, operation, params):
    Notify(["bytearray: ", revesedContractAddress])
    res = DynamicAppCall(revesedContractAddress, operation, params)
    Notify(["111_DynamicSendToken", revesedContractAddress, operation, params])
    Notify(["222_DynamicSendToken", res])
    return res
Ejemplo n.º 19
0
def DynamicCallFunction(callAddress, callbackFunctionId, params):
    res = DynamicAppCall(callAddress, callbackFunctionId, params)
    if res and res == b'\x01':
        return True
    else:
        return False
Ejemplo n.º 20
0
def testSource(buff):
    sourceHash = Get(GetContext(), "SOURCE")
    Notify(["sourceHash", sourceHash])
    offset = 0
    res = DynamicAppCall(sourceHash, "NextBool", [buff, offset])
    Notify([1, res, False])

    res = DynamicAppCall(sourceHash, "NextUint8", [buff, res[1]])
    Notify([2, res, 255])

    res = DynamicAppCall(sourceHash, "NextUint16", [buff, res[1]])
    Notify([3, res, 65535])

    res = DynamicAppCall(sourceHash, "NextUint32", [buff, res[1]])
    Notify([4, res, 4294967295])

    res = DynamicAppCall(sourceHash, "NextUint64", [buff, res[1]])
    Notify([5, res, 10100])

    res = DynamicAppCall(sourceHash, "NextByte", [buff, res[1]])
    Notify([6, res, 200])

    res = DynamicAppCall(sourceHash, "NextBytes", [buff, res[1], 6])
    Notify([7, res, "hahaha"])

    res = DynamicAppCall(sourceHash, "NextVarBytes", [buff, res[1]])
    Notify([8, res, "heiheihei"])

    res = DynamicAppCall(sourceHash, "NextBytes20", [buff, res[1]])
    Notify([9, res])

    res = DynamicAppCall(sourceHash, "NextBytes32", [buff, res[1]])
    Notify([10, res])

    res = DynamicAppCall(sourceHash, "NextString", [buff, res[1]])
    Notify([11, res])

    return buff
Ejemplo n.º 21
0
def testSink(bl, u8, u16, u32, u64, b, bs, vbs, addr, hash1, str1):
    sinkHash = Get(GetContext(), "SINK")
    Notify(["sinkHash", sinkHash])
    buff = None
    buff = DynamicAppCall(sinkHash, "WriteBool", [bl, buff])
    Notify([1, buff])

    buff = DynamicAppCall(sinkHash, "WriteUint8", [u8, buff])
    Notify([2, buff])

    buff = DynamicAppCall(sinkHash, "WriteUint16", [u16, buff])
    Notify([3, buff, 0xFFFF, 0xFFFF > 0])

    buff = DynamicAppCall(sinkHash, "WriteUint32", [u32, buff])
    Notify([4, buff])

    buff = DynamicAppCall(sinkHash, "WriteUint64", [u64, buff])
    Notify([5, buff, u64, 18446744073709551615])

    buff = DynamicAppCall(sinkHash, "WriteByte", [b, buff])
    Notify([6, buff])

    buff = DynamicAppCall(sinkHash, "WriteBytes", [bs, buff])
    Notify([7, buff])

    buff = DynamicAppCall(sinkHash, "WriteVarBytes", [vbs, buff])
    Notify([8, buff])

    buff = DynamicAppCall(sinkHash, "WriteBytes20", [addr, buff])
    Notify([9, buff])

    buff = DynamicAppCall(sinkHash, "WriteBytes32", [hash1, buff])
    Notify([10, buff])

    buff = DynamicAppCall(sinkHash, "WriteString", [str1, buff])
    Notify([11, buff])

    return buff
Ejemplo n.º 22
0
def DynamicCallFunctionResult(callAddress, callbackFunctionId, params):
    return DynamicAppCall(callAddress, callbackFunctionId, params)
Ejemplo n.º 23
0
def getBalanceFor(_assetAddress):
    if _assetAddress == ONG_ADDRESS or _assetAddress == ONT_ADDRESS:
        return Invoke(0, _assetAddress, "balanceOf", SelfContractAddress)
    else:
        return DynamicAppCall(_assetAddress, "balanceOf",
                              [SelfContractAddress])
Ejemplo n.º 24
0
def _chargeToken(account, amount):
    res = DynamicAppCall(_getTokenAddress(), "transfer", [account, MyAddress, amount])
    assert (res)
Ejemplo n.º 25
0
def _transferTokenTo(account, amount):
    res = DynamicAppCall(_getTokenAddress(), "transfer", [MyAddress, account, amount])
    assert (res)
Ejemplo n.º 26
0
def getTokenName():
    tokenAddress = _getTokenAddress()
    res = DynamicAppCall(tokenAddress, "name", [])
    return res
Ejemplo n.º 27
0
def TokenTransferFrom(exchangeId, order):
    """

    :param exchangeId:exchange id, Invoked only by registered exchange
    :param order: the order is a map structure.
    :return:True or false, if success, the maker and taker will get purpose token.
    """
    expireTime = order['expireTime']
    require(GetTime() <= expireTime, "order expired")
    require(CheckWitness(exchangeId), "invalid exchange")

    if not Get(ctx, concatKey(EXCHANGE_ID_PREFIX, exchangeId)):
        return False

    maker = order['makerAddress']
    makerAmount = order['makerAssetAmount']
    makerHash = order['makerTokenHash']
    taker = order['takerAddress']
    takerAmount = order['takerAssetAmount']
    takerHash = order['takerTokenHash']
    makerFee = order['makerFee']
    takerFee = order['takerFee']
    feeTokenHash = order['feeTokenHash']
    feeFeceiver = order['feeReceiver']

    if isNativeAsset(makerHash):
        require(
            transferFromNative(makerHash, ContractAddress, maker, taker,
                               makerAmount),
            "transfer from maker asset failed")
    else:
        require(
            DynamicAppCall(bytearray_reverse(makerHash), "transferFrom",
                           [ContractAddress, maker, taker, makerAmount]),
            "transfer maker token to taker failed")

    Notify("111")

    if isNativeAsset(takerHash):
        require(
            transferFromNative(takerHash, ContractAddress, taker, maker,
                               takerAmount),
            "transfer from taker asset failed")
    else:
        require(
            DynamicAppCall(bytearray_reverse(takerHash), "transferFrom",
                           [ContractAddress, taker, maker, takerAmount]),
            "transfer taker token to maker failed")

    Notify("222")

    if isNativeAsset(feeTokenHash):
        require(
            transferFromNative(feeTokenHash, ContractAddress, maker,
                               feeFeceiver, makerFee),
            "charge maker fee failed")
        require(
            transferFromNative(feeTokenHash, ContractAddress, taker,
                               feeFeceiver, takerFee),
            "charge taker fee failed")
    else:
        require(
            DynamicAppCall(bytearray_reverse(feeTokenHash), "transferFrom",
                           [ContractAddress, maker, feeFeceiver, makerFee]),
            "charge maker fee failed")
        require(
            DynamicAppCall(bytearray_reverse(feeTokenHash), "transferFrom",
                           [ContractAddress, taker, feeFeceiver, takerFee]),
            "charge taker fee failed")

    Notify("success")

    return True
Ejemplo n.º 28
0
def aes_supply():
    aesContractHash = "7f0ac00575b34c1f8a4fd4641f6c58721f668fd4"
    return DynamicAppCall(bytearray_reverse(aesContractHash), "balanceOf",
                          [contract_address])