发起一笔交易
提示
学习本教程的前提是您已经拥有了一个资产钱包,并且钱包中拥有一些 ETH(SEPOLIA)_ETHEREUM_SEPOLIA
Token,您也可以查看 创建一个钱包
教程学习如何创建钱包。
本教程以转账交易任务为例进行说明,点击了解交易任务。
通过此教程,您将:
- 创建转账交易任务。
- 如何审批和签名。
- 交易的进度和状态。
创建转账交易任务
假设交易场景为,从 accountKey
为 account4b8d2c00520646c8862b68420aa1bc55
的资产钱包向外部陌生地址 0x9437A77E6BE3a7Bf5F3cfE611BfCd1Fd30BF95f5
转账 ETH(SEPOLIA)_ETHEREUM_SEPOLIA
Token,数量为:0.1 ETH。
请求参数
提示
示例中采用的是交易手续费费率等级,即设置 txFeeLevel
参数,关于交易手续费的更多说明,请参考设置交易手续费。
- TypeScript
- Golang
- Java
interface CreateTransactionRequest {
customerRefId: string;
coinKey: string;
txFeeLevel?: string;
feeRateDto?: {
feeRate?: string;
gasLimit?: string;
maxPriorityFee?: string;
maxFee?: string
};
txAmount: string;
sourceAccountKey: string;
sourceAccountType: string;
destinationAccountKey?: string;
destinationAccountType: string;
destinationAddress: string;
}
const request: CreateTransactionRequest = {
sourceAccountKey: 'account4b8d2c00520646c8862b68420aa1bc55',
sourceAccountType: 'VAULT_ACCOUNT',
destinationAccountType: 'ONE_TIME_ADDRESS',
destinationAddress: '0x9437A77E6BE3a7Bf5F3cfE611BfCd1Fd30BF95f5',
coinKey: 'ETH(SEPOLIA)_ETHEREUM_SEPOLIA',
txAmount: '0.1',
txFeeLevel: 'MIDDLE',
customerRefId: uuid(),
};
type CreateTransactionRequest struct {
CustomerRefId string `json:"customerRefId"`
CoinKey string `json:"coinKey,omitempty"`
TxFeeLevel string `json:"txFeeLevel,omitempty"`
FeeRateDto struct {
FeeRate string `json:"feeRate,omitempty"`
GasLimit string `json:"gasLimit,omitempty"`
MaxPriorityFee string `json:"maxPriorityFee,omitempty"`
MaxFee string `json:"maxFee,omitempty"`
} `json:"feeRateDto,omitempty"`
TxAmount string `json:"txAmount,omitempty"`
SourceAccountKey string `json:"sourceAccountKey,omitempty"`
SourceAccountType string `json:"sourceAccountType,omitempty"`
DestinationAccountKey string `json:"destinationAccountKey,omitempty"`
DestinationAccountType string `json:"destinationAccountType,omitempty"`
DestinationAddress string `json:"destinationAddress,omitempty"`
}
createTransactionRequest := CreateTransactionRequest{
SourceAccountKey: "account4b8d2c00520646c8862b68420aa1bc55",
SourceAccountType: "VAULT_ACCOUNT",
DestinationAccountType: "ONE_TIME_ADDRESS",
DestinationAddress: "0x9437A77E6BE3a7Bf5F3cfE611BfCd1Fd30BF95f5",
CoinKey: "ETH(SEPOLIA)_ETHEREUM_SEPOLIA",
TxAmount: "0.1",
TxFeeLevel: "MIDDLE",
CustomerRefId: uuid.New().String(),
}
CreateTransactionRequest.java
public class CreateTransactionRequest {
private String customerRefId;
private String coinKey;
private String txFeeLevel;
private String txAmount;
private String sourceAccountKey;
private String sourceAccountType;
private String destinationAccountKey;
private String destinationAccountType;
private String destinationAddress;
}
CreateTransactionRequest createTransactionRequest = new CreateTransactionRequest();
createTransactionRequest.setSourceAccountKey("account4b8d2c00520646c8862b68420aa1bc55");
createTransactionRequest.setSourceAccountType("VAULT_ACCOUNT");
createTransactionRequest.setDestinationAccountType("ONE_TIME_ADDRESS");
createTransactionRequest.setDestinationAddress("0x9437A77E6BE3a7Bf5F3cfE611BfCd1Fd30BF95f5");
createTransactionRequest.setCoinKey("ETH(SEPOLIA)_ETHEREUM_SEPOLIA");
createTransactionRequest.setTxAmount("0.1");
createTransactionRequest.setTxFeeLevel("MIDDLE");
createTransactionRequest.setCustomerRefId(UUID.randomUUID().toString());
请求接口
- TypeScript
- Golang
- Java
interface CreateTransactionResponse {
txKey: string;
}
const response = await client.doRequest<CreateTransactionRequest, CreateTransactionResponse>('/v2/transactions/create', request);
type CreateTransactionResponse struct {
TxKey string `json:"txKey"`
}
var createTransactionResponse CreateTransactionResponse
transactionApi.SendTransaction(createTransactionRequest, &createTransactionResponse)
CreateTransactionResponse.java
public class CreateTransactionResponse {
private String txKey;
}
CreateTransactionResponse createTransactionResponse = ServiceExecutor.execute(transactionApi.createTransaction(createTransactionRequest));
响应数据示例
提示
txKey
唯一代表一笔交易。
{
"txKey": "tx46461daa9b7a4612abce99e7ce598844"
}
如何审批和签名
交易任务创建后,将根据您的策略决定,此笔交易是由人工审核签名还是由 API Co-Signer 自动化审核签名。
- 人工审核签名
- API Co-Signer 审核签名
交易创建后,Safeheron 手机 App 会收到推送通知,并且代办任务列表会显示此笔待审核状态的交易,审核通过后,由最后一个审批人的 App 参与完成签名。
API Co-Signer 自动获取到待审核状态的交易,并向您的业务系统发起审核请求,审核通过后,将由API Co-Signer 参与完成签名。
交易的进度和状态
交易任务创建后,会经历审批,签名,广播上链,链上确认等过程,您可以点此查看交易任务生命周期所经历的全部状态,我们推荐您使用 Webhook
来感知交易任务状态的变化。
提示
通过 Webhook 来感知交易任务的状态变化,需要在 Safeheron Web 控制台 API 管理页面中配置 Webhook URL。
下一步
本教程涉及到的代码,已经在 Github 开源,获取完整源代码:
至此,您已经了解了怎样创建一笔转账交易任务,您也可以进一步了解通过 API 创建其他类型的交易: