Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'beta' into feat/transfer-ticker-ownership
- Loading branch information
Showing
9 changed files
with
638 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,94 @@ | ||
import { SubModule } from './SubModule'; | ||
import { PolymathError } from '../../PolymathError'; | ||
import { ErrorCode } from '../../types'; | ||
import { SetDocument } from '../../procedures/SetDocument'; | ||
import { RemoveDocument } from '../../procedures/RemoveDocument'; | ||
|
||
export class Documents extends SubModule {} | ||
interface Document { | ||
name: string; | ||
documentUri: string; | ||
documentHash: string; | ||
updatedAt: Date; | ||
} | ||
|
||
export class Documents extends SubModule { | ||
/** | ||
* Attach a new document to the contract, or update the URI or hash of an existing attached document | ||
* @param name Name of the document. It should be unique always | ||
* @param uri Off-chain uri of the document from where it is accessible to investors/advisors to read. | ||
* @param documentHash hash (of the contents) of the document. | ||
*/ | ||
public async set(args: { name: string; uri: string; documentHash: string }) { | ||
const { symbol } = this.securityToken; | ||
|
||
const procedure = new SetDocument({ symbol, ...args }, this.context); | ||
|
||
return procedure.prepare(); | ||
} | ||
|
||
/** | ||
* Remove an existing document from the contract giving the name of the document. | ||
* @param name Name of the document. It should be unique always | ||
*/ | ||
public async remove(args: { name: string }) { | ||
const { symbol } = this.securityToken; | ||
|
||
const procedure = new RemoveDocument({ symbol, ...args }, this.context); | ||
|
||
return procedure.prepare(); | ||
} | ||
|
||
/** | ||
* Retrieve a specific document's data by name | ||
* @param name Unique name of the document | ||
*/ | ||
public getDocument = async (args: { name: string }): Promise<Document> => { | ||
const { | ||
context: { contractWrappers }, | ||
securityToken, | ||
} = this; | ||
|
||
const { symbol } = securityToken; | ||
let securityTokenInstance; | ||
|
||
try { | ||
securityTokenInstance = await contractWrappers.tokenFactory.getSecurityTokenInstanceFromTicker( | ||
symbol | ||
); | ||
} catch (err) { | ||
throw new PolymathError({ | ||
code: ErrorCode.FetcherValidationError, | ||
message: `There is no Security Token with symbol ${symbol}`, | ||
}); | ||
} | ||
|
||
const { documentTime: updatedAt, ...info } = await securityTokenInstance.getDocument(args); | ||
return { updatedAt, ...args, ...info }; | ||
}; | ||
|
||
/** | ||
* Retrieve an array of all the documents attached to the security token | ||
*/ | ||
public getAllDocuments = async (): Promise<Document[]> => { | ||
const { | ||
context: { contractWrappers }, | ||
securityToken, | ||
} = this; | ||
|
||
const { symbol } = securityToken; | ||
let securityTokenInstance; | ||
|
||
try { | ||
securityTokenInstance = await contractWrappers.tokenFactory.getSecurityTokenInstanceFromTicker( | ||
symbol | ||
); | ||
} catch (err) { | ||
throw new PolymathError({ | ||
code: ErrorCode.FetcherValidationError, | ||
message: `There is no Security Token with symbol ${symbol}`, | ||
}); | ||
} | ||
const documentList = await securityTokenInstance.getAllDocuments(); | ||
return Promise.all(documentList.map(docname => this.getDocument({ name: docname }))); | ||
}; | ||
} |
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,64 @@ | ||
import { Procedure } from './Procedure'; | ||
import { | ||
ProcedureType, | ||
PolyTransactionTag, | ||
RemoveDocumentProcedureArgs, | ||
ErrorCode, | ||
} from '../types'; | ||
import { PolymathError } from '../PolymathError'; | ||
|
||
export class RemoveDocument extends Procedure<RemoveDocumentProcedureArgs> { | ||
public type = ProcedureType.RemoveDocument; | ||
|
||
public async prepareTransactions() { | ||
const { symbol, name } = this.args; | ||
const { contractWrappers, currentWallet } = this.context; | ||
|
||
let securityToken; | ||
|
||
try { | ||
securityToken = await contractWrappers.tokenFactory.getSecurityTokenInstanceFromTicker( | ||
symbol | ||
); | ||
} catch (err) { | ||
throw new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `There is no Security Token with symbol ${symbol}`, | ||
}); | ||
} | ||
|
||
const [owner, account, allDocumentsList] = await Promise.all([ | ||
securityToken.owner(), | ||
currentWallet.address(), | ||
securityToken.getAllDocuments(), | ||
]); | ||
|
||
if (account !== owner) { | ||
throw new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `You must be the owner of this Security Token to remove a document`, | ||
}); | ||
} | ||
|
||
if (name.length < 1 || name.length > 32) { | ||
throw new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `You must provide a valid name between 1 and 32 characters long`, | ||
}); | ||
} | ||
|
||
if (!allDocumentsList.includes(name)) { | ||
throw new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `The document you are trying to remove does not exist on this security token`, | ||
}); | ||
} | ||
|
||
/** | ||
* Transactions | ||
*/ | ||
await this.addTransaction(securityToken.removeDocument, { | ||
tag: PolyTransactionTag.RemoveDocument, | ||
})({ name }); | ||
} | ||
} |
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,55 @@ | ||
import { Procedure } from './Procedure'; | ||
import { ProcedureType, PolyTransactionTag, SetDocumentProcedureArgs, ErrorCode } from '../types'; | ||
import { PolymathError } from '../PolymathError'; | ||
|
||
export class SetDocument extends Procedure<SetDocumentProcedureArgs> { | ||
public type = ProcedureType.SetDocument; | ||
|
||
public async prepareTransactions() { | ||
const { symbol, name, uri, documentHash } = this.args; | ||
const { contractWrappers, currentWallet } = this.context; | ||
|
||
let securityToken; | ||
|
||
try { | ||
securityToken = await contractWrappers.tokenFactory.getSecurityTokenInstanceFromTicker( | ||
symbol | ||
); | ||
} catch (err) { | ||
throw new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `There is no Security Token with symbol ${symbol}`, | ||
}); | ||
} | ||
|
||
const [owner, account] = await Promise.all([securityToken.owner(), currentWallet.address()]); | ||
|
||
if (account !== owner) { | ||
throw new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `You must be the owner of this Security Token to set the document`, | ||
}); | ||
} | ||
|
||
if (name.length < 1 || name.length > 32) { | ||
throw new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `You must provide a valid name between 1 and 32 characters long`, | ||
}); | ||
} | ||
|
||
if (documentHash.length < 1 || documentHash.length > 32) { | ||
throw new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `You must provide a valid document hash between between 1 and 32 characters long`, | ||
}); | ||
} | ||
|
||
/** | ||
* Transactions | ||
*/ | ||
await this.addTransaction(securityToken.setDocument, { | ||
tag: PolyTransactionTag.SetDocument, | ||
})({ name, uri, documentHash }); | ||
} | ||
} |
Oops, something went wrong.