Create Swap
- OTR
- HTTPS
To create a swap order, you must call create
on dataset(Dataset.SWAP)
.
create
takes an object of type SwapCreateTangleRequest
as parameter, in which you can specify the recipient, native tokens, NFTs and base token you are interested in.
const otrRequest = otr(otrAddress)
.dataset(Dataset.SWAP)
.create({
recipient: 'recipient UID or address',
nfts: ['nftUid1', 'nftUid2'],
setFunded: true,
});
create
returns an oject of type OtrRequest
<
SwapCreateTangleRequest
>
The SDK provides the helper functions getFireflyDeepLink()
and getBloomDeepLink()
to generate deep links for OTR requests.
To create a swap order, you must call create
on dataset(Dataset.SWAP)
.
create
takes an object of type SwapCreateRequest
as parameter, in which you can specify the recipient, native tokens, NFTs and base token you are interested in.
response = await https(origin)
.project(SoonaverseApiKey[origin])
.dataset(Dataset.SWAP)
.create({
address: address.bech32,
signature: userSign.signature,
publicKey: {
hex: userSign.publicKey,
network: Network.RMS,
},
body: {
network: Network.RMS,
recipient: 'recipient UID or address',
nfts: ['nftUid1', 'nftUid2'],
},
});
create
returns an oject of type Transaction
afterwards you need to fund a specific address:
const targetAddress = response.payload.targetAddress;
and set the order as funded:
await https(origin)
.project(SoonaverseApiKey[origin])
.dataset(Dataset.SWAP)
.setFunded({
address: address.bech32,
signature: userSign.signature,
publicKey: {
hex: userSign.publicKey,
network: Network.RMS,
},
body: { uid: response.payload.swap },
});
setFunded
returns an oject of type Transaction
With setFunded
, you can specify if you, as the bidder, are giving your final bid or keeping it open.
Full How-To Code
- OTR
- HTTPS
import { Dataset } from '@build-5/interfaces';
import { Build5, SoonaverseOtrAddress, otr } from '@build-5/sdk';
async function main() {
const origin = Build5.TEST;
// @ts-ignore
const otrAddress = SoonaverseOtrAddress[origin];
console.log('Create swap. Set network, recipient and asks.');
console.log('Asks can be base token, native tokens and nfts');
console.log(
'If setFunded is set to true, the swap will be created' +
" and the bid side is already set with the reques's assets",
);
console.log(
'If setFunded is set to false, swap will be created empty ' +
' and funds will be returned to the sender address with swap id and address',
);
try {
const otrRequest = otr(otrAddress)
.dataset(Dataset.SWAP)
.create({
recipient: 'recipient UID or address',
nfts: ['nftUid1', 'nftUid2'],
setFunded: true,
});
const fireflyDeeplink = otrRequest.getFireflyDeepLink();
console.log(fireflyDeeplink);
} catch (error) {
console.error('Error: ', error);
}
}
main().then(() => process.exit());
import { Dataset, Network, Transaction } from '@build-5/interfaces';
import { Build5, SoonaverseApiKey, https } from '@build-5/sdk';
import { address } from '../../utils/secret';
import { walletSign } from '../../utils/utils';
async function main() {
const origin = Build5.TEST;
let response: Transaction;
const userSign = await walletSign(address.bech32, address);
console.log('Create swap. Set network, recipient and asks.');
console.log('Asks can be base token, native tokens and nfts');
try {
response = await https(origin)
.project(SoonaverseApiKey[origin])
.dataset(Dataset.SWAP)
.create({
address: address.bech32,
signature: userSign.signature,
publicKey: {
hex: userSign.publicKey,
network: Network.RMS,
},
body: {
network: Network.RMS,
recipient: 'recipient UID or address',
nfts: ['nftUid1', 'nftUid2'],
},
});
const targetAddress = response.payload.targetAddress;
console.log('Send bids to swap order address', targetAddress);
console.log('Once bids are sent mark the swap as funded.');
await https(origin)
.project(SoonaverseApiKey[origin])
.dataset(Dataset.SWAP)
.setFunded({
address: address.bech32,
signature: userSign.signature,
publicKey: {
hex: userSign.publicKey,
network: Network.RMS,
},
body: { uid: response.payload.swap },
});
console.log('Once asks are sent as well the swap will be fulfilled.');
} catch (error) {
console.error('Error: ', error);
}
}
main().then(() => process.exit());