@arcblock/swap-storage
v1.7.1
Published
Storage interface for atomic-swap
Downloads
114
Readme
@arcblock/swap-storage
Interface for define a storage class that can be used by forge atomic swap process.
Table of Contents
Motivation & Spec
Since traceIds are used everywhere make achieve better QR code experience, we should allow users to customize how to generate/store/update traceId records.
Swap transactions are tracked using a traceId, which is generated when creating a new atomic swap.
Fields
Possible fields to exist in a swap record
traceId
: primary keystatus
: default tonot_started
, will change on eventsofferUserAddress
: '',offerAssets
: [],offerToken
: 0,offerLocktime
: 28800,offerChain
: '', // 卖家要卖货的链offerSetupHash
: '', // 卖家 setup_swap 的 hashofferSwapAddress
: '', // 卖家 setup_swap 的地址offerRetrieveHash
: '', // 卖家 retrieve_swap 的 hashofferRevokeHash
: '', // 卖家 revoke_swap 的 hashdemandUserAddress
: '',demandAssets
: [],demandToken
: 0,demandLocktime
: 57600,demandChain
: '', // 买家要付款的链demandSetupHash
: '', // 买家 setup_swap 的 hashdemandSwapAddress
: '', // 买家 setup_swap 的地址demandRetrieveHash
: '', // 买家 retrieve_swap 的 hashdemandRevokeHash
: '', // 买家 revoke_swap 的 hasherrorMessage
: '', // detailed error messagecreatedAt
: new Date(), // 创建时间updatedAt
: new Date(), // 更新时间
APIs
Basic APIs that a traceId storage should support:
create(traceId, payload)
: 创建记录update(traceId, updates)
: 更新记录read(traceId)
:按 traceId 查询delete(traceId)
:删除记录finalize(traceId, payload)
:把订单交换物、交换双方固定下来listByStatus(status)
: 按状态查询订单listByOfferAddress(address, status)
: 按卖方查询订单listByDemandAddress(address, status)
:按买方查询订单purge(ttl)
:清理掉无效的数据
Events
Events that will be emitted during a atomic-swap process
create
: when creating a new recordupdate
: when updating the recorddestroy
: when deleting the recordfinalize
: when finalize swap payloaduser_setup
: when buyer have done setup_swapseller_setup
: when seller is doing setupboth_setup
: when seller have done setup_swapuser_retrieve
: when buyer have done retrieve_swapboth_retrieve
: when seller have done retrieve_swapuser_revoke
: when buyer have done revoke_swapseller_revoke
: when seller have done revoke_swapboth_revoke
: when seller have done revoke_swap
Install
npm install @arcblock/swap-storage
// or
yarn add @arcblock/swap-storage
Usage
const StorageInterface = require('@arcblock/swap-storage');
const keystone = require('keystone');
module.exports = class KeystoneStorage extends StorageInterface {
constructor() {
this.model = keystone.list('LoginToken').model;
}
create(traceId, status = 'created') {
const LoginToken = this.model;
const item = new LoginToken({ traceId, status });
return item.save();
}
read(traceId) {
return this.model.findOne({ traceId });
}
update(traceId, updates) {
return this.model.findOneAndUpdate({ traceId }, updates);
}
delete(traceId) {
return this.model.remove({ traceId });
}
exist(traceId, did) {
return this.model.findOne({ traceId, did });
}
};
Contributors
| Name | Website | | -------------- | -------------------------- | | wangshijun | https://ocap.arcblock.io |