-
Notifications
You must be signed in to change notification settings - Fork 8
Exchange transactions example
Chun Lam edited this page Feb 21, 2024
·
3 revisions
import { AddExchangeOfferTransaction, ExchangeOfferTransaction,
RemoveExchangeOfferTransaction, Deadline, Account, NetworkType, AddExchangeOffer,
ExchangeOffer, RemoveExchangeOffer, UInt64, ExchangeOfferType, MosaicId, MosaicNonce
} from 'tsjs-xpx-chain-sdk';
import { Network } from '../utils';
const network = new Network('http://localhost:3000');
const alice = Account.createFromPrivateKey(
'c6f77df1c55b84a7673561eb221c282edb8d9a3f4d66f9320b85736d2e3cdeda',
NetworkType.MIJIN_TEST, 1);
const aliceMosaicId = new MosaicId([
3205233868,
173793843
]);
const bob = Account.createFromPrivateKey(
'7ad5e23f2e71e7a470deaa0f4f85c414ff915d8a670a598c6600c461b72e9f18',
NetworkType.MIJIN_TEST, 1);
// SELL OFFER
// alice offers some mosaic she owns for sale
const addExchangeSellOffer = () => {
const offers = [
new AddExchangeOffer(
aliceMosaicId, // mosaic
UInt64.fromUint(10000000), // amount of mosaic
UInt64.fromUint(10000000), // asked price per amount in xpx
ExchangeOfferType.SELL_OFFER, // alice is selling
UInt64.fromUint(1000) // duration of the offer - number of blocks
)
];
const tx = AddExchangeOfferTransaction.create(
Deadline.create(),
offers,
NetworkType.MIJIN_TEST
);
return network.networkProperties.then(networkProperties => {
const signedTx = alice.preV2Sign(tx, networkProperties.generationHash);
return network.announceAndWaitForConfirmation(signedTx).then(tx => {
console.log('Confirmed:');
console.log(tx);
});
});
};
// bob can try to buy some mosaic from alice
const exchangeSellOffer = () => {
const offers = [
new ExchangeOffer(
aliceMosaicId, // mosaic
UInt64.fromUint(100), // amount of mosaic
UInt64.fromUint(100), // proposed price per amount in xpx
ExchangeOfferType.SELL_OFFER, // alice is selling
alice.publicAccount // from alice
)
];
const tx = ExchangeOfferTransaction.create(
Deadline.create(),
offers,
NetworkType.MIJIN_TEST
);
return network.networkProperties.then(networkProperties => {
const signedTx = bob.preV2Sign(tx, networkProperties.generationHash);
return network.announceAndWaitForConfirmation(signedTx).then(tx => {
console.log('Confirmed:');
console.log(tx);
});
});
};
// alice can cancel unmatched rest of the published sell offer
const removeExchangeSellOffer = () => {
const removeOffers = [
new RemoveExchangeOffer(
aliceMosaicId, // mosaic
ExchangeOfferType.SELL_OFFER, // alice is selling
)
];
const tx = RemoveExchangeOfferTransaction.create(
Deadline.create(),
removeOffers,
NetworkType.MIJIN_TEST
);
return network.networkProperties.then(networkProperties => {
const signedTx = alice.preV2Sign(tx, networkProperties.generationHash);
return network.announceAndWaitForConfirmation(signedTx).then(tx => {
console.log('Confirmed:');
console.log(tx);
});
});
};
// or,
// BUY OFFER
// bob advertises his interest in some mosaic
const addExchangeBuyOffer = () => {
const offers = [
new AddExchangeOffer(
aliceMosaicId, // mosaic
UInt64.fromUint(10000000), // amount of mosaic
UInt64.fromUint(10000000), // proposed price per amount in xpx
ExchangeOfferType.BUY_OFFER, // bob is buying
UInt64.fromUint(1000) // duration of the offer - number of blocks
)
];
const tx = AddExchangeOfferTransaction.create(
Deadline.create(),
offers,
NetworkType.MIJIN_TEST
);
return network.networkProperties.then(networkProperties => {
const signedTx = bob.preV2Sign(tx, networkProperties.generationHash);
return network.announceAndWaitForConfirmation(signedTx).then(tx => {
console.log('Confirmed:');
console.log(tx);
});
});
};
// alice can try to offer some to bob
const exchangeBuyOffer = () => {
const offers = [
new ExchangeOffer(
aliceMosaicId, // mosaic
UInt64.fromUint(100), // amount of mosaic
UInt64.fromUint(100), // asked price per amount in xpx
ExchangeOfferType.BUY_OFFER, // bob is buying
bob.publicAccount // to bob
)
];
const tx = ExchangeOfferTransaction.create(
Deadline.create(),
offers,
NetworkType.MIJIN_TEST
);
return network.networkProperties.then(networkProperties => {
const signedTx = alice.preV2Sign(tx, networkProperties.generationHash);
return network.announceAndWaitForConfirmation(signedTx).then(tx => {
console.log('Confirmed:');
console.log(tx);
});
});
};
// bob can cancel unmatched rest of the published buy offer
const removeExchangeBuyOffer = () => {
const removeOffers = [
new RemoveExchangeOffer(
aliceMosaicId, // mosaic
ExchangeOfferType.BUY_OFFER, // bob is buying
)
];
const tx = RemoveExchangeOfferTransaction.create(
Deadline.create(),
removeOffers,
NetworkType.MIJIN_TEST
);
return network.networkProperties.then(networkProperties => {
const signedTx = bob.preV2Sign(tx, networkProperties.generationHash);
return network.announceAndWaitForConfirmation(signedTx).then(tx => {
console.log('Confirmed:');
console.log(tx);
});
});
};
addExchangeSellOffer().then(_ => {
exchangeSellOffer().then(_ => {
removeExchangeSellOffer().then(_ => {
addExchangeBuyOffer().then(_ => {
exchangeBuyOffer().then(_ => {
removeExchangeBuyOffer().then(_ => {
console.log('All done.');
});
});
});
});
});
});