com.github.DNAProject.smartcontract.nativevm.Gas Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see .
*
*/
package com.github.DNAProject.smartcontract.nativevm;
import com.alibaba.fastjson.JSONObject;
import com.github.DNAProject.DnaSdk;
import com.github.DNAProject.account.Account;
import com.github.DNAProject.common.Address;
import com.github.DNAProject.common.ErrorCode;
import com.github.DNAProject.common.Helper;
import com.github.DNAProject.core.asset.State;
import com.github.DNAProject.core.transaction.Transaction;
import com.github.DNAProject.sdk.exception.SDKException;
import com.github.DNAProject.smartcontract.nativevm.abi.NativeBuildParams;
import com.github.DNAProject.smartcontract.nativevm.abi.Struct;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class Gas {
private DnaSdk sdk;
private final String contract = "0000000000000000000000000000000000000001";
private final String gasContract = "0000000000000000000000000000000000000002";
public Gas(DnaSdk sdk) {
this.sdk = sdk;
}
public String getContractAddress() {
return gasContract;
}
/**
*
* @param sendAcct
* @param recvAddr
* @param amount
* @param payerAcct
* @param gaslimit
* @param gasprice
* @return
* @throws Exception
*/
public String sendTransfer(Account sendAcct, String recvAddr, long amount, Account payerAcct, long gaslimit, long gasprice) throws Exception {
if (sendAcct == null || payerAcct == null ) {
throw new SDKException(ErrorCode.ParamErr("parameters should not be null"));
}
if (amount <= 0 || gasprice < 0 || gaslimit < 0) {
throw new SDKException(ErrorCode.ParamErr("amount or gasprice or gaslimit should not be less than 0"));
}
Transaction tx = makeTransfer(sendAcct.getAddressU160().toBase58(), recvAddr, amount, payerAcct.getAddressU160().toBase58(), gaslimit, gasprice);
sdk.signTx(tx, new Account[][]{{sendAcct}});
if(!sendAcct.equals(payerAcct)){
sdk.addSign(tx,payerAcct);
}
boolean b = sdk.getConnect().sendRawTransaction(tx.toHexString());
if (b) {
return tx.hash().toString();
}
return null;
}
public String sendTransferMulti(Account[] accounts, State[] states,Account payerAcct,long gaslimit, long gasprice) throws Exception {
if (accounts == null || payerAcct == null ) {
throw new SDKException(ErrorCode.ParamErr("parameters should not be null"));
}
if (gasprice < 0 || gaslimit < 0) {
throw new SDKException(ErrorCode.ParamErr("amount or gasprice or gaslimit should not be less than 0"));
}
Transaction tx = makeTransfer(states, payerAcct.getAddressU160().toBase58(), gaslimit, gasprice);
sdk.signTx(tx, new Account[][]{{payerAcct}});
for(int i =0;i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy