generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add examples related to confidential assets
- Loading branch information
1 parent
41f7646
commit 22388b5
Showing
14 changed files
with
573 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { BigNumber } from '@polymeshassociation/polymesh-sdk'; | ||
import { ConfidentialAsset } from '@polymeshassociation/polymesh-sdk/types'; | ||
|
||
import { getClient } from '~/common/client'; | ||
import { toHumanObject } from '~/common/utils'; | ||
|
||
/** | ||
* Retrieves all relevant info about a venue filtering for an asset | ||
*/ | ||
export async function getVenueFiltering(asset: ConfidentialAsset) { | ||
console.log('\n----------------------------------------------------------------------\n'); | ||
console.log('\n💡 Fetching venue filtering details : '); | ||
|
||
const venueFilteringDetails = await asset.getVenueFilteringDetails(); | ||
|
||
console.log( | ||
`\nℹ️ Venue Filtering details - ${JSON.stringify(toHumanObject(venueFilteringDetails))}` | ||
); | ||
} | ||
|
||
/* | ||
This script showcases how to get a confidential asset instance with its ID and get all the relevant info about it | ||
*/ | ||
(async (): Promise<void> => { | ||
console.log('Connecting to the node...\n'); | ||
const account = process.argv[2]; | ||
const assetId = process.argv[3]; | ||
const allowedVenue = process.argv[4]; | ||
if (!account || !process.env[account]) { | ||
throw new Error('Please specify the account to be used to create the venue'); | ||
} | ||
if (!assetId || !allowedVenue) { | ||
throw new Error('Please specify asset ID and allowed Venue'); | ||
} | ||
const api = await getClient(process.env[account]); | ||
|
||
const signingIdentity = await api.getSigningIdentity(); | ||
if (!signingIdentity) { | ||
throw new Error(`Account information not found for ${account}`); | ||
} | ||
console.log( | ||
`\n💡 Enabling venue filtering for asset ID - ${assetId} and allowing venue - ${allowedVenue}` | ||
); | ||
|
||
const asset = await api.confidentialAssets.getConfidentialAsset({ id: assetId }); | ||
|
||
const setFilteringTx = await asset.setVenueFiltering({ | ||
enabled: true, | ||
allowedVenues: [new BigNumber(allowedVenue)], | ||
}); | ||
|
||
await setFilteringTx.run(); | ||
|
||
console.log(`\n✅ Venue filtering has been set for asset ID - ${asset.toHuman()}`); | ||
|
||
await getVenueFiltering(asset); | ||
|
||
await api.disconnect(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { BigNumber } from '@polymeshassociation/polymesh-sdk'; | ||
|
||
import { getClient } from '~/common/client'; | ||
import { getTransactionDetails } from '~/examples/confidentialTransactionDetails'; | ||
|
||
/* | ||
This script showcases how to get a confidential asset instance with its ID and get all the relevant info about it | ||
*/ | ||
(async (): Promise<void> => { | ||
console.log('Connecting to the node...\n'); | ||
const account = process.argv[2]; | ||
const sender = process.argv[3]; | ||
const receiver = process.argv[4]; | ||
const mediator = process.argv[5]; | ||
const venueId = process.argv[6]; | ||
const assetId = process.argv[7]; | ||
if (!account || !process.env[account]) { | ||
throw new Error('Please specify the account to be used to create the venue'); | ||
} | ||
if (!sender || !receiver || !mediator || !venueId) { | ||
throw new Error('Please specify all the transaction details'); | ||
} | ||
|
||
const api = await getClient(process.env[account]); | ||
|
||
const signingIdentity = await api.getSigningIdentity(); | ||
if (!signingIdentity) { | ||
throw new Error(`Account information not found for ${account}`); | ||
} | ||
|
||
console.log( | ||
`\n💡 Creating a new confidential transaction using ${account} DID - ${signingIdentity.did}` | ||
); | ||
|
||
const venue = await api.confidentialSettlements.getVenue({ id: new BigNumber(venueId) }); | ||
|
||
const addTransactionTx = await venue.addTransaction({ | ||
legs: [ | ||
{ | ||
sender, | ||
receiver, | ||
auditors: [], | ||
mediators: [mediator], | ||
assets: [assetId], | ||
}, | ||
], | ||
}); | ||
|
||
const createdTransaction = await addTransactionTx.run(); | ||
|
||
console.log( | ||
`\n✅ New confidential transaction created with ID - ${createdTransaction.toHuman()}` | ||
); | ||
|
||
await getTransactionDetails(createdTransaction); | ||
|
||
await api.disconnect(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { BigNumber } from '@polymeshassociation/polymesh-sdk'; | ||
import { | ||
AffirmConfidentialTransactionParams, | ||
ConfidentialAffirmParty, | ||
} from '@polymeshassociation/polymesh-sdk/types'; | ||
|
||
import { getClient } from '~/common/client'; | ||
|
||
/* | ||
This script showcases affirming of confidential transaction | ||
*/ | ||
(async (): Promise<void> => { | ||
console.log('Connecting to the node...\n'); | ||
const account = process.argv[2]; | ||
const transactionId = process.argv[3]; | ||
const legId = process.argv[4]; | ||
const party = process.argv[5] as ConfidentialAffirmParty; | ||
const assetId = process.argv[6]; | ||
const proof = process.argv[7]; | ||
|
||
if (!account) { | ||
throw new Error('Please specify both account and public key for creating confidential account'); | ||
} | ||
const api = await getClient(process.env[account]); | ||
|
||
const signingIdentity = await api.getSigningIdentity(); | ||
if (!signingIdentity) { | ||
throw new Error(`Account information not found for ${account}`); | ||
} | ||
|
||
console.log(`\n💡 Affirming transaction ${transactionId} for ${party}`); | ||
|
||
const transaction = await api.confidentialSettlements.getTransaction({ | ||
id: new BigNumber(transactionId), | ||
}); | ||
|
||
let params: AffirmConfidentialTransactionParams; | ||
if (party === ConfidentialAffirmParty.Sender) { | ||
params = { | ||
legId: new BigNumber(legId), | ||
party, | ||
proofs: [ | ||
{ | ||
asset: assetId, | ||
proof, | ||
}, | ||
], | ||
}; | ||
} else { | ||
params = { | ||
legId: new BigNumber(legId), | ||
party, | ||
}; | ||
} | ||
const affirmTx = await transaction.affirmLeg(params); | ||
|
||
await affirmTx.run(); | ||
|
||
console.log(`\n✅ Transaction ${transactionId} is now affirmed by ${party}`); | ||
|
||
// await getTransactionDetails(transaction); | ||
await api.disconnect(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ConfidentialAsset } from '@polymeshassociation/polymesh-sdk/types'; | ||
|
||
import { getClient } from '~/common/client'; | ||
import { toHumanObject } from '~/common/utils'; | ||
|
||
/** | ||
* Retrieves all relevant info about a confidential asset | ||
*/ | ||
export async function getConfidentialAssetDetails(asset: ConfidentialAsset) { | ||
console.log('\n----------------------------------------------------------------------\n'); | ||
console.log('\n💡 Getting all the information about asset ID : ', asset.toHuman()); | ||
|
||
const [exists, details, auditorInfo, venueFilteringDetails] = await Promise.all([ | ||
asset.exists(), | ||
asset.details(), | ||
asset.getAuditors(), | ||
asset.getVenueFilteringDetails(), | ||
]); | ||
|
||
console.log('\nℹ️ Exists - ', exists); | ||
console.log('\nℹ️ Details - ', toHumanObject(details)); | ||
console.log('\nℹ️ Auditors - ', toHumanObject(auditorInfo)); | ||
console.log('\nℹ️ Venue filtering details - ', toHumanObject(venueFilteringDetails)); | ||
} | ||
/* | ||
This script showcases how to get a confidential asset instance with its ID and get all the relevant info about it | ||
*/ | ||
(async (): Promise<void> => { | ||
console.log('Connecting to the node...\n'); | ||
const assetId = process.argv[2]; | ||
if (!assetId) { | ||
throw new Error('Please specify asset ID to fetch the details'); | ||
} | ||
const api = await getClient(); | ||
|
||
const asset = await api.confidentialAssets.getConfidentialAsset({ id: assetId }); | ||
|
||
await getConfidentialAssetDetails(asset); | ||
|
||
await api.disconnect(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { BigNumber } from '@polymeshassociation/polymesh-sdk'; | ||
import { ConfidentialTransaction } from '@polymeshassociation/polymesh-sdk/types'; | ||
|
||
import { getClient } from '~/common/client'; | ||
import { toHumanObject } from '~/common/utils'; | ||
|
||
/** | ||
* Retrieves all relevant info about a confidential transaction | ||
*/ | ||
export async function getTransactionDetails(transaction: ConfidentialTransaction) { | ||
console.log('\n----------------------------------------------------------------------\n'); | ||
console.log('\n💡 Getting info about confidential transaction : ', transaction.toHuman()); | ||
|
||
const [exists, details, legs, involvedParties, legStates, pendingAffirmCount] = await Promise.all( | ||
[ | ||
transaction.exists(), | ||
transaction.details(), | ||
transaction.getLegs(), | ||
transaction.getInvolvedParties(), | ||
transaction.getLegStates(), | ||
transaction.getPendingAffirmsCount(), | ||
] | ||
); | ||
|
||
console.log('\nℹ️ Exists - ', exists); | ||
console.log('\nℹ️ Details - ', toHumanObject(details)); | ||
console.log('\nℹ️ Legs - ', toHumanObject(legs)); | ||
console.log('\nℹ️ Involved Parties - ', toHumanObject(involvedParties)); | ||
console.log('\nℹ️ Leg states - ', toHumanObject(legStates)); | ||
console.log('\nℹ️ Pending Affirmations - ', toHumanObject(pendingAffirmCount)); | ||
} | ||
/* | ||
This script showcases how to get a confidential asset instance with its ID and get all the relevant info about it | ||
*/ | ||
(async (): Promise<void> => { | ||
console.log('Connecting to the node...\n'); | ||
const id = process.argv[2]; | ||
if (!id) { | ||
throw new Error('Please specify asset ID to fetch the details'); | ||
} | ||
const api = await getClient(); | ||
|
||
const transaction = await api.confidentialSettlements.getTransaction({ id: new BigNumber(id) }); | ||
|
||
await getTransactionDetails(transaction); | ||
|
||
await api.disconnect(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { BigNumber } from '@polymeshassociation/polymesh-sdk'; | ||
|
||
import { getClient } from '~/common/client'; | ||
import { getConfidentialAssetDetails } from '~/examples/confidentialAssetDetails'; | ||
|
||
/* | ||
This script showcases Confidential Asset related functionality. It: | ||
- it will create a new asset | ||
- mint into given account | ||
*/ | ||
(async (): Promise<void> => { | ||
console.log('Connecting to the node...\n'); | ||
const account = process.argv[2]; | ||
const ticker = process.argv[3]; | ||
const auditor = process.argv[4]; | ||
const owner = process.argv[5]; | ||
if (!account || !ticker || !auditor) { | ||
throw new Error('Please specify the account, ticker, auditor for creating confidential asset'); | ||
} | ||
const api = await getClient(process.env[account]); | ||
|
||
const signingIdentity = await api.getSigningIdentity(); | ||
if (!signingIdentity) { | ||
throw new Error(`Account information not found for ${account}`); | ||
} | ||
console.log( | ||
`\n💡 Creating a new confidential asset with ticker ${ticker} for ${account} DID - ${signingIdentity.did}` | ||
); | ||
|
||
const createTx = await api.confidentialAssets.createConfidentialAsset({ | ||
ticker, | ||
data: 'Some Random Data', | ||
auditors: [auditor], | ||
}); | ||
|
||
const createdConfidentialAsset = await createTx.run(); | ||
|
||
console.log(`\n✅ New confidential asset created with ID - ${createdConfidentialAsset.id}`); | ||
|
||
await getConfidentialAssetDetails(createdConfidentialAsset); | ||
|
||
console.log('\n----------------------------------------------------------------------\n'); | ||
|
||
console.log('\n💡 Minting some assets : '); | ||
|
||
const amount = new BigNumber(10000); | ||
const issueTx = await createdConfidentialAsset.issue({ | ||
account: owner, | ||
amount, | ||
}); | ||
|
||
await issueTx.run(); | ||
|
||
console.log( | ||
`\n✅ Minted a total of ${amount.toString()} assets with ID - ${createdConfidentialAsset.id}` | ||
); | ||
|
||
await api.disconnect(); | ||
})(); |
Oops, something went wrong.